/** * plugins/workflow-runner/test/skillDocSync.test.ts —— 创建 skill 的参考文档漂移守护 * * 守的是一类**只会静默教错**的问题:`skills/workflow-creator/references/api.md` * 是 agent 写工作流时唯一会照抄的参考。它与引擎注入的实际能力一旦分家: * · 漏写 → agent 不知道那个能力存在,会绕远路自己造轮子 * · 多写 → agent 照着写出 `undefined is not a function` * * 光这一轮就新增了 sh / resetContext / parseJson / images / contextFiles 五样 —— * 手抄的参考一周内必然漂移,所以必须有自动对账。 * * `skills/workflow-creator/scripts/validate.mjs --api-doc` 跑的是同一份判据; * 这里再放一份进 `bun test`,是因为没人会记得每次改完 api.ts 都手动跑那个脚本。 */ import { describe, expect, test } from 'bun:test'; import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { WORKFLOW_CONTEXT_CAPABILITIES } from '../src/workflowContext.js'; import { parseFrontmatter } from '../../../src/prompts/skillList.js'; const REPO = join(import.meta.dir, '..', '..', '..'); const API_DOC = join( REPO, 'skills', 'workflow-creator', 'references', 'api.md', ); /** 抽出 api.md「能力表」里列出的名字(表格首列的反引号形式)。 */ function documentedNames(): Set { const doc = readFileSync(API_DOC, 'utf8'); const start = doc.indexOf('## 能力表'); const nextHeading = doc.indexOf('\n## ', start + 4); const table = doc.slice(start, nextHeading < 0 ? undefined : nextHeading); const names = new Set(); for (const m of table.matchAll( /^\|\s*`(wf\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)?)`\s*\|/gm, )) { names.add(m[1]); } return names; } describe('创建 skill 的 api.md 与引擎清单同步', () => { test('★ 能力表列出的路径集合 == 机器可读能力注册表', () => { const registered = Object.entries(WORKFLOW_CONTEXT_CAPABILITIES).flatMap( ([group, names]) => names.map((name) => `wf.${group}.${name}`), ); expect([...documentedNames()].sort()).toEqual(registered.sort()); }); test('能力表本身非空 —— 防「切片没切到、空集合等于空集合」的假通过', () => { expect(documentedNames().size).toBeGreaterThan(10); }); test('★ SKILL.md 能被生产解析器认出 —— 认不出整个技能不会被扫描到', () => { const dir = join(REPO, 'skills', 'workflow-creator'); const meta = parseFrontmatter(readFileSync(join(dir, 'SKILL.md'), 'utf8'), dir); // ⚠️ 断言必须走 skillList.ts 的**同一个**解析器,不能另写判据。 // 曾经这里写 `startsWith('---\n')`,而解析器用的是 `/^---\r?\n/` —— // core.autocrlf=true 的 Windows 检出把 LF 换成 CRLF,测试当场红, // 而技能其实一直好好地被扫描到。测试比生产严 = 训练人忽略红灯。 expect(meta).not.toBeNull(); expect(meta!.name).toBe('workflow-creator'); // description 决定 agent 会不会想起来用它,缺了等于技能存在但无人问津 expect(meta!.description.length).toBeGreaterThan(0); }); test('CRLF 与 LF 两种行尾都必须解析得出 —— 锁死上面那条教训', () => { const body = '---\nname: x\ndescription: d\n---\n正文\n'; expect(parseFrontmatter(body, '/tmp/x')?.name).toBe('x'); expect(parseFrontmatter(body.replace(/\n/g, '\r\n'), '/tmp/x')?.name).toBe('x'); }); });