Wire bake orchestrator and CLI

Connects scan + lock + pack + write into the full pipeline. CLI entry
parses args, dispatches to bake(), prints summary or error. Five
integration tests cover fresh bake, lock-aware ID preservation,
deleted-tile tracking, oversize hard-fail, and bit-identical
determinism across re-bakes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Axel Meyer
2026-05-21 22:48:08 +02:00
parent 589bdaef10
commit dea2d8fb88
7 changed files with 309 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
const { test } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');
const fs = require('node:fs');
const os = require('node:os');
const { bake } = require('../src/bake');
test('bake-deleted: missing source moves ID to deleted[]', async () => {
const srcDir = fs.mkdtempSync(path.join(os.tmpdir(), 'atlas-src-'));
const outDir = fs.mkdtempSync(path.join(os.tmpdir(), 'atlas-out-'));
try {
// Copy only grass + stone from fixture (omit water)
const FIX = path.resolve(__dirname, 'fixtures/small-fresh');
fs.copyFileSync(path.join(FIX, 'grass_diffuse.png'), path.join(srcDir, 'grass_diffuse.png'));
fs.copyFileSync(path.join(FIX, 'stone_diffuse.png'), path.join(srcDir, 'stone_diffuse.png'));
// Pre-existing lock has all 3 names
fs.writeFileSync(
path.join(outDir, 'tiles.atlas.lock.json'),
JSON.stringify({
atlas_id: 'small_demo',
bindings: { grass: 1, stone: 2, water: 3 },
deleted: [],
next_id: 4,
}, null, 2),
);
await bake({ inDir: srcDir, outDir, atlasId: 'small_demo', tileSize: 32 });
const lock = JSON.parse(fs.readFileSync(path.join(outDir, 'tiles.atlas.lock.json'), 'utf8'));
assert.strictEqual(lock.bindings.water, undefined);
assert.deepStrictEqual(lock.deleted, [3]);
assert.strictEqual(lock.next_id, 4); // not advanced
} finally {
fs.rmSync(srcDir, { recursive: true, force: true });
fs.rmSync(outDir, { recursive: true, force: true });
}
});

View File

@@ -0,0 +1,34 @@
const { test } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');
const fs = require('node:fs');
const os = require('node:os');
const crypto = require('node:crypto');
const { bake } = require('../src/bake');
const FIX = path.resolve(__dirname, 'fixtures/small-fresh');
function hashFile(p) {
return crypto.createHash('sha256').update(fs.readFileSync(p)).digest('hex');
}
test('bake-determinism: two fresh bakes produce identical outputs', async () => {
const out1 = fs.mkdtempSync(path.join(os.tmpdir(), 'atlas-det1-'));
const out2 = fs.mkdtempSync(path.join(os.tmpdir(), 'atlas-det2-'));
try {
await bake({ inDir: FIX, outDir: out1, atlasId: 'd', tileSize: 32 });
await bake({ inDir: FIX, outDir: out2, atlasId: 'd', tileSize: 32 });
const files = ['tiles.diffuse.atlas.png', 'tiles.height.atlas.png',
'tiles.atlas.json', 'tiles.atlas.lock.json'];
for (const f of files) {
assert.strictEqual(
hashFile(path.join(out1, f)),
hashFile(path.join(out2, f)),
`${f} not bit-identical`,
);
}
} finally {
fs.rmSync(out1, { recursive: true, force: true });
fs.rmSync(out2, { recursive: true, force: true });
}
});

43
tests/bake-fresh.test.js Normal file
View File

@@ -0,0 +1,43 @@
const { test } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');
const fs = require('node:fs');
const os = require('node:os');
const { bake } = require('../src/bake');
const FIX = path.resolve(__dirname, 'fixtures/small-fresh');
test('bake-fresh: 3 sources, no lock, IDs 1..3', async () => {
const outDir = fs.mkdtempSync(path.join(os.tmpdir(), 'atlas-bake-fresh-'));
try {
const result = await bake({
inDir: FIX,
outDir,
atlasId: 'small_demo',
tileSize: 32,
});
assert.strictEqual(result.tileCount, 3);
const atlas = JSON.parse(fs.readFileSync(path.join(outDir, 'tiles.atlas.json'), 'utf8'));
assert.strictEqual(atlas.atlas_id, 'small_demo');
assert.strictEqual(atlas.tiles.length, 3);
// Alphabetical sort: grass < stone < water
assert.strictEqual(atlas.tiles[0].name, 'grass');
assert.strictEqual(atlas.tiles[0].id, 1);
assert.strictEqual(atlas.tiles[1].name, 'stone');
assert.strictEqual(atlas.tiles[1].id, 2);
assert.strictEqual(atlas.tiles[2].name, 'water');
assert.strictEqual(atlas.tiles[2].id, 3);
const lock = JSON.parse(fs.readFileSync(path.join(outDir, 'tiles.atlas.lock.json'), 'utf8'));
assert.strictEqual(lock.bindings.grass, 1);
assert.strictEqual(lock.bindings.water, 3);
assert.strictEqual(lock.next_id, 4);
assert.deepStrictEqual(lock.deleted, []);
assert.ok(fs.existsSync(path.join(outDir, 'tiles.diffuse.atlas.png')));
assert.ok(fs.existsSync(path.join(outDir, 'tiles.height.atlas.png')));
} finally {
fs.rmSync(outDir, { recursive: true, force: true });
}
});

View File

@@ -0,0 +1,32 @@
const { test } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');
const fs = require('node:fs');
const os = require('node:os');
const { PNG } = require('pngjs');
const { bake } = require('../src/bake');
test('bake-oversize: tile that does not fit hard-fails', async () => {
const srcDir = fs.mkdtempSync(path.join(os.tmpdir(), 'atlas-over-src-'));
const outDir = fs.mkdtempSync(path.join(os.tmpdir(), 'atlas-over-out-'));
try {
// Single huge 256x256 tile, max canvas 128 -> cannot fit
const png = new PNG({ width: 256, height: 256 });
png.data.fill(255);
fs.writeFileSync(path.join(srcDir, 'huge_diffuse.png'), PNG.sync.write(png));
await assert.rejects(
() => bake({
inDir: srcDir,
outDir,
atlasId: 'over',
tileSize: 256,
maxSize: 128,
}),
/does not fit/,
);
} finally {
fs.rmSync(srcDir, { recursive: true, force: true });
fs.rmSync(outDir, { recursive: true, force: true });
}
});

View File

@@ -0,0 +1,43 @@
const { test } = require('node:test');
const assert = require('node:assert');
const path = require('node:path');
const fs = require('node:fs');
const os = require('node:os');
const { bake } = require('../src/bake');
const FIX = path.resolve(__dirname, 'fixtures/small-fresh');
test('bake-with-lock: existing IDs preserved, new tile appended', async () => {
const outDir = fs.mkdtempSync(path.join(os.tmpdir(), 'atlas-bake-lock-'));
try {
// Pre-seed lock with grass=5, stone=6 (non-default IDs)
fs.writeFileSync(
path.join(outDir, 'tiles.atlas.lock.json'),
JSON.stringify({
atlas_id: 'small_demo',
bindings: { grass: 5, stone: 6 },
deleted: [],
next_id: 7,
}, null, 2),
);
const result = await bake({
inDir: FIX,
outDir,
atlasId: 'small_demo',
tileSize: 32,
});
assert.strictEqual(result.tileCount, 3);
const lock = JSON.parse(fs.readFileSync(path.join(outDir, 'tiles.atlas.lock.json'), 'utf8'));
assert.strictEqual(lock.bindings.grass, 5); // preserved
assert.strictEqual(lock.bindings.stone, 6); // preserved
assert.strictEqual(lock.bindings.water, 7); // new
assert.strictEqual(lock.next_id, 8);
const atlas = JSON.parse(fs.readFileSync(path.join(outDir, 'tiles.atlas.json'), 'utf8'));
const ids = atlas.tiles.map(t => t.id).sort();
assert.deepStrictEqual(ids, [5, 6, 7]);
} finally {
fs.rmSync(outDir, { recursive: true, force: true });
}
});