1
0

generate.test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { fileURLToPath } from 'node:url'
  4. import { generateHostShells, renderTemplate } from '../../src/host-shells/generate.js'
  5. const V7 = fileURLToPath(new URL('../../', import.meta.url))
  6. test('renderTemplate:if/unless 条件块 + 变量插值', () => {
  7. const t = '{{#if a}}A入{{/if}}{{#unless a}}A去{{/unless}} {{x.y}}'
  8. assert.equal(renderTemplate(t, { a: true, x: { y: '值' } }).trim(), 'A入 值')
  9. assert.equal(renderTemplate(t, { a: false, x: { y: '值' } }).trim(), 'A去 值')
  10. })
  11. test('renderTemplate:agentCapable=false → 渲染兼容(降级)模式块', () => {
  12. const t = '{{#if agentCapable}}完整{{/if}}{{#unless agentCapable}}兼容模式{{/unless}}'
  13. assert.match(renderTemplate(t, { agentCapable: false }), /兼容模式/)
  14. assert.ok(!renderTemplate(t, { agentCapable: false }).includes('完整'))
  15. })
  16. test('生成 claude-code 壳:hasHooks 块入、unless 块去;两审完整模式;F1 命令接线;占位符全渲染', async () => {
  17. const out = await generateHostShells(V7)
  18. const skill = out['claude-code']['skills/webnovel-writer/SKILL.md']
  19. assert.match(skill, /SessionStart 已注入/)
  20. assert.ok(!skill.includes('session-context`,向作者报'), 'hasHooks=true 应去掉 unless 块')
  21. assert.match(skill, /独立 subagent/)
  22. assert.ok(!skill.includes('兼容模式'), 'agentCapable=true 应去掉兼容模式块')
  23. assert.match(skill, /node \.webnovel\/bin\/webnovel-writer\.js next --json/, '命令引用变量应渲染为 vendored 调用')
  24. for (const cmdName of ['review-input', 'save-review', 'finalize', 'persist-book', 'persist-outline']) {
  25. assert.ok(skill.includes(cmdName), `写章流程应接 F1 命令 ${cmdName}`)
  26. }
  27. assert.ok(!skill.includes('{{'), '占位符应全部渲染')
  28. })
  29. test('生成 codex 壳:无 hook → unless 块入;角色输出 TOML', async () => {
  30. const out = await generateHostShells(V7)
  31. const skill = out['codex']['skills/webnovel-writer/SKILL.md']
  32. assert.match(skill, /session-context/)
  33. assert.ok(!skill.includes('SessionStart 已注入'))
  34. const role = out['codex']['agents/事实审查.toml']
  35. assert.match(role, /name = "事实审查"/)
  36. assert.match(role, /instructions = """/)
  37. })
  38. test('角色占位符注入 category(来自 schema.js 单源)', async () => {
  39. const out = await generateHostShells(V7)
  40. const role = out['claude-code']['agents/事实审查.md']
  41. assert.match(role, /unregistered_thread/)
  42. assert.ok(!role.includes('{{categories'), 'category 占位符已渲染')
  43. assert.ok(!role.includes('{{schema'), 'schema 占位符已渲染')
  44. })
  45. test('drift check:同输入连跑两次逐字节一致', async () => {
  46. const a = await generateHostShells(V7)
  47. const b = await generateHostShells(V7)
  48. assert.deepEqual(a, b)
  49. })