ktg-plugin-marketplace/scripts/sync-design-system.test.mjs

60 lines
2.8 KiB
JavaScript

// Step 2 test — --source parity (SC5), --check pass/fail, in-repo default fallback.
// Pattern: plugins/voyage/tests/scripts/*.test.mjs (node:test, zero deps).
// All vendoring is done into throwaway --target dirs so the repo's own vendored trees are never touched.
import { test, after } from 'node:test';
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdtempSync, rmSync, cpSync, readFileSync, appendFileSync, mkdirSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const here = path.dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = path.resolve(here, '..');
const SCRIPT = path.join(REPO_ROOT, 'scripts', 'sync-design-system.mjs');
const IN_REPO_DS = path.join(REPO_ROOT, 'shared', 'playground-design-system');
const VENDOR_REL = path.join('playground', 'vendor', 'playground-design-system');
const tmp = mkdtempSync(path.join(os.tmpdir(), 'ds-sync-'));
after(() => rmSync(tmp, { recursive: true, force: true }));
const run = (args) => spawnSync(process.execPath, [SCRIPT, ...args], { encoding: 'utf8' });
const manifestOf = (pluginRoot) =>
JSON.parse(readFileSync(path.join(pluginRoot, VENDOR_REL, 'MANIFEST.json'), 'utf8'));
const pluginA = path.join(tmp, 'pluginA');
const pluginB = path.join(tmp, 'pluginB');
const dsCopy = path.join(tmp, 'ds-copy');
test('default (no --source) vendors from the in-repo design system', () => {
mkdirSync(pluginA, { recursive: true });
const r = run(['dummy', '--target', pluginA]);
assert.equal(r.status, 0, r.stderr);
const m = manifestOf(pluginA);
assert.equal(m.source, 'shared/playground-design-system/');
assert.ok(m.file_count > 0, 'expected vendored files');
});
test('--source <dir> vendors byte-identically to the in-repo default (SC5 parity)', () => {
cpSync(IN_REPO_DS, dsCopy, { recursive: true });
mkdirSync(pluginB, { recursive: true });
const r = run(['dummy', '--source', dsCopy, '--target', pluginB]);
assert.equal(r.status, 0, r.stderr);
const a = manifestOf(pluginA);
const b = manifestOf(pluginB);
assert.deepEqual(b.files, a.files, 'vendored hashes must match the default source');
assert.equal(b.source, dsCopy, '--source label recorded in MANIFEST');
});
test('--check passes (exit 0, MANIFEST OK) on an unmodified vendored tree', () => {
const r = run(['dummy', '--check', '--target', pluginA]);
assert.equal(r.status, 0, r.stderr);
assert.match(r.stdout, /MANIFEST OK/);
});
test('--check fails (exit 2) after a tampered byte', () => {
appendFileSync(path.join(pluginA, VENDOR_REL, 'tokens.css'), '\n/* tampered */\n');
const r = run(['dummy', '--check', '--target', pluginA]);
assert.equal(r.status, 2, `expected exit 2, got ${r.status}: ${r.stderr}`);
assert.match(r.stderr, /MANIFEST DRIFT/);
});