Skip to content

Commit 42b6bcc

Browse files
authored
feat(web): add opt-in in-app thread notifications (pingdotgg#11570)
1 parent ca2cc13 commit 42b6bcc

8 files changed

Lines changed: 367 additions & 20 deletions

File tree

‎apps/desktop/src/settings/DesktopClientSettings.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as DesktopClientSettings from "./DesktopClientSettings.ts";
1919
const clientSettings: ClientSettings = {
2020
...DEFAULT_CLIENT_SETTINGS,
2121
notificationMode: "notifications-and-sound",
22+
inAppNotificationsEnabled: true,
2223
appearanceContrast: 100,
2324
browserDefaultViewport: { _tag: "preset", width: 1024, height: 600, presetId: "nest-hub" },
2425
browserDefaultZoomFactor: 1.25,
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
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+
inApp: true,
10+
active: { environmentId: "env-1", threadId: "other-thread" },
11+
focused: true,
12+
visible: "visible",
13+
live: true,
14+
completedAt: null as string | null,
15+
archivedAt: null as string | null,
16+
input: false,
17+
approval: false,
18+
sessionError: false,
19+
turnError: false,
20+
add: vi.fn(
21+
(_toast: { title: string; description: string; actionProps: { onClick: () => void } }) =>
22+
"toast-1",
23+
),
24+
close: vi.fn(),
25+
navigate: vi.fn(),
26+
sound: vi.fn(),
27+
notification: vi.fn(function () {}),
28+
}));
29+
30+
vi.mock("@effect/atom-react", () => ({
31+
useAtomValue: () => ({
32+
status: state.live ? "live" : "disconnected",
33+
snapshot: Option.some({
34+
threads: [
35+
{
36+
id: "thread-1",
37+
title: "Fix the login form",
38+
archivedAt: state.archivedAt,
39+
hasPendingUserInput: state.input,
40+
hasPendingApprovals: state.approval,
41+
session: state.sessionError ? { status: "error" } : null,
42+
latestTurn: {
43+
turnId: "turn-1",
44+
state: state.turnError ? "error" : state.completedAt ? "completed" : "running",
45+
completedAt: state.completedAt,
46+
},
47+
},
48+
],
49+
}),
50+
}),
51+
}));
52+
vi.mock("@tanstack/react-router", () => ({
53+
useNavigate: () => state.navigate,
54+
useParams: () => state.active,
55+
}));
56+
vi.mock("../hooks/useSettings", () => ({
57+
useClientSettings: (
58+
select: (
59+
settings: Pick<ClientSettings, "notificationMode" | "inAppNotificationsEnabled">,
60+
) => unknown,
61+
) => select({ notificationMode: state.mode, inAppNotificationsEnabled: state.inApp }),
62+
getClientSettings: () => ({ notificationMode: state.mode }),
63+
}));
64+
vi.mock("../state/environments", () => ({
65+
useEnvironments: () => ({ environments: [{ environmentId: "env-1" }] }),
66+
}));
67+
vi.mock("../state/shell", () => ({
68+
environmentShell: { stateValueAtom: vi.fn() },
69+
}));
70+
vi.mock("../threadNotifications", async (importOriginal) => ({
71+
...(await importOriginal<typeof import("../threadNotifications")>()),
72+
playNotificationSound: state.sound,
73+
}));
74+
vi.mock("./ui/toast", () => ({
75+
toastManager: { add: state.add, close: state.close },
76+
}));
77+
78+
import { ThreadNotificationCoordinator } from "./ThreadNotificationCoordinator";
79+
80+
let renderer: ReactTestRenderer | undefined;
81+
82+
async function render() {
83+
await act(() => {
84+
if (renderer) renderer.update(<ThreadNotificationCoordinator />);
85+
else renderer = create(<ThreadNotificationCoordinator />);
86+
});
87+
}
88+
89+
async function complete() {
90+
state.completedAt = "2026-09-13T10:00:00.000Z";
91+
await render();
92+
}
93+
94+
beforeEach(() => {
95+
vi.clearAllMocks();
96+
Object.assign(state, {
97+
mode: "off",
98+
inApp: true,
99+
active: { environmentId: "env-1", threadId: "other-thread" },
100+
focused: true,
101+
visible: "visible",
102+
live: true,
103+
completedAt: null,
104+
archivedAt: null,
105+
input: false,
106+
approval: false,
107+
sessionError: false,
108+
turnError: false,
109+
});
110+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
111+
vi.stubGlobal("document", {
112+
get visibilityState() {
113+
return state.visible;
114+
},
115+
hasFocus: () => state.focused,
116+
addEventListener: vi.fn(),
117+
removeEventListener: vi.fn(),
118+
});
119+
vi.stubGlobal("Notification", Object.assign(state.notification, { permission: "granted" }));
120+
});
121+
122+
afterEach(async () => {
123+
await act(() => renderer?.unmount());
124+
renderer = undefined;
125+
vi.unstubAllGlobals();
126+
});
127+
128+
describe("thread notifications", () => {
129+
it("alerts once with system alerts off and opens the completed thread", async () => {
130+
await render();
131+
await complete();
132+
await render();
133+
expect(state.add).toHaveBeenCalledTimes(1);
134+
const toast = state.add.mock.calls[0]?.[0];
135+
expect(toast?.title).toBe("Thread completed");
136+
expect(toast?.description).toBe("Fix the login form");
137+
toast?.actionProps.onClick();
138+
expect(state.close).toHaveBeenCalledWith("toast-1");
139+
expect(state.navigate).toHaveBeenCalledWith({
140+
to: "/$environmentId/$threadId",
141+
params: { environmentId: "env-1", threadId: "thread-1" },
142+
});
143+
expect(state.notification).not.toHaveBeenCalled();
144+
});
145+
146+
it.each(["active", "blurred", "hidden", "archived", "disabled"])(
147+
"does not show a completion toast for %s threads",
148+
async (condition) => {
149+
await render();
150+
if (condition === "active") state.active.threadId = "thread-1";
151+
if (condition === "blurred") state.focused = false;
152+
if (condition === "hidden") state.visible = "hidden";
153+
if (condition === "archived") state.archivedAt = "2026-09-13T09:00:00.000Z";
154+
if (condition === "disabled") state.inApp = false;
155+
await complete();
156+
expect(state.add).not.toHaveBeenCalled();
157+
},
158+
);
159+
160+
it.each([
161+
["input", "Input needed"],
162+
["approval", "Approval needed"],
163+
["sessionError", "Thread failed"],
164+
["turnError", "Thread failed"],
165+
] as const)("uses the same %s event for in-app and desktop alerts", async (event, title) => {
166+
state.mode = "notifications-and-sound";
167+
await render();
168+
state[event] = true;
169+
await render();
170+
await render();
171+
expect(state.add).toHaveBeenCalledTimes(1);
172+
expect(state.add).toHaveBeenLastCalledWith(expect.objectContaining({ title }));
173+
expect(state.sound).toHaveBeenCalledWith("input", expect.any(Function));
174+
expect(state.notification).not.toHaveBeenCalled();
175+
176+
state[event] = false;
177+
await render();
178+
state.focused = false;
179+
state[event] = true;
180+
await render();
181+
await render();
182+
expect(state.add).toHaveBeenCalledTimes(1);
183+
expect(state.notification).toHaveBeenCalledTimes(1);
184+
expect(state.notification).toHaveBeenCalledWith(title, {
185+
body: "Fix the login form",
186+
tag: "env-1:thread-1",
187+
silent: true,
188+
});
189+
});
190+
191+
it("keeps desktop alerts when in-app notifications are disabled", async () => {
192+
state.inApp = false;
193+
state.mode = "notifications";
194+
await render();
195+
await complete();
196+
expect(state.add).not.toHaveBeenCalled();
197+
expect(state.notification).toHaveBeenCalledTimes(1);
198+
state.inApp = true;
199+
await render();
200+
expect(state.add).not.toHaveBeenCalled();
201+
});
202+
203+
it("does not replay a completion when opting in from all alerts off", async () => {
204+
state.inApp = false;
205+
await render();
206+
await complete();
207+
state.inApp = true;
208+
await render();
209+
expect(state.add).not.toHaveBeenCalled();
210+
});
211+
212+
it("compares the environment as well as the thread", async () => {
213+
state.active = { environmentId: "env-2", threadId: "thread-1" };
214+
await render();
215+
await complete();
216+
expect(state.add).toHaveBeenCalledTimes(1);
217+
});
218+
219+
it("does not replay completed threads on first load or reconnect", async () => {
220+
await complete();
221+
state.live = false;
222+
await render();
223+
state.live = true;
224+
await render();
225+
expect(state.add).not.toHaveBeenCalled();
226+
});
227+
228+
it("keeps sound but replaces the system popup when showing a toast", async () => {
229+
state.mode = "notifications-and-sound";
230+
await render();
231+
await complete();
232+
expect(state.sound).toHaveBeenCalledWith("completion", expect.any(Function));
233+
expect(state.add).toHaveBeenCalledTimes(1);
234+
expect(state.notification).not.toHaveBeenCalled();
235+
});
236+
237+
it("keeps system alerts when the app is in the background", async () => {
238+
state.mode = "notifications";
239+
state.focused = false;
240+
await render();
241+
await complete();
242+
expect(state.add).not.toHaveBeenCalled();
243+
expect(state.notification).toHaveBeenCalledWith("Thread completed", {
244+
body: "Fix the login form",
245+
tag: "env-1:thread-1",
246+
silent: true,
247+
});
248+
});
249+
});

0 commit comments

Comments
 (0)