- Pure Rule Model & AST Parser:
- Eliminate redundant title field; model rules purely via scope (scene index) and natural language body.
- Implement AST parser for inline clauses: [action]{硬性|弹性|建议|特殊}.
- Validate and enforce preference references with @{级别} syntax, strictly blocking invalid grammar.
- Embedded SQLite Database & Repository:
- Native bun:sqlite integration with zero external dependencies, stored at ~/.config/hose-use/approval.db.
- 5 core schemas: rules, preferences, approval_runs, approval_findings, and aipro_logs.
- Support deduplicated scope indexing (listScopes with 1-based indices) and scope drill-down (getScopeDetail).
- Bill Requisition & Backlog Query Extension:
- Add formatFlowAsBill adapter in domains/bill/tools.ts.
- Fall back to flow and backlog inspection when bills are not found in invoice endpoints, unlocking full structured inspection for requisitions (such as S26000001) and loan requests.
- Deterministic Approval Gateway & Lifecycle:
- Enforce mandatory approval lifecycle: points -> checklist -> review -> decide.
- Physical gateway blocking: forbid decision if audit checklist is missing or empty, or if pending points remain unverified.
- Hard tier severance: automatically sever and throw GatewayBlockError on approval attempts violating {硬性} clauses.
- Dedicated hose-aipro Global CLI & Immutable Audit Ledger:
- Register secondary global binary hose-aipro alongside hose-use in package.json and build-binary.mjs.
- Automatically record every AI Pro invocation, document context, args snapshot, operator, status, and verdict in aipro_logs.
- Provide intuitive subcommands: points, checklist, review, decide, logs, rules.
- Bump project version to v0.7.0 and regenerate OpenAPI bundles.
116 lines
4.3 KiB
TypeScript
116 lines
4.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
import { runCli } from "../runner.js";
|
|
import { closeApprovalDatabase, getApprovalDatabase } from "../../domains/approval/db.js";
|
|
import { ApprovalRepository } from "../../domains/approval/repository.js";
|
|
|
|
describe("CLI - hose-use rule 规则与场景索引命令", () => {
|
|
beforeEach(() => {
|
|
process.env.HOSE_APPROVAL_DB = ":memory:";
|
|
const db = getApprovalDatabase(":memory:");
|
|
const repo = new ApprovalRepository(db);
|
|
|
|
// 预置几条测试规则 (仅需 适用场景 + 规约正文 两个要素)
|
|
repo.createRule({
|
|
scope: "差旅报销",
|
|
body: "差旅报销时,[必须上传发票]{硬性}。"
|
|
});
|
|
repo.createRule({
|
|
scope: "差旅报销",
|
|
body: "差旅报销时,[尽量上传支付凭证]{弹性}。"
|
|
});
|
|
repo.createRule({
|
|
scope: "业务招待",
|
|
body: "招待报销时,[必须提供餐饮点餐水单]{硬性}。"
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
closeApprovalDatabase();
|
|
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"
|
|
]);
|
|
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 语法错误拦截并阻止入库", async () => {
|
|
const failRes = await runCli([
|
|
"rule",
|
|
"add",
|
|
"差旅报销",
|
|
"差旅报销时,[必须附发票]{严重}。" // 非法严格程度
|
|
]);
|
|
expect(failRes.exitCode).not.toBe(0);
|
|
expect(failRes.stderr).toContain("非法严格程度");
|
|
});
|
|
});
|