Files
sporel-tool-atlas-baker/tests/write-sprite.test.js

47 lines
2.0 KiB
JavaScript

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 Jimp = require('jimp');
const { writeSpriteOutputs, buildUvJson } = require('../src/write-sprite');
test('buildUvJson: emits atlas_id + atlas_size + sprites table', () => {
const placed = [
{ alias: 'bed1', x: 0, y: 0, w: 64, h: 32, sourceFile: 'Bed1.png' },
{ alias: 'sack', x: 64, y: 0, w: 32, h: 32, sourceFile: 'Sack.png' },
];
const json = buildUvJson('tcbasics', 96, 32, placed);
const parsed = JSON.parse(json);
assert.strictEqual(parsed.atlas_id, 'tcbasics');
assert.deepStrictEqual(parsed.atlas_size, [96, 32]);
assert.strictEqual(parsed.sprites.bed1.x, 0);
assert.strictEqual(parsed.sprites.bed1.w, 64);
assert.strictEqual(parsed.sprites.bed1.source_file, 'Bed1.png');
assert.strictEqual(parsed.sprites.sack.x, 64);
});
test('writeSpriteOutputs: produces 3 files', async (t) => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'write-sprite-test-'));
t.after(() => fs.rmSync(tmp, { recursive: true, force: true }));
// Build a 2-sprite atlas in memory
const a = new Jimp(32, 32, 0xff0000ff);
const b = new Jimp(32, 32, 0x00ff00ff);
const placed = [
{ alias: 'red', x: 0, y: 0, w: 32, h: 32, sourceFile: 'Red.png', image: a, padPx: 0 },
{ alias: 'green', x: 32, y: 0, w: 32, h: 32, sourceFile: 'Green.png', image: b, padPx: 0 },
];
await writeSpriteOutputs({
atlasId: 'test',
outDir: tmp,
placed,
boundsW: 64,
boundsH: 32,
lock: { atlas_id: 'test', version: 1, aliases: ['red', 'green'], sources: { red: 'Red.png', green: 'Green.png' } },
});
assert.ok(fs.existsSync(path.join(tmp, 'test/sprites.diffuse.atlas.png')));
assert.ok(fs.existsSync(path.join(tmp, 'test/sprites.uv.json')));
assert.ok(fs.existsSync(path.join(tmp, 'test/sprites.atlas.lock.json')));
});