import { expect, test } from 'bun:test'; import { mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { workflowFingerprint } from '../src/workflowFingerprint.js'; import { WorkflowState } from '../src/workflowState.js'; test('bundle changes invalidate resume AND completed; output changes do not', async () => { const dir = await mkdtemp(join(tmpdir(), 'wf-fingerprint-')); try { await mkdir(join(dir, 'scripts')); await mkdir(join(dir, 'prompts')); await writeFile(join(dir, 'scripts', 'helper.mjs'), 'v1'); const source = 'echo("ok")'; const before = await workflowFingerprint(source, dir); const state = new WorkflowState(dir, 'test', 'session'); await state.init({ goal: 'same' }, before, []); await state.markDone([]); await state.archive(); expect( ( await new WorkflowState(dir, 'test', 'session').init( { goal: 'same' }, before, [], ) ).mode, ).toBe('completed'); await writeFile(join(dir, 'scripts', 'helper.mjs'), 'v2'); const after = await workflowFingerprint(source, dir); expect(after).not.toBe(before); const changed = await new WorkflowState(dir, 'test', 'session').init( { goal: 'same' }, after, [], ); expect(changed.scriptChanged).toBe(true); await writeFile(join(dir, 'README.md'), 'not an executable dependency'); expect(await workflowFingerprint(source, dir)).toBe(after); await writeFile(join(dir, 'prompts', 'qa.md'), 'new acceptance contract'); expect(await workflowFingerprint(source, dir)).not.toBe(after); expect(await workflowFingerprint('new source', dir)).not.toBe( await workflowFingerprint(source, dir), ); } finally { await rm(dir, { recursive: true, force: true }); } }); test('bundle symlinks fail visibly instead of reusing untracked external dependencies', async () => { const dir = await mkdtemp(join(tmpdir(), 'wf-link-')); try { await mkdir(join(dir, 'scripts')); await writeFile(join(dir, 'external.mjs'), 'v1'); await symlink( join(dir, 'external.mjs'), join(dir, 'scripts', 'linked.mjs'), ); await expect(workflowFingerprint('source', dir)).rejects.toThrow( '符号链接', ); } finally { await rm(dir, { recursive: true, force: true }); } }); test('import of the host package itself must be rejected — bypasses wf entirely, declaring it does not help', async () => { // package.json::exports 里的 "./src/*"/"./plugins/*" 通配符 + Node/Bun 包 // 自引用会让 `minimal-agent/src/xxx` 解析到真实内核文件,完全绕开 wf 分组 // 能力对象对调度/记账/心跳/预算的管控——这是 scriptGuard.mjs 同名检查的 // 第二道闸,验证即使调用方跳过了 discovery.ts 的静态校验,这里仍然拦得住。 const dir = await mkdtemp(join(import.meta.dir, 'wf-selfref-')); try { const source = "import { runQuery } from 'minimal-agent/src/loop.js';\n"; await expect(workflowFingerprint(source, dir)).rejects.toThrow('宿主引擎自身'); // 声明进 meta.packages 也无法绕过——这不是"没声明"的问题。 await expect( workflowFingerprint(source, dir, ['minimal-agent']), ).rejects.toThrow('宿主引擎自身'); } finally { await rm(dir, { recursive: true, force: true }); } }); test('bare packages imported by relative helpers must be declared and versioned', async () => { // ⚠️ 必须落在仓库树内(而不是 OS 系统临时目录):`workflowFingerprint` 校验 // `meta.packages` 时用 `createRequire(entry).resolve(name)`,从 fixture 目录 // 向上找 node_modules——系统临时目录(如 Windows 的 %TEMP%)没有任何 // node_modules 祖先,会让这里恒定解析失败,与"包名没声明"这个真正要测的 // 场景无关。落在 `test/` 下能向上找到仓库根的 node_modules(含 yaml)。 const dir = await mkdtemp(join(import.meta.dir, 'wf-package-')); try { await mkdir(join(dir, 'scripts')); await writeFile( join(dir, 'scripts', 'helper.mjs'), "import YAML from 'yaml'; export const parse = YAML.parse;\n", ); const source = "import './scripts/helper.mjs';\n"; await expect(workflowFingerprint(source, dir)).rejects.toThrow( '必须声明在 meta.packages', ); expect(await workflowFingerprint(source, dir, ['yaml'])).toHaveLength(16); } finally { await rm(dir, { recursive: true, force: true }); } });