| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- import { test } from 'node:test'
- import assert from 'node:assert/strict'
- import path from 'node:path'
- import os from 'node:os'
- import { promises as fs } from 'node:fs'
- import { execFile } from 'node:child_process'
- import { promisify } from 'node:util'
- import { CacheManager } from '../../src/cache/index.js'
- import { determineNextState } from '../../src/state-machine/index.js'
- import { runHealthCheck } from '../../src/health-check/index.js'
- const execFileAsync = promisify(execFile)
- // 造 git 书仓库 + 缓存。files = {相对路径: 内容};committed=true 时初始全部提交
- async function makeGitBook(files, { commit = true } = {}) {
- const root = await fs.mkdtemp(path.join(os.tmpdir(), 'wnw-sm-'))
- const git = (a) => execFileAsync('git', a, { cwd: root })
- await git(['init', '-q'])
- await git(['config', 'user.email', 't@example.com'])
- await git(['config', 'user.name', 'test'])
- await fs.writeFile(path.join(root, '.gitignore'), '.cache/\n工作区/\n', 'utf8')
- for (const [rel, content] of Object.entries(files)) {
- const full = path.join(root, rel)
- await fs.mkdir(path.dirname(full), { recursive: true })
- await fs.writeFile(full, content, 'utf8')
- }
- if (commit) {
- await git(['add', '-A'])
- await git(['commit', '-q', '-m', 'init'])
- }
- const dbDir = await fs.mkdtemp(path.join(os.tmpdir(), 'wnw-sm-db-'))
- const cache = new CacheManager(path.join(dbDir, 'index.db'))
- await cache.ensureReady(root)
- return {
- root,
- git,
- ctx: { repoPath: root, cache },
- cleanup: async () => {
- await cache.close()
- await fs.rm(root, { recursive: true, force: true })
- await fs.rm(dbDir, { recursive: true, force: true })
- },
- }
- }
- const ch = (n, vol = 1, pos = '推进', 收卷 = false) =>
- `---\n章号: ${n}\n标题: 第${n}章\n卷: ${vol}\n字数: 100\n章定位: ${pos}\n钩子: 危机钩-强\n情绪定位: 铺垫${收卷 ? '\n收卷: 是' : ''}\n---\n正文。`
- const healthyBook = (extra = {}) => ({
- 'book.yaml': 'spec_version: "7.0"\n书名: 测\n卷规模: 40\n体检周期: 50\n',
- '大纲/总纲.md': '# 总纲\n## 结局\nx',
- '定稿/正文/0001-第1章.md': ch(1),
- ...extra,
- })
- test('序1:无 book.yaml → 建书引导', async () => {
- const { ctx, cleanup } = await makeGitBook({ '大纲/占位.md': 'x' })
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 1)
- assert.equal(r.state, 'create-book')
- } finally {
- await cleanup()
- }
- })
- test('序6:健康书、无异常 → 起草新章细纲', async () => {
- const { ctx, cleanup } = await makeGitBook(healthyBook())
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 6, `实际:${JSON.stringify(r)}`)
- assert.equal(r.state, 'draft-outline')
- } finally {
- await cleanup()
- }
- })
- test('序0:源文件解析失败 → 修复确认', async () => {
- const { ctx, cleanup } = await makeGitBook(
- healthyBook({ '定稿/正文/0002-坏章.md': '---\n章号: 2\n标题: [未闭合\n卷: : :\n---\n正文' })
- )
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 0)
- assert.equal(r.state, 'repair-confirm')
- } finally {
- await cleanup()
- }
- })
- test('序2:定稿有未登记手改 → 提议补登', async () => {
- const { ctx, root, cleanup } = await makeGitBook(healthyBook())
- try {
- // 提交后手改一个已跟踪文件(不提交)
- await fs.writeFile(path.join(root, '定稿/正文/0001-第1章.md'), ch(1) + '\n手改了一句。', 'utf8')
- const r = await determineNextState(ctx)
- assert.equal(r.序, 2)
- assert.equal(r.state, 'relink-manual-edits')
- } finally {
- await cleanup()
- }
- })
- test('序3:工作区有未完成草稿 → 断点续跑', async () => {
- const { ctx, root, cleanup } = await makeGitBook(healthyBook())
- try {
- await fs.mkdir(path.join(root, '工作区'), { recursive: true })
- await fs.writeFile(path.join(root, '工作区/草稿-A.md'), '半成品草稿', 'utf8')
- const r = await determineNextState(ctx)
- assert.equal(r.序, 3)
- assert.equal(r.state, 'resume')
- } finally {
- await cleanup()
- }
- })
- test('序4:最新定稿章声明收卷 → 卷复盘(声明制)', async () => {
- const { ctx, cleanup } = await makeGitBook({
- 'book.yaml': 'spec_version: "7.0"\n书名: 测\n卷规模: 40\n体检周期: 50\n',
- '大纲/总纲.md': '# 总纲',
- '定稿/正文/0001-第1章.md': ch(1),
- '定稿/正文/0002-第2章.md': ch(2, 1, '推进', true),
- })
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 4, `实际:${JSON.stringify(r)}`)
- assert.equal(r.state, 'volume-review')
- assert.equal(r.dto.卷, 1)
- } finally {
- await cleanup()
- }
- })
- test('序4 反例:章号为卷规模整数倍但未声明收卷 → 不触发卷复盘(旧整除行为消失)', async () => {
- const { ctx, cleanup } = await makeGitBook({
- 'book.yaml': 'spec_version: "7.0"\n书名: 测\n卷规模: 2\n体检周期: 50\n',
- '大纲/总纲.md': '# 总纲',
- '定稿/正文/0001-第1章.md': ch(1),
- '定稿/正文/0002-第2章.md': ch(2),
- })
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 6, `实际:${JSON.stringify(r)}`)
- } finally {
- await cleanup()
- }
- })
- test('序4:收卷但该卷卷摘要已存在(复盘已做)→ 不再触发', async () => {
- const { ctx, cleanup } = await makeGitBook({
- 'book.yaml': 'spec_version: "7.0"\n书名: 测\n卷规模: 40\n体检周期: 50\n',
- '大纲/总纲.md': '# 总纲',
- '定稿/正文/0001-第1章.md': ch(1, 1, '推进', true),
- '定稿/摘要/卷摘要/第01卷.md': '# 第一卷摘要\n收束。',
- })
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 6, `实际:${JSON.stringify(r)}`)
- } finally {
- await cleanup()
- }
- })
- test('序5:距上次体检满周期 → 体检', async () => {
- const { ctx, cleanup } = await makeGitBook({
- 'book.yaml': 'spec_version: "7.0"\n书名: 测\n卷规模: 3\n体检周期: 2\n',
- '大纲/总纲.md': '# 总纲',
- '定稿/正文/0001-第1章.md': ch(1),
- '定稿/正文/0002-第2章.md': ch(2),
- })
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 5, `实际:${JSON.stringify(r)}`)
- assert.equal(r.state, 'health-check')
- } finally {
- await cleanup()
- }
- })
- test('序5:体检执行后记录章号,未到下个周期不再触发(体检不卡主循环)', async () => {
- const { ctx, cleanup } = await makeGitBook({
- 'book.yaml': 'spec_version: "7.0"\n书名: 测\n卷规模: 40\n体检周期: 2\n',
- '大纲/总纲.md': '# 总纲',
- '定稿/正文/0001-第1章.md': ch(1),
- '定稿/正文/0002-第2章.md': ch(2),
- })
- try {
- const first = await determineNextState(ctx)
- assert.equal(first.序, 5)
- const hc = await runHealthCheck(ctx)
- assert.equal(hc.ok, true, hc.error)
- const second = await determineNextState(ctx)
- assert.equal(second.序, 6, `实际:${JSON.stringify(second)}`)
- } finally {
- await cleanup()
- }
- })
- test('序0:book.yaml 解析失败 → 修复确认(不得静默用默认值)', async () => {
- const { ctx, cleanup } = await makeGitBook(
- healthyBook({ 'book.yaml': '书名: [未闭合\n卷规模: : :\n' })
- )
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 0, `实际:${JSON.stringify(r)}`)
- assert.ok(r.dto.failures.some((f) => f.file === 'book.yaml'))
- } finally {
- await cleanup()
- }
- })
- test('序0:文风铁律 front matter 解析失败 → 修复确认', async () => {
- const { ctx, cleanup } = await makeGitBook(
- healthyBook({ '文风/文风铁律.md': '---\n禁词: [未闭合\n---\n## 铁律\nx' })
- )
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 0)
- assert.ok(r.dto.failures.some((f) => f.file === '文风/文风铁律.md'))
- } finally {
- await cleanup()
- }
- })
- test('序0:名册/时间线表解析失败 → 修复确认', async () => {
- const { ctx, cleanup } = await makeGitBook(
- healthyBook({
- '定稿/设定/名册.md': '## 名册\n没有表格',
- '定稿/设定/时间线/第01卷.md': '不是表格的内容',
- })
- )
- try {
- const r = await determineNextState(ctx)
- assert.equal(r.序, 0)
- const files = r.dto.failures.map((f) => f.file)
- assert.ok(files.includes('定稿/设定/名册.md'), `实际:${files}`)
- assert.ok(files.includes('定稿/设定/时间线/第01卷.md'), `实际:${files}`)
- } finally {
- await cleanup()
- }
- })
- test('序3:工作区仅存细纲 → 断点续跑,细纲不被改写(AC5)', async () => {
- const { ctx, root, cleanup } = await makeGitBook(healthyBook())
- try {
- const 细纲内容 = '# 第 2 章细纲\n## 本章要写到的事\n- [ ] 已确认的事'
- await fs.mkdir(path.join(root, '工作区'), { recursive: true })
- await fs.writeFile(path.join(root, '工作区/细纲.md'), 细纲内容, 'utf8')
- const r = await determineNextState(ctx)
- assert.equal(r.序, 3, `实际:${JSON.stringify(r)}`)
- assert.equal(r.state, 'resume')
- assert.equal(await fs.readFile(path.join(root, '工作区/细纲.md'), 'utf8'), 细纲内容)
- } finally {
- await cleanup()
- }
- })
- test('命中即停:手改(序2) + 工作区草稿(序3) 同时存在 → 先报序2', async () => {
- const { ctx, root, cleanup } = await makeGitBook(healthyBook())
- try {
- await fs.writeFile(path.join(root, '定稿/正文/0001-第1章.md'), ch(1) + '\n手改。', 'utf8')
- await fs.mkdir(path.join(root, '工作区'), { recursive: true })
- await fs.writeFile(path.join(root, '工作区/草稿-A.md'), '草稿', 'utf8')
- const r = await determineNextState(ctx)
- assert.equal(r.序, 2)
- } finally {
- await cleanup()
- }
- })
|