// Exercise still through the real CLI: its PNG must match a bundle frame, with the same safe file writes. import test from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const root = fileURLToPath(new URL('../', import.meta.url)); const executable = fileURLToPath(new URL('../bin/pixelbolt.js', import.meta.url)); const cli = (args, input) => spawnSync(process.execPath, [executable, ...args], { cwd: root, input, encoding: 'utf8' }); const accepted = out => { assert.equal(out.status, 0, out.stderr || out.error?.message); assert.equal(out.stderr, ''); return JSON.parse(out.stdout); }; const refused = out => { assert.equal(out.status, 1, out.stdout || out.error?.message); assert.equal(out.stdout, ''); const reply = JSON.parse(out.stderr); assert.equal(reply.ok, false); return reply; }; // The CLI names a file inside its working folder relatively (CI's temporary folder is inside the checkout). const at = value => path.resolve(root, value); const temp = async t => { const dir = await mkdtemp(path.join(os.tmpdir(), 'pixelbolt-still-')); t.after(() => rm(dir, { recursive: true, force: true })); return dir; }; const icon = () => ({ version: 1, name: 'icon', width: 3, height: 2, palette: { k: '#123456' }, frames: [{ name: 'rest', ops: [{ op: 'pixel', x: 1, y: 0, color: 'k' }] }], animations: {}, sheet: { scale: 4 } }); test('still writes the exact later or first frame PNG from render at scale 3', async t => { const dir = await temp(t), bundle = path.join(dir, 'bundle'); const recipe = JSON.parse(await readFile(path.join(root, 'examples/coin.json'), 'utf8')); accepted(cli(['render', 'examples/coin.json', '--scale', '3', '--out', bundle])); for (const name of ['turn-3', undefined]) { const frame = name ?? recipe.frames[0].name, file = path.join(dir, name ? 'later.png' : 'first.png'); const reply = accepted(cli(['still', 'examples/coin.json', '--out', file, '--scale', '3', ...(name ? ['--frame', name] : [])])); const bytes = await readFile(file); assert.deepEqual(bytes, await readFile(path.join(bundle, 'frames', `${frame}.png`))); assert.deepEqual({ ...reply, file: at(reply.file) }, { ok: true, name: recipe.name, frame, width: recipe.width * 3, height: recipe.height * 3, scale: 3, bytes: bytes.length, file, ...(name ? {} : { frames: recipe.frames.map(frame => frame.name) }) }); assert.equal(bytes.readUInt32BE(16), reply.width); assert.equal(bytes.readUInt32BE(20), reply.height); } assert.deepEqual((await readdir(dir)).sort(), ['bundle', 'first.png', 'later.png']); }); test('a single frame omits the frames list and, without --scale, is render\'s frame at the recipe\'s sheet.scale', async t => { const dir = await temp(t), source = path.join(dir, 'icon.json'), file = path.join(dir, 'icon.PNG'); await writeFile(source, JSON.stringify(icon())); const reply = accepted(cli(['still', source, '--out', file])); const bytes = await readFile(file); assert.deepEqual({ ...reply, file: at(reply.file) }, { ok: true, name: 'icon', frame: 'rest', width: 12, height: 8, scale: 4, bytes: bytes.length, file }); assert.deepEqual([bytes.readUInt32BE(16), bytes.readUInt32BE(20)], [12, 8]); const bundle = path.join(dir, 'bundle'); accepted(cli(['render', source, '--out', bundle])); assert.deepEqual(bytes, await readFile(path.join(bundle, 'frames/rest.png'))); // --scale 1 is the frame's own size, whatever sheet.scale says. const small = accepted(cli(['still', source, '--scale', '1', '--out', path.join(dir, 'small.png')])); assert.deepEqual([small.width, small.height, small.scale], [3, 2, 1]); }); for (const [label, options, message] of [ ['missing --out', [], 'still requires --out '], ['non-PNG --out', ['--out', 'x.gif'], 'still requires --out '], ...['0', '17', '1.5', 'large'].map(scale => [`scale ${scale}`, ['--scale', scale], '--scale expects a whole number from 1 to 16: how many times to enlarge every exported pixel']) ]) test(`still refuses ${label}`, async t => { const dir = await temp(t); const args = ['still', 'examples/coin.json', ...options]; if (label.startsWith('scale')) args.push('--out', path.join(dir, 'icon.png')); assert.equal(refused(cli(args)).error, message); assert.deepEqual(await readdir(dir), []); }); test('still matches frame names exactly and names available frames on a miss', async t => { const dir = await temp(t), file = path.join(dir, 'icon.png'); const { error } = refused(cli(['still', 'examples/coin.json', '--frame', 'Turn-0', '--out', file])); assert.equal(error, '--frame "Turn-0" is not in recipe "coin"; its frames: turn-0, turn-1, turn-2, turn-3, turn-4, turn-5'); const recipe = icon(); recipe.frames = Array.from({ length: 15 }, (_, i) => ({ name: `frame-${i}`, ops: [] })); const long = refused(cli(['still', '-', '--frame', 'absent', '--out', file], JSON.stringify(recipe))); assert.equal(long.error, `--frame "absent" is not in recipe "icon"; its frames: ${recipe.frames.slice(0, 12).map(frame => frame.name).join(', ')} and 3 more`); // Without --frame the reply names at most 12 frames and counts the rest; with it, none. const listed = accepted(cli(['still', '-', '--out', file], JSON.stringify(recipe))); assert.deepEqual([listed.frame, listed.frames, listed.moreFrames], ['frame-0', recipe.frames.slice(0, 12).map(frame => frame.name), 3]); const picked = accepted(cli(['still', '-', '--frame', 'frame-14', '--out', file, '--force'], JSON.stringify(recipe))); assert.deepEqual([picked.frame, picked.frames, picked.moreFrames], ['frame-14', undefined, undefined]); assert.deepEqual(await readdir(dir), ['icon.png']); }); test('still preserves existing files unless --force replaces them', async t => { const dir = await temp(t), file = path.join(dir, 'icon.png'), old = Buffer.from('earlier output'); await writeFile(file, old); const args = ['still', 'examples/coin.json', '--out', file]; assert.equal(refused(cli(args)).error, `Output already exists: ${file}. Nothing was written, so that file is an earlier run's, not this one's. Choose a new file or pass --force.`); assert.deepEqual(await readFile(file), old); const reply = accepted(cli([...args, '--force'])), bytes = await readFile(file); assert.equal(bytes.subarray(0, 8).toString('hex'), '89504e470d0a1a0a'); assert.equal(reply.bytes, bytes.length); assert.equal(at(reply.file), file); assert.deepEqual(await readdir(dir), ['icon.png']); }); test('still never overwrites its recipe or referenced palette, even with --force', async t => { const dir = await temp(t), source = path.join(dir, 'source.png'), palette = path.join(dir, 'palette.png'); const recipe = icon(); await writeFile(palette, JSON.stringify(recipe.palette)); recipe.palette = { $ref: 'palette.png' }; await writeFile(source, JSON.stringify(recipe)); for (const file of [source, palette]) { const before = await readFile(file); const { error } = refused(cli(['still', source, '--out', file, '--force'])); assert.equal(error, `${file} is a file this command reads; even --force never writes over it. Choose another file; no files were written.`); assert.deepEqual(await readFile(file), before); } }); test('still refuses compiler sources with the render guidance naming still', async t => { const dir = await temp(t), source = 'examples/effects.fx.json'; const still = refused(cli(['still', source, '--out', path.join(dir, 'icon.png')])); const render = refused(cli(['render', source, '--out', path.join(dir, 'bundle')])); assert.equal(still.error, render.error.replace('then render the recipe', 'then run still on the recipe')); assert.match(still.error, /this is a pixelforge-fx source: compile it first .*then run still on the recipe it writes/); assert.equal(still.path, 'project.format'); assert.deepEqual(await readdir(dir), []); }); test('still reads a recipe from stdin', async t => { const dir = await temp(t), file = path.join(dir, 'icon.png'); const reply = accepted(cli(['still', '-', '--out', file, '--scale', '4'], JSON.stringify(icon()))); assert.equal(reply.frame, 'rest'); assert.deepEqual([reply.width, reply.height, reply.scale], [12, 8, 4]); assert.equal(reply.bytes, (await readFile(file)).length); }); test('still reports project warnings and resolved palette paths', async t => { const dir = await temp(t), source = path.join(dir, 'icon.json'), palette = path.join(dir, 'palette.json'); const recipe = icon(); await writeFile(palette, JSON.stringify(recipe.palette)); recipe.palette = { $ref: 'palette.json' }; recipe.frames[0].ops.push({ op: 'pixel', x: 3, y: 0, color: 'k' }); await writeFile(source, JSON.stringify(recipe)); const reply = accepted(cli(['still', source, '--out', path.join(dir, 'icon.png')])); const render = accepted(cli(['render', source, '--scale', '1', '--out', path.join(dir, 'bundle')])); assert.deepEqual(reply.resolved.map(at), [palette]); assert.deepEqual(reply.warnings, render.warnings); assert.match(reply.warnings[0], /Some drawing falls outside the canvas/); }); test('still compiles sprite stacks as render does', async t => { const dir = await temp(t), source = 'examples/iso-crate.stack.json', bundle = path.join(dir, 'bundle'), file = path.join(dir, 'crate.png'); accepted(cli(['render', source, '--scale', '2', '--out', bundle])); const reply = accepted(cli(['still', source, '--scale', '2', '--out', file])); assert.equal(reply.name, 'iso-crate'); assert.deepEqual(await readFile(file), await readFile(path.join(bundle, 'frames', `${reply.frame}.png`))); }); test('still refuses an oversized PNG before scaling and names the largest scale that fits', async t => { const dir = await temp(t), recipe = { ...icon(), width: 1024, height: 1024, sheet: { scale: 1, trim: true } }; const reply = refused(cli(['still', '-', '--scale', '5', '--out', path.join(dir, 'large.png')], JSON.stringify(recipe))); assert.equal(reply.path, '--scale'); assert.match(reply.error, /a 1024×1024 frame at scale 5 is 5120×5120, past the 16,777,216 pixels one PNG holds; scale 4 is the largest that fits$/); assert.deepEqual(await readdir(dir), []); }); test('still help lists its options and keeps its notes with its command', () => { for (const args of [['help'], ['still', '--help']]) { const out = cli(args); assert.equal(out.status, 0, out.stderr); assert.match(out.stdout, /pixelbolt still --out \[--frame name\] \[--scale 1-16\] \[--force\]/); assert.match(out.stdout, /still writes the first frame unless --frame names one exactly/); assert.match(out.stdout, /Default scale: the recipe's sheet\.scale; --scale \(1 to 16\) sets that one frame's scale/); } const out = cli(['gif-frames', '--help']); assert.equal(out.status, 0, out.stderr); assert.doesNotMatch(out.stdout, /still writes/); });