f24597dfdb
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.
121 lines
6.9 KiB
TypeScript
121 lines
6.9 KiB
TypeScript
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/);
|
||
});
|
||
});
|