import { createUnsupportedMetricsSnapshot as createBaseUnsupportedMetricsSnapshot } from "./metricsFixtures"; export type AuditStatus = { timestamp: number; connectionState: string; websocketState: string; browserOnline: boolean; hasConnectedOnce: boolean; isRecovering: boolean; isBufferingAudio: boolean; isDrainingAudio: boolean; transportBufferedAmountBytes: number; pendingBufferedAudioBytes: number; pendingBufferedAudioChunks: number; persistedBufferedAudioBytes: number; persistedBufferedAudioSegments: number; totalBufferedAudioBytes: number; bufferingStartedAt: number | null; drainStartedAt: number | null; lastDrainProgressAt: number | null; drainCycles: number; drainExitReason: | "completed" | "socket_unavailable" | "send_failed" | "stalled" | "stopped" | null; reconnectAttempt: number | null; nextReconnectDelayMs: number | null; finishDeliveryState: "sent" | "skipped_socket_closed" | "timed_out" | null; maxReconnectAttempts: number; remainingReconnectAttempts: number | null; lastDisconnect: { code?: number; reason?: string; wasClean?: boolean; errorMessage?: string; } | null; }; export type AuditState = { enabled?: boolean; autoDownload?: boolean; fileName?: string | null; metadata?: Record | null; sessionId: string; label: string | null; createdAt: number; startedAt: number | null; stoppedAt: number | null; requestedProvider: string; resolvedProvider: string | null; phases: Array<{ type: string; timestamp: number }>; incidents: Array<{ type: string; timestamp: number; endedAt: number | null; detail: unknown; }>; transcriptMilestones: Array<{ type: string; timestamp: number; text: string | null }>; checkpoints: Array<{ timestamp: number; resilienceStatus: AuditStatus | null; metricsSnapshot: Record | null; }>; finalDigests: Array<{ digest: string; timestamp: number }>; }; export type SessionReportFinding = { code: string; severity: "info" | "warning" | "critical"; message: string; }; export type SessionReport = { schemaVersion: 1; generatedAt: string; session: { sessionId: string; label: string | null; requestedProvider: string; resolvedProvider: string | null; createdAt: string; startedAt: string | null; stoppedAt: string | null; durationMs: number | null; hasStarted: boolean; hasStopped: boolean; }; summary: { outcome: "healthy" | "degraded_recovered" | "degraded_unrecovered" | "failed"; sessionDurationMs: number | null; partialTranscriptCount: number | null; finalTranscriptCount: number | null; diarizationCount: number | null; avgFirstPartialLatencyMs: number | null; p95FirstPartialLatencyMs: number | null; avgFinalLatencyMs: number | null; p95FinalLatencyMs: number | null; avgStabilizationLatencyMs: number | null; p95StabilizationLatencyMs: number | null; reconnectCount: number | null; recoverySuccessCount: number | null; terminalDisconnectCount: number | null; avgRecoveryDurationMs: number | null; p95RecoveryDurationMs: number | null; peakBufferedAudioBytes: number | null; bufferingDurationMs: number | null; drainingDurationMs: number | null; bufferStallCount: number | null; drainExitReason: AuditStatus["drainExitReason"]; finishDeliveryState: AuditStatus["finishDeliveryState"]; offlineDurationMs: number | null; flapCount: number | null; rttMs: number | null; downlinkMbps: number | null; clippingEventCount: number | null; silenceRatio: number | null; speechActivityRatio: number | null; audioCallbackGapCount: number | null; longestAudioCallbackGapMs: number | null; }; health: Record< string, { supported: boolean; status: "inactive" | "healthy" | "degraded" | "critical" | "unsupported"; reasons: string[]; } >; timeline: { phases: Array<{ type: string; timestamp: number }>; incidents: Array<{ type: string; timestamp: number; endedAt: number | null; detail: unknown; }>; transcriptMilestones: Array<{ type: string; timestamp: number; text: string | null }>; }; findings: SessionReportFinding[]; dashboard: { heroStats: { outcome: SessionReport["summary"]["outcome"]; sessionDurationMs: number | null; finalTranscriptCount: number | null; reconnectCount: number | null; peakBufferedAudioBytes: number | null; offlineDurationMs: number | null; }; cards: Record< string, { status: "inactive" | "healthy" | "degraded" | "critical" | "unsupported"; headline: string; metrics: Record; } >; }; }; const hashText = (text: string) => { let hash = 2166136261; for (let index = 0; index < text.length; index += 1) { hash ^= text.charCodeAt(index); hash = Math.imul(hash, 16777619); } return `${text.length}:${(hash >>> 0).toString(16)}`; }; export const createStatus = ( overrides: Partial = {} ): AuditStatus => ({ timestamp: 1_700_000_000_000, connectionState: "connected", websocketState: "open", browserOnline: true, hasConnectedOnce: true, isRecovering: false, isBufferingAudio: false, isDrainingAudio: false, transportBufferedAmountBytes: 0, pendingBufferedAudioBytes: 0, pendingBufferedAudioChunks: 0, persistedBufferedAudioBytes: 0, persistedBufferedAudioSegments: 0, totalBufferedAudioBytes: 0, bufferingStartedAt: null, drainStartedAt: null, lastDrainProgressAt: null, drainCycles: 0, drainExitReason: null, reconnectAttempt: null, nextReconnectDelayMs: null, finishDeliveryState: null, maxReconnectAttempts: 4, remainingReconnectAttempts: 4, lastDisconnect: null, ...overrides, }); export const createBaseAuditState = ( overrides: Partial = {} ): AuditState => { const baseTimestamp = 1_700_000_000_000; const finalText = "hello world"; return { enabled: true, autoDownload: false, fileName: null, metadata: null, sessionId: "session-1", label: "debug profile", createdAt: baseTimestamp, startedAt: baseTimestamp + 100, stoppedAt: baseTimestamp + 2_500, requestedProvider: "sofya_as_service", resolvedProvider: "sofya_whisper_flow", phases: [ { type: "ready", timestamp: baseTimestamp + 100 }, { type: "started", timestamp: baseTimestamp + 100 }, { type: "connected", timestamp: baseTimestamp + 300 }, { type: "stopped", timestamp: baseTimestamp + 2_500 }, ], incidents: [], transcriptMilestones: [ { type: "first_partial", timestamp: baseTimestamp + 900, text: "hello wo" }, { type: "first_final", timestamp: baseTimestamp + 1_200, text: finalText }, { type: "last_final", timestamp: baseTimestamp + 1_200, text: finalText }, ], checkpoints: [ { timestamp: baseTimestamp + 100, resilienceStatus: createStatus({ timestamp: baseTimestamp + 100, connectionState: "connecting", websocketState: "connecting", }), metricsSnapshot: null, }, { timestamp: baseTimestamp + 300, resilienceStatus: createStatus({ timestamp: baseTimestamp + 300, connectionState: "connected", websocketState: "open", transportBufferedAmountBytes: 0, totalBufferedAudioBytes: 0, }), metricsSnapshot: null, }, { timestamp: baseTimestamp + 1_500, resilienceStatus: createStatus({ timestamp: baseTimestamp + 1_500, connectionState: "connected", websocketState: "open", transportBufferedAmountBytes: 0, totalBufferedAudioBytes: 0, }), metricsSnapshot: null, }, ], finalDigests: [{ digest: hashText(finalText), timestamp: baseTimestamp + 1_200 }], ...overrides, }; }; export const createUnsupportedMetricsSnapshot = () => createBaseUnsupportedMetricsSnapshot({ updatedAt: 1_700_000_000_500, }); export const createRecoveredAuditState = (): AuditState => createBaseAuditState({ phases: [ { type: "ready", timestamp: 1_700_000_000_100 }, { type: "started", timestamp: 1_700_000_000_100 }, { type: "connected", timestamp: 1_700_000_000_300 }, { type: "reconnecting", timestamp: 1_700_000_001_000 }, { type: "reconnected", timestamp: 1_700_000_001_600 }, { type: "stopped", timestamp: 1_700_000_002_500 }, ], incidents: [ { type: "offline", timestamp: 1_700_000_001_000, endedAt: null, detail: { browserOnline: false }, }, { type: "buffering_started", timestamp: 1_700_000_001_000, endedAt: null, detail: { totalBufferedAudioBytes: 2_048 }, }, { type: "online", timestamp: 1_700_000_001_600, endedAt: null, detail: { browserOnline: true }, }, { type: "buffering_cleared", timestamp: 1_700_000_001_600, endedAt: null, detail: { totalBufferedAudioBytes: 0 }, }, { type: "drain_completed", timestamp: 1_700_000_001_600, endedAt: null, detail: { drainExitReason: "completed", finishDeliveryState: "sent", }, }, ], transcriptMilestones: [ { type: "first_partial", timestamp: 1_700_000_000_900, text: "recovered part" }, { type: "first_final", timestamp: 1_700_000_002_100, text: "recovered final" }, { type: "last_final", timestamp: 1_700_000_002_100, text: "recovered final" }, ], checkpoints: [ { timestamp: 1_700_000_000_100, resilienceStatus: createStatus({ timestamp: 1_700_000_000_100, connectionState: "connecting", websocketState: "connecting", }), metricsSnapshot: null, }, { timestamp: 1_700_000_000_300, resilienceStatus: createStatus({ timestamp: 1_700_000_000_300, connectionState: "connected", websocketState: "open", }), metricsSnapshot: null, }, { timestamp: 1_700_000_001_000, resilienceStatus: createStatus({ timestamp: 1_700_000_001_000, connectionState: "reconnecting", websocketState: "closed", browserOnline: false, isRecovering: true, isBufferingAudio: true, pendingBufferedAudioBytes: 2_048, totalBufferedAudioBytes: 2_048, bufferingStartedAt: 1_700_000_001_000, reconnectAttempt: 1, nextReconnectDelayMs: 250, remainingReconnectAttempts: 3, }), metricsSnapshot: null, }, { timestamp: 1_700_000_001_600, resilienceStatus: createStatus({ timestamp: 1_700_000_001_600, connectionState: "connected", websocketState: "open", browserOnline: true, isRecovering: false, isBufferingAudio: false, isDrainingAudio: false, totalBufferedAudioBytes: 0, transportBufferedAmountBytes: 0, drainCycles: 1, drainExitReason: "completed", finishDeliveryState: "sent", }), metricsSnapshot: null, }, ], finalDigests: [ { digest: hashText("recovered final"), timestamp: 1_700_000_002_100 }, ], stoppedAt: 1_700_000_002_500, }); export const createFailedAuditState = (): AuditState => createBaseAuditState({ phases: [ { type: "ready", timestamp: 1_700_000_000_100 }, { type: "started", timestamp: 1_700_000_000_100 }, { type: "connected", timestamp: 1_700_000_000_300 }, { type: "disconnected", timestamp: 1_700_000_001_200 }, { type: "stopped", timestamp: 1_700_000_001_400 }, ], incidents: [ { type: "error", timestamp: 1_700_000_001_100, endedAt: null, detail: { message: "socket closed" }, }, { type: "terminal_disconnect", timestamp: 1_700_000_001_200, endedAt: null, detail: { connectionState: "disconnected", lastDisconnect: { code: 1006, reason: "abnormal", wasClean: false }, }, }, ], transcriptMilestones: [], checkpoints: [ { timestamp: 1_700_000_000_100, resilienceStatus: createStatus({ timestamp: 1_700_000_000_100, connectionState: "connecting", websocketState: "connecting", }), metricsSnapshot: null, }, { timestamp: 1_700_000_000_300, resilienceStatus: createStatus({ timestamp: 1_700_000_000_300, connectionState: "connected", websocketState: "open", }), metricsSnapshot: null, }, { timestamp: 1_700_000_001_200, resilienceStatus: createStatus({ timestamp: 1_700_000_001_200, connectionState: "disconnected", websocketState: "closed", lastDisconnect: { code: 1006, reason: "abnormal", wasClean: false }, }), metricsSnapshot: null, }, ], finalDigests: [], stoppedAt: 1_700_000_001_400, });