Files
sprites/tools/deploy/test/config.test.ts
Christopher Monsanto 40bd3052f5 Make the deploy layer and tool I/O async
DeployCtx read/list/hash return promises (copy and write stay sync:
they only queue ops), the stamped copy helpers and buildfile deploy
blocks await them, ActionQueue materialization and packing use
fs/promises, and the CLI, config loaders, spritesheet writer, and
trim tool follow. Declaration-time globbing and the module-init data
reads stay sync: rules exist by the end of the import.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-21 01:13:15 -04:00

76 lines
3.2 KiB
TypeScript

import assert from 'node:assert/strict';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as pathlib from 'node:path';
import {test} from 'node:test';
import {loadDeployConfig, matchSubsets} from '../config.ts';
function configFile(text: string): string {
let dir = fs.mkdtempSync(pathlib.join(os.tmpdir(), 'deploy-config-test-'));
let p = pathlib.join(dir, 'deploy.json5');
fs.writeFileSync(p, text);
return p;
}
test('loadDeployConfig parses json5 with comments and trailing commas', async () => {
let config = await loadDeployConfig(configFile(`{
// dex assets
assets: {
buildFile: "assets.build.ts",
deploy: [
{subset: ["**"], cmd: "cat > /dev/null"},
],
},
}`));
assert.deepEqual([...config.keys()], ['assets']);
assert.deepEqual(config.get('assets'), {
buildFile: 'assets.build.ts',
deploy: [{subset: ['**'], cmd: 'cat > /dev/null'}],
});
});
test('loadDeployConfig ties the dir flag to %d in the cmd', async () => {
let dir = await loadDeployConfig(configFile(
'{ps: {buildFile: "ps.build.ts", deploy: [{subset: ["ani/**"], dir: true, cmd: "rsync -a %d/ani/ h:a/"}]}}'));
assert.equal(dir.get('ps')!.deploy[0]!.dir, true);
await assert.rejects(() => loadDeployConfig(configFile(
'{a: {buildFile: "x.build.ts", deploy: [{subset: ["**"], dir: true, cmd: "rsync -a h:a/"}]}}')),
/dir entry's cmd must use %d/);
await assert.rejects(() => loadDeployConfig(configFile(
'{a: {buildFile: "x.build.ts", deploy: [{subset: ["**"], cmd: "rsync -a %d/ h:a/"}]}}')),
/must not use %d \(missing dir: true\?\)/);
});
test('loadDeployConfig rejects missing files and malformed shapes', async () => {
await assert.rejects(() => loadDeployConfig('/nonexistent/deploy.json5'), /missing/);
await assert.rejects(() => loadDeployConfig(configFile('[1]')), /must be an object/);
await assert.rejects(() => loadDeployConfig(configFile('{a: {deploy: []}}')), /expected \{buildFile/);
await assert.rejects(() => loadDeployConfig(configFile(
'{a: {buildFile: "x.build.ts", deploy: [{subset: "**", cmd: "c"}]}}')),
/needs \{subset/);
await assert.rejects(() => loadDeployConfig(configFile('{a: {buildFile:')), /deploy.json5:/);
});
test('matchSubsets routes dsts to entries, overlap allowed', () => {
let dsts = ['xy/a.gif', 'xy/manifest.json', 'xyicons/b.png'];
let [xy, manifests] = matchSubsets(dsts, [
{subset: ['xy/**', 'xyicons/**'], cmd: 'one'},
{subset: ['**/manifest.json'], cmd: 'two'},
]);
assert.deepEqual([...xy!.matched].sort(), dsts);
assert.equal(xy!.entry.cmd, 'one');
assert.deepEqual([...manifests!.matched], ['xy/manifest.json']);
});
test('matchSubsets errors on a glob matching nothing', () => {
assert.throws(() => matchSubsets(['xy/a.gif'], [{subset: ['xy/**', 'zz/**'], cmd: 'c'}]),
/matches no outputs: zz\/\*\*/);
});
test('matchSubsets errors on uncovered outputs, listing them', () => {
assert.throws(() => matchSubsets(['xy/a.gif', 'stray.txt'], [{subset: ['xy/**'], cmd: 'c'}]),
/not covered by any deploy entry:\n {2}stray.txt/);
});