import { expect, test } from "bun:test"; import { workbenchKafkaTransportSummary, workbenchRailProjectionSummary, workbenchSteerControlSummary, workbenchStreamWarningSummary, workbenchUserInputSummary } from "./workbench-cli.ts"; test("events inspect summarizes Kafka rail projection and non-blocking warnings", () => { const frames = [ { name: "workbench.session.rail", observedAt: "2026-07-22T00:00:00.000Z", data: { authority: "hwlab.kafka.session-index", session: { sessionId: "ses_rail_summary", firstUserMessagePreview: "visible title", lastUserMessageAt: "2026-07-22T00:00:00.000Z", lastTraceId: "trc_rail_summary", status: "completed", revision: { topic: "hwlab.event.v1", partition: 0, offset: "42" } } } }, { name: "workbench.error", observedAt: "2026-07-22T00:00:01.000Z", data: { phase: "session-rail", error: { code: "rail-degraded", message: "rail warning", blocking: false } } }, { name: "hwlab.event.v1", id: "kafka|hwlab.event.v1|0|30", observedAt: "2026-07-22T00:00:02.000Z", data: { traceId: "trc_steer_summary", event: { type: "user", delivery: "steer", targetTraceId: "trc_rail_summary", messageId: "msg_steer_summary", submittedAt: "2026-07-22T00:00:02.000Z" } } }, { name: "hwlab.event.v1", id: "kafka|hwlab.event.v1|0|31", observedAt: "2026-07-22T00:00:03.000Z", data: { traceId: "trc_rail_summary", event: { sourceEventId: "evt_steer_acknowledged", sourceSeq: 30, type: "backend", label: "agentrun:backend:steer-command-acknowledged", commandType: "steer", steerTraceId: "trc_steer_summary", targetTraceId: "trc_rail_summary", backendAccepted: false, deliveryState: "acknowledged-by-runner", createdAt: "2026-07-22T00:00:03.000Z" } } }, { name: "hwlab.event.v1", id: "kafka|hwlab.event.v1|0|32", observedAt: "2026-07-22T00:00:04.000Z", data: { traceId: "trc_rail_summary", event: { sourceEventId: "evt_steer_completed", sourceSeq: 31, type: "backend", label: "agentrun:backend:turn/steer:completed", commandType: "steer", steerTraceId: "trc_steer_summary", targetTraceId: "trc_rail_summary", userMessageId: "msg_steer_summary", backendAccepted: true, deliveryState: "forwarded-to-backend", createdAt: "2026-07-22T00:00:04.000Z" } } } ]; expect(workbenchRailProjectionSummary(frames)).toMatchObject({ eventCount: 1, sessionCount: 1, sessions: [{ sessionId: "ses_rail_summary", firstUserMessagePreview: "visible title", status: "completed", authority: "hwlab.kafka.session-index", revision: { topic: "hwlab.event.v1", partition: 0, offset: "42" } }] }); expect(workbenchStreamWarningSummary(frames)).toMatchObject({ count: 1, warnings: [{ code: "rail-degraded", phase: "session-rail", blocking: false }] }); expect(workbenchSteerControlSummary(frames)).toEqual({ count: 1, items: [{ steerTraceId: "trc_steer_summary", targetTraceId: "trc_rail_summary", messageId: "msg_steer_summary", submittedAt: "2026-07-22T00:00:02.000Z", lifecycle: "applied", acknowledgedAt: "2026-07-22T00:00:03.000Z", appliedAt: "2026-07-22T00:00:04.000Z", backendAccepted: true, deliveryState: "forwarded-to-backend", appliedSourceEventId: "evt_steer_completed", appliedSourceSeq: 31 }], truncated: false, valuesPrinted: false }); expect(workbenchUserInputSummary(frames)).toMatchObject({ count: 1, items: [{ traceId: "trc_steer_summary", targetTraceId: "trc_rail_summary", delivery: "steer", messageId: "msg_steer_summary" }] }); expect(workbenchKafkaTransportSummary(frames)).toEqual({ eventCount: 3, identifiedCount: 3, missingCount: 0, invalidCount: 0, partitions: [{ topic: "hwlab.event.v1", partition: 0, firstOffset: "30", lastOffset: "32", eventCount: 3, nonMonotonicCount: 0 }], valuesPrinted: false }); }); test("events inspect reports missing, malformed, and non-monotonic Kafka SSE ids", () => { const frame = (id: string | null) => ({ name: "hwlab.event.v1", id, observedAt: "2026-07-22T00:00:00.000Z", data: {} }); expect(workbenchKafkaTransportSummary([frame(null), frame("bad"), frame("kafka|hwlab.event.v1|2|10"), frame("kafka|hwlab.event.v1|2|10")])).toEqual({ eventCount: 4, identifiedCount: 2, missingCount: 1, invalidCount: 1, partitions: [{ topic: "hwlab.event.v1", partition: 2, firstOffset: "10", lastOffset: "10", eventCount: 2, nonMonotonicCount: 1 }], valuesPrinted: false }); });