import { afterEach, describe, expect, test } from 'bun:test'; import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { WorkflowScriptError, runScript } from '../src/sandbox.js'; import { validateScript } from '../src/scriptGuard.mjs'; import type { WorkflowScript } from '../src/types.js'; const dirs: string[] = []; afterEach(async () => { for (const dir of dirs.splice(0)) await rm(dir, { recursive: true, force: true }); }); async function workflow(source: string, name = 'test-flow'): Promise { const root = await mkdtemp(join(tmpdir(), 'wf-esm-')); dirs.push(root); const dir = join(root, name); await mkdir(dir, { recursive: true }); const scriptPath = join(dir, 'workflow.mjs'); await writeFile(scriptPath, source); const verdict = validateScript(source, { filename: scriptPath }); if (!verdict.ok || verdict.bodyOffset === undefined) { throw new Error(verdict.errors.join('; ')); } return { name, dir, scriptPath, source, meta: { apiVersion: 2, description: 'test' }, bodyOffset: verdict.bodyOffset, }; } const head = `export const meta = { apiVersion: 2, description: 'test' };\n`; describe('trusted ESM workflow loader', () => { test('default run receives an isolated wf object and returns JSON data', async () => { const script = await workflow( `${head}export default async function run(wf) { return { value: wf.input.values.x }; }`, ); expect( await runScript(script, { input: { values: { x: 42 } } }, 'bundle-a'), ).toEqual({ value: 42 }); }); test('native relative imports resolve from the workflow directory', async () => { const script = await workflow( `${head}import { twice } from './scripts/math.mjs';\nexport default async function run(wf) { return twice(6); }`, ); await mkdir(join(script.dir, 'scripts'), { recursive: true }); await writeFile( join(script.dir, 'scripts', 'math.mjs'), 'export const twice = (value) => value * 2;\n', ); expect(await runScript(script, {}, 'bundle-b')).toBe(12); }); test('bundle hash selects a fresh physical ESM graph when a helper changes', async () => { const script = await workflow( `${head}import { value } from './scripts/value.mjs';\nexport default async function run(wf) { return value; }`, ); const helper = join(script.dir, 'scripts', 'value.mjs'); await mkdir(join(script.dir, 'scripts'), { recursive: true }); await writeFile(helper, 'export const value = 1;\n'); expect(await runScript(script, {}, 'bundle-helper-v1')).toBe(1); await writeFile(helper, 'export const value = 2;\n'); expect(await runScript(script, {}, 'bundle-helper-v2')).toBe(2); }); test('concurrent instances do not share run-local state', async () => { const script = await workflow( `${head}export default async function run(wf) { let value = wf.input.values.value; await Promise.resolve(); value += 1; return value; }`, ); expect( await Promise.all([ runScript(script, { input: { values: { value: 1 } } }, 'bundle-c'), runScript(script, { input: { values: { value: 9 } } }, 'bundle-c'), ]), ).toEqual([2, 10]); }); test('wide concurrent fan-out sharing one bundle hash does not race the ESM cache rename (Windows EPERM regression)', async () => { const script = await workflow( `${head}export default async function run(wf) { let value = wf.input.values.value; await Promise.resolve(); value += 1; return value; }`, ); const N = 16; const results = await Promise.all( Array.from({ length: N }, (_, i) => runScript(script, { input: { values: { value: i } } }, 'bundle-wide'), ), ); expect(results).toEqual(Array.from({ length: N }, (_, i) => i + 1)); }); test('runtime errors preserve workflow outcome and cause', async () => { const script = await workflow( `${head}export default async function run(wf) { throw Object.assign(new Error('budget'), { workflowOutcome: 'budget_limited' }); }`, ); const error = await runScript(script, {}, 'bundle-d').catch((value) => value); expect(error).toBeInstanceOf(WorkflowScriptError); expect(error).toMatchObject({ workflowOutcome: 'budget_limited' }); expect((error as WorkflowScriptError).cause).toBeInstanceOf(Error); }); test('AbortError passes through without workflow-failure wrapping', async () => { const script = await workflow( `${head}export default async function run(wf) { const e = new Error('stop'); e.name = 'AbortError'; throw e; }`, ); const error = await runScript(script, {}, 'bundle-e').catch((value) => value); expect((error as Error).name).toBe('AbortError'); expect(error).not.toBeInstanceOf(WorkflowScriptError); }); test('module parse failures are reported as module-load failures', async () => { const script = await workflow( `${head}export default async function run(wf) { return 1; }`, ); const invalid = `${head}export default async function run( {`; await writeFile(script.scriptPath, invalid); await expect( runScript({ ...script, source: invalid }, {}, 'bundle-f-invalid'), ).rejects.toThrow(/模块加载失败/); }); });