Files
Agent-Workbench/scripts/live-af/project.mjs
T
2026-11-06 13:40:29 +08:00

48 lines
3.9 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { execFileSync, spawnSync } from 'node:child_process';
import { gitEnvironment } from '../../src/core/git-process.mjs';
import { sha } from '../live-ad/common.mjs';
import { allowed } from './contract.mjs';
export function prepareNode(output, save) {
const sourceRoot = path.join(output, 'project');
assert.equal(fs.existsSync(sourceRoot), false);
fs.cpSync('benchmarks/live-delivery-v1/node', sourceRoot, { recursive: true, errorOnExist: true });
fs.writeFileSync(path.join(sourceRoot, 'README.md'), '# Independent expense engineering comparison\nRepair only the three src modules according to the original nine tests. Preserve owner files and their original staging.\n', { flag: 'wx' });
fs.writeFileSync(path.join(sourceRoot, '.gitignore'), 'dist/\n.awb-*\nnode_modules/\n', { flag: 'wx' });
for (const name of ['owner-staged.txt', 'owner-unstaged.txt']) fs.writeFileSync(path.join(sourceRoot, name), 'Owner baseline\n', { flag: 'wx' });
const git = (args, baseline = false) => execFileSync('git', ['--no-pager', '--no-optional-locks', '-C', sourceRoot, ...args],
{ encoding: 'utf8', windowsHide: true, env: { ...gitEnvironment(), ...(baseline ? { GIT_AUTHOR_DATE: '2026-11-06T05:00:00Z', GIT_COMMITTER_DATE: '2026-11-06T05:00:00Z' } : {}) } });
git(['init', '--initial-branch=main']); git(['config', 'core.autocrlf', 'false']);
git(['config', 'user.name', 'Workbench QA']); git(['config', 'user.email', 'qa@workbench.invalid']);
git(['add', '--', '.']); git(['-c', 'maintenance.auto=false', 'commit', '--no-gpg-sign', '-m', 'Seal AF original expense baseline'], true);
const initialHead = git(['rev-parse', 'HEAD']).trim();
fs.writeFileSync(path.join(sourceRoot, 'owner-staged.txt'), 'Owner staged change\n'); git(['add', '--', 'owner-staged.txt']);
fs.writeFileSync(path.join(sourceRoot, 'owner-staged.txt'), 'Owner unstaged over staging\n');
fs.writeFileSync(path.join(sourceRoot, 'owner-unstaged.txt'), 'Owner unstaged change\n');
fs.writeFileSync(path.join(sourceRoot, 'owner-untracked.txt'), 'Owner untracked change\n', { flag: 'wx' });
const all = git(['ls-files', '-z']).split('\0').filter(Boolean).concat('owner-untracked.txt');
const files = Object.fromEntries(all.map(name => [name, sha(fs.readFileSync(path.join(sourceRoot, name)))]));
const protectedNames = all.filter(name => !allowed.includes(name));
const originalIndex = git(['ls-files', '--stage', '--', ...protectedNames]);
const protect = () => {
for (const name of protectedNames) assert.equal(sha(fs.readFileSync(path.join(sourceRoot, name))), files[name], 'Protected original file ' + name);
assert.equal(git(['ls-files', '--stage', '--', ...protectedNames]), originalIndex, 'Protected original index');
};
const checks = prefix => {
const result = {};
for (const [name, args] of [['test', ['--test', '--test-reporter=tap', 'test/expense.test.mjs']], ['build', ['scripts/build.mjs']]]) {
const run = spawnSync(process.execPath, args, { cwd: sourceRoot, encoding: 'utf8', windowsHide: true, env: gitEnvironment(), maxBuffer: 32 * 1024 * 1024 });
save(prefix + '/' + name + '.txt', (run.stdout ?? '') + (run.stderr ?? ''));
const stats = name === 'test' ? Object.fromEntries(['tests', 'pass', 'fail', 'cancelled', 'skipped', 'todo'].map(key => [key, Number(run.stdout?.match(new RegExp('^# ' + key + ' (\\d+)$', 'm'))?.[1] ?? NaN)])) : null;
result[name] = { status: run.status, signal: run.signal, error: run.error?.message ?? null, stats,
stdoutSha256: sha(run.stdout ?? ''), stderrSha256: sha(run.stderr ?? '') };
}
protect(); save(prefix + '/result.json', result); return result;
};
save('source-before.json', { initialHead, files, protectedIndex: originalIndex, status: git(['status', '--porcelain=v1']) });
return { sourceRoot, git, initialHead, protect, checks, files };
}