|
| 1 | +/** |
| 2 | + * Verifies that agent:event:v1 payloads carry generationId through to log |
| 3 | + * payloads. The handlers in useAgentStream extract event.generationId into |
| 4 | + * console.debug calls — this test exercises the extraction logic in isolation |
| 5 | + * without needing a React renderer or Electron IPC. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, expect, it } from 'vitest'; |
| 9 | +import type { AgentStreamEvent } from '../../../../preload/index'; |
| 10 | + |
| 11 | +interface LogPayload { |
| 12 | + generationId: string; |
| 13 | + designId: string; |
| 14 | + textLen?: number | undefined; |
| 15 | + message?: string | undefined; |
| 16 | + code?: string | undefined; |
| 17 | + toolName?: string | undefined; |
| 18 | + toolCallId?: string | undefined; |
| 19 | +} |
| 20 | + |
| 21 | +/** Simulates the log-payload extraction performed by handleTurnStart. */ |
| 22 | +function turnStartLogPayload(event: AgentStreamEvent): LogPayload { |
| 23 | + return { generationId: event.generationId, designId: event.designId }; |
| 24 | +} |
| 25 | + |
| 26 | +/** Simulates the log-payload extraction performed by handleTurnEnd. */ |
| 27 | +function turnEndLogPayload(event: AgentStreamEvent, textBuffer: string): LogPayload { |
| 28 | + return { |
| 29 | + generationId: event.generationId, |
| 30 | + designId: event.designId, |
| 31 | + textLen: (event.finalText ?? textBuffer).length, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +/** Simulates the log-payload extraction performed by handleError. */ |
| 36 | +function errorLogPayload(event: AgentStreamEvent): LogPayload { |
| 37 | + return { |
| 38 | + generationId: event.generationId, |
| 39 | + designId: event.designId, |
| 40 | + message: event.message, |
| 41 | + code: event.code, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +/** Simulates the log-payload extraction performed by handleAgentEnd. */ |
| 46 | +function agentEndLogPayload(event: AgentStreamEvent): LogPayload { |
| 47 | + return { generationId: event.generationId, designId: event.designId }; |
| 48 | +} |
| 49 | + |
| 50 | +/** Simulates the log-payload extraction performed by handleToolCallStart. */ |
| 51 | +function toolCallStartLogPayload(event: AgentStreamEvent): LogPayload { |
| 52 | + return { |
| 53 | + generationId: event.generationId, |
| 54 | + designId: event.designId, |
| 55 | + toolName: event.toolName ?? 'unknown', |
| 56 | + toolCallId: event.toolCallId, |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +describe('useAgentStream — generationId in log payloads', () => { |
| 61 | + const GEN_ID = 'lf3a2k-xyz9'; |
| 62 | + const DESIGN_ID = 'design-001'; |
| 63 | + |
| 64 | + const baseEvent = ( |
| 65 | + type: AgentStreamEvent['type'], |
| 66 | + extra: Partial<AgentStreamEvent> = {}, |
| 67 | + ): AgentStreamEvent => ({ |
| 68 | + type, |
| 69 | + designId: DESIGN_ID, |
| 70 | + generationId: GEN_ID, |
| 71 | + ...extra, |
| 72 | + }); |
| 73 | + |
| 74 | + it('turn_start log carries generationId', () => { |
| 75 | + const payload = turnStartLogPayload(baseEvent('turn_start')); |
| 76 | + expect(payload.generationId).toBe(GEN_ID); |
| 77 | + expect(payload.designId).toBe(DESIGN_ID); |
| 78 | + }); |
| 79 | + |
| 80 | + it('turn_end log carries generationId and textLen', () => { |
| 81 | + const payload = turnEndLogPayload(baseEvent('turn_end', { finalText: 'hello' }), ''); |
| 82 | + expect(payload.generationId).toBe(GEN_ID); |
| 83 | + expect(payload.textLen).toBe(5); |
| 84 | + }); |
| 85 | + |
| 86 | + it('turn_end falls back to textBuffer when finalText absent', () => { |
| 87 | + const payload = turnEndLogPayload(baseEvent('turn_end'), 'buffered text'); |
| 88 | + expect(payload.generationId).toBe(GEN_ID); |
| 89 | + expect(payload.textLen).toBe('buffered text'.length); |
| 90 | + }); |
| 91 | + |
| 92 | + it('error log carries generationId, message, code', () => { |
| 93 | + const payload = errorLogPayload( |
| 94 | + baseEvent('error', { message: 'timeout', code: 'GENERATION_TIMEOUT' }), |
| 95 | + ); |
| 96 | + expect(payload.generationId).toBe(GEN_ID); |
| 97 | + expect(payload.message).toBe('timeout'); |
| 98 | + expect(payload.code).toBe('GENERATION_TIMEOUT'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('agent_end log carries generationId', () => { |
| 102 | + const payload = agentEndLogPayload(baseEvent('agent_end')); |
| 103 | + expect(payload.generationId).toBe(GEN_ID); |
| 104 | + }); |
| 105 | + |
| 106 | + it('tool_call_start log carries generationId and toolName', () => { |
| 107 | + const payload = toolCallStartLogPayload( |
| 108 | + baseEvent('tool_call_start', { toolName: 'str_replace', toolCallId: 'tc-1' }), |
| 109 | + ); |
| 110 | + expect(payload.generationId).toBe(GEN_ID); |
| 111 | + expect(payload.toolName).toBe('str_replace'); |
| 112 | + expect(payload.toolCallId).toBe('tc-1'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('AgentStreamEvent.generationId is a non-empty string', () => { |
| 116 | + // Verifies the type contract: generationId: string (required, not undefined). |
| 117 | + const event: AgentStreamEvent = { |
| 118 | + type: 'turn_start', |
| 119 | + designId: DESIGN_ID, |
| 120 | + generationId: GEN_ID, |
| 121 | + }; |
| 122 | + expect(typeof event.generationId).toBe('string'); |
| 123 | + expect(event.generationId.length).toBeGreaterThan(0); |
| 124 | + }); |
| 125 | +}); |
0 commit comments