import { describe, expect, it } from "bun:test"; import { buildFlowViewedV1, buildFlowViewedV1AppEvent } from "./app-event.js"; import type { TelemetryEnvelope } from "./telemetry-envelope.js"; import { trafficAppEventRpcParamsSchema, trafficAppEventRpcResultSchema, trafficTelemetryRpcParamsSchema, trafficTelemetryRpcResultSchema, } from "./traffic-rpc.js"; const telemetryEnvelopes = [ { kind: "widget_event", event: { type: "test.widget.event", timestamp: 1_700_000_000_000, apiKey: "test-api-key", debug: false, identities: [], context: { page: { origin: "https://example.com" }, widgetVersion: "1.0.0", }, }, }, { kind: "server_event", event: { type: "test.server.event", timestamp: 1_700_000_000_001, source: { runtime: "core", subsystem: "server_runtime" }, }, }, { kind: "error", event: { id: "error-a", name: "Error", message: "test error", category: "INTERNAL", timestamp: 1_700_000_000_002, context: { widgetVersion: "1.0.0" }, }, }, ] satisfies TelemetryEnvelope[]; describe("telemetry traffic RPC contract", () => { it("accepts every canonical telemetry envelope variant", () => { for (const envelope of telemetryEnvelopes) { expect(trafficTelemetryRpcParamsSchema.parse({ envelope })).toEqual({ envelope, }); } }); it("rejects invalid telemetry RPC params", () => { for (const params of [ null, {}, { envelope: null }, { envelope: { kind: "unknown", event: {} } }, { envelope: { kind: "widget_event", event: { type: "incomplete" } } }, ]) { expect(trafficTelemetryRpcParamsSchema.safeParse(params).success).toBe( false, ); } }); it("requires the echoed envelope only for allowed responses", () => { const envelope = telemetryEnvelopes[0]; expect( trafficTelemetryRpcResultSchema.parse({ allowed: true, envelope }), ).toEqual({ allowed: true, envelope }); expect(trafficTelemetryRpcResultSchema.parse({ allowed: false })).toEqual({ allowed: false, }); expect( trafficTelemetryRpcResultSchema.safeParse({ allowed: true }).success, ).toBe(false); expect( trafficTelemetryRpcResultSchema.parse({ allowed: false, envelope }), ).toEqual({ allowed: false }); }); }); describe("app-event traffic RPC contract", () => { const payload = buildFlowViewedV1AppEvent({ event: buildFlowViewedV1({ flowId: "flow-a", flowVersionId: "flow-version-a", flowVersionNumber: 1, flowRunId: "flow-run-a", flowSurface: "widget", }), flowOccurrenceId: "occurrence-a", timestamp: 1_700_000_000_000, }); it("normalizes a non-empty instance ID", () => { expect( trafficAppEventRpcParamsSchema.parse({ instanceId: " instance-a ", payload, }), ).toEqual({ instanceId: "instance-a", payload }); }); it("rejects empty and whitespace-only instance IDs", () => { for (const instanceId of ["", " ", "\t\n"]) { expect( trafficAppEventRpcParamsSchema.safeParse({ instanceId, payload }) .success, ).toBe(false); } }); it("requires the echoed payload only for allowed responses", () => { expect( trafficAppEventRpcResultSchema.parse({ allowed: true, payload }), ).toEqual({ allowed: true, payload }); expect(trafficAppEventRpcResultSchema.parse({ allowed: false })).toEqual({ allowed: false, }); expect( trafficAppEventRpcResultSchema.safeParse({ allowed: true }).success, ).toBe(false); expect( trafficAppEventRpcResultSchema.parse({ allowed: false, payload }), ).toEqual({ allowed: false }); }); });