Files
Rosea/tools/art-prep/test/layers.test.ts
T

176 lines
9.9 KiB
TypeScript

import { readFileSync } from 'node:fs';
import { GARDENER_OPTIONS } from '@rosea/schema';
import sharp from 'sharp';
import { describe, expect, it } from 'vitest';
import { Refused, acceptPicture, type Guide } from '../src/accept.js';
import { boardData } from '../src/board.js';
import { checkManifest, expandLayers, layerId, type Layered, type Manifest } from '../src/manifest.js';
import { placeholders } from '../src/placeholders.js';
import { reviewPoints } from '../src/style.js';
import { clone, readManifest, readStyle, root } from './fixtures.js';
const raw = readManifest();
const m = expandLayers(raw);
const POINTS = reviewPoints(readStyle()).length;
const layers = m.assets.filter((a) => a.over === 'gardener-poses-sheet');
const ids = new Set(layers.map((a) => a.id));
describe('the gardener in layered parts (STYLE.md §3b, decided by Mark 2026-11-17)', () => {
it('every look the player can choose can be stacked: a layer for every value of every choice', () => {
for (const skin of GARDENER_OPTIONS.skin) expect(ids.has(layerId('gardener', 'body', [skin])), skin).toBe(true);
for (const p of GARDENER_OPTIONS.presentation) {
for (const c of GARDENER_OPTIONS.clothes) expect(ids.has(layerId('gardener', 'clothes', [p, c])), `${p} ${c}`).toBe(true);
}
expect(ids.has('gardener-apron')).toBe(true);
for (const h of GARDENER_OPTIONS.hair) {
for (const c of GARDENER_OPTIONS.hairColour) expect(ids.has(layerId('gardener', 'hair', [h, c])), `${h} ${c}`).toBe(true);
}
for (const hat of GARDENER_OPTIONS.hat) expect(ids.has(layerId('gardener', 'hat', [hat]))).toBe(hat !== 'none');
expect(layers).toHaveLength(6 + 10 + 1 + 36 + 2);
});
it("the words name no value the app does not offer, so no layer is painted for nothing", () => {
const words = raw.layers?.[0]?.words ?? {};
for (const [axis, values] of Object.entries(words)) {
expect(Object.keys(values).sort(), axis).toEqual([...(GARDENER_OPTIONS[axis as keyof typeof GARDENER_OPTIONS] ?? [])].sort());
}
});
it("the colours in the prompts are the app's own (Gardener.svelte), so a painted look matches the one chosen", () => {
const svelte = readFileSync(`${root}packages/ui/src/scene/Gardener.svelte`, 'utf8');
const words = raw.layers?.[0]?.words ?? {};
for (const axis of ['skin', 'hairColour', 'clothes']) {
for (const [value, text] of Object.entries(words[axis] ?? {})) {
const hex = /#[0-9A-F]{6}/i.exec(text)?.[0].toLowerCase();
expect(svelte, `${axis} ${value}`).toMatch(new RegExp(`'?${value}'?: '${hex}'`));
}
}
});
it('come right after the guide, the seven of the two first looks first, then back to front', () => {
const at = m.assets.findIndex((a) => a.id === 'gardener-poses-sheet');
expect(m.assets.slice(at + 1, at + 8).map((a) => a.id)).toEqual([
'gardener-body-skin-3', 'gardener-clothes-girl-sage', 'gardener-clothes-boy-sage', 'gardener-apron',
'gardener-hair-short-brown', 'gardener-hair-braids-brown', 'gardener-hat-straw-hat'
]);
const back = (from: number): number[] => layers.slice(from).map((a) => a.part?.stack ?? -1);
for (const run of [back(0).slice(0, 7), back(7)]) expect(run).toEqual([...run].sort((x, y) => x - y));
expect(layers.find((a) => a.id === 'gardener-hat-cap')?.part).toEqual({ name: 'hat', stack: 4 });
});
it("have the guide's grid and poses, each picture named after its pose; the guide is never shipped", () => {
const guide = m.assets.find((a) => a.id === 'gardener-poses-sheet');
expect(guide?.shipped).toBe(false);
const hair = m.assets.find((a) => a.id === 'gardener-hair-curly-red');
expect(hair?.grid).toEqual(guide?.grid);
expect(hair?.cells?.map((c) => c.id)).toEqual((guide?.cells ?? []).map((c) => c.id.replace('gardener-', 'gardener-hair-curly-red-')));
expect(hair?.cells?.map((c) => c.does)).toEqual(guide?.cells?.map((c) => c.does));
});
it('expanding twice changes nothing', () => {
expect(expandLayers(m)).toEqual(m);
});
it("a layer's prompt attaches the character sheet and the guide, names only its part, and has no one-at-a-time prompts", () => {
const board = boardData(raw, readStyle());
const hair = board.assets.find((a) => a.id === 'gardener-hair-bun-auburn');
expect(hair?.attach).toEqual(['gardener-sheet', 'gardener-poses-sheet']);
expect(hair?.over).toBe('gardener-poses-sheet');
expect(hair?.prompt).toContain('Guide: attach the image gardener-poses-sheet and paint over it.');
expect(hair?.prompt).toContain("Part: only the gardener's hair: hair in a small round bun at the back of the head, auburn (#9A4A2A)");
expect(hair?.prompt).toContain('Avoid: any part of the gardener other than the one named');
// The character's whole first look would contradict a part: it is not in a layer's prompt.
expect(hair?.prompt).not.toContain('two braids, a wide straw hat');
expect(hair?.cells?.every((c) => c.prompt === '')).toBe(true);
expect(board.assets.find((a) => a.id === 'gardener-poses-sheet')?.cells?.every((c) => c.prompt !== '')).toBe(true);
});
it("each layer's stand-in draws its part only, in its own colour", () => {
const stand = placeholders(raw);
expect(stand.get('gardener-body-skin-6')).toContain('#553422');
expect(stand.get('gardener-body-skin-6')).not.toContain('#e6c98a'); // no straw hat in the body
expect(stand.get('gardener-hair-short-fair')).toContain('#d9b26a');
expect(stand.get('gardener-hair-short-fair')).not.toContain('#cf9a72'); // no skin in the hair
expect(stand.get('gardener-clothes-boy-dusk')).toContain('#5b6fa6');
expect(stand.get('gardener-hat-straw-hat')).toContain('#e6c98a');
});
});
describe('checkManifest refuses layers that cannot work', () => {
const broken = (change: (l: { -readonly [K in keyof Layered]: Layered[K] }, m: Manifest) => void): string => {
const copy = clone(raw);
change((copy.layers as Layered[])[0] as never, copy);
return checkManifest(copy, POINTS).join('\n');
};
it('a shipped guide: the app stacks the layers, so the guide must never appear', () => {
expect(broken((_, c) => { (c.assets.find((a) => a.id === 'gardener-poses-sheet') as { shipped: boolean }).shipped = true; })).toMatch(/guide "gardener-poses-sheet" is shipped/);
});
it('a guide that is missing or not a sprite sheet', () => {
expect(broken((l) => { l.over = 'nowhere'; })).toMatch(/guide "nowhere" is not in the manifest/);
expect(broken((l) => { l.over = 'gardener-sheet'; })).toMatch(/guide "gardener-sheet" is a sheet, not a sprite sheet/);
});
it('a part that names a choice it is not painted for, or a choice with no words', () => {
expect(broken((l) => { (l.parts[2] as { subject: string }).subject = 'only the apron, in {clothes}'; })).toMatch(/part apron says \{clothes\} but is not painted per clothes/);
expect(broken((l) => { delete (l.words as Record<string, unknown>)['hat']; })).toMatch(/part hat needs words for "hat"/);
});
it("a forbidden word in a layer's words", () => {
expect(broken((l) => { ((l.words as Record<string, Record<string, string>>)['hat'] ?? {})['cap'] = 'a cap to heal the heart'; })).toMatch(/"heal" \(medical\)/);
});
});
describe('art:accept and a layer (in register with the guide)', () => {
const W = 1536, H = 1024, COLS = 4, ROWS = 2;
const cw = W / COLS, ch = H / ROWS;
/** One rectangle per cell, at the given share of the cell: a figure, or a part of one. */
async function sheet(box: { x: number; y: number; w: number; h: number }): Promise<Buffer> {
const rects = Array.from({ length: COLS * ROWS }, (_, i) =>
`<rect x="${(i % COLS) * cw + box.x * cw}" y="${Math.floor(i / COLS) * ch + box.y * ch}" width="${box.w * cw}" height="${box.h * ch}" fill="#7fa07a"/>`).join('');
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}">${rects}</svg>`;
return sharp({ create: { width: W, height: H, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } } }).composite([{ input: Buffer.from(svg) }]).png().toBuffer();
}
const FIGURE = { x: 0.3, y: 0.15, w: 0.4, h: 0.7 };
let guide: Guide | undefined;
async function theGuide(): Promise<Guide> {
if (!guide) {
const { files } = await acceptPicture(await sheet(FIGURE), 'gardener-poses-sheet', m);
guide = new Map(files.map((f) => [f.id, f.data]));
}
return guide;
}
const refusal = async (p: Promise<unknown>): Promise<string> => {
try {
await p;
} catch (e) {
if (e instanceof Refused) return e.message;
throw e;
}
throw new Error('expected a refusal');
};
it('a part lying on the guide\'s figure is accepted and cut into its eight pictures', async () => {
const { files } = await acceptPicture(await sheet({ x: 0.33, y: 0.15, w: 0.34, h: 0.2 }), 'gardener-hair-braids-brown', m, await theGuide());
expect(files.map((f) => f.id)).toEqual((m.assets.find((a) => a.id === 'gardener-hair-braids-brown')?.cells ?? []).map((c) => c.id));
});
it('a part a little fuller than the guide (a wide hat) is still in register', async () => {
await acceptPicture(await sheet({ x: 0.25, y: 0.12, w: 0.5, h: 0.12 }), 'gardener-hat-straw-hat', m, await theGuide());
});
it('a part painted somewhere else is refused, naming the pictures', async () => {
const message = await refusal(acceptPicture(await sheet({ x: 0.75, y: 0.15, w: 0.2, h: 0.2 }), 'gardener-hair-braids-brown', m, await theGuide()));
expect(message).toMatch(/not in register with gardener-poses-sheet in gardener-hair-braids-brown-stand \(\d+ % of the cell/);
});
it('before the guide is accepted, a layer is refused', async () => {
expect(await refusal(acceptPicture(await sheet(FIGURE), 'gardener-apron', m))).toMatch(/guide gardener-poses-sheet is not in art\/final\/ yet/);
});
it('one picture of a layer, made on its own, is refused: it would not line up', async () => {
expect(await refusal(acceptPicture(await sheet(FIGURE), 'gardener-apron-pour', m, await theGuide()))).toMatch(/never one picture at a time/);
});
});