generate.test.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 块去;两审完整模式;占位符全渲染', 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('扫描含'), 'hasHooks=true 应去掉 unless 块')
  21. assert.match(skill, /完整模式/)
  22. assert.ok(!skill.includes('{{'), '占位符应全部渲染')
  23. })
  24. test('生成 codex 壳:无 hook → unless 块入;角色输出 TOML', async () => {
  25. const out = await generateHostShells(V7)
  26. const skill = out['codex']['skills/webnovel-writer/SKILL.md']
  27. assert.match(skill, /读工作目录/)
  28. assert.ok(!skill.includes('SessionStart 已注入'))
  29. const role = out['codex']['agents/事实审查.toml']
  30. assert.match(role, /name = "事实审查"/)
  31. assert.match(role, /instructions = """/)
  32. })
  33. test('角色占位符注入 category(来自 schema.js 单源)', async () => {
  34. const out = await generateHostShells(V7)
  35. const role = out['claude-code']['agents/事实审查.md']
  36. assert.match(role, /unregistered_thread/)
  37. assert.ok(!role.includes('{{categories'), 'category 占位符已渲染')
  38. assert.ok(!role.includes('{{schema'), 'schema 占位符已渲染')
  39. })
  40. test('drift check:同输入连跑两次逐字节一致', async () => {
  41. const a = await generateHostShells(V7)
  42. const b = await generateHostShells(V7)
  43. assert.deepEqual(a, b)
  44. })