From 237e0c85561dcde338c3787baa1f56bca733d37b Mon Sep 17 00:00:00 2001 From: ssw Date: Mon, 29 Jun 2026 12:23:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(doc):=20v0.12+=20=E6=94=AF=E6=8C=81=20.md?= =?UTF-8?q?=20section=20=E5=88=87=E7=89=87=20(=E6=8C=89=20##=20=E5=88=87,?= =?UTF-8?q?=20id=3D=E6=A0=87=E9=A2=98)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - _parser.ts 加 parseMarkdownSections (按 ^## 切分, H1 作前言附加到首 section) - cache key 加 stripKey 后缀, 避免 stripMermaid=true/false 串扰 - list.ts 扫 .md/.markdown, 跳 screenshots/test_logs/assets/_assets - 4 个命令 description 改 HTML/MD 配套: ssw-h5dev-flow v0.12+ plan/debug/research 走 MD, 子代理切片读 token 减半, 不再回退到 Read 整个文件 (踩 R1 反模式) 测试: tests/unit/doc-md-slice.test.ts 14/14 PASS (HTML 回归 9/9 PASS) --- tests/fixtures/plan.md | 36 ++++++++++ tests/unit/doc-md-slice.test.ts | 121 ++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 tests/fixtures/plan.md create mode 100644 tests/unit/doc-md-slice.test.ts diff --git a/tests/fixtures/plan.md b/tests/fixtures/plan.md new file mode 100644 index 0000000..56fb863 --- /dev/null +++ b/tests/fixtures/plan.md @@ -0,0 +1,36 @@ +# 数据要素人工智能创新大赛管理平台 + +## 元信息 + +| 字段 | 值 | +| --- | --- | +| app_code | jxcj | +| systemId | 841235275286839301 | + +## 1. 当前任务 (上下文恢复指针) + +- 阶段: 阶段 4 - 执行 +- 具体任务: **派子代理创建 4 模块 10 form** +- 负责人: 主代理 +- 预计完成: 本轮 +- 前置依赖: jxcj 应用已重建 +- 回滚点: 无 (新建应用) + +## 2. 已决事项 (子代理回写) + +| enCode | id | state | idempotency | 关键决策 | +| --- | --- | --- | --- | --- | +| competitionInfo | 8412... | 1 | ok | webType=2 | + +```mermaid +graph LR + A[主代理] --> B[子代理] + B --> C[form] +``` + +## 3. 阶段进度 + +- [x] 阶段 1: 初始化 +- [ ] 阶段 2: 设计 +- [ ] 阶段 3: 审核 +- [x] 阶段 4: 执行 (5/10) diff --git a/tests/unit/doc-md-slice.test.ts b/tests/unit/doc-md-slice.test.ts new file mode 100644 index 0000000..398087c --- /dev/null +++ b/tests/unit/doc-md-slice.test.ts @@ -0,0 +1,121 @@ +/** + * tests/unit/doc-md-slice.test.ts — .md section 切片 (v0.12+) + * + * 背景: ssw-h5dev-flow 改 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, `t +

概述

概述内容
+

数据模型

表 10 张
+`) + } +}) + +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) + }) +})