gate.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { afterAll, describe, expect, it } from 'vitest'
  2. import * as fs from 'node:fs'
  3. import * as path from 'node:path'
  4. import * as os from 'node:os'
  5. import { decidePreExecute } from '../src/gate/preExecute'
  6. import { reportDraftReadiness } from '../src/gate/writeDraft'
  7. import { serializeDocument } from '../src/repo/frontmatter'
  8. const roots: string[] = []
  9. function mkBook(): string {
  10. const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'webnovel-gate-'))
  11. roots.push(dir)
  12. return dir
  13. }
  14. function put(root: string, rel: string, content: string): void {
  15. fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true })
  16. fs.writeFileSync(path.join(root, rel), content, 'utf-8')
  17. }
  18. afterAll(() => { for (const r of roots) { try { fs.rmSync(r, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 }) } catch { /* Windows 句柄延迟释放,%TEMP% 残留无害 */ } } })
  19. function write(file_path: string) {
  20. return { name: 'write', arguments: { file_path } }
  21. }
  22. describe('文件门禁(不变量 4/5)', () => {
  23. it('允许写入草稿区/章细纲/', () => {
  24. const root = mkBook()
  25. const d = decidePreExecute(root, write(path.join(root, '草稿区/章细纲/卷01-初见.md')))
  26. expect(d).toEqual({ kind: 'allow' })
  27. })
  28. it('拒绝写入定稿/', () => {
  29. const root = mkBook()
  30. const d = decidePreExecute(root, write(path.join(root, '定稿/卷01/0001-初见.md')))
  31. expect(d.kind).toBe('deny')
  32. if (d.kind === 'deny') expect(d.reason).toMatch(/不变量 4/)
  33. })
  34. it('拒绝写入构想/', () => {
  35. const root = mkBook()
  36. const d = decidePreExecute(root, write(path.join(root, '构想/构想快照.md')))
  37. expect(d.kind).toBe('deny')
  38. if (d.kind === 'deny') expect(d.reason).toMatch(/不变量 5/)
  39. })
  40. it('拒绝写入大纲/(真源,非文件工具)', () => {
  41. const root = mkBook()
  42. const d = decidePreExecute(root, write(path.join(root, '大纲/故事骨架.md')))
  43. expect(d.kind).toBe('deny')
  44. if (d.kind === 'deny') expect(d.reason).toMatch(/不变量 4/)
  45. })
  46. it('路径逃逸拒绝', () => {
  47. const root = mkBook()
  48. const escaped = decidePreExecute(root, write(path.join(root, '..', '越界.md')))
  49. expect(escaped.kind).toBe('deny')
  50. if (escaped.kind === 'deny') expect(escaped.reason).toMatch(/逃逸|不变量 4/)
  51. })
  52. it('改写工具缺路径→拒绝', () => {
  53. const root = mkBook()
  54. expect(decidePreExecute(root, { name: 'write', arguments: {} }).kind).toBe('deny')
  55. expect(decidePreExecute(root, { name: 'edit', arguments: { path: '草稿区/x.md' } }).kind).toBe('deny')
  56. })
  57. })
  58. describe('写稿门槛(R1 落码:播报不拒绝)', () => {
  59. it('未确认细纲:放行写入,readiness 如实播报', () => {
  60. const root = mkBook()
  61. const rel = '草稿区/草稿/卷01-初见/稿1.md'
  62. const d = decidePreExecute(root, write(path.join(root, rel)))
  63. expect(d).toEqual({ kind: 'allow' })
  64. const r = reportDraftReadiness(root, rel)
  65. expect(r?.细纲已确认).toBe(false)
  66. expect(r?.说明).toMatch(/细纲未确认/)
  67. })
  68. it('材料包组装中:放行写入,readiness 播报材料包状态', () => {
  69. const root = mkBook()
  70. put(root, '大纲/卷规划/卷01/章细纲/0003-初见.md', serializeDocument({ 状态: '已确认' }, '细纲'))
  71. put(root, '草稿区/材料包/卷01-初见/材料清单.json', JSON.stringify({ schemaVersion: 1, 状态: '组装中' }))
  72. const rel = '草稿区/草稿/卷01-初见/稿1.md'
  73. expect(decidePreExecute(root, write(path.join(root, rel)))).toEqual({ kind: 'allow' })
  74. const r = reportDraftReadiness(root, rel)
  75. expect(r?.细纲已确认).toBe(true)
  76. expect(r?.材料包状态).toBe('组装中')
  77. })
  78. it('草稿路径无法解析章节键→拒绝(路径形状校验保留)', () => {
  79. const root = mkBook()
  80. const d = decidePreExecute(root, write(path.join(root, '草稿区/草稿/随意路径/稿1.md')))
  81. expect(d.kind).toBe('deny')
  82. if (d.kind === 'deny') expect(d.reason).toMatch(/章节键/)
  83. })
  84. it('确认细纲+材料包可写→允许写草稿,readiness 全绿', () => {
  85. const root = mkBook()
  86. put(root, '大纲/卷规划/卷01/章细纲/0003-初见.md', serializeDocument({ 状态: '已确认' }, '细纲'))
  87. put(root, '草稿区/材料包/卷01-初见/材料清单.json', JSON.stringify({ schemaVersion: 1, 状态: '可写' }))
  88. const rel = '草稿区/草稿/卷01-初见/稿1.md'
  89. const d = decidePreExecute(root, write(path.join(root, rel)))
  90. expect(d).toEqual({ kind: 'allow' })
  91. const r = reportDraftReadiness(root, rel)
  92. expect(r?.细纲已确认).toBe(true)
  93. expect(r?.材料包状态).toBe('可写')
  94. })
  95. })
  96. describe('str_replace_editor 命令分流', () => {
  97. it('view 允许读定稿;str_replace 拒绝', () => {
  98. const root = mkBook()
  99. const dest = path.join(root, '定稿/卷01/0001-初见.md')
  100. const view = decidePreExecute(root, { name: 'str_replace_editor', arguments: { command: 'view', path: dest } })
  101. expect(view).toEqual({ kind: 'allow' })
  102. const edit = decidePreExecute(root, { name: 'str_replace_editor', arguments: { command: 'str_replace', path: dest } })
  103. expect(edit.kind).toBe('deny')
  104. })
  105. })