Open History to see the last commit
1import type { NotificationResponse } from "expo-notifications";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3
4import { consumeLastAgentNotificationResponse } from "./notificationResponseConsumer";
5
6import {
7 extractAgentNotificationDeepLink,
8 routeAgentNotificationResponseOnce,
9} from "./notificationPayload";
10
11function responseWithData(data: Record<string, unknown>, identifier = "notification-1") {
12 return {
13 notification: {
14 request: {
15 identifier,
16 content: {
17 data,
18 },
19 },
20 },
21 };
22}
23
24afterEach(() => {
25 vi.restoreAllMocks();
26});
27
28describe("consumeLastAgentNotificationResponse", () => {
29 it("reports routing failures before clearing the response", async () => {
30 const cause = new Error("notification routing unavailable");
31 const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);
32 const response = responseWithData({}, "notification-route") as NotificationResponse;
33 const clearLastResponse = vi.fn(() => Promise.resolve());
34
35 await consumeLastAgentNotificationResponse({
36 getLastResponse: () => Promise.resolve(response),
37 clearLastResponse,
38 handleResponse: () => {
39 throw cause;
40 },
41 });
42
43 expect(clearLastResponse).not.toHaveBeenCalled();
44 expect(consoleError).toHaveBeenCalledWith(
45 expect.objectContaining({
46 _tag: "NotificationNavigationError",
47 operation: "route",
48 notificationId: "notification-route",
49 }),
50 );
51 });
52});
53
54describe("extractAgentNotificationDeepLink", () => {
55 it("uses explicit deep links from APNs payload data", () => {
56 expect(
57 extractAgentNotificationDeepLink(
58 responseWithData({
59 deepLink: "/threads/env/thread",
60 environmentId: "ignored",
61 threadId: "ignored",
62 }),
63 ),
64 ).toBe("/threads/env/thread");
65 });
66
67 it("falls back to the thread route from environment and thread ids", () => {
68 expect(
69 extractAgentNotificationDeepLink(
70 responseWithData({
71 environmentId: "env 1",
72 threadId: "thread/2",
73 }),
74 ),
75 ).toBe("/threads/env%201/thread%2F2");
76 });
77
78 it("falls back to ids when explicit deep link is not an agent thread route", () => {
79 expect(
80 extractAgentNotificationDeepLink(
81 responseWithData({
82 deepLink: "/",
83 environmentId: "env",
84 threadId: "thread",
85 }),
86 ),
87 ).toBe("/threads/env/thread");
88 });
89
90 it("ignores malformed or external links", () => {
91 expect(
92 extractAgentNotificationDeepLink(responseWithData({ deepLink: "https://example.com" })),
93 ).toBeNull();
94 expect(
95 extractAgentNotificationDeepLink(responseWithData({ deepLink: "/settings" })),
96 ).toBeNull();
97 expect(
98 extractAgentNotificationDeepLink(responseWithData({ deepLink: "//example.com" })),
99 ).toBeNull();
100 expect(
101 extractAgentNotificationDeepLink(responseWithData({ deepLink: "/threads/env/thread?x=1" })),
102 ).toBeNull();
103 expect(extractAgentNotificationDeepLink({})).toBeNull();
104 });
105});
106
107describe("routeAgentNotificationResponseOnce", () => {
108 it("does not navigate twice when the initial and listener responses refer to one notification", () => {
109 const handledResponseIds = new Set<string>();
110 const navigations: Array<string> = [];
111 const response = responseWithData({
112 environmentId: "env",
113 threadId: "thread",
114 });
115
116 routeAgentNotificationResponseOnce({
117 handledResponseIds,
118 response,
119 navigate: (deepLink) => navigations.push(deepLink),
120 });
121 routeAgentNotificationResponseOnce({
122 handledResponseIds,
123 response,
124 navigate: (deepLink) => navigations.push(deepLink),
125 });
126
127 expect(navigations).toEqual(["/threads/env/thread"]);
128 });
129});