77 lines
5.4 KiB
TypeScript
77 lines
5.4 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test"
|
||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs"
|
||
import { tmpdir } from "node:os"
|
||
import { join } from "node:path"
|
||
import { validateReportDocument } from "./validate.js"
|
||
|
||
const valid = "# 报告\n\n| 报告信息 | 内容 |\n| --- | --- |\n| 报告编号 | TEST |\n| 客户名称 | 企业 |\n| 统计期间 | 2026-08 |\n| 报告状态 | 草稿 |\n| 生成日期 | 2026-10-12 |\n"
|
||
const dirs: string[] = []
|
||
afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }) })
|
||
function check(content: string) {
|
||
const dir = mkdtempSync(join(tmpdir(), "seal-validator-test-"))
|
||
dirs.push(dir)
|
||
const file = join(dir, "report.md")
|
||
writeFileSync(file, content)
|
||
return validateReportDocument(file)
|
||
}
|
||
|
||
describe("native report validator", () => {
|
||
test("valid information and CRLF have the same clean result", () => {
|
||
expect(check(valid)).toMatchObject({ valid: true, tables: 1, figures: 0, issues: [] })
|
||
expect(check(valid.replaceAll("\n", "\r\n"))).toMatchObject({ valid: true, issues: [] })
|
||
})
|
||
test.each(["<!--slot:", "<!--/slot", "<!--@table", "<!-- render:stop", "{{value}}"])("rejects template traces: %s", (mark) => {
|
||
expect(check(valid + mark).issues).toContainEqual(expect.objectContaining({ code: "MK001" }))
|
||
})
|
||
test("reports frontmatter and unclosed code fences", () => {
|
||
expect(check("---\nx: y\n---\n" + valid + "\n```text\nunfinished").issues.map((issue) => issue.code)).toEqual(["FM001", "MD001"])
|
||
})
|
||
test("table columns respect escaped pipes and retain source line numbers", () => {
|
||
const content = valid + "\n| **标题** | 第二列 |\n| --- | --- |\n| a\\|b | good |\n| incomplete |\n"
|
||
const result = check(content)
|
||
expect(result.issues.map((issue) => issue.code)).toEqual(["TB001", "TB002"])
|
||
expect(result.issues[0]!.message).toContain(`第 ${content.split("\n").indexOf("| incomplete |") + 1} 行`)
|
||
})
|
||
test("validates heading depth and the seven section numbers", () => {
|
||
expect(check(valid + "\n### skipped\n## 2. only\n").issues.map((issue) => issue.code)).toEqual(["HD001", "HD002"])
|
||
expect(check(valid + [1, 2, 3, 4, 5, 6, 7].map((n) => `\n## ${n}. section`).join("")).valid).toBe(true)
|
||
})
|
||
test("identifies missing and empty report metadata", () => {
|
||
expect(check(valid.replace("| 报告编号 | TEST |\n", "")).issues).toContainEqual(expect.objectContaining({ code: "INFO001" }))
|
||
expect(check(valid.replace("| 客户名称 | 企业 |", "| 客户名称 | | ")).issues).toContainEqual(expect.objectContaining({ code: "INFO002" }))
|
||
expect(check("# Empty").issues).toEqual([{ level: "ERROR", code: "INFO001", message: "缺少「报告信息」表" }])
|
||
})
|
||
test("accepts remote/data images and returns a structured issue for malformed local URLs", () => {
|
||
const result = check(valid + "\n\n\n")
|
||
expect(result.figures).toBe(3)
|
||
expect(result.issues).toEqual([{ level: "ERROR", code: "IMG001", message: "图片不存在:bad%zz.png" }])
|
||
})
|
||
test("checks totals and percentages in both overview tables", () => {
|
||
const result = check(valid + "\n| 排名 | 失准类型 | 占比 | 张数 |\n| --- | --- | --- | --- |\n| TOP1 | 类型甲 | 10% | 5 |\n| 合计 | — | 100% | 10 |\n\n| 排名 | 风险类型 | 占比 | 张数 |\n| --- | --- | --- | --- |\n| TOP1 | 风险甲 | 10% | 5 |\n| 合计 | — | 100% | 10 |\n")
|
||
expect(result.issues.map((issue) => issue.code)).toEqual(["NM001", "NM003", "NM002", "NM003"])
|
||
})
|
||
test("checks subsection counts against the overview", () => {
|
||
const result = check(valid + "\n| 排名 | 失准类型 | 占比 | 张数 |\n| --- | --- | --- | --- |\n| TOP1 | 类型甲 | 100% | 5 |\n| 合计 | — | 100% | 5 |\n\n## 1. start\n### 2.1 TOP1 类型甲(4 张)\n")
|
||
expect(result.issues).toContainEqual(expect.objectContaining({ code: "NM001", message: "TOP1 小节标题写 4 张,总览表写 5 张" }))
|
||
})
|
||
test("checks detail classification counts against the narrative", () => {
|
||
const result = check(valid + "\n(2 / 3 笔),仅有 **2 笔\n\n| 单号 | 科目 | AI | 人工 | 原因分类 |\n| --- | --- | --- | --- | --- |\n| A | — | — | — | 依规正确 |\n| B | — | — | — | 失准 |\n")
|
||
expect(result.issues.map((issue) => issue.code)).toEqual(["NM004", "NM004", "NM004"])
|
||
})
|
||
test("checks formula arithmetic and numbers missing from the narrative", () => {
|
||
expect(check(valid + "\n1、2、50\n$$\\frac{1}{2} = 50\\%$$\n").valid).toBe(true)
|
||
expect(check(valid + "\n1、2、70\n$$\\frac{1}{2} = 70\\%$$\n").issues).toContainEqual(expect.objectContaining({ code: "NM005" }))
|
||
expect(check(valid + "\n$$123456$$\n").issues).toContainEqual(expect.objectContaining({ code: "NM005", message: "公式中的数值 123456 在正文别处找不到,可能与数据脱节" }))
|
||
})
|
||
test("checks companion ledger CSV and blocks when unreviewed records exist", () => {
|
||
const dir = mkdtempSync(join(tmpdir(), "validate-ledger-"))
|
||
dirs.push(dir)
|
||
const md = join(dir, "报告.md")
|
||
writeFileSync(md, valid)
|
||
writeFileSync(join(dir, "报告-明细台账.csv"), "SN,单据模板,人机一致性,复核定性结论\nS1,对公,分歧,待复核\n")
|
||
const res = validateReportDocument(md)
|
||
expect(res.valid).toBe(false)
|
||
expect(res.issues).toContainEqual(expect.objectContaining({ code: "LD001" }))
|
||
})
|
||
})
|