|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act, renderHook, waitFor } from "@testing-library/react"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { useBatchTriageManager } from "./useBatchTriageManager"; |
| 5 | +import type { QueueItem } from "../../inbox/queueModel"; |
| 6 | +import type { SavedDraft } from "../../../types/workspace"; |
| 7 | + |
| 8 | +function makeItem(id: string, summary: string): QueueItem { |
| 9 | + return { |
| 10 | + draft: { |
| 11 | + id, |
| 12 | + ticket_id: `INC-${id}`, |
| 13 | + summary_text: summary, |
| 14 | + input_text: summary, |
| 15 | + } as unknown as SavedDraft, |
| 16 | + meta: { |
| 17 | + owner: "alice", |
| 18 | + state: "open", |
| 19 | + priority: "normal", |
| 20 | + updatedAt: "2026-04-01T00:00:00Z", |
| 21 | + }, |
| 22 | + slaDueAt: "2026-04-02T00:00:00Z", |
| 23 | + isAtRisk: false, |
| 24 | + } as QueueItem; |
| 25 | +} |
| 26 | + |
| 27 | +afterEach(() => { |
| 28 | + vi.clearAllMocks(); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("useBatchTriageManager", () => { |
| 32 | + it("seeds the input from the first 25 filtered items", () => { |
| 33 | + const { result } = renderHook(() => |
| 34 | + useBatchTriageManager({ |
| 35 | + filteredItems: [ |
| 36 | + makeItem("1", "VPN down"), |
| 37 | + makeItem("2", "Password reset"), |
| 38 | + ], |
| 39 | + operatorName: "alice", |
| 40 | + clusterTicketsForTriage: vi.fn(), |
| 41 | + listRecentTriageClusters: vi.fn(async () => []), |
| 42 | + setTriageHistory: vi.fn(), |
| 43 | + logEvent: vi.fn(), |
| 44 | + showSuccess: vi.fn(), |
| 45 | + showError: vi.fn(), |
| 46 | + }), |
| 47 | + ); |
| 48 | + |
| 49 | + act(() => result.current.handleSeedBatchTriage()); |
| 50 | + |
| 51 | + expect(result.current.batchTriageInput).toBe( |
| 52 | + "INC-1|VPN down\nINC-2|Password reset", |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it("clusters tickets, refreshes history, and logs success", async () => { |
| 57 | + const cluster = vi.fn(async () => [ |
| 58 | + { |
| 59 | + cluster_key: "outage", |
| 60 | + summary: "VPN outage", |
| 61 | + ticket_count: 1, |
| 62 | + ticket_ids: ["INC-1"], |
| 63 | + }, |
| 64 | + ]); |
| 65 | + const listRecent = vi.fn(async () => [ |
| 66 | + { |
| 67 | + id: "c1", |
| 68 | + cluster_key: "outage", |
| 69 | + summary: "VPN outage", |
| 70 | + ticket_count: 2, |
| 71 | + } as unknown as never, |
| 72 | + ]); |
| 73 | + const setTriageHistory = vi.fn(); |
| 74 | + const showSuccess = vi.fn(); |
| 75 | + const showError = vi.fn(); |
| 76 | + |
| 77 | + const { result } = renderHook(() => |
| 78 | + useBatchTriageManager({ |
| 79 | + filteredItems: [], |
| 80 | + operatorName: "alice", |
| 81 | + clusterTicketsForTriage: cluster as never, |
| 82 | + listRecentTriageClusters: listRecent as never, |
| 83 | + setTriageHistory, |
| 84 | + logEvent: vi.fn(), |
| 85 | + showSuccess, |
| 86 | + showError, |
| 87 | + }), |
| 88 | + ); |
| 89 | + |
| 90 | + act(() => { |
| 91 | + result.current.setBatchTriageInput("INC-1|VPN outage"); |
| 92 | + }); |
| 93 | + |
| 94 | + await act(async () => { |
| 95 | + await result.current.handleRunBatchTriage(); |
| 96 | + }); |
| 97 | + |
| 98 | + await waitFor(() => { |
| 99 | + expect(setTriageHistory).toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + expect(cluster).toHaveBeenCalledWith([ |
| 102 | + expect.objectContaining({ id: "INC-1", summary: "VPN outage" }), |
| 103 | + ]); |
| 104 | + expect(showSuccess).toHaveBeenCalledWith("Batch triage completed"); |
| 105 | + expect(showError).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it("surfaces an error when the input is empty", async () => { |
| 109 | + const showError = vi.fn(); |
| 110 | + const cluster = vi.fn(); |
| 111 | + |
| 112 | + const { result } = renderHook(() => |
| 113 | + useBatchTriageManager({ |
| 114 | + filteredItems: [], |
| 115 | + operatorName: "alice", |
| 116 | + clusterTicketsForTriage: cluster as never, |
| 117 | + listRecentTriageClusters: vi.fn(async () => []), |
| 118 | + setTriageHistory: vi.fn(), |
| 119 | + logEvent: vi.fn(), |
| 120 | + showSuccess: vi.fn(), |
| 121 | + showError, |
| 122 | + }), |
| 123 | + ); |
| 124 | + |
| 125 | + await act(async () => { |
| 126 | + await result.current.handleRunBatchTriage(); |
| 127 | + }); |
| 128 | + |
| 129 | + expect(showError).toHaveBeenCalledWith( |
| 130 | + "Add at least one ticket before running batch triage", |
| 131 | + ); |
| 132 | + expect(cluster).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments