|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * sessionEvents — message ⇄ event translation — run: node test/sessionEvents.test.js |
| 3 | + * |
| 4 | + * Two guarantees: (1) VERBATIM — messages → events → messages is byte-identical, so a verbatim resume |
| 5 | + * sees exactly what it left; (2) the card stats (sparkline, files-edited) are DERIVED from the turn's |
| 6 | + * own messages, not hand-passed — so they can't drift from the transcript. The last test wires this to |
| 7 | + * sessionStore.deriveEntry to prove the two modules agree end-to-end. |
| 8 | + *--------------------------------------------------------------------------------------------*/ |
| 9 | +// @ts-check |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +const assert = require('assert'); |
| 13 | +const E = require('../sessionEvents'); |
| 14 | +const S = require('../sessionStore'); |
| 15 | + |
| 16 | +let n = 0; |
| 17 | +function test(name, fn) { fn(); n++; console.log(' ok - ' + name); } |
| 18 | + |
| 19 | +// A realistic agent turn: read one file, then edit two, then answer. |
| 20 | +function conversation() { |
| 21 | + return [ |
| 22 | + { role: 'user', content: 'add idempotency to refunds' }, |
| 23 | + { role: 'assistant', content: [{ type: 'text', text: 'reading' }, { type: 'tool_use', id: 'a', name: 'read_file', input: { path: 'refund.rb' } }] }, |
| 24 | + { role: 'user', content: [{ type: 'tool_result', tool_use_id: 'a', content: '…' }] }, |
| 25 | + { role: 'assistant', content: [ |
| 26 | + { type: 'tool_use', id: 'b', name: 'edit_file', input: { path: 'refund.rb' } }, |
| 27 | + { type: 'tool_use', id: 'c', name: 'write_file', input: { path: 'redis_lock.rb' } } |
| 28 | + ] }, |
| 29 | + { role: 'user', content: [{ type: 'tool_result', tool_use_id: 'b' }, { type: 'tool_result', tool_use_id: 'c' }] }, |
| 30 | + { role: 'assistant', content: 'Done: idempotent refunds via Redis keys' } |
| 31 | + ]; |
| 32 | +} |
| 33 | + |
| 34 | +test('STATS: tool count is the sparkline; only edit_file/write_file count as files edited', () => { |
| 35 | + const { tools, edits } = E.toolStatsFromMessages(conversation()); |
| 36 | + assert.strictEqual(tools, 3, 'read_file + edit_file + write_file = 3 tool calls'); |
| 37 | + assert.deepStrictEqual(edits, [{ path: 'refund.rb' }, { path: 'redis_lock.rb' }], 'the read is not an edit'); |
| 38 | + assert.deepStrictEqual(E.toolStatsFromMessages(null), { tools: 0, edits: [] }, 'never throws on junk'); |
| 39 | +}); |
| 40 | + |
| 41 | +test('BUILD: user/agent events carry content + verbatim messages + derived stats', () => { |
| 42 | + const msgs = conversation(); |
| 43 | + const u = E.userTurnEvent(msgs[0], 't1'); |
| 44 | + assert.strictEqual(u.kind, 'user'); |
| 45 | + assert.strictEqual(u.content, 'add idempotency to refunds'); |
| 46 | + assert.deepStrictEqual(u.messages, [msgs[0]]); |
| 47 | + |
| 48 | + const a = E.agentTurnEvent(msgs.slice(1), 'anthropic/claude-opus-5', 't2'); |
| 49 | + assert.strictEqual(a.kind, 'agent'); |
| 50 | + assert.strictEqual(a.model, 'anthropic/claude-opus-5'); |
| 51 | + assert.strictEqual(a.tools, 3); |
| 52 | + assert.deepStrictEqual(a.messages, msgs.slice(1), 'the turn is stored verbatim'); |
| 53 | +}); |
| 54 | + |
| 55 | +test('VERBATIM: messages → events → eventsToMessages is byte-identical (lossless resume)', () => { |
| 56 | + const msgs = conversation(); |
| 57 | + const events = [E.userTurnEvent(msgs[0], 't1'), E.agentTurnEvent(msgs.slice(1), 'm', 't2')]; |
| 58 | + assert.deepStrictEqual(E.eventsToMessages(events), msgs, 'rebuild == original, exactly'); |
| 59 | + // non-transcript events contribute nothing to the rebuild |
| 60 | + const withMeta = events.concat([E.titleEvent('T', 't3'), E.labelEvent({ pinned: true }, 't4'), E.endEvent('done', 't5')]); |
| 61 | + assert.deepStrictEqual(E.eventsToMessages(withMeta), msgs, 'title/label/end carry no messages'); |
| 62 | +}); |
| 63 | + |
| 64 | +test('TAIL: only the new messages since the stored count are appended (incremental, not the whole lot)', () => { |
| 65 | + const msgs = conversation(); |
| 66 | + assert.deepStrictEqual(E.tailFrom(msgs, 3), msgs.slice(3)); |
| 67 | + assert.deepStrictEqual(E.tailFrom(msgs, 0), msgs); |
| 68 | + assert.deepStrictEqual(E.tailFrom(msgs, msgs.length), []); |
| 69 | +}); |
| 70 | + |
| 71 | +test('LABEL: archiving/pinning is an append-only event, never a rewrite', () => { |
| 72 | + assert.deepStrictEqual(E.labelEvent({ lifecycle: 'archived' }, 't'), { kind: 'label', t: 't', lifecycle: 'archived' }); |
| 73 | + assert.deepStrictEqual(E.labelEvent({ pinned: true }, 't'), { kind: 'label', t: 't', pinned: true }); |
| 74 | +}); |
| 75 | + |
| 76 | +// ── cross-module: sessionEvents output feeds sessionStore.deriveEntry correctly ─────────────────── |
| 77 | + |
| 78 | +test('END-TO-END: events built here derive the right card in sessionStore (one source of truth)', () => { |
| 79 | + const msgs = conversation(); |
| 80 | + const meta = { kind: 'meta', v: 1, id: 's1', project: '/p', createdAt: 'c0', title: null }; |
| 81 | + const events = [ |
| 82 | + E.userTurnEvent(msgs[0], 't1'), |
| 83 | + E.agentTurnEvent(msgs.slice(1), 'anthropic/claude-opus-5', 't2'), |
| 84 | + E.titleEvent('Idempotent refunds via Redis keys', 't3'), |
| 85 | + E.endEvent('done', 't4') |
| 86 | + ]; |
| 87 | + const card = S.deriveEntry(meta, events); |
| 88 | + assert.strictEqual(card.title, 'Idempotent refunds via Redis keys'); |
| 89 | + assert.strictEqual(card.turns, 1); |
| 90 | + assert.strictEqual(card.model, 'anthropic/claude-opus-5'); |
| 91 | + assert.strictEqual(card.state, 'done'); |
| 92 | + assert.deepStrictEqual(card.spark, [3], 'the sparkline came from the turn messages via the agent event'); |
| 93 | + assert.deepStrictEqual(card.filesEdited, ['refund.rb', 'redis_lock.rb'], 'and so did the files-edited chips'); |
| 94 | +}); |
| 95 | + |
| 96 | +console.log('sessionEvents: ' + n + ' tests passed'); |
0 commit comments