import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { execFileSync } from 'node:child_process'; import { inflateRawSync } from 'node:zlib'; import { createHash } from 'node:crypto'; import { packageSource } from '../scripts/package-source.mjs'; import { createProgramGuides } from '../scripts/documentation-bundle.mjs'; function zipFiles(file) { const bytes = fs.readFileSync(file), end = bytes.lastIndexOf(Buffer.from([0x50, 0x4b, 0x05, 0x06])); assert.ok(end >= 0); const count = bytes.readUInt16LE(end + 10), files = new Map(); let offset = bytes.readUInt32LE(end + 16); for (let index = 0; index < count; index++) { assert.equal(bytes.readUInt32LE(offset), 0x02014b50); const nameLength = bytes.readUInt16LE(offset + 28), extraLength = bytes.readUInt16LE(offset + 30), commentLength = bytes.readUInt16LE(offset + 32); const name = bytes.subarray(offset + 46, offset + 46 + nameLength).toString('utf8'); const local = bytes.readUInt32LE(offset + 42), start = local + 30 + bytes.readUInt16LE(local + 26) + bytes.readUInt16LE(local + 28); const compressed = bytes.subarray(start, start + bytes.readUInt32LE(offset + 20)); files.set(name, bytes.readUInt16LE(offset + 10) === 8 ? inflateRawSync(compressed) : compressed); offset += 46 + nameLength + extraLength + commentLength; } return files; } test('source ZIP exports committed program bytes without runtime, user data, dirty inputs or historical evidence', t => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'awb-source-')); t.after(() => fs.rmSync(root, { recursive: true, force: true })); const write = (name, content) => { const file = path.join(root, name); fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, content); }; const git = args => execFileSync('git', args, { cwd: root, windowsHide: true, encoding: 'utf8' }).trim(); git(['init']); write('package.json', JSON.stringify({ name: 'owned-source', version: '1.25.6' })); write('package-lock.json', '{}\n'); write('.gitattributes', '*.cmd text eol=crlf\n'); write('Start-Workbench.cmd', '@echo off\necho owned\n'); const original = Buffer.from('\ufeff// 中文原字节\r\nexport const value = 42;\r\n'); write('src/中文.mjs', original); for (const name of ['scripts/build.mjs', 'tests/owned.test.mjs', 'docs/PACKAGING.md', 'docs/research/NOTES.md']) write(name, 'Committed guide or code\n'); for (const name of ['data/workbench.sqlite', '.data/session.json', '.env', 'dist-client/index.html', 'runtime/node.exe', 'docs/historical-evidence/raw.json']) write(name, 'OWNED-EXCLUDED-CANARY'); git(['add', '--all']); git(['-c', 'user.name=Source QA', '-c', 'user.email=source-qa@local.invalid', 'commit', '-m', 'Owned source fixture']); const commit = git(['rev-parse', 'HEAD']); write('src/中文.mjs', 'Uncommitted changed code'); write('src/untracked.mjs', 'Untracked changed code'); const result = packageSource({ root }), entries = zipFiles(result.path), prefix = 'Agent-Workbench-1.25.6-source/'; assert.equal(result.sourceCommit, commit); assert.equal(result.version, '1.25.6'); assert.deepEqual(entries.get(prefix + 'src/中文.mjs'), original); const launcher = entries.get(prefix + 'Start-Workbench.cmd'), declaredLauncher = result.files.find(row => row.path === 'Start-Workbench.cmd'); assert.equal(launcher.toString(), '@echo off\r\necho owned\r\n'); assert.equal(declaredLauncher.bytes, launcher.length); assert.ok(declaredLauncher.gitBytes < declaredLauncher.bytes); for (const row of result.files) { const archived = entries.get(prefix + row.path); assert.equal(row.bytes, archived.length); assert.equal(row.sha256, createHash('sha256').update(archived).digest('hex')); } assert.ok(entries.has(prefix + 'package-lock.json')); assert.ok(entries.has(prefix + 'docs/PACKAGING.md')); assert.equal([...entries.keys()].some(name => /(?:sqlite|session\.json|\.env|dist-client|runtime|historical-evidence|untracked)/.test(name)), false); for (const value of entries.values()) assert.equal(value.includes('OWNED-EXCLUDED-CANARY'), false); assert.equal(fs.readFileSync(path.join(root, 'src/中文.mjs'), 'utf8'), 'Uncommitted changed code'); const second = packageSource({ root }); assert.notEqual(result.path, second.path); assert.ok(fs.existsSync(result.path)); }); test('program guides are published without reading historical evidence', t => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'awb-guides-')); t.after(() => fs.rmSync(root, { recursive: true, force: true })); const portableRoot = path.join(root, 'program'); fs.mkdirSync(portableRoot); fs.mkdirSync(path.join(root, 'docs/research'), { recursive: true }); fs.writeFileSync(path.join(root, 'README.md'), 'Program instructions\n'); fs.writeFileSync(path.join(root, 'docs/GUIDE.md'), 'Guide\n'); fs.writeFileSync(path.join(root, 'docs/research/NOTES.md'), 'Notes\n'); // A broken link makes any accidental recursive evidence traversal fail. fs.symlinkSync(path.join(root, 'missing-history'), path.join(root, 'docs/historical-evidence'), process.platform === 'win32' ? 'junction' : 'dir'); const result = createProgramGuides({ root, portableRoot, sourceCommit: 'a'.repeat(40) }); assert.equal(result.shipped.length, 2); assert.equal(fs.existsSync(path.join(portableRoot, 'docs/historical-evidence')), false); assert.equal(fs.readFileSync(path.join(portableRoot, 'docs/GUIDE.md'), 'utf8'), 'Guide\n'); });