|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { batchView, isTerminal, shouldClose } from "./batch-panel.js"; |
| 4 | + |
| 5 | +const pending = (over = {}) => ({ |
| 6 | + state: "pending", |
| 7 | + processing_status: "in_progress", |
| 8 | + request_count: 10, |
| 9 | + request_counts: { succeeded: 3, errored: 0, canceled: 0, expired: 0, processing: 7 }, |
| 10 | + ended_at: null, |
| 11 | + batch_id: "msgbatch_123", |
| 12 | + ...over, |
| 13 | +}); |
| 14 | + |
| 15 | +describe("batchView", () => { |
| 16 | + it("hides the panel for the none state", () => { |
| 17 | + expect(batchView({ state: "none" })).toEqual({ visible: false }); |
| 18 | + }); |
| 19 | + |
| 20 | + it("shows an error frame with detail passed through, bar untouched", () => { |
| 21 | + const v = batchView({ state: "error", detail: "boom" }); |
| 22 | + expect(v).toMatchObject({ visible: true, label: "Status unavailable", counts: "", detail: "boom" }); |
| 23 | + expect(v.pct).toBeNull(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("falls back to 'retry' when an error frame has no detail", () => { |
| 27 | + expect(batchView({ state: "error" }).detail).toBe("retry"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("renders a pending frame: pct, counts, status label, batch detail", () => { |
| 31 | + const v = batchView(pending()); |
| 32 | + expect(v).toMatchObject({ |
| 33 | + visible: true, |
| 34 | + pct: 30, // (3 done / 10 total) |
| 35 | + counts: "3/10", |
| 36 | + label: "in_progress", |
| 37 | + detail: "batch msgbatch_123", |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("renders a terminal frame as a completion summary", () => { |
| 42 | + const v = batchView( |
| 43 | + pending({ |
| 44 | + processing_status: "ended", |
| 45 | + ended_at: "2026-06-14T12:00:00Z", |
| 46 | + request_counts: { succeeded: 9, errored: 1, canceled: 0, expired: 0 }, |
| 47 | + }), |
| 48 | + ); |
| 49 | + expect(v.label).toBe("Completed — 9 succeeded, 1 errored"); |
| 50 | + expect(v.pct).toBe(100); // (9 + 1) / 10 |
| 51 | + }); |
| 52 | + |
| 53 | + it("guards against divide-by-zero: total 0 → pct 0, not NaN", () => { |
| 54 | + const v = batchView(pending({ request_count: 0, request_counts: {} })); |
| 55 | + expect(v.pct).toBe(0); |
| 56 | + expect(v.counts).toBe(""); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe("isTerminal", () => { |
| 61 | + it.each(["ended", "canceled", "expired"])("is true for processing_status %s", (s) => { |
| 62 | + expect(isTerminal({ processing_status: s })).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it("is true when ended_at is set regardless of status", () => { |
| 66 | + expect(isTerminal({ processing_status: "in_progress", ended_at: "2026-06-14T12:00:00Z" })).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it("is false for an in-progress frame", () => { |
| 70 | + expect(isTerminal({ processing_status: "in_progress", ended_at: null })).toBe(false); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe("shouldClose", () => { |
| 75 | + it("closes on none and error frames", () => { |
| 76 | + expect(shouldClose({ state: "none" })).toBe(true); |
| 77 | + expect(shouldClose({ state: "error" })).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it("closes on a terminal pending frame", () => { |
| 81 | + expect(shouldClose(pending({ processing_status: "ended" }))).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + it("keeps the stream open on a non-terminal pending frame", () => { |
| 85 | + // The reconnect-suppression invariant: must NOT close here, or the |
| 86 | + // panel would never receive subsequent progress frames. |
| 87 | + expect(shouldClose(pending())).toBe(false); |
| 88 | + }); |
| 89 | +}); |
0 commit comments