#!/usr/bin/env node /** * 重新生成 src/renderer/src/fixtures/golden.json。 * * golden.json 是冻结的真实引擎输出,被测试套件当夹具用。此前没有任何东西 * 能重新生成它 —— schema 一改,夹具就悄悄过期,而依赖它的测试照样全绿。 * * 用法:node scripts/regen-golden.mjs (或 npm run fixture:golden) * PYTHON=python3.9 npm run fixture:golden 指定解释器 * * 注意:environment(python 版本 / platform / processor / timerResolution)和 * wallTime 的具体数字必然随机器和每次运行而变,所以"重新生成后应与仓库内容逐字节 * 一致"这种校验是做不到的,也没有加。这个脚本的作用是让夹具在 schema 演化后 * 能被有意识地刷新 —— 刷新后请跑 npm test 确认依赖它的断言仍然成立。 */ import { spawnSync } from 'child_process' import { mkdtempSync, writeFileSync, readFileSync } from 'fs' import { tmpdir } from 'os' import { join, dirname, resolve } from 'path' import { fileURLToPath } from 'url' const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..') const PY = process.env.PYTHON || 'python' // 与 golden.json 现有内容同源:functions[].file 指向的就是这个脚本 const SOURCE = join(ROOT, 'engine', 'tests', 'fixtures', 'nested_calls.py') const TARGET = join(ROOT, 'src', 'renderer', 'src', 'fixtures', 'golden.json') const outDir = mkdtempSync(join(tmpdir(), 'golden-')) const outFile = join(outDir, 'result.json') const args = ['-m', 'engine.runner', '--script', SOURCE, '--out', outFile] const r = spawnSync(PY, args, { cwd: ROOT, encoding: 'utf-8', env: { ...process.env, PYTHONUTF8: '1', PYTHONIOENCODING: 'utf-8' } }) if (r.error) { console.error(`无法启动 ${PY}:${r.error.message}`) console.error('设置 PYTHON 环境变量指向可用的解释器后重试。') process.exit(1) } if (r.status !== 0) { console.error(`引擎退出码 ${r.status}`) console.error(r.stderr?.slice(-2000) ?? '') process.exit(1) } let parsed try { parsed = JSON.parse(readFileSync(outFile, 'utf-8')) } catch (e) { console.error(`读取/解析引擎结果失败:${e.message}`) console.error(r.stderr?.slice(-2000) ?? '') process.exit(1) } if (parsed.status !== 'ok') { console.error(`引擎返回 status=${parsed.status},不写入夹具:`, parsed.error) process.exit(1) } // functions[].file 用仓库相对路径(POSIX 分隔符),否则夹具里会带上本机绝对路径 const rel = (p) => p .replace(ROOT, '') .replace(/^[\\/]+/, '') .replace(/\\/g, '/') for (const f of parsed.functions) f.file = rel(f.file) writeFileSync(TARGET, JSON.stringify(parsed, null, 2) + '\n', 'utf-8') console.log(`已写入 ${rel(TARGET)}`) console.log(` functions=${parsed.functions.length}`) console.log(` wallTime=${parsed.wallTime?.seconds?.toFixed(6)}s`) console.log('接下来跑 npm test 确认依赖这份夹具的断言仍然成立。')