/** * plugins/workflow-runner/test/discovery.test.ts —— 工作流目录发现 * * 这里守的重点是**坏工作流的可见性**。 * * legacy loader 的做法是「坏 yaml → console.warn 后从列表里消失」,于是用户看到的 * 现象是「我的工作流不见了」而不是「我的工作流写错了」——两者的排查成本差一个数量级。 * 本项目在别处已经为同一类静默降级付过学费(插件漏注册悄悄退化成 markdown prompt、 * 资产目录漏拷导致「未找到说明书」),所以新引擎把失败原因显式带出来。 * * 用 discoverWorkflowsIn(roots) 而不是 mock 掉 getResourceSearchPaths:项目惯例是 * 依赖注入,`mock.module` 会全局泄漏污染同批其它测试文件。 */ import { afterEach, describe, expect, test } from 'bun:test'; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { discoverWorkflowsIn, findWorkflowByName, loadWorkflowDir, } from '../src/discovery.js'; const created: string[] = []; afterEach(() => { for (const dir of created.splice(0)) { // Windows 上偶发 EBUSY(杀毒/索引器短暂持有句柄),重试 + 吞掉, // 否则会把上一条**已通过**的测试标成失败,误导排查方向。 try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* 清理失败不影响断言结论 */ } } }); /** 建一个临时的 workflows 根,返回该根路径。 */ function makeRoot(): string { const base = mkdtempSync(join(tmpdir(), 'wf-discovery-')); created.push(base); const root = join(base, 'workflows'); mkdirSync(root, { recursive: true }); return root; } /** 在根下建一个工作流目录,写入给定脚本内容。 */ function makeWorkflow(root: string, name: string, source: string): string { const dir = join(root, name); mkdirSync(dir, { recursive: true }); writeFileSync(join(dir, 'workflow.mjs'), source, 'utf8'); return dir; } const GOOD = `export const meta = { apiVersion: 2, description: '一个合法工作流' } export default async function run(wf) { wf.output.message('hi') } `; describe('工作流目录发现', () => { test('发现合法工作流,name 取目录名而非任何脚本内字段', async () => { const root = makeRoot(); makeWorkflow(root, 'my-flow', GOOD); const { workflows, failures } = await discoverWorkflowsIn([root]); expect(failures).toEqual([]); expect(workflows.map((w) => w.name)).toEqual(['my-flow']); expect(workflows[0].meta.description).toBe('一个合法工作流'); expect(workflows[0].scriptPath).toBe(join(root, 'my-flow', 'workflow.mjs')); }); test('meta 的 inputs / artifacts 被解析出来', async () => { const root = makeRoot(); makeWorkflow( root, 'with-meta', `export const meta = { apiVersion: 2, description: 'x', inputs: [{ name: 'topic', greedy: true, required: true }], artifacts: ['out/a.md', 'out/b.md'], } export default async function run(wf) { wf.output.message('ok') } `, ); const { workflows } = await discoverWorkflowsIn([root]); expect(workflows[0].meta.inputs?.[0]).toMatchObject({ name: 'topic', greedy: true, }); expect(workflows[0].meta.artifacts).toEqual(['out/a.md', 'out/b.md']); }); test('managedRuntime 只是外层控制器声明,不改变普通工作流脚本形态', async () => { const root = makeRoot(); makeWorkflow( root, 'managed', `export const meta = { apiVersion: 2, description: 'x', managedRuntime: 'goal' } export default async function run(wf) { wf.output.message('ok') } `, ); const { workflows } = await discoverWorkflowsIn([root]); expect(workflows[0]?.meta.managedRuntime).toBe('goal'); }); test('★ 坏脚本进 failures 并带上原因,不是静默消失', async () => { const root = makeRoot(); makeWorkflow( root, 'broken', `export const meta = { apiVersion: 2, description: 'x' }\nexport default async function run() { const t = Date.now() }\n`, ); const { workflows, failures } = await discoverWorkflowsIn([root]); expect(workflows).toEqual([]); expect(failures).toHaveLength(1); expect(failures[0].name).toBe('broken'); expect(failures[0].errors.join('\n')).toContain('Date.now()'); }); test('★ 一个坏工作流不拖累同目录下的好工作流', async () => { const root = makeRoot(); makeWorkflow( root, 'broken', `export const meta = { apiVersion: 2, description: 'x' }\nexport default async function run() { Date.now() }\n`, ); makeWorkflow(root, 'fine', GOOD); const { workflows, failures } = await discoverWorkflowsIn([root]); expect(workflows.map((w) => w.name)).toEqual(['fine']); expect(failures.map((f) => f.name)).toEqual(['broken']); }); test('★ 没有 workflow.mjs 的普通目录既不算工作流也不算失败', async () => { // assets/、模板目录、编辑器的临时目录都会出现在这里。把它们报成 // 「加载失败」等于每次 /workflows 都刷一堆假告警,用户很快就不看告警了。 const root = makeRoot(); mkdirSync(join(root, 'just-a-folder'), { recursive: true }); makeWorkflow(root, 'real', GOOD); const { workflows, failures } = await discoverWorkflowsIn([root]); expect(workflows.map((w) => w.name)).toEqual(['real']); expect(failures).toEqual([]); }); test('目录里的 *.yaml 不被代码引擎认领(legacy 引擎的地盘)', async () => { const root = makeRoot(); writeFileSync( join(root, 'old-style.yaml'), 'name: old\nsteps: []\n', 'utf8', ); const { workflows, failures } = await discoverWorkflowsIn([root]); expect(workflows).toEqual([]); expect(failures).toEqual([]); }); test('搜索根不存在 → 空结果,不抛', async () => { const r = await discoverWorkflowsIn([ join(tmpdir(), 'definitely-not-here-xyz'), ]); expect(r).toEqual({ workflows: [], failures: [] }); }); describe('多根优先级', () => { test('★ 前面的根覆盖后面的同名工作流(cwd 覆盖包内自带)', async () => { const high = makeRoot(); const low = makeRoot(); makeWorkflow( high, 'dup', `export const meta = { apiVersion: 2, description: '高优先级' }\nexport default async function run(wf){wf.output.message('a')}\n`, ); makeWorkflow( low, 'dup', `export const meta = { apiVersion: 2, description: '低优先级' }\nexport default async function run(wf){wf.output.message('b')}\n`, ); const { workflows } = await discoverWorkflowsIn([high, low]); expect(workflows).toHaveLength(1); expect(workflows[0].meta.description).toBe('高优先级'); }); test('★ 高优先级里写错的工作流**占住**这个名字,不被低优先级同名顶替', async () => { // 否则现象是「我改坏了 cwd 里的工作流,跑起来却是包内那个旧版本」—— // 静默换了实现体,比直接报错难查得多。 const high = makeRoot(); const low = makeRoot(); makeWorkflow( high, 'dup', `export const meta = { apiVersion: 2, description: 'x' }\nexport default async function run(){Date.now()}\n`, ); makeWorkflow( low, 'dup', `export const meta = { apiVersion: 2, description: '包内版本' }\nexport default async function run(wf){wf.output.message('b')}\n`, ); const { workflows, failures } = await discoverWorkflowsIn([high, low]); expect(workflows).toEqual([]); expect(failures.map((f) => f.name)).toEqual(['dup']); }); }); }); describe('loadWorkflowDir / findWorkflowByName', () => { test('targeted lookup honors broken overrides and excludes unrelated scripts even for aliases', async () => { const high = makeRoot(); const low = makeRoot(); makeWorkflow(high, 'broken-other', 'not a workflow'); makeWorkflow(high, 'My-Plan', 'malformed'); makeWorkflow(low, 'My-Plan', GOOD); makeWorkflow(low, 'not-requested', GOOD); const selected = await discoverWorkflowsIn([high, low], { name: 'my-plan', ignoreCase:true }); expect(selected.workflows).toEqual([]); expect(selected.failures.map(f => f.name)).toEqual(['My-Plan']); expect(await discoverWorkflowsIn([low], {name:'../My-Plan'})).toEqual({workflows:[], failures:[]}); makeWorkflow(high, 'My-Plan', GOOD); expect((await discoverWorkflowsIn([high, low], {name:'My-Plan'})).workflows.map(w => w.name)).toEqual(['My-Plan']); }); test('loadWorkflowDir 对缺失脚本给出可读原因', async () => { const root = makeRoot(); mkdirSync(join(root, 'empty'), { recursive: true }); const r = await loadWorkflowDir(join(root, 'empty')); expect(r.ok).toBe(false); if (!r.ok) { expect(r.reason).toBe('no-script'); expect(r.errors[0]).toContain('workflow.mjs'); } }); test('findWorkflowByName 找不到时返回 null(可能是 legacy yaml,由调用方决定)', async () => { expect(await findWorkflowByName('绝不存在的工作流名-xyz')).toBeNull(); }); });