|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { describeRetryEvent, extractModelId, extractProviderId } from "../src/telemetry.ts"; |
| 3 | + |
| 4 | +// Forma real del evento `retry` capturada en OpenCode v2.0.32 con un servidor |
| 5 | +// de prueba que corta el stream a mitad (verificacion de campo). |
| 6 | +const REAL_EVENT = { |
| 7 | + sessionID: "ses_eaa6f80fbffe0Tv4gX6eSqfnGl", |
| 8 | + agent: "build", |
| 9 | + model: { id: "probe-model", providerID: "probe" }, |
| 10 | + error: { |
| 11 | + type: "provider.transport", |
| 12 | + message: |
| 13 | + "Connection lost while reading the response: ECONNRESET: The socket connection was closed unexpectedly.", |
| 14 | + status: 200, |
| 15 | + }, |
| 16 | + attempt: 2, |
| 17 | + decision: { retry: true, delay: 2281 }, |
| 18 | +}; |
| 19 | + |
| 20 | +describe("retry telemetry", () => { |
| 21 | + test("extrae el modelo desde model.id (no existe modelID)", () => { |
| 22 | + expect(extractModelId(REAL_EVENT as any)).toBe("probe-model"); |
| 23 | + expect(extractProviderId(REAL_EVENT as any)).toBe("probe"); |
| 24 | + }); |
| 25 | + |
| 26 | + test("no pierde la atribucion cuando el evento trae modelID", () => { |
| 27 | + expect(extractModelId({ modelID: "legacy-model" } as any)).toBe("legacy-model"); |
| 28 | + expect(extractModelId({ model: { modelID: "nested" } } as any)).toBe("nested"); |
| 29 | + expect(extractModelId({ model: "plain-string" } as any)).toBe("plain-string"); |
| 30 | + }); |
| 31 | + |
| 32 | + test("normaliza el evento real a la linea de telemetria", () => { |
| 33 | + const t = describeRetryEvent(REAL_EVENT as any, "2026-10-19T19:08:20.424Z"); |
| 34 | + expect(t.at).toBe("2026-10-19T19:08:20.424Z"); |
| 35 | + expect(t.sessionID).toBe("ses_eaa6f80fbffe0Tv4gX6eSqfnGl"); |
| 36 | + expect(t.agent).toBe("build"); |
| 37 | + expect(t.providerID).toBe("probe"); |
| 38 | + expect(t.modelID).toBe("probe-model"); |
| 39 | + expect(t.type).toBe("provider.transport"); |
| 40 | + expect(t.status).toBe(200); |
| 41 | + expect(t.attempt).toBe(2); |
| 42 | + expect(t.retry).toBe(true); |
| 43 | + expect(t.delayMs).toBe(2281); |
| 44 | + expect(t.message).toContain("ECONNRESET"); |
| 45 | + }); |
| 46 | + |
| 47 | + test("trunca el mensaje a 400 caracteres", () => { |
| 48 | + const long = { error: { type: "provider.transport", message: "x".repeat(900) } } as any; |
| 49 | + const t = describeRetryEvent(long); |
| 50 | + expect(t.message!.length).toBe(400); |
| 51 | + }); |
| 52 | + |
| 53 | + test("tolera eventos incompletos sin lanzar", () => { |
| 54 | + const t = describeRetryEvent({} as any); |
| 55 | + expect(t.modelID).toBeUndefined(); |
| 56 | + expect(t.type).toBeUndefined(); |
| 57 | + expect(t.message).toBeUndefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + test("soporta el formato plano (sin objeto model)", () => { |
| 61 | + const t = describeRetryEvent({ |
| 62 | + sessionID: "s", |
| 63 | + modelID: "m", |
| 64 | + providerID: "p", |
| 65 | + type: "provider.internal", |
| 66 | + status: 503, |
| 67 | + } as any); |
| 68 | + expect(t.modelID).toBe("m"); |
| 69 | + expect(t.providerID).toBe("p"); |
| 70 | + expect(t.type).toBe("provider.internal"); |
| 71 | + expect(t.status).toBe(503); |
| 72 | + }); |
| 73 | +}); |
0 commit comments