Files
hose-use/src/cli/commands/rule.test.ts
T
gitea 95eb8ee676 chore(legacy): remove ekb-mcp migration layer, approval db compat shim, and stale daily notes
The ekb-mcp -> hose-use rename migration (LEGACY_* constants,
syncLegacyEnterprisesDir/syncLegacyGlobalConfig with 7-day grace
cleanup, dual-source enterprises resolution) has completed its job:
legacy data self-deleted on active machines years of grace ago.
Config resolution now returns the single primary
~/.config/hose-use source; startup no longer scans the legacy dir.

- core/config/loader.ts: drop legacy constants, sync functions, and
  LEGACY path from diagnostics candidates; loader.test.ts drops the
  three legacy-migration cases
- domains/approval/db.ts deleted: pure pass-through over core/db whose
  consumers (repository.ts) already call core/db directly; the four
  test files now import getDatabase/closeDatabase from core/db
- scripts/dev-hygiene.ts: drop the config.legacy finding and
  legacyConfig snapshot field
- docs/daily/* removed: Python-era (ekb-cli/ekb-mcp) notes describing
  commands and architecture that no longer exist (preserved in git
  history)

No behavior change on machines without legacy data: tsc clean,
313 tests pass (316 minus the 3 removed legacy cases).
2026-10-05 16:29:19 +08:00

152 lines
5.3 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { runCli } from "../runner.js";
import { closeDatabase, getDatabase } from "../../core/db/index.js";
import { ApprovalRepository } from "../../domains/approval/repository.js";
describe("CLI - hose-use rule 规则与场景索引命令", () => {
beforeEach(() => {
process.env.HOSE_APPROVAL_DB = ":memory:";
const db = getDatabase(":memory:");
const repo = new ApprovalRepository(db);
// 预置几条测试规则 (仅需 适用场景 + 规约正文 两个要素)
repo.createRule({
scope: "差旅报销",
body: "差旅报销时,[必须上传发票]{硬性}。"
});
repo.createRule({
scope: "差旅报销",
body: "差旅报销时,[尽量上传支付凭证]{弹性}。"
});
repo.createRule({
scope: "业务招待",
body: "招待报销时,[必须提供餐饮点餐水单]{硬性}。"
});
});
afterEach(() => {
closeDatabase();
delete process.env.HOSE_APPROVAL_DB;
});
test("hose-use rule scopes 一键获取去重场景索引列表", async () => {
const res = await runCli(["rule", "scopes", "--json"]);
expect(res.exitCode).toBe(0);
const data = JSON.parse(res.stdout);
expect(data.count).toBe(2);
expect(data.scopes[0].index).toBe(1);
expect(data.scopes[0].scope).toBe("差旅报销");
expect(data.scopes[0].ruleCount).toBe(2);
expect(data.scopes[0].ruleCodes).toEqual([1, 2]);
expect(data.scopes[1].index).toBe(2);
expect(data.scopes[1].scope).toBe("业务招待");
expect(data.scopes[1].ruleCount).toBe(1);
expect(data.scopes[1].ruleCodes).toEqual([3]);
});
test("hose-use rule scope <1> 输入索引号获取该场景下的全部规则明细", async () => {
const res = await runCli(["rule", "scope", "1", "--json"]);
expect(res.exitCode).toBe(0);
const data = JSON.parse(res.stdout);
expect(data.index).toBe(1);
expect(data.scope).toBe("差旅报销");
expect(data.ruleCount).toBe(2);
expect(data.rules.length).toBe(2);
expect(data.rules[0].code).toBe(1);
expect(data.rules[0].parsed.clauses[0].strictness).toBe("硬性");
expect(data.rules[1].code).toBe(2);
expect(data.rules[1].parsed.clauses[0].strictness).toBe("弹性");
});
test("hose-use rule scope <场景名称> 输入名称同样可以获取明细", async () => {
const res = await runCli(["rule", "scope", "业务招待", "--json"]);
expect(res.exitCode).toBe(0);
const data = JSON.parse(res.stdout);
expect(data.index).toBe(2);
expect(data.scope).toBe("业务招待");
expect(data.ruleCount).toBe(1);
expect(data.rules[0].code).toBe(3);
expect(data.rules[0].parsed.clauses[0].action).toBe("必须提供餐饮点餐水单");
});
test("hose-use rule scope <不存在> 返回 1 并提示未找到", async () => {
const res = await runCli(["rule", "scope", "999", "--json"]);
expect(res.exitCode).toBe(1);
const data = JSON.parse(res.stdout);
expect(data.found).toBe(false);
});
test("hose-use rule add 纯双参数 (索引 + 正文) 快速新增并强校验", async () => {
// 成功添加:支持直接传 [适用场景] [正文] 两个参数
const addRes = await runCli([
"rule",
"add",
"办公采购",
"单笔采购大于1万,[必须附采购合同]{硬性};[建议由采购员集中办理]{建议}。",
"--json",
"--yes"
]);
expect(addRes.exitCode).toBe(0);
const addData = JSON.parse(addRes.stdout);
expect(addData.success).toBe(true);
expect(addData.rule.code).toBe(4);
expect(addData.rule.scope).toBe("办公采购");
// 再次调用 scopes 应该出现第 3 个索引
const scopesRes = await runCli(["rule", "scopes", "--json"]);
const scopesData = JSON.parse(scopesRes.stdout);
expect(scopesData.count).toBe(3);
expect(scopesData.scopes[2].scope).toBe("办公采购");
});
test("hose-use rule add --dry-run 只预览且不写入数据库", async () => {
const res = await runCli([
"rule",
"add",
"办公采购",
"单笔采购大于1万,[必须附采购合同]{硬性}。",
"--json",
"--dry-run",
]);
expect(res.exitCode).toBe(0);
const data = JSON.parse(res.stdout);
expect(data.dryRun).toBe(true);
expect(data.operation).toBe("新增审批规则");
expect(data.changes.scope).toBe("办公采购");
const scopes = await runCli(["rule", "scopes", "--json"]);
expect(JSON.parse(scopes.stdout).count).toBe(2);
});
test("hose-use rule add 未确认时拒绝写入并给出下一步", async () => {
const res = await runCli([
"rule",
"add",
"办公采购",
"单笔采购大于1万,[必须附采购合同]{硬性}。",
]);
expect(res.exitCode).not.toBe(0);
expect(res.stderr).toContain("--dry-run");
expect(res.stderr).toContain("--yes");
const scopes = await runCli(["rule", "scopes", "--json"]);
expect(JSON.parse(scopes.stdout).count).toBe(2);
});
test("hose-use rule add 语法错误拦截并阻止入库", async () => {
const failRes = await runCli([
"rule",
"add",
"差旅报销",
"差旅报销时,[必须附发票]{严重}。" // 非法严格程度
]);
expect(failRes.exitCode).not.toBe(0);
expect(failRes.stderr).toContain("非法严格程度");
});
});