diff --git a/test/benchmark.test.mjs b/test/benchmark.test.mjs new file mode 100644 index 0000000..44df534 --- /dev/null +++ b/test/benchmark.test.mjs @@ -0,0 +1,49 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { cp, mkdtemp, rm, writeFile, mkdir } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { execFile } from 'node:child_process'; +import { promisify } from 'node:util'; + +const run = promisify(execFile); + +async function fixture(t) { + const root = await mkdtemp(join(tmpdir(), 'freedom-benchmark-test-')); + t.after(() => rm(root, { recursive: true, force: true })); + for (const directory of ['scripts']) { + await cp(new URL(`../${directory}/`, import.meta.url), join(root, directory), { recursive: true }); + } + return { root, run: () => run(process.execPath, ['scripts/benchmark.mjs'], { cwd: root }) }; +} + +test('benchmark script handles successful file size measurements', async (t) => { + const { root, run } = await fixture(t); + + // Create mock dist directory and files + await mkdir(join(root, 'dist/privacy/discord-bot'), { recursive: true }); + await writeFile(join(root, 'dist/index.html'), 'Mock Index'); + await writeFile(join(root, 'dist/privacy/discord-bot/index.html'), 'Mock Privacy'); + + const { stdout, stderr } = await run(); + + assert.equal(stderr, ''); + assert.match(stdout, /--- Performance Benchmark ---/); + assert.match(stdout, /1\. File Sizes/); + assert.match(stdout, /dist\/index\.html: [0-9.]+ KB/); + assert.match(stdout, /dist\/privacy\/discord-bot\/index\.html: [0-9.]+ KB/); + assert.match(stdout, /2\. Font Loading Performance/); + assert.match(stdout, /3\. Hydration Speed/); +}); + +test('benchmark script gracefully handles missing files', async (t) => { + const { root, run } = await fixture(t); + + // Ensure we don't have dist directory in the root + const { stdout, stderr } = await run(); + + assert.equal(stderr, ''); + assert.match(stdout, /--- Performance Benchmark ---/); + assert.match(stdout, /\.\.\/dist\/index\.html: File not found/); + assert.match(stdout, /\.\.\/dist\/privacy\/discord-bot\/index\.html: File not found/); +}); diff --git a/test/verify-contracts.test.mjs b/test/verify-contracts.test.mjs new file mode 100644 index 0000000..53fdfda --- /dev/null +++ b/test/verify-contracts.test.mjs @@ -0,0 +1,68 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { cp, mkdtemp, rm, writeFile, readFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { execFile } from 'node:child_process'; +import { promisify } from 'node:util'; + +const run = promisify(execFile); + +async function fixture(t) { + const root = await mkdtemp(join(tmpdir(), 'freedom-verify-contracts-test-')); + t.after(() => rm(root, { recursive: true, force: true })); + for (const directory of ['scripts', 'vendor']) { + await cp(new URL(`../${directory}/`, import.meta.url), join(root, directory), { recursive: true }); + } + await cp(new URL('../contracts.lock.json', import.meta.url), join(root, 'contracts.lock.json')); + return { + root, + run: () => run(process.execPath, ['scripts/verify-contracts.mjs'], { cwd: root }) + }; +} + +test('verify-contracts succeeds with valid canonical repository', async (t) => { + const { run } = await fixture(t); + const { stdout, stderr } = await run(); + assert.equal(stderr, ''); + assert.match(stdout, /artifacts verified/); +}); + +test('verify-contracts rejects invalid source repository', async (t) => { + const { root, run } = await fixture(t); + const lockPath = join(root, 'contracts.lock.json'); + const lock = JSON.parse(await readFile(lockPath, 'utf8')); + lock.source_repository = 'Fake/Repository'; + await writeFile(lockPath, JSON.stringify(lock)); + await assert.rejects(run(), error => error.code === 1 && /Invalid canonical contract source/.test(error.stderr)); +}); + +test('verify-contracts rejects modified bundle digest in lock', async (t) => { + const { root, run } = await fixture(t); + const lockPath = join(root, 'contracts.lock.json'); + const lock = JSON.parse(await readFile(lockPath, 'utf8')); + lock.bundle_sha256 = '1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; + await writeFile(lockPath, JSON.stringify(lock)); + await assert.rejects(run(), error => error.code === 1 && /Contract bundle digest differs from lock/.test(error.stderr)); +}); + +test('verify-contracts rejects modified canonical artifact file content', async (t) => { + const { root, run } = await fixture(t); + const protocolPath = join(root, 'vendor/freedom-platform/protocol.mjs'); + const content = await readFile(protocolPath, 'utf8'); + await writeFile(protocolPath, content + '\n// modified'); + await assert.rejects(run(), error => error.code === 1 && /Modified canonical artifact protocol.mjs/.test(error.stderr)); +}); + +test('verify-contracts rejects missing canonical artifact file', async (t) => { + const { root, run } = await fixture(t); + const protocolPath = join(root, 'vendor/freedom-platform/protocol.mjs'); + await rm(protocolPath); + await assert.rejects(run(), error => error.code === 1 && /ENOENT: no such file or directory/.test(error.stderr)); +}); + +test('verify-contracts rejects unexpected file in vendor bundle', async (t) => { + const { root, run } = await fixture(t); + await writeFile(join(root, 'vendor/freedom-platform/unexpected.txt'), 'hello'); + await assert.rejects(run(), error => error.code === 1 && /Unexpected bundle file unexpected\.txt/.test(error.stderr)); +});