|
| 1 | +import type { ClientSettings } from "@t3tools/contracts/settings"; |
| 2 | +import * as Option from "effect/Option"; |
| 3 | +import { act } from "react"; |
| 4 | +import { create, type ReactTestRenderer } from "react-test-renderer"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 6 | + |
| 7 | +const state = vi.hoisted(() => ({ |
| 8 | + mode: "off" as ClientSettings["notificationMode"], |
| 9 | + active: { environmentId: "env-1", threadId: "other-thread" }, |
| 10 | + focused: true, |
| 11 | + visible: "visible", |
| 12 | + live: true, |
| 13 | + completedAt: null as string | null, |
| 14 | + archivedAt: null as string | null, |
| 15 | + input: false, |
| 16 | + add: vi.fn( |
| 17 | + (_toast: { title: string; description: string; actionProps: { onClick: () => void } }) => |
| 18 | + "toast-1", |
| 19 | + ), |
| 20 | + close: vi.fn(), |
| 21 | + navigate: vi.fn(), |
| 22 | + sound: vi.fn(), |
| 23 | + notification: vi.fn(function () {}), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("@effect/atom-react", () => ({ |
| 27 | + useAtomValue: () => ({ |
| 28 | + status: state.live ? "live" : "disconnected", |
| 29 | + snapshot: Option.some({ |
| 30 | + threads: [ |
| 31 | + { |
| 32 | + id: "thread-1", |
| 33 | + title: "Fix the login form", |
| 34 | + archivedAt: state.archivedAt, |
| 35 | + hasPendingUserInput: state.input, |
| 36 | + latestTurn: { |
| 37 | + turnId: "turn-1", |
| 38 | + state: state.completedAt ? "completed" : "running", |
| 39 | + completedAt: state.completedAt, |
| 40 | + }, |
| 41 | + }, |
| 42 | + ], |
| 43 | + }), |
| 44 | + }), |
| 45 | +})); |
| 46 | +vi.mock("@tanstack/react-router", () => ({ |
| 47 | + useNavigate: () => state.navigate, |
| 48 | + useParams: () => state.active, |
| 49 | +})); |
| 50 | +vi.mock("../hooks/useSettings", () => ({ |
| 51 | + useClientSettings: (select: (settings: Pick<ClientSettings, "notificationMode">) => unknown) => |
| 52 | + select({ notificationMode: state.mode }), |
| 53 | + getClientSettings: () => ({ notificationMode: state.mode }), |
| 54 | +})); |
| 55 | +vi.mock("../state/environments", () => ({ |
| 56 | + useEnvironments: () => ({ environments: [{ environmentId: "env-1" }] }), |
| 57 | +})); |
| 58 | +vi.mock("../state/shell", () => ({ |
| 59 | + environmentShell: { stateValueAtom: vi.fn() }, |
| 60 | +})); |
| 61 | +vi.mock("../threadNotifications", async (importOriginal) => ({ |
| 62 | + ...(await importOriginal<typeof import("../threadNotifications")>()), |
| 63 | + playNotificationSound: state.sound, |
| 64 | +})); |
| 65 | +vi.mock("./ui/toast", () => ({ |
| 66 | + toastManager: { add: state.add, close: state.close }, |
| 67 | +})); |
| 68 | + |
| 69 | +import { ThreadNotificationCoordinator } from "./ThreadNotificationCoordinator"; |
| 70 | + |
| 71 | +let renderer: ReactTestRenderer | undefined; |
| 72 | + |
| 73 | +async function render() { |
| 74 | + await act(() => { |
| 75 | + if (renderer) renderer.update(<ThreadNotificationCoordinator />); |
| 76 | + else renderer = create(<ThreadNotificationCoordinator />); |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +async function complete() { |
| 81 | + state.completedAt = "2026-09-13T10:00:00.000Z"; |
| 82 | + await render(); |
| 83 | +} |
| 84 | + |
| 85 | +beforeEach(() => { |
| 86 | + vi.clearAllMocks(); |
| 87 | + Object.assign(state, { |
| 88 | + mode: "off", |
| 89 | + active: { environmentId: "env-1", threadId: "other-thread" }, |
| 90 | + focused: true, |
| 91 | + visible: "visible", |
| 92 | + live: true, |
| 93 | + completedAt: null, |
| 94 | + archivedAt: null, |
| 95 | + input: false, |
| 96 | + }); |
| 97 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 98 | + vi.stubGlobal("document", { |
| 99 | + get visibilityState() { |
| 100 | + return state.visible; |
| 101 | + }, |
| 102 | + hasFocus: () => state.focused, |
| 103 | + addEventListener: vi.fn(), |
| 104 | + removeEventListener: vi.fn(), |
| 105 | + }); |
| 106 | + vi.stubGlobal("Notification", Object.assign(state.notification, { permission: "granted" })); |
| 107 | +}); |
| 108 | + |
| 109 | +afterEach(async () => { |
| 110 | + await act(() => renderer?.unmount()); |
| 111 | + renderer = undefined; |
| 112 | + vi.unstubAllGlobals(); |
| 113 | +}); |
| 114 | + |
| 115 | +describe("thread completion toasts", () => { |
| 116 | + it("alerts once with system alerts off and opens the completed thread", async () => { |
| 117 | + await render(); |
| 118 | + await complete(); |
| 119 | + await render(); |
| 120 | + expect(state.add).toHaveBeenCalledTimes(1); |
| 121 | + const toast = state.add.mock.calls[0]?.[0]; |
| 122 | + expect(toast?.title).toBe("Thread completed"); |
| 123 | + expect(toast?.description).toBe("Fix the login form"); |
| 124 | + toast?.actionProps.onClick(); |
| 125 | + expect(state.close).toHaveBeenCalledWith("toast-1"); |
| 126 | + expect(state.navigate).toHaveBeenCalledWith({ |
| 127 | + to: "/$environmentId/$threadId", |
| 128 | + params: { environmentId: "env-1", threadId: "thread-1" }, |
| 129 | + }); |
| 130 | + expect(state.notification).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + it.each(["active", "blurred", "hidden", "archived", "input"])( |
| 134 | + "does not show a completion toast for %s threads", |
| 135 | + async (condition) => { |
| 136 | + await render(); |
| 137 | + if (condition === "active") state.active.threadId = "thread-1"; |
| 138 | + if (condition === "blurred") state.focused = false; |
| 139 | + if (condition === "hidden") state.visible = "hidden"; |
| 140 | + if (condition === "archived") state.archivedAt = "2026-09-13T09:00:00.000Z"; |
| 141 | + if (condition === "input") state.input = true; |
| 142 | + await complete(); |
| 143 | + expect(state.add).not.toHaveBeenCalled(); |
| 144 | + }, |
| 145 | + ); |
| 146 | + |
| 147 | + it("compares the environment as well as the thread", async () => { |
| 148 | + state.active = { environmentId: "env-2", threadId: "thread-1" }; |
| 149 | + await render(); |
| 150 | + await complete(); |
| 151 | + expect(state.add).toHaveBeenCalledTimes(1); |
| 152 | + }); |
| 153 | + |
| 154 | + it("does not replay completed threads on first load or reconnect", async () => { |
| 155 | + await complete(); |
| 156 | + state.live = false; |
| 157 | + await render(); |
| 158 | + state.live = true; |
| 159 | + await render(); |
| 160 | + expect(state.add).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | + |
| 163 | + it("keeps sound but replaces the system popup when showing a toast", async () => { |
| 164 | + state.mode = "notifications-and-sound"; |
| 165 | + await render(); |
| 166 | + await complete(); |
| 167 | + expect(state.sound).toHaveBeenCalledWith("completion", expect.any(Function)); |
| 168 | + expect(state.add).toHaveBeenCalledTimes(1); |
| 169 | + expect(state.notification).not.toHaveBeenCalled(); |
| 170 | + }); |
| 171 | + |
| 172 | + it("keeps system alerts when the app is in the background", async () => { |
| 173 | + state.mode = "notifications"; |
| 174 | + state.focused = false; |
| 175 | + await render(); |
| 176 | + await complete(); |
| 177 | + expect(state.add).not.toHaveBeenCalled(); |
| 178 | + expect(state.notification).toHaveBeenCalledWith("Thread completed", { |
| 179 | + body: "Fix the login form", |
| 180 | + tag: "env-1:thread-1", |
| 181 | + silent: true, |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments