import { test } from 'node:test'; import assert from 'node:assert/strict'; import { isSameMessage, filterPendingLiveMessages, mergeLiveOverHistory, } from './messageIdentity'; import type { ChatMessage } from '../types/workbench'; const base = (patch: Partial): ChatMessage => ({ id: 'tmp-1', sessionId: 'sess-1', role: 'user', content: 'hello', timestamp: '10:00', ...patch, }); test('isSameMessage: clientTurnId match', () => { const local = base({ id: 'tmp-user', clientTurnId: 'turn-abc' }); const server = base({ id: 'msg-101', serverId: 101, clientTurnId: 'turn-abc' }); assert.equal(isSameMessage(local, server), true); }); test('isSameMessage: serverId match without turn', () => { const local = base({ id: 'ast-123', serverId: 102, role: 'assistant' }); const server = base({ id: 'msg-102', serverId: 102, role: 'assistant' }); assert.equal(isSameMessage(local, server), true); }); test('filterPendingLiveMessages: real temp vs server id', () => { const history = [base({ id: 'msg-101', serverId: 101, clientTurnId: 'turn-abc' })]; const live = [ base({ id: 'tmp-user', clientTurnId: 'turn-abc' }), base({ id: 'tmp-asst', role: 'assistant', clientTurnId: 'turn-abc', serverId: 102 }), ]; const pending = filterPendingLiveMessages(live, history); assert.equal(pending.length, 1); assert.equal(pending[0].role, 'assistant'); }); test('mergeLiveOverHistory preserves server id from live', () => { const history = base({ id: 'msg-101', serverId: 101, content: 'hello' }); const live = base({ id: 'tmp-user', serverId: 101, content: 'hello', status: undefined }); const merged = mergeLiveOverHistory(history, live); assert.equal(merged.id, 'msg-101'); assert.equal(merged.serverId, 101); });