Files
dacli/tests/unit/doc-md-slice.test.ts
sushub f403a7e80e import: ssw/dacli@eea0ebc (2026-08-12) 起点版本
本仓 2026-08-12 从 ssw main (192.168.1.18:13300/ssw/ssw-mvp) 的
dacli/ subpath 抽取,作为独立仓托管到 gitea:3000/ssw/dacli.git。

- 工作树为 ssw main dacli subpath 最新版 (110 commit, 08-12)
- 历史未带 ssw main 整体历史(避免大仓污染)
- 此为新仓基线 commit,未来以本仓历史为主
2026-08-12 15:58:06 +08:00

122 lines
4.5 KiB
TypeScript

/**
* tests/unit/doc-md-slice.test.ts — .md section 切片 (v0.12+)
*
* 背景: ssw-loop 改 plan/debug/research 走 MD 后, dacli doc get 必须能按 ## 切分,
* 否则子代理回退到 Read 整个 .md (踩 R1 反模式, 25K+ tokens).
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { mkdtempSync, copyFileSync, rmSync, existsSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import {
parseFile, getSection, getRawLines, listSections, isMarkdown,
} from '../../src/commands/doc/_parser.ts'
let tmpDir: string
let mdPath: string
let htmlPath: string
const FIXTURE_MD = join(__dirname, '..', 'fixtures', 'plan.md')
const FIXTURE_HTML = join(__dirname, '..', 'fixtures', 'design-sample.html')
beforeAll(() => {
tmpDir = mkdtempSync(join(tmpdir(), 'dacli-md-'))
mdPath = join(tmpDir, 'plan.md')
if (existsSync(FIXTURE_MD)) copyFileSync(FIXTURE_MD, mdPath)
else writeFileSync(mdPath, '# placeholder\n')
htmlPath = join(tmpDir, 'design.html')
if (existsSync(FIXTURE_HTML)) copyFileSync(FIXTURE_HTML, htmlPath)
else {
writeFileSync(htmlPath, `<!DOCTYPE html><html><head><title>t</title></head><body>
<div id="h2_overview"><h2>概述</h2>概述内容</div>
<div id="h2_models"><h2>数据模型</h2>表 10 张</div>
</body></html>`)
}
})
afterAll(() => { rmSync(tmpDir, { recursive: true, force: true }) })
describe('isMarkdown', () => {
it('.md → true', () => expect(isMarkdown('plan.md')).toBe(true))
it('.markdown → true', () => expect(isMarkdown('plan.markdown')).toBe(true))
it('.html → false', () => expect(isMarkdown('design.html')).toBe(false))
it('大写 .MD → true', () => expect(isMarkdown('PLAN.MD')).toBe(true))
})
describe('parseFile (.md)', () => {
it('按 ## 切分出 4 个 section', () => {
const { sections } = parseFile(mdPath, false)
const ids = Array.from(sections.keys())
expect(ids).toEqual([
'元信息',
'1. 当前任务 (上下文恢复指针)',
'2. 已决事项 (子代理回写)',
'3. 阶段进度',
])
})
it('section 行数合理 (>=1)', () => {
const { sections } = parseFile(mdPath, false)
for (const sec of sections.values()) expect(sec.lines).toBeGreaterThanOrEqual(1)
})
})
describe('getSection (.md)', () => {
it('按中文标题 id 命中 (核心用例: 上下文恢复读 plan.md 当前任务)', () => {
const sec = getSection(mdPath, '1. 当前任务 (上下文恢复指针)', false)
expect(sec).not.toBeNull()
expect(sec!.title).toBe('1. 当前任务 (上下文恢复指针)')
expect(sec!.text).toContain('**派子代理创建 4 模块 10 form**')
// 6 个动态指针字段 (阶段/具体任务/负责人/预计完成/前置依赖/回滚点)
expect(sec!.text).toContain('回滚点: 无 (新建应用)')
// H1 应用名作为前言
expect(sec!.text).toContain('数据要素人工智能创新大赛管理平台')
})
it('不存在的 id → null', () => {
expect(getSection(mdPath, '不存在的标题', false)).toBeNull()
})
it('--section=元信息 也命中', () => {
const sec = getSection(mdPath, '元信息', false)
expect(sec).not.toBeNull()
expect(sec!.text).toContain('jxcj')
})
})
describe('getRawLines (.md)', () => {
it('按行号切片', () => {
const r = getRawLines(mdPath, 1, 5, false)
expect(r).not.toBeNull()
expect(r!.content).toContain('数据要素人工智能创新大赛管理平台')
})
})
describe('listSections (.md)', () => {
it('返回所有 section 元数据', () => {
const list = listSections(mdPath)
expect(list.length).toBe(4)
expect(list[0].id).toBe('元信息')
expect(list[0].title).toBe('元信息')
})
})
describe('stripMermaid (.md)', () => {
it('--strip-mermaid=true 去除 mermaid 围栏', () => {
const sec = getSection(mdPath, '2. 已决事项 (子代理回写)', true)
expect(sec!.text).not.toContain('graph LR')
expect(sec!.text).not.toContain('mermaid')
expect(sec!.text).toContain('competitionInfo')
})
it('默认保留 mermaid (对理解设计有用)', () => {
const sec = getSection(mdPath, '2. 已决事项 (子代理回写)', false)
expect(sec!.text).toContain('graph LR')
})
})
describe('HTML 文件仍按原 HTML 解析 (回归)', () => {
it('HTML 文件解析出 h2_overview/h2_models', () => {
const { sections } = parseFile(htmlPath, false)
expect(sections.has('h2_overview')).toBe(true)
expect(sections.has('h2_models')).toBe(true)
})
})