import assert from 'node:assert/strict'; import { promises as fs } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import test from 'node:test'; import { syncSkill } from '../scripts/sync-frontend-fidelity.mjs'; async function fixture(t) { const root = await fs.mkdtemp(path.join(os.tmpdir(), 'fidelity-sync-')); t.after(() => fs.rm(root, { recursive: true, force: true })); const source = path.join(root, '.claude/skills/frontend-fidelity'); const target = path.join(root, '.agents/skills/frontend-fidelity'); await fs.mkdir(path.join(source, 'references'), { recursive: true }); await fs.writeFile( path.join(source, 'SKILL.md'), '---\nname: frontend-fidelity\n---\n', ); await fs.writeFile(path.join(source, 'references/guide.md'), 'Original\n'); return { root, source, target }; } test('fresh installation, read-only check, and idempotent synchronization', async (t) => { const { root, source, target } = await fixture(t); await assert.rejects(syncSkill(root, { check: true }), /out of sync/); await assert.rejects(fs.access(target)); assert.equal((await syncSkill(root)).changed, 2); assert.equal((await syncSkill(root, { check: true })).files, 2); assert.equal((await syncSkill(root)).changed, 0); assert.equal( await fs.readFile(path.join(target, 'references/guide.md'), 'utf8'), 'Original\n', ); await fs.writeFile(path.join(source, 'references/guide.md'), 'Updated\n'); await assert.rejects(syncSkill(root, { check: true }), /out of sync/); assert.equal((await syncSkill(root)).changed, 1); assert.equal( await fs.readFile(path.join(target, 'references/guide.md'), 'utf8'), 'Updated\n', ); }); test('independent changes are preserved before any source updates are written', async (t) => { const { root, source, target } = await fixture(t); await syncSkill(root); await fs.writeFile(path.join(source, 'SKILL.md'), 'New source\n'); await fs.writeFile(path.join(target, 'references/guide.md'), 'User edit\n'); await assert.rejects(syncSkill(root), /changed independently/); assert.equal( await fs.readFile(path.join(target, 'references/guide.md'), 'utf8'), 'User edit\n', ); assert.match( await fs.readFile(path.join(target, 'SKILL.md'), 'utf8'), /name: frontend-fidelity/, ); }); test('a preexisting unowned target is not overwritten', async (t) => { const { root, target } = await fixture(t); await fs.mkdir(target, { recursive: true }); await fs.writeFile(path.join(target, 'SKILL.md'), 'User skill'); await assert.rejects(syncSkill(root), /no manifest/); assert.equal( await fs.readFile(path.join(target, 'SKILL.md'), 'utf8'), 'User skill', ); }); for (const mutation of [ 'unknown', 'missing', 'removed-source', 'invalid-manifest', ]) { test(`reject ${mutation} without silently accepting drift`, async (t) => { const { root, source, target } = await fixture(t); await syncSkill(root); if (mutation === 'unknown') await fs.writeFile(path.join(target, 'custom.md'), 'Custom'); if (mutation === 'missing') await fs.unlink(path.join(target, 'references/guide.md')); if (mutation === 'removed-source') await fs.unlink(path.join(source, 'references/guide.md')); if (mutation === 'invalid-manifest') await fs.writeFile(path.join(target, '.sync-manifest.json'), '{}'); await assert.rejects(syncSkill(root)); await assert.rejects(syncSkill(root, { check: true })); }); } test('line ending normalization works across Git checkouts', async (t) => { const { root, target } = await fixture(t); await syncSkill(root); const file = path.join(target, 'SKILL.md'); await fs.writeFile( file, (await fs.readFile(file, 'utf8')).replace(/\n/g, '\r\n'), ); assert.equal((await syncSkill(root, { check: true })).status, 'PASS'); }); test('Python bytecode caches do not enter the distributed skill', async (t) => { const { root, source, target } = await fixture(t); await fs.mkdir(path.join(source, '__pycache__')); await fs.writeFile(path.join(source, '__pycache__/cache.pyc'), 'cache'); assert.equal((await syncSkill(root)).files, 2); await assert.rejects(fs.access(path.join(target, '__pycache__'))); }); test('binary assets are compared byte for byte', async (t) => { const { root, source, target } = await fixture(t); await fs.writeFile(path.join(source, 'asset.png'), Buffer.from([0x80, 0x0a])); await syncSkill(root); await fs.writeFile(path.join(source, 'asset.png'), Buffer.from([0x81, 0x0a])); await assert.rejects(syncSkill(root, { check: true }), /out of sync/); await syncSkill(root); assert.deepEqual( await fs.readFile(path.join(target, 'asset.png')), Buffer.from([0x81, 0x0a]), ); }); test('linked target directories cannot redirect writes outside the project', async (t) => { const { root, target } = await fixture(t); const outside = await fs.mkdtemp( path.join(os.tmpdir(), 'fidelity-external-'), ); t.after(() => fs.rm(outside, { recursive: true, force: true })); await fs.mkdir(path.dirname(target), { recursive: true }); await fs.symlink( outside, target, process.platform === 'win32' ? 'junction' : 'dir', ); await assert.rejects(syncSkill(root), /ordinary directory/); assert.deepEqual(await fs.readdir(outside), []); });