snapshot-normalize.spec.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { describe, expect, it } from 'vitest'
  2. import { type NormalizeContext, normalizeSessionLog, normalizeStdout } from '../tests/snapshot-normalize.ts'
  3. /**
  4. * Unit tests for the pure snapshot normalizers. Live as a *.spec.ts (runs in
  5. * the default unit gate) and import the harness-side normalizers directly.
  6. */
  7. const ctx: NormalizeContext = {
  8. sessionIds: ['11111111-2222-3333-4444-555555555555'],
  9. cwd: '/tmp/acp-snap-cwd-abc123',
  10. }
  11. describe('normalizeStdout', () => {
  12. it('rewrites JSON-RPC ids to a stable first-seen sequence', () => {
  13. const raw = [
  14. JSON.stringify({ jsonrpc: '2.0', id: 42, method: 'initialize' }),
  15. JSON.stringify({ jsonrpc: '2.0', id: 42, result: {} }),
  16. JSON.stringify({ jsonrpc: '2.0', id: 99, method: 'session/new' }),
  17. ].join('\n')
  18. const out = normalizeStdout(raw, ctx)
  19. expect(out).toContain('"id":1')
  20. expect(out).toContain('"id":2')
  21. expect(out).not.toContain('42')
  22. expect(out).not.toContain('99')
  23. })
  24. it('scrubs the cwd and session id anywhere they appear', () => {
  25. const raw = JSON.stringify({
  26. jsonrpc: '2.0', method: 'session/update',
  27. params: { sessionId: ctx.sessionIds[0], cwd: ctx.cwd, note: `at ${ctx.cwd}/x` },
  28. })
  29. const out = normalizeStdout(raw, ctx)
  30. expect(out).toContain('{{sessionId}}')
  31. expect(out).toContain('{{cwd}}')
  32. expect(out).not.toContain(ctx.cwd)
  33. expect(out).not.toContain(ctx.sessionIds[0] as string)
  34. })
  35. it('scrubs a stray UUID not in the known list', () => {
  36. const raw = JSON.stringify({ jsonrpc: '2.0', method: 'x', params: { id: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' } })
  37. expect(normalizeStdout(raw, ctx)).toContain('{{sessionId}}')
  38. })
  39. it('leaves notification frames without an id untouched in id-space', () => {
  40. const raw = JSON.stringify({ jsonrpc: '2.0', method: 'session/update', params: {} })
  41. const out = normalizeStdout(raw, ctx)
  42. expect(out).not.toContain('"id"')
  43. })
  44. it('throws on a non-JSON stdout line (the purity check)', () => {
  45. const raw = `${JSON.stringify({ jsonrpc: '2.0', id: 1 })}\noops a log leaked\n`
  46. expect(() => normalizeStdout(raw, ctx)).toThrow()
  47. })
  48. it('ignores blank lines', () => {
  49. const raw = `\n${JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'm' })}\n\n`
  50. expect(() => normalizeStdout(raw, ctx)).not.toThrow()
  51. })
  52. })
  53. describe('normalizeSessionLog', () => {
  54. const header = (over: object) => JSON.stringify({ type: 'session', version: 0, id: 's', createdAt: 123, ...over })
  55. const event = (over: object) => JSON.stringify({ type: 'turn/start', seq: 1, time: 999, data: { turn: 1 }, ...over })
  56. it('zeroes the header createdAt', () => {
  57. const out = normalizeSessionLog(`${header({})}\n`, ctx)
  58. expect(out).toContain('"createdAt":0')
  59. expect(out).not.toContain('123')
  60. })
  61. it('zeroes each event time but keeps seq', () => {
  62. const out = normalizeSessionLog(`${header({})}\n${event({ seq: 7, time: 999 })}\n`, ctx)
  63. expect(out).toContain('"time":0')
  64. expect(out).toContain('"seq":7') // seq is deterministic — NOT scrubbed
  65. expect(out).not.toContain('999')
  66. })
  67. it('scrubs cwd and session id deep inside event data', () => {
  68. const ev = JSON.stringify({
  69. type: 'tool/result', seq: 2, time: 5,
  70. data: { content: [{ type: 'text', text: `wrote ${ctx.cwd}/proof.txt` }] },
  71. })
  72. const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
  73. expect(out).toContain('{{cwd}}')
  74. expect(out).not.toContain(ctx.cwd)
  75. })
  76. it('scrubs the session id in the header', () => {
  77. const out = normalizeSessionLog(`${header({ id: ctx.sessionIds[0] })}\n`, ctx)
  78. expect(out).toContain('{{sessionId}}')
  79. })
  80. it('zeroes a hook/result durationMs (run-to-run noise) but keeps its decision', () => {
  81. const ev = JSON.stringify({
  82. type: 'hook/result', seq: 2, time: 5,
  83. data: { turn: 1, point: 'UserPromptSubmit', handlerId: 'h', decision: 'block', exitCode: 2, durationMs: 37 },
  84. })
  85. const out = normalizeSessionLog(`${header({})}\n${ev}\n`, ctx)
  86. expect(out).toContain('"durationMs":0')
  87. expect(out).not.toContain('37')
  88. expect(out).toContain('"decision":"block"') // the decision is the behavior — kept
  89. })
  90. it('leaves a non-hook event durationMs untouched (only hook/result is scrubbed)', () => {
  91. const ev = JSON.stringify({ type: 'tool/result', seq: 2, time: 5, data: { durationMs: 88 } })
  92. const out = normalizeSessionLog(`${header({})}\n${ev}\n`, ctx)
  93. expect(out).toContain('"durationMs":88')
  94. })
  95. })