协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
test-cli-application【开发与工程】使用内置的 node:test 模块为 Node.js CLI 应用编写集成测试。涵盖 exec 辅助器模式、输出断言、文件系统状态验证、清理 hook、JSON 输出解析、 错误情况测试,以及破坏性测试后的状态恢复。在向现有 CLI 添加测试、 测试新命令、跨框架验证适配器行为,或为 CLI 工具设置 CI 时使用。
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
使用带 execSync 的内置 node:test 模块为 Node.js CLI 编写集成测试。
cli/index.js)import { describe, it, before, after } from 'node:test';
import assert from 'node:assert/strict';
import { execSync } from 'child_process';
import { existsSync, rmSync } from 'fs';
import { resolve } from 'path';
const CLI = 'node cli/index.js';
const ROOT = process.cwd();
function run(args) {
return execSync(`${CLI} ${args}`, {
cwd: ROOT,
encoding: 'utf8',
timeout: 10000,
});
}
关键设计决策:
node:test 是内置的 —— 无需测试运行器依赖execSync 将 CLI 作为子进程运行 —— 测试实际二进制,非内部函数encoding: 'utf8' 给字符串输出用于正则匹配ROOT 以求可重复性预期结果: 从 node:test 导入并有可工作 run() 辅助器的测试文件。
失败处理: 若 node:test 不可用,您的 Node.js 版本低于 18。升级或使用 polyfill。
冒烟测试验证 CLI 启动、解析参数并产出预期输出形状:
describe('meta', () => {
it('shows version', () => {
const out = run('--version');
assert.match(out, /\d+\.\d+\.\d+/);
});
it('shows help with all commands', () => {
const out = run('--help');
assert.match(out, /install/);
assert.match(out, /list/);
assert.match(out, /detect/);
});
});
describe('registry', () => {
it('list shows expected counts', () => {
const out = run('list --domains');
assert.match(out, /\d+ domains/);
});
it('search finds known items', () => {
const out = run('search "docker"');
assert.match(out, /result\(s\) for "docker"/);
});
it('search returns 0 for nonsense', () => {
const out = run('search "xyzzy-nonexistent"');
assert.match(out, /0 result/);
});
});
冒烟测试模式:
--version 和 --help 始终工作预期结果: 冒烟测试确认 CLI 功能正常且数据已加载。
失败处理: 若注册表计数频繁变化,使用 \d+ 而非硬编码数字。
生命周期测试验证带清理的 create → verify → delete 序列:
describe('install', () => {
const testPath = resolve(ROOT, '.agents/skills/commit-changes');
after(() => {
// Always clean up, even if tests fail
try { rmSync(testPath); } catch {}
try { rmSync(resolve(ROOT, '.agents/skills'), { recursive: true }); } catch {}
try { rmSync(resolve(ROOT, '.agents'), { recursive: true }); } catch {}
});
it('dry-run does not create files', () => {
const out = run('install commit-changes --dry-run');
assert.match(out, /DRY RUN/);
assert.ok(!existsSync(testPath));
});
it('installs creates the target', () => {
run('install commit-changes');
assert.ok(existsSync(testPath));
});
it('skips already installed', () => {
const out = run('install commit-changes');
assert.match(out, /skipped/);
});
it('uninstall removes the target', () => {
run('uninstall commit-changes');
assert.ok(!existsSync(testPath));
});
});
清理规则:
after() hook,不用 afterEach() —— 生命周期测试相互构建try/catch 包裹清理 —— 清理不能让测试套件失败预期结果: 测试在 describe 块内顺序运行,即使失败清理也运行。
失败处理: 若测试并行运行(node:test 中非默认),用 { concurrency: 1 } 强制顺序。
测试每个适配器的目标路径而不进行变更:
describe('adapter: cursor (dry-run)', () => {
it('targets .cursor/skills/ path', () => {
const out = run('install commit-changes --framework cursor --dry-run');
assert.match(out, /\.cursor\/skills/i);
});
});
describe('adapter: copilot (dry-run)', () => {
it('targets .github/ path', () => {
const out = run('install commit-changes --framework copilot --dry-run');
assert.match(out, /\.github/i);
});
});
此模式可扩展到任意数量适配器。每个测试:
--framework 绕过自动检测--dry-run 使无文件被创建预期结果: 每个适配器一个 describe 块,每个至少有路径断言。
失败处理: 若适配器在项目中不存在,测试将以 "Unknown framework" 失败。这正确 —— 适配器测试应仅对已实现适配器存在。
describe('errors', () => {
it('rejects unknown items', () => {
assert.throws(
() => run('install nonexistent-skill-xyz'),
/No matching items|Unknown/,
);
});
it('rejects unknown framework', () => {
assert.throws(
() => run('install commit-changes --framework nonexistent'),
/Unknown framework/,
);
});
it('handles missing state gracefully', () => {
assert.throws(
() => run('scatter nonexistent-team'),
/not burning|Unknown/,
);
});
});
错误测试模式:
assert.throws 捕捉来自 execSync 的非零退出码预期结果: 所有错误路径产出非零退出码和有帮助消息。
失败处理: execSync 在非零退出时抛出。错误的 stderr 或 stdout 包含消息。若 assert.throws 正则不匹配,检查 error.stdout。
describe('json output', () => {
it('campfire --json outputs valid JSON', () => {
const out = run('campfire --json');
const data = JSON.parse(out);
assert.ok(typeof data.totalTeams === 'number');
assert.ok(Array.isArray(data.fires));
});
it('gather --dry-run --json outputs structured data', () => {
const out = run('gather tending --dry-run --json');
// JSON may follow a DRY RUN header — extract from first '{'
const jsonStart = out.indexOf('{');
assert.ok(jsonStart >= 0, 'Should contain JSON');
const data = JSON.parse(out.slice(jsonStart));
assert.equal(data.team, 'tending');
});
});
JSON 测试陷阱:
{ 字符提取 JSON预期结果: JSON 输出可解析且包含预期键。
失败处理: 若 JSON.parse 失败,命令可能将人类文本与 JSON 混合。要么修复命令在 --json 模式输出纯 JSON,要么提取 JSON 子串。
describe('stateful commands', () => {
const stateDir = resolve(ROOT, '.agent-almanac');
after(() => {
// Remove state file created by tests
try { rmSync(stateDir, { recursive: true }); } catch {}
});
// Tests that create/modify state...
});
// Restore symlinks that destructive tests may remove
describe('destructive tests', () => {
after(() => {
// Restore symlinks that scatter/uninstall removed
const skills = ['heal', 'meditate', 'remote-viewing'];
for (const skill of skills) {
const link = resolve(ROOT, `.claude/skills/${skill}`);
if (!existsSync(link)) {
try {
execSync(`ln -s ../../skills/${skill} ${link}`, { cwd: ROOT });
} catch {}
}
}
});
});
状态恢复规则:
.agent-almanac/state.json)必须在测试后清理scatter/uninstall 移除的符号链接必须恢复init 创建的 manifest 文件(agent-almanac.yml)必须移除after() hook 按声明顺序反向运行 —— 最后声明恢复 hook预期结果: 测试套件让项目处于发现时的相同状态。
失败处理: 若 CI 在测试运行后报告剩余文件,将清理添加到 after()。在测试运行后用 git status 检测泄漏状态。
node --test cli/test/cli.test.js 运行--version、--help 和注册表加载\d+ 正则或动态读取计数,而非断言 329 skills。node:test 默认按声明顺序运行套件,但套件内的测试可能不。在单一 describe 内使用生命周期套件(create → verify → delete)保证顺序。after() 仍运行。但若您在 before() 中抛出,后续测试和 after() 可能不运行。保持 before() 最少。execSync。要么 echo y | 管道,要么确保测试中始终传递 --yes。.claude/skills/ 或 .agents/skills/ 中创建文件的测试修改工作树。CI 可能在 "脏工作目录" 检查上失败。始终清理。scaffold-cli-command —— 构建这些测试验证的命令build-cli-plugin —— 构建第 4 步测试的适配器design-cli-output —— 测试断言对照的输出模式