164 lines
5.2 KiB
TypeScript
164 lines
5.2 KiB
TypeScript
import { describe, expect, test, afterEach } from "bun:test";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import {
|
|
parseLocalRulesContent,
|
|
loadLocalRulesFile,
|
|
saveLocalRulesFile,
|
|
localRulesToLintTargets
|
|
} from "./rules-local.js";
|
|
import { findSealTool } from "./tools.js";
|
|
|
|
const tempDirs: string[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const dir of tempDirs.splice(0)) {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
describe("rules-local workspace", () => {
|
|
test("parses wrapped rules object", () => {
|
|
const raw = JSON.stringify({
|
|
corpId: "corp-1",
|
|
source: "draft",
|
|
rules: [
|
|
{ code: 1, description: "餐饮发票必须提供水单", scope: "MEAL", strictness: "MUST_FOLLOW" },
|
|
{ code: 2, description: "单张发票金额不得超标", scope: "ALL" }
|
|
]
|
|
});
|
|
|
|
const parsed = parseLocalRulesContent(raw);
|
|
expect(parsed.rules.length).toBe(2);
|
|
expect(parsed.meta.corpId).toBe("corp-1");
|
|
expect(parsed.meta.source).toBe("draft");
|
|
expect(parsed.rules[0]!.description).toBe("餐饮发票必须提供水单");
|
|
expect(parsed.rules[0]!.strictness).toBe("MUST_FOLLOW");
|
|
expect(parsed.rules[1]!.strictness).toBe("MUST_FOLLOW"); // default
|
|
});
|
|
|
|
test("parses raw rules array", () => {
|
|
const raw = JSON.stringify([
|
|
{ description: "打车必须备注起止地点", scope: "TAXI" }
|
|
]);
|
|
|
|
const parsed = parseLocalRulesContent(raw);
|
|
expect(parsed.rules.length).toBe(1);
|
|
expect(parsed.rules[0]!.description).toBe("打车必须备注起止地点");
|
|
expect(parsed.rules[0]!.code).toBe(0);
|
|
});
|
|
|
|
test("rejects invalid format or empty description", () => {
|
|
expect(() => parseLocalRulesContent("not-json")).toThrow("JSON 解析失败");
|
|
expect(() => parseLocalRulesContent(JSON.stringify({ other: 123 }))).toThrow("本地规则文件格式错误");
|
|
expect(() => parseLocalRulesContent(JSON.stringify([{ code: 1 }]))).toThrow("缺少有效的 'description'");
|
|
});
|
|
|
|
test("saves and loads local rules file accurately", () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "seal-rules-test-"));
|
|
tempDirs.push(dir);
|
|
const filePath = join(dir, "sub", "rules.json");
|
|
|
|
const savedPath = saveLocalRulesFile(filePath, {
|
|
corpId: "test-corp",
|
|
source: "version:5",
|
|
rules: [
|
|
{ code: 10, description: "酒店住宿必须提供明细清单", scope: "HOTEL", strictness: "MUST_FOLLOW" }
|
|
]
|
|
});
|
|
|
|
expect(savedPath).toBe(filePath);
|
|
|
|
const loaded = loadLocalRulesFile(filePath);
|
|
expect(loaded.rules.length).toBe(1);
|
|
expect(loaded.meta.corpId).toBe("test-corp");
|
|
expect(loaded.rules[0]!.code).toBe(10);
|
|
expect(loaded.rules[0]!.description).toBe("酒店住宿必须提供明细清单");
|
|
});
|
|
|
|
test("converts local rules to lint targets", () => {
|
|
const targets = localRulesToLintTargets([
|
|
{ code: 0, description: "规则零", name: "R0" },
|
|
{ code: "0001", description: "规则一" },
|
|
{ description: "规则二" }
|
|
]);
|
|
|
|
expect(targets[0]!.code).toBe("0000");
|
|
expect(targets[0]!.name).toBe("R0");
|
|
expect(targets[1]!.code).toBe("0001");
|
|
expect(targets[2]!.code).toBe("0002");
|
|
});
|
|
});
|
|
|
|
describe("rules-local offline lint tool integration", () => {
|
|
test("lints local file directly with 0 network calls and high score", async () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "seal-lint-test-"));
|
|
tempDirs.push(dir);
|
|
const filePath = join(dir, "clean-rules.json");
|
|
|
|
saveLocalRulesFile(filePath, {
|
|
corpId: "test-corp",
|
|
rules: [
|
|
{
|
|
code: 1,
|
|
description: "单张发票金额超过500元时,必须提供经办人签字的费用明细水单与消费清单。",
|
|
scope: "MEAL",
|
|
strictness: "MUST_FOLLOW"
|
|
}
|
|
]
|
|
});
|
|
|
|
const lintTool = findSealTool("seal_approval_rules_lint")!;
|
|
expect(lintTool).toBeDefined();
|
|
|
|
const result = await lintTool.handler(
|
|
null as any,
|
|
{ file: filePath } as any,
|
|
{ corp: { id: "test-corp" } } as any
|
|
) as any;
|
|
|
|
expect(result.totalRules).toBe(1);
|
|
expect(result.passed).toBe(true);
|
|
expect(result.overallScore).toBe(100);
|
|
});
|
|
|
|
test("detects vague wording in local rules file and enforces minScore gate", async () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "seal-lint-test-"));
|
|
tempDirs.push(dir);
|
|
const filePath = join(dir, "vague-rules.json");
|
|
|
|
saveLocalRulesFile(filePath, {
|
|
corpId: "test-corp",
|
|
rules: [
|
|
{
|
|
code: 1,
|
|
description: "金额适当即可,视情况而定。",
|
|
scope: "ALL"
|
|
}
|
|
]
|
|
});
|
|
|
|
const lintTool = findSealTool("seal_approval_rules_lint")!;
|
|
|
|
const result = await lintTool.handler(
|
|
null as any,
|
|
{ file: filePath } as any,
|
|
{ corp: { id: "test-corp" } } as any
|
|
) as any;
|
|
|
|
expect(result.totalRules).toBe(1);
|
|
expect(result.overallScore).toBeLessThan(100);
|
|
expect(result.issues[0].issues.some((i: any) => i.code === "VAGUE_WORD")).toBe(true);
|
|
|
|
// minScore enforcement
|
|
await expect(
|
|
lintTool.handler(
|
|
null as any,
|
|
{ file: filePath, minScore: 95 } as any,
|
|
{ corp: { id: "test-corp" } } as any
|
|
)
|
|
).rejects.toThrow("Linter 质量门禁未通过");
|
|
});
|
|
});
|