[art-prep] ArtBoard: Build prompts, stand-ins and the art board from the manifest, and accept pictures into art/final

tools/art-prep reads art/STYLE.md (draft) and art/manifest.json (25 Phase 1
pictures), refuses forbidden words in any prompt, writes 29 stand-ins and the
board files, and cuts and encodes accepted pictures as WebP within budget.
This commit is contained in:
2026-11-01 20:16:59 +01:00
parent 1bfbd4a66c
commit f24597dfdb
50 changed files with 3199 additions and 1 deletions
+120
View File
@@ -0,0 +1,120 @@
import sharp from 'sharp';
import { describe, expect, it } from 'vitest';
import { BUDGET, Refused, acceptPicture } from '../src/accept.js';
import { readManifest } from './fixtures.js';
const m = readManifest();
const CLEAR = { r: 0, g: 0, b: 0, alpha: 0 };
/** A transparent canvas with soft blobs drawn where asked: what a well-behaved generator returns. */
async function picture(width: number, height: number, blobs: { x: number; y: number; w: number; h: number }[], background = CLEAR): Promise<Buffer> {
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">${blobs
.map((b) => `<ellipse cx="${b.x + b.w / 2}" cy="${b.y + b.h / 2}" rx="${b.w / 2}" ry="${b.h / 2}" fill="#c85a6e"/>`)
.join('')}</svg>`;
return sharp({ create: { width, height, channels: 4, background } }).composite([{ input: Buffer.from(svg) }]).png().toBuffer();
}
/** One blob in the middle of every cell of a grid, optionally leaving cells empty. */
const sheetBlobs = (width: number, height: number, cols: number, rows: number, skip: number[] = []) =>
Array.from({ length: cols * rows }, (_, i) => i)
.filter((i) => !skip.includes(i))
.map((i) => {
const cw = width / cols, ch = height / rows;
return { x: (i % cols) * cw + cw * 0.25, y: Math.floor(i / cols) * ch + ch * 0.25, w: cw * 0.5, h: ch * 0.5 };
});
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');
};
describe('art:accept', () => {
it('a single transparent picture becomes WebP at its size, within its budget', async () => {
const { files, notes } = await acceptPicture(await picture(1024, 1024, [{ x: 300, y: 300, w: 400, h: 400 }]), 'garden-chair', m);
expect(files).toHaveLength(1);
expect(files[0]).toMatchObject({ id: 'garden-chair', width: 1024, height: 1024 });
expect(files[0]!.data.subarray(8, 12).toString()).toBe('WEBP');
expect(files[0]!.data.length).toBeLessThanOrEqual(BUDGET.sprite);
expect(notes).toEqual([]);
});
it('refuses a transparent picture painted on a background', async () => {
const opaque = await picture(1024, 1024, [], { r: 251, g: 246, b: 241, alpha: 1 });
expect(await refusal(acceptPicture(opaque, 'garden-chair', m))).toMatch(/background is not transparent/);
});
it('cuts a sprite sheet into its cells, each at the size it ships at', async () => {
const { files } = await acceptPicture(await picture(1536, 1024, sheetBlobs(1536, 1024, 4, 1)), 'rose-stages-sheet', m);
expect(files.map((f) => f.id)).toEqual(['rose-bud', 'rose-opening', 'rose-half-bloom', 'rose-full-bloom']);
for (const f of files) expect([f.width, f.height]).toEqual([384, 1024]);
});
it('cuts by the grid whatever size the generator made, and says so', async () => {
const { files, notes } = await acceptPicture(await picture(1200, 1000, sheetBlobs(1200, 1000, 3, 2)), 'care-props-sheet', m);
expect(files).toHaveLength(6);
expect(notes.join('\n')).toMatch(/made at 1200 × 1000, not 1536 × 1024; cut by its 3 × 2 grid anyway/);
for (const f of files) expect(Math.max(f.width, f.height)).toBeLessThanOrEqual(512);
});
it('refuses the whole sheet when a cell is empty, naming the cell', async () => {
const img = await picture(1536, 1024, sheetBlobs(1536, 1024, 3, 2, [4]));
expect(await refusal(acceptPicture(img, 'care-props-sheet', m))).toBe('care-props-sheet: nothing drawn in petal-folded. The whole sheet is refused.');
});
it('refuses the whole sheet when a cell is painted on a background', async () => {
const blobs = sheetBlobs(1536, 1024, 3, 2);
blobs[2] = { x: 1024, y: 0, w: 512, h: 512 };
const img = await sharp(await picture(1536, 1024, blobs))
.composite([{ input: { create: { width: 512, height: 512, channels: 4, background: { r: 200, g: 90, b: 110, alpha: 1 } } }, left: 1024, top: 0 }])
.png().toBuffer();
expect(await refusal(acceptPicture(img, 'care-props-sheet', m))).toMatch(/painted on a background in weed-clump/);
});
it('warns when a drawing touches its cell edge, without refusing', async () => {
const blobs = sheetBlobs(1536, 1024, 4, 1);
blobs[0] = { x: 0, y: 200, w: 300, h: 600 };
const { notes } = await acceptPicture(await picture(1536, 1024, blobs), 'rose-stages-sheet', m);
expect(notes).toEqual(["rose-bud: the drawing touches its cell's edge; a neighbour may spill in."]);
});
it('takes one cell made on its own and fits it to the cell', async () => {
const { files } = await acceptPicture(await picture(1024, 1536, [{ x: 300, y: 200, w: 400, h: 1100 }]), 'rose-waiting', m);
expect(files).toHaveLength(1);
expect(files[0]!.id).toBe('rose-waiting');
expect(files[0]!.width).toBeLessThanOrEqual(384);
expect(files[0]!.height).toBeLessThanOrEqual(1024);
});
it('a garden layer needs a clear sky but may be solid below the horizon', async () => {
const ground = { input: { create: { width: 1024, height: 922, channels: 4 as const, background: { r: 169, g: 196, b: 142, alpha: 1 } } }, left: 0, top: 614 };
const good = await sharp(await picture(1024, 1536, [])).composite([ground]).png().toBuffer();
expect((await acceptPicture(good, 'garden-day', m)).files[0]).toMatchObject({ width: 1024, height: 1536 });
const painted = await sharp({ create: { width: 1024, height: 1536, channels: 4, background: { r: 207, g: 224, b: 234, alpha: 1 } } }).png().toBuffer();
expect(await refusal(acceptPicture(painted, 'garden-day', m))).toMatch(/sky is not transparent/);
});
it('a full-background picture of the wrong shape is cropped from the middle, and a small one never blown up', async () => {
const icon = await sharp({ create: { width: 900, height: 600, channels: 3, background: { r: 233, g: 132, b: 106 } } }).png().toBuffer();
const { files, notes } = await acceptPicture(icon, 'app-icon', m);
expect(notes.join('\n')).toMatch(/made at 900 × 600, not 1024 × 1024; cropped from the middle/);
expect(notes.join('\n')).toMatch(/only 900 px wide; kept at that size, never blown up/);
expect([files[0]!.width, files[0]!.height]).toEqual([600, 600]);
});
it('refuses a picture over its budget instead of shipping it', async () => {
const noise = Buffer.alloc(1024 * 1024 * 3);
for (let i = 0; i < noise.length; i++) noise[i] = (i * 2654435761) >>> 24;
const img = await sharp(noise, { raw: { width: 1024, height: 1024, channels: 3 } }).png().toBuffer();
expect(await refusal(acceptPicture(img, 'app-icon', m))).toMatch(/over its budget of 120 KB/);
});
it('refuses a name that is not in the manifest, and a file that is not a picture', async () => {
expect(await refusal(acceptPicture(Buffer.from('x'), 'fox-sheet', m))).toMatch(/"fox-sheet" is not a picture/);
expect(await refusal(acceptPicture(Buffer.from('not an image'), 'garden-chair', m))).toMatch(/could not be read as a picture/);
});
});
+61
View File
@@ -0,0 +1,61 @@
import { readFileSync, readdirSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { boardData } from '../src/board.js';
import { placeholders } from '../src/placeholders.js';
import { readManifest, readStyle, root } from './fixtures.js';
const manifest = readManifest();
const data = boardData(manifest, readStyle());
const stand = placeholders(manifest);
const page = readFileSync(`${root}tools/art-prep/board/index.html`, 'utf8');
describe('the board data', () => {
it('has the shape the board page reads', () => {
for (const a of data.assets) {
expect(a).toMatchObject({ id: expect.any(String), width: expect.any(Number), height: expect.any(Number), prompt: expect.any(String) });
expect(Array.isArray(a.usedBy) && Array.isArray(a.checks) && Array.isArray(a.attach)).toBe(true);
if (a.type === 'spritesheet') {
expect(a.cells).toHaveLength((a.grid?.cols ?? 0) * (a.grid?.rows ?? 0));
for (const c of a.cells ?? []) expect(c).toMatchObject({ id: expect.any(String), width: expect.any(Number), height: expect.any(Number), prompt: expect.any(String) });
}
}
expect(data.checklist).toHaveLength(8);
});
it('every kind in the manifest has a group on the board page, or its cards would never show', () => {
for (const type of new Set(data.assets.map((a) => a.type))) expect(page).toContain(`{ type: '${type}',`);
});
it('the page takes the product name from @rosea/schema, never spells it', () => {
expect(page).toContain('<title>%PRODUCT_NAME% Art Board</title>');
expect(page).not.toMatch(/Rosea Domi/);
});
it("shows Claude's review and the move into art/final/ when Claude writes them into a row", () => {
expect(page).toMatch(/r\.claude \? `Claude's review: \$\{r\.claude\}`/);
expect(page).toMatch(/r\.final \? `In art\/final\/:/);
});
});
describe('the stand-ins', () => {
it('one for every picture and every cell, at its own size', () => {
const ids = manifest.assets.flatMap((a) => [a.id, ...(a.cells ?? []).map((c) => c.id)]);
expect([...stand.keys()].sort()).toEqual([...ids].sort());
for (const a of manifest.assets) expect(stand.get(a.id)).toContain(`viewBox="0 0 ${a.width} ${a.height}"`);
expect(stand.get('rose-bud')).toContain('viewBox="0 0 384 1024"');
expect(stand.get('watering-can')).toContain('viewBox="0 0 512 512"');
});
it('load nothing from anywhere', () => {
for (const svg of stand.values()) {
expect(svg.replace('xmlns="http://www.w3.org/2000/svg"', '')).not.toMatch(/https?:|href=|<image|<script/);
}
});
it('art/placeholders/ holds exactly what the generator makes (run pnpm art:board after changing the manifest)', () => {
const dir = `${root}art/placeholders/`;
const files = readdirSync(dir).filter((f) => f.endsWith('.svg'));
expect(files.map((f) => f.slice(0, -4)).sort()).toEqual([...stand.keys()].sort());
for (const [id, svg] of stand) expect(readFileSync(`${dir}${id}.svg`, 'utf8').replace(/\r\n/g, '\n')).toBe(svg);
});
});
+10
View File
@@ -0,0 +1,10 @@
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import type { Manifest } from '../src/manifest.js';
export const root = fileURLToPath(new URL('../../../', import.meta.url));
export const readManifest = (): Manifest => JSON.parse(readFileSync(`${root}art/manifest.json`, 'utf8')) as Manifest;
export const readStyle = (): string => readFileSync(`${root}art/STYLE.md`, 'utf8');
/** A deep copy to break on purpose. */
export const clone = <T>(v: T): T => JSON.parse(JSON.stringify(v)) as T;
+63
View File
@@ -0,0 +1,63 @@
import { describe, expect, it } from 'vitest';
import { checkManifest, type Asset, type Manifest } from '../src/manifest.js';
import { reviewPoints } from '../src/style.js';
import { clone, readManifest, readStyle } from './fixtures.js';
const POINTS = reviewPoints(readStyle()).length;
/** The real manifest with one asset changed. */
function withAsset(id: string, change: (a: Record<string, unknown>) => void): Manifest {
const m = clone(readManifest());
const a = m.assets.find((x) => x.id === id) as unknown as Record<string, unknown>;
change(a);
return m;
}
describe('art/manifest.json', () => {
it('passes every check', () => {
expect(checkManifest(readManifest(), POINTS)).toEqual([]);
});
it('asks for the Phase 1 pictures: the rose sheet first, then stages, care, moments, props, chair, four garden layers, the icon', () => {
const m = readManifest();
expect(m.assets[0]?.id).toBe('rose-sheet');
const pictures = m.assets.flatMap((a: Asset) => (a.cells ? a.cells.map((c) => c.id) : [a.id]));
expect(pictures).toHaveLength(25);
expect(pictures).toEqual(expect.arrayContaining(['rose-bud', 'rose-full-bloom', 'rose-waiting', 'petal-folded', 'garden-chair', 'garden-sunset', 'garden-night', 'app-icon']));
});
});
describe('checkManifest refuses', () => {
const cases: [string, Manifest, RegExp][] = [
['an id used twice', withAsset('garden-day', (a) => { a.id = 'garden-night'; }), /used twice/],
['a cell id that repeats an asset id', withAsset('care-props-sheet', (a) => { (a.cells as { id: string }[])[0]!.id = 'garden-chair'; }), /garden-chair.*used twice/],
['an id that is not lower-case words', withAsset('garden-day', (a) => { a.id = 'Garden_Day'; }), /lower-case/],
['a size no generator offers', withAsset('garden-chair', (a) => { a.width = 2048; }), /not a size image generators offer/],
['a grid whose cells do not match', withAsset('rose-stages-sheet', (a) => { a.grid = { cols: 3, rows: 1 }; }), /4 cells for a 3 × 1 grid/],
['a grid that does not divide the sheet', withAsset('care-props-sheet', (a) => { a.grid = { cols: 3, rows: 3 }; (a.cells as unknown[]).push({ id: 'x-1', does: 'x' }, { id: 'x-2', does: 'x' }, { id: 'x-3', does: 'x' }); }), /does not divide/],
['a grid on a single picture', withAsset('garden-chair', (a) => { a.grid = { cols: 1, rows: 1 }; }), /only a sprite sheet/],
['an unknown character', withAsset('app-icon', (a) => { a.character = 'fox'; }), /unknown character "fox"/],
['a shipped character sheet', withAsset('rose-sheet', (a) => { a.shipped = true; }), /never shipped/],
['a sprite on a full background', withAsset('garden-chair', (a) => { a.background = 'full'; }), /must be transparent/],
['a review point STYLE.md §7 does not have', withAsset('garden-chair', (a) => { a.checks = [0, POINTS]; }), new RegExp(`review point ${POINTS} is not in`)],
['a picture with no subject', withAsset('garden-chair', (a) => { delete a.subject; }), /no subject/],
['a picture nobody uses', withAsset('garden-chair', (a) => { a.usedBy = []; }), /usedBy is empty/],
['a name from the book in a prompt', withAsset('garden-chair', (a) => { a.subject = 'a chair for the Little Prince'; }), /"Little Prince" \(little-prince\)/],
['a medical word in a cell', withAsset('rose-care-sheet', (a) => { (a.cells as { does: string }[])[0]!.does = 'thirsty, waiting to be healed'; }), /"healed" \(medical\)/],
['the word the game never says, in the shared avoid list', (() => { const m = clone(readManifest()); (m.avoid as string[]).push('a lonely look'); return m; })(), /avoid list: "lonely"/]
];
for (const [what, manifest, message] of cases) {
it(what, () => {
expect(checkManifest(manifest, POINTS).join('\n')).toMatch(message);
});
}
it('a character whose sheet is missing or not a sheet', () => {
const m = clone(readManifest()) as { characters: Record<string, { sheet: string }> } & Manifest;
m.characters.rose!.sheet = 'garden-chair';
expect(checkManifest(m, POINTS).join('\n')).toMatch(/"garden-chair" is a sprite, not a character sheet/);
m.characters.rose!.sheet = 'nowhere';
expect(checkManifest(m, POINTS).join('\n')).toMatch(/"nowhere" is not in the manifest/);
});
});
+96
View File
@@ -0,0 +1,96 @@
import { describe, expect, it } from 'vitest';
import { findForbidden } from '@rosea/content';
import { boardData } from '../src/board.js';
import { cellBoxes, cutLines, soloCanvas } from '../src/prompt.js';
import { reviewPoints, stylePrompt } from '../src/style.js';
import { clone, readManifest, readStyle } from './fixtures.js';
const style = stylePrompt(readStyle());
const data = boardData(readManifest(), readStyle());
const allPrompts = data.assets.flatMap((a) => [a.prompt, ...(a.cells ?? []).map((c) => c.prompt)]);
describe('STYLE.md', () => {
it('§1 is the quoted system prompt, line breaks kept and the markers gone', () => {
expect(style.startsWith('Soft, hand-made 2D storybook illustration painted in matte gouache')).toBe(true);
expect(style).toContain('\n');
expect(style).not.toMatch(/^>/m);
expect(style.endsWith('book, illustrator or film.')).toBe(true);
});
it('§7 has the eight review points, a point split over two lines read as one', () => {
const points = reviewPoints(readStyle());
expect(points).toHaveLength(8);
expect(points[3]).toBe('Nothing from the book: no glass dome, screen, tiny planet, desert, child or scarf, and no look of its drawings.');
});
it('says so when a section is missing, instead of building prompts without a style', () => {
expect(() => stylePrompt('# STYLE\n\n## 2. Palette\n')).toThrow(/no "## 1\." section/);
expect(() => reviewPoints('## 7. Review points\n\nNone yet.\n')).toThrow(/no review points/);
});
});
describe('the prompts', () => {
it('every one starts with STYLE.md §1 word for word: 11 cards and 18 one-at-a-time prompts', () => {
expect(allPrompts).toHaveLength(11 + 18);
for (const p of allPrompts) expect(p.startsWith(style + '\n\nAsset: ')).toBe(true);
});
it('every one carries the shared avoid list, so the likeness guard is never left out', () => {
for (const p of allPrompts) expect(p).toMatch(/Must avoid: .*a glass dome or bell jar; a folding screen; a tiny round planet/);
});
it('none names the book, Tolkien, or a medical or unnamed word', () => {
for (const p of allPrompts) expect(findForbidden(p)).toEqual([]);
});
it('a rose picture asks for the rose sheet as its reference; the sheet itself does not', () => {
const by = new Map(data.assets.map((a) => [a.id, a]));
expect(by.get('rose-sheet')?.attach).toEqual([]);
expect(by.get('rose-sheet')?.prompt).not.toContain('Attach the character sheet');
for (const id of ['rose-stages-sheet', 'rose-care-sheet', 'rose-moments-sheet', 'app-icon']) {
expect(by.get(id)?.attach).toEqual(['rose-sheet']);
expect(by.get(id)?.prompt).toContain('Attach the character sheet rose-sheet as the reference picture');
}
expect(by.get('care-props-sheet')?.attach).toEqual([]);
});
it('a manifest with a forbidden name never reaches the board', () => {
const m = clone(readManifest());
(m.assets[1] as { purpose: string }).purpose = 'In the style of Saint-Exupéry';
expect(() => boardData(m, readStyle())).toThrow(/Saint-Exupéry/);
});
});
describe('the grid', () => {
it('cuts are rounded once and include both edges', () => {
expect(cutLines(1536, 4)).toEqual([0, 384, 768, 1152, 1536]);
expect(cutLines(1000, 3)).toEqual([0, 333, 667, 1000]);
});
it('every safe box lies inside its cell, 12 % of the shorter side clear', () => {
for (const b of cellBoxes(1536, 1024, 3, 2)) {
expect(b.safe.x0 - b.x0).toBe(61);
expect(b.x1 - b.safe.x1).toBe(61);
expect(b.safe.y0).toBeGreaterThan(b.y0);
expect(b.safe.y1).toBeLessThan(b.y1);
}
});
it("a sheet's prompt gives each cell the numbers cellBoxes computes", () => {
const props = data.assets.find((a) => a.id === 'care-props-sheet');
expect(props?.prompt).toContain('Cut lines down the image at x = 512, 1024. Cut lines across the image at y = 512.');
expect(props?.prompt).toContain('- Cell 5, petal-folded: starts at (512, 512), ends at (1024, 1024); centre the drawing on (768, 768); safe box x 573–963, y 573–963.');
const stages = data.assets.find((a) => a.id === 'rose-stages-sheet');
expect(stages?.prompt).toContain('each 384 × 1024 px');
expect(stages?.prompt).not.toContain('Cut lines across');
});
it('a tall cell made on its own is made on the tall canvas, a square one on the square', () => {
const m = readManifest();
expect(soloCanvas(m.assets.find((a) => a.id === 'rose-stages-sheet')!)).toEqual({ width: 1024, height: 1536 });
expect(soloCanvas(m.assets.find((a) => a.id === 'care-props-sheet')!)).toEqual({ width: 1024, height: 1024 });
const bud = data.assets.find((a) => a.id === 'rose-stages-sheet')?.cells?.[0];
expect(bud?.prompt).toContain('Canvas: 1024 × 1536 px, fully transparent background. One subject, centred on (512, 768)');
expect(bud?.prompt).toContain('This picture: a tight bud');
});
});