Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions test/benchmark.test.mjs
Original file line number Diff line number Diff line change
@@ -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'), '<html>Mock Index</html>');
await writeFile(join(root, 'dist/privacy/discord-bot/index.html'), '<html>Mock Privacy</html>');

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/);
});
68 changes: 68 additions & 0 deletions test/verify-contracts.test.mjs
Original file line number Diff line number Diff line change
@@ -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));
});
Loading