Files
hose-use/src/domains/approval/parser.test.ts
T
gitea 4e36a1d9a5 feat(approval): introduce sqlite rule engine, deterministic gateway, and hose-aipro cli (v0.7.0)
- 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.
2026-09-06 19:03:06 +08:00

85 lines
4.3 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { parseRuleBody, validatePreferenceText } from "./parser.js";
import { ApprovalRuleValidationError } from "./types.js";
describe("Approval Rule Parser - 规则语法解析与校验", () => {
test("正常解析包含单一硬性条款的规则", () => {
const text = "差旅报销时,[必须上传真实有效的发票]{硬性}。";
const result = parseRuleBody(text);
expect(result.clauses.length).toBe(1);
expect(result.clauses[0]!.action).toBe("必须上传真实有效的发票");
expect(result.clauses[0]!.strictness).toBe("硬性");
expect(result.cleanText).toBe("差旅报销时,必须上传真实有效的发票。");
});
test("正常解析包含复合梯度(硬性、弹性、建议、特殊)的整段自然语言规约", () => {
const text =
"员工进行差旅报销时,[一定需要上传发票]{硬性}。[尽量上传支付凭证]{弹性}以佐证真实性;" +
"如涉及大额费用或合规争议,[须提交法务专项审核]{特殊};针对市内同城交通,[建议补充说明消费事由]{建议}。";
const result = parseRuleBody(text);
expect(result.clauses.length).toBe(4);
expect(result.clauses[0]!.action).toBe("一定需要上传发票");
expect(result.clauses[0]!.strictness).toBe("硬性");
expect(result.clauses[1]!.action).toBe("尽量上传支付凭证");
expect(result.clauses[1]!.strictness).toBe("弹性");
expect(result.clauses[2]!.action).toBe("须提交法务专项审核");
expect(result.clauses[2]!.strictness).toBe("特殊");
expect(result.clauses[3]!.action).toBe("建议补充说明消费事由");
expect(result.clauses[3]!.strictness).toBe("建议");
expect(result.cleanText).toBe(
"员工进行差旅报销时,一定需要上传发票。尽量上传支付凭证以佐证真实性;" +
"如涉及大额费用或合规争议,须提交法务专项审核;针对市内同城交通,建议补充说明消费事由。"
);
});
test("拦截非法的严格程度标签", () => {
const text = "差旅报销时,[必须上传发票]{必须遵守}。";
expect(() => parseRuleBody(text)).toThrow(ApprovalRuleValidationError);
expect(() => parseRuleBody(text)).toThrow('非法严格程度 "必须遵守"');
});
test("拦截悬空的中括号(未指定严格程度)", () => {
const text = "差旅报销时,[必须上传发票],请大家注意。";
expect(() => parseRuleBody(text)).toThrow(ApprovalRuleValidationError);
expect(() => parseRuleBody(text)).toThrow('检测到未指定严格程度的标记 "[必须上传发票]"');
});
test("拦截括号未闭合的语法错误", () => {
expect(() => parseRuleBody("差旅报销时,[必须上传发票{硬性}。")).toThrow("未闭合的左中括号");
expect(() => parseRuleBody("差旅报销时,[必须上传发票]{硬性。")).toThrow("未闭合的左花括号");
expect(() => parseRuleBody("差旅报销时,必须上传发票]{硬性}。")).toThrow("未匹配的多余右中括号");
});
test("拦截空的动作句式", () => {
const text = "差旅报销时,[ ]{硬性}。";
expect(() => parseRuleBody(text)).toThrow(ApprovalRuleValidationError);
expect(() => parseRuleBody(text)).toThrow("动作句式不能为空");
});
test("拦截未包含任何判定标记的纯文字", () => {
const text = "这是一段关于差旅报销的日常说明,没有任何裁判条款。";
expect(() => parseRuleBody(text)).toThrow(ApprovalRuleValidationError);
expect(() => parseRuleBody(text)).toThrow("未包含任何有效的规则标记");
});
});
describe("Approval Preference Parser - 偏好引用语法校验", () => {
test("校验通过合法的 @{级别} 引用", () => {
const text = "满足以下条件:触犯任何 @{硬性} 条款,或触发 @{特殊} 条款,或未满足 @{弹性} 且金额 > 500";
expect(validatePreferenceText(text)).toBe(true);
});
test("拦截非法的 @{级别} 引用", () => {
const text = "若违反 @{严重} 条款则驳回";
expect(() => validatePreferenceText(text)).toThrow(ApprovalRuleValidationError);
expect(() => validatePreferenceText(text)).toThrow('非法的偏好引用 "@{严重}"');
});
});