feat(lock-sprite): stable alias-set + source-file persistence across re-bakes
This commit is contained in:
80
tests/lock-sprite.test.js
Normal file
80
tests/lock-sprite.test.js
Normal file
@@ -0,0 +1,80 @@
|
||||
const { test } = require('node:test');
|
||||
const assert = require('node:assert');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const os = require('node:os');
|
||||
const { loadSpriteLock, mergeSpriteLock, serializeSpriteLock } = require('../src/lock-sprite');
|
||||
|
||||
test('loadSpriteLock: missing file -> null', () => {
|
||||
const result = loadSpriteLock('/nonexistent/lock.json');
|
||||
assert.strictEqual(result, null);
|
||||
});
|
||||
|
||||
test('loadSpriteLock: reads valid lock', () => {
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'lock-test-'));
|
||||
const lockPath = path.join(tmp, 'lock.json');
|
||||
fs.writeFileSync(lockPath, JSON.stringify({
|
||||
atlas_id: 'tcbasics',
|
||||
version: 1,
|
||||
aliases: ['bed1', 'bench1'],
|
||||
sources: { bed1: 'Bed1.png', bench1: 'Bench1.png' },
|
||||
}));
|
||||
const lock = loadSpriteLock(lockPath);
|
||||
assert.strictEqual(lock.atlas_id, 'tcbasics');
|
||||
assert.deepStrictEqual(lock.aliases, ['bed1', 'bench1']);
|
||||
});
|
||||
|
||||
test('mergeSpriteLock: no existing lock -> all new', () => {
|
||||
const sources = [
|
||||
{ alias: 'bed1', sourceFile: 'Bed1.png' },
|
||||
{ alias: 'bench1',sourceFile: 'Bench1.png' },
|
||||
];
|
||||
const { merged, removed } = mergeSpriteLock(null, sources);
|
||||
assert.deepStrictEqual(merged.aliases, ['bed1', 'bench1']);
|
||||
assert.deepStrictEqual(removed, []);
|
||||
});
|
||||
|
||||
test('mergeSpriteLock: existing alias preserved; new appended', () => {
|
||||
const existing = {
|
||||
atlas_id: 'tcbasics',
|
||||
version: 1,
|
||||
aliases: ['bed1'],
|
||||
sources: { bed1: 'Bed1.png' },
|
||||
};
|
||||
const sources = [
|
||||
{ alias: 'bed1', sourceFile: 'Bed1.png' },
|
||||
{ alias: 'bench1', sourceFile: 'Bench1.png' },
|
||||
];
|
||||
const { merged, removed } = mergeSpriteLock(existing, sources);
|
||||
assert.deepStrictEqual(merged.aliases, ['bed1', 'bench1']);
|
||||
assert.deepStrictEqual(removed, []);
|
||||
});
|
||||
|
||||
test('mergeSpriteLock: removed alias appears in `removed`', () => {
|
||||
const existing = {
|
||||
atlas_id: 'tcbasics',
|
||||
version: 1,
|
||||
aliases: ['bed1', 'bench1'],
|
||||
sources: { bed1: 'Bed1.png', bench1: 'Bench1.png' },
|
||||
};
|
||||
const sources = [
|
||||
{ alias: 'bed1', sourceFile: 'Bed1.png' },
|
||||
];
|
||||
const { merged, removed } = mergeSpriteLock(existing, sources);
|
||||
assert.deepStrictEqual(merged.aliases, ['bed1']);
|
||||
assert.deepStrictEqual(removed, ['bench1']);
|
||||
});
|
||||
|
||||
test('serializeSpriteLock: stable JSON shape', () => {
|
||||
const lock = {
|
||||
atlas_id: 'tcbasics',
|
||||
version: 1,
|
||||
aliases: ['bed1'],
|
||||
sources: { bed1: 'Bed1.png' },
|
||||
};
|
||||
const json = serializeSpriteLock(lock);
|
||||
const parsed = JSON.parse(json);
|
||||
assert.deepStrictEqual(parsed, lock);
|
||||
// Trailing newline for POSIX-friendliness
|
||||
assert.ok(json.endsWith('\n'));
|
||||
});
|
||||
Reference in New Issue
Block a user