import { describe, expect, it } from "bun:test"; import { normalizeTemplateGroups } from "./templates.js"; const raw = [ { name: "报销单拆单", active: true, specifications: [ { id: "a:1", originalId: "a", name: "AI合同审批单", type: "custom", active: true }, { id: "a:2", originalId: "a", name: "报销单-汇总", type: "expense", active: true }, ], }, { name: "历史模板", active: true, specifications: [ { id: "b:1", originalId: "b", name: "旧报销单", type: "expense", active: false }, ], }, { name: "空分组", active: true, specifications: [], }, { name: "停用分组", active: false, specifications: [ { id: "c:1", originalId: "c", name: "隐藏模板", type: "expense", active: true }, ], }, ]; describe("normalizeTemplateGroups", () => { it("默认只返回启用模板,并跳过空分组/停用分组", () => { const result = normalizeTemplateGroups(raw); expect(result.total).toBe(2); expect(result.inactive).toBe(1); expect(result.groups.map((g) => g.name)).toEqual(["报销单拆单"]); expect(result.groups[0].specifications.map((s) => s.name)).toEqual([ "AI合同审批单", "报销单-汇总", ]); }); it("includeInactive 返回停用模板与停用分组", () => { const result = normalizeTemplateGroups(raw, { includeInactive: true }); expect(result.total).toBe(4); expect(result.groups.map((g) => g.name)).toEqual(["报销单拆单", "历史模板", "停用分组"]); const inactiveSpecs = result.groups.flatMap((g) => g.specifications).filter((s) => !s.active); expect(inactiveSpecs.map((s) => s.name)).toEqual(["旧报销单"]); }); it("优先使用 originalId,兼容含冒号的原始 ID", () => { const result = normalizeTemplateGroups([ { name: "系统模板", active: true, specifications: [ { id: "ID01ofWoOC1wiX:system:预算调整申请单:d3ac4cb25935090ab684d3fa4537b3517a849646", originalId: "ID01ofWoOC1wiX:system:预算调整申请单", name: "预算调整申请单", type: "custom", active: true, }, ], }, ]); expect(result.groups[0].specifications[0].id).toBe("ID01ofWoOC1wiX:system:预算调整申请单"); expect(result.groups[0].specifications[0].versionedId).toContain(":d3ac4cb"); }); it("保留分组名与模板类型", () => { const result = normalizeTemplateGroups(raw); expect(result.groups[0].specifications[0]).toMatchObject({ id: "a", versionedId: "a:1", name: "AI合同审批单", type: "custom", group: "报销单拆单", active: true, }); }); });