diff --git a/src/hooks/useTailStream.test.ts b/src/hooks/useTailStream.test.ts index 5247e4b..4664247 100644 --- a/src/hooks/useTailStream.test.ts +++ b/src/hooks/useTailStream.test.ts @@ -15,7 +15,7 @@ import { } from "vitest"; import { useTailStream } from "./useTailStream"; import { useTailStore } from "@/store/tail-store"; -import type { Claim } from "@/types"; +import type { Ack, Claim, Ta1Ack } from "@/types"; import type { TailEvent } from "@/lib/tail-stream"; // --------------------------------------------------------------------------- @@ -204,6 +204,34 @@ function makeClaim(id: string, patientName: string): Claim { }; } +function makeAck(id: number, sourceBatchId: string): Ack { + return { + id, + sourceBatchId, + acceptedCount: 1, + rejectedCount: 0, + receivedCount: 1, + ackCode: "A", + parsedAt: "2026-07-02T12:00:00Z", + patientControlNumber: `PCN-${id}`, + }; +} + +function makeTa1Ack(id: number, controlNumber: string): Ta1Ack { + return { + id, + controlNumber, + ackCode: "A", + noteCode: "000", + interchangeDate: "2026-07-02", + interchangeTime: "1200", + senderId: "S", + receiverId: "R", + sourceBatchId: `TA1-${id}`, + parsedAt: "2026-07-02T12:00:00Z", + }; +} + describe("useTailStream", () => { beforeEach(() => { mockStreamTail.mockReset(); @@ -212,6 +240,8 @@ describe("useTailStream", () => { useTailStore.getState().reset("claims"); useTailStore.getState().reset("remittances"); useTailStore.getState().reset("activity"); + useTailStore.getState().reset("acks"); + useTailStore.getState().reset("ta1_acks"); }); afterEach(() => { @@ -400,4 +430,60 @@ describe("useTailStream", () => { unmount(); }); + + // ------------------------------------------------------------------------- + // SP25: the Acks page mounts useTailStream("acks") and + // useTailStream("ta1_acks"). The dispatcher must route their item events + // into the matching store slices. + // ------------------------------------------------------------------------- + + it("test_acks_item_event_lands_in_acks_slice", async () => { + const { ctrls } = trackCalls(); + const { unmount } = renderHook(() => useTailStream("acks")); + + await waitFor(() => ctrls.length === 1); + ctrls[0].push({ type: "snapshot_end", data: { count: 0 } }); + await waitFor(() => useTailStore.getState().acks !== undefined); + + ctrls[0].push({ + type: "item", + data: makeAck(42, "BATCH-LIVE"), + }); + + await waitFor( + () => useTailStore.getState().acks["42"] !== undefined, + ); + const stored = useTailStore.getState().acks["42"]; + expect(stored).toBeDefined(); + expect(stored?.sourceBatchId).toBe("BATCH-LIVE"); + // The Acks page must NOT see this row in the claims slice. + expect(useTailStore.getState().claims["42"]).toBeUndefined(); + + unmount(); + }); + + it("test_ta1_acks_item_event_lands_in_ta1_acks_slice", async () => { + const { ctrls } = trackCalls(); + const { unmount } = renderHook(() => useTailStream("ta1_acks")); + + await waitFor(() => ctrls.length === 1); + ctrls[0].push({ type: "snapshot_end", data: { count: 0 } }); + await waitFor(() => useTailStore.getState().ta1Acks !== undefined); + + ctrls[0].push({ + type: "item", + data: makeTa1Ack(7, "000000007"), + }); + + await waitFor( + () => useTailStore.getState().ta1Acks["7"] !== undefined, + ); + const stored = useTailStore.getState().ta1Acks["7"]; + expect(stored).toBeDefined(); + expect(stored?.controlNumber).toBe("000000007"); + // Must NOT bleed into the acks slice — keyed by id, separate dicts. + expect(useTailStore.getState().acks["7"]).toBeUndefined(); + + unmount(); + }); }); diff --git a/src/hooks/useTailStream.ts b/src/hooks/useTailStream.ts index 5394c22..5494f1e 100644 --- a/src/hooks/useTailStream.ts +++ b/src/hooks/useTailStream.ts @@ -27,7 +27,7 @@ import { useCallback, useEffect, useRef, useState } from "react"; import { streamTail, type TailResource } from "@/lib/tail-stream"; import { useTailStore } from "@/store/tail-store"; -import type { Activity, Claim, Remittance } from "@/types"; +import type { Ack, Activity, Claim, Remittance, Ta1Ack } from "@/types"; export type TailStatus = | "connecting" @@ -74,6 +74,17 @@ function dispatch(resource: TailResource, data: unknown): void { case "activity": store.addActivity(data as Activity); break; + // SP25: the Acks page opens two streams — one per ack flavor — + // and the live event payloads are the same shape as the list + // endpoint (to_ui_ack / to_ui_ta1_ack in store/ui.py). The first + // write wins, so a snapshot replay on reconnect won't trample + // the canonical row. + case "acks": + store.addAck(data as Ack); + break; + case "ta1_acks": + store.addTa1Ack(data as Ta1Ack); + break; } }