Files
t3code/apps/web/src/components/ThreadNotificationCoordinator.test.tsx
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

327 lines
10 KiB
TypeScript

import type { ClientSettings } from "@t3tools/contracts/settings";
import * as DateTime from "effect/DateTime";
import * as Option from "effect/Option";
import { act } from "react";
import { create, type ReactTestRenderer } from "react-test-renderer";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
const state = vi.hoisted(() => ({
mode: "off" as ClientSettings["notificationMode"],
inApp: true,
active: { environmentId: "env-1", threadId: "other-thread" },
focused: true,
visible: "visible",
live: true,
completedAt: null as string | null,
archivedAt: null as string | null,
input: false,
approval: false,
sessionError: false,
turnError: false,
limited: false,
subagent: false,
background: [] as Array<{ taskId: string; kind: "command" | "monitor" }>,
add: vi.fn(
(_toast: { title: string; description: string; actionProps: { onClick: () => void } }) =>
"toast-1",
),
close: vi.fn(),
navigate: vi.fn(),
sound: vi.fn(),
notification: vi.fn(function (_title: string, options: NotificationOptions) {
return Object.assign(new EventTarget(), { tag: options.tag, close: vi.fn() });
}),
}));
const SHELL_NOW = DateTime.makeUnsafe("2026-09-13T09:00:00.000Z");
function mockThreadShell() {
return {
id: "thread-1",
projectId: "project-1",
title: "Fix the login form",
providerInstanceId: "codex",
modelSelection: { instanceId: "codex", model: "gpt-5.4" },
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
activeProviderThreadId: null,
lineage: {
rootThreadId: "thread-1",
parentThreadId: state.subagent ? "parent" : null,
relationshipToParent: state.subagent ? "subagent" : null,
},
forkedFrom: null,
createdBy: "user",
creationSource: "web",
latestRunId: "run-1",
activeRunId: null,
status: state.completedAt
? "completed"
: state.sessionError || state.turnError || state.limited
? "failed"
: "running",
lastErrorClass: state.limited ? "usage_limit" : null,
pendingRuntimeRequest: state.input
? { id: "request-1", kind: "user_input", createdAt: SHELL_NOW }
: state.approval
? { id: "request-1", kind: "command", createdAt: SHELL_NOW }
: null,
latestVisibleMessage: null,
latestUserMessageAt: null,
hasActionableProposedPlan: false,
pendingBackgroundTasks: state.background,
itemCount: 0,
visibleItemCount: 0,
createdAt: SHELL_NOW,
updatedAt: SHELL_NOW,
latestRunRequestedAt: SHELL_NOW,
latestRunStartedAt: SHELL_NOW,
latestRunCompletedAt: state.completedAt ? DateTime.makeUnsafe(state.completedAt) : undefined,
archivedAt: state.archivedAt ? DateTime.makeUnsafe(state.archivedAt) : null,
settledOverride: null,
settledAt: null,
lastVisitedAt: null,
deletedAt: null,
};
}
vi.mock("@effect/atom-react", () => ({
useAtomValue: () => ({
status: state.live ? "live" : "disconnected",
snapshot: Option.some({ threads: [mockThreadShell()] }),
}),
}));
vi.mock("@tanstack/react-router", () => ({
useNavigate: () => state.navigate,
useParams: () => state.active,
}));
vi.mock("../hooks/useSettings", () => ({
useClientSettings: (
select: (
settings: Pick<ClientSettings, "notificationMode" | "inAppNotificationsEnabled">,
) => unknown,
) => select({ notificationMode: state.mode, inAppNotificationsEnabled: state.inApp }),
getClientSettings: () => ({ notificationMode: state.mode }),
}));
vi.mock("../state/environments", () => ({
useEnvironmentIds: () => ["env-1"],
}));
vi.mock("../state/shell", () => ({
environmentShell: { stateValueAtom: vi.fn() },
}));
vi.mock("../threadNotifications", async (importOriginal) => ({
...(await importOriginal<typeof import("../threadNotifications")>()),
playNotificationSound: state.sound,
setNotificationBadge: vi.fn(),
}));
vi.mock("./ui/toast", () => ({
toastManager: { add: state.add, close: state.close },
}));
import { ThreadNotificationCoordinator } from "./ThreadNotificationCoordinator";
let renderer: ReactTestRenderer | undefined;
async function render() {
await act(() => {
if (renderer) renderer.update(<ThreadNotificationCoordinator />);
else renderer = create(<ThreadNotificationCoordinator />);
});
}
async function complete() {
state.completedAt = "2026-09-13T10:00:00.000Z";
await render();
}
beforeEach(() => {
vi.clearAllMocks();
Object.assign(state, {
mode: "off",
inApp: true,
active: { environmentId: "env-1", threadId: "other-thread" },
focused: true,
visible: "visible",
live: true,
completedAt: null,
archivedAt: null,
input: false,
approval: false,
sessionError: false,
turnError: false,
limited: false,
subagent: false,
background: [],
});
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
vi.stubGlobal("window", new EventTarget());
vi.stubGlobal("document", {
get visibilityState() {
return state.visible;
},
hasFocus: () => state.focused,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
});
vi.stubGlobal("Notification", Object.assign(state.notification, { permission: "granted" }));
});
afterEach(async () => {
await act(() => renderer?.unmount());
renderer = undefined;
vi.unstubAllGlobals();
});
describe("thread notifications", () => {
it.each([true, false])("keeps subagents silent with focus=%s", async (focused) => {
state.subagent = true;
state.focused = focused;
state.mode = "notifications-and-sound";
await render();
await complete();
state.input = true;
await render();
expect(state.sound).not.toHaveBeenCalled();
expect(state.add).not.toHaveBeenCalled();
expect(state.notification).not.toHaveBeenCalled();
});
it("alerts once with system alerts off and opens the completed thread", async () => {
await render();
await complete();
await render();
expect(state.add).toHaveBeenCalledTimes(1);
const toast = state.add.mock.calls[0]?.[0];
expect(toast?.title).toBe("Thread completed");
expect(toast?.description).toBe("Fix the login form");
toast?.actionProps.onClick();
expect(state.close).toHaveBeenCalledWith("toast-1");
expect(state.navigate).toHaveBeenCalledWith({
to: "/$environmentId/$threadId",
params: { environmentId: "env-1", threadId: "thread-1" },
});
expect(state.notification).not.toHaveBeenCalled();
});
it.each(["active", "blurred", "hidden", "archived", "disabled"])(
"does not show a completion toast for %s threads",
async (condition) => {
await render();
if (condition === "active") state.active.threadId = "thread-1";
if (condition === "blurred") state.focused = false;
if (condition === "hidden") state.visible = "hidden";
if (condition === "archived") state.archivedAt = "2026-09-13T09:00:00.000Z";
if (condition === "disabled") state.inApp = false;
await complete();
expect(state.add).not.toHaveBeenCalled();
},
);
it.each([
["input", "Input needed"],
["approval", "Approval needed"],
["sessionError", "Thread failed"],
["turnError", "Thread failed"],
["limited", "Usage limit reached"],
] as const)("uses the same %s event for in-app and desktop alerts", async (event, title) => {
state.mode = "notifications-and-sound";
await render();
state[event] = true;
await render();
await render();
expect(state.add).toHaveBeenCalledTimes(1);
expect(state.add).toHaveBeenLastCalledWith(expect.objectContaining({ title }));
expect(state.sound).toHaveBeenCalledWith("input", expect.any(Function));
expect(state.notification).not.toHaveBeenCalled();
state[event] = false;
await render();
state.focused = false;
state[event] = true;
await render();
await render();
expect(state.add).toHaveBeenCalledTimes(1);
expect(state.notification).toHaveBeenCalledTimes(1);
expect(state.notification).toHaveBeenCalledWith(title, {
body: "Fix the login form",
tag: "env-1:thread-1",
silent: true,
});
});
it("alerts when only a dev server is left running, not while a monitor can wake the agent", async () => {
await render();
state.background = [{ taskId: "watch", kind: "monitor" }];
await complete();
expect(state.add).not.toHaveBeenCalled();
state.background = [{ taskId: "dev", kind: "command" }];
await render();
expect(state.add).toHaveBeenCalledTimes(1);
expect(state.add).toHaveBeenLastCalledWith(
expect.objectContaining({ title: "Thread completed" }),
);
});
it("keeps background desktop alerts when in-app notifications are disabled", async () => {
state.focused = false;
state.inApp = false;
state.mode = "notifications";
await render();
await complete();
expect(state.add).not.toHaveBeenCalled();
expect(state.notification).toHaveBeenCalledTimes(1);
state.inApp = true;
await render();
expect(state.add).not.toHaveBeenCalled();
});
it("does not replay a completion when opting in from all alerts off", async () => {
state.inApp = false;
await render();
await complete();
state.inApp = true;
await render();
expect(state.add).not.toHaveBeenCalled();
});
it("compares the environment as well as the thread", async () => {
state.active = { environmentId: "env-2", threadId: "thread-1" };
await render();
await complete();
expect(state.add).toHaveBeenCalledTimes(1);
});
it("does not replay completed threads on first load or reconnect", async () => {
await complete();
state.live = false;
await render();
state.live = true;
await render();
expect(state.add).not.toHaveBeenCalled();
});
it("keeps sound but replaces the system popup when showing a toast", async () => {
state.mode = "notifications-and-sound";
await render();
await complete();
expect(state.sound).toHaveBeenCalledWith("completion", expect.any(Function));
expect(state.add).toHaveBeenCalledTimes(1);
expect(state.notification).not.toHaveBeenCalled();
});
it("keeps system alerts when the app is in the background", async () => {
state.mode = "notifications";
state.focused = false;
await render();
await complete();
expect(state.add).not.toHaveBeenCalled();
expect(state.notification).toHaveBeenCalledWith("Thread completed", {
body: "Fix the login form",
tag: "env-1:thread-1",
silent: true,
});
});
});