|
| 1 | +import type { NotificationResponse } from "expo-notifications"; |
| 2 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 3 | + |
| 4 | +import { notificationThreadPath, routeNotificationResponseOnce } from "./notificationPayload"; |
| 5 | +import { consumeLastNotificationResponse } from "./notificationResponseConsumer"; |
| 6 | + |
| 7 | +function responseWithData(data: Record<string, unknown>, identifier = "notification-1") { |
| 8 | + return { notification: { request: { identifier, content: { data } } } }; |
| 9 | +} |
| 10 | + |
| 11 | +describe("notificationThreadPath", () => { |
| 12 | + it("encodes the thread route from environment and thread ids", () => { |
| 13 | + expect( |
| 14 | + notificationThreadPath(responseWithData({ environmentId: "env 1", threadId: "thread/2" })), |
| 15 | + ).toBe("/threads/env%201/thread%2F2"); |
| 16 | + }); |
| 17 | + |
| 18 | + it("ignores pushes without both ids", () => { |
| 19 | + expect(notificationThreadPath(responseWithData({ environmentId: "env" }))).toBeNull(); |
| 20 | + expect( |
| 21 | + notificationThreadPath(responseWithData({ environmentId: "", threadId: "t" })), |
| 22 | + ).toBeNull(); |
| 23 | + expect(notificationThreadPath({})).toBeNull(); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe("routeNotificationResponseOnce", () => { |
| 28 | + it("retries a failed cold-start tap and deduplicates it after navigation succeeds", async () => { |
| 29 | + using _consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 30 | + const response = responseWithData({ |
| 31 | + environmentId: "env", |
| 32 | + threadId: "thread", |
| 33 | + }) as NotificationResponse; |
| 34 | + const handledResponseIds = new Set<string>(); |
| 35 | + const navigate = vi.fn().mockImplementationOnce(() => { |
| 36 | + throw new Error("navigation not ready"); |
| 37 | + }); |
| 38 | + const clearLastResponse = vi.fn(() => Promise.resolve()); |
| 39 | + const consume = () => |
| 40 | + consumeLastNotificationResponse({ |
| 41 | + getLastResponse: () => Promise.resolve(response), |
| 42 | + clearLastResponse, |
| 43 | + handleResponse: (response) => |
| 44 | + routeNotificationResponseOnce({ handledResponseIds, response, navigate }), |
| 45 | + }); |
| 46 | + |
| 47 | + await consume(); |
| 48 | + expect(handledResponseIds.size).toBe(0); |
| 49 | + expect(clearLastResponse).not.toHaveBeenCalled(); |
| 50 | + |
| 51 | + await consume(); |
| 52 | + expect(navigate).toHaveBeenCalledTimes(2); |
| 53 | + expect(navigate).toHaveBeenLastCalledWith("/threads/env/thread"); |
| 54 | + expect(clearLastResponse).toHaveBeenCalledOnce(); |
| 55 | + |
| 56 | + routeNotificationResponseOnce({ handledResponseIds, response, navigate }); |
| 57 | + expect(navigate).toHaveBeenCalledTimes(2); |
| 58 | + }); |
| 59 | + |
| 60 | + it("discards malformed ids without leaving the response pending", async () => { |
| 61 | + const response = responseWithData({ |
| 62 | + environmentId: "env", |
| 63 | + threadId: "\ud800", |
| 64 | + }) as NotificationResponse; |
| 65 | + const handledResponseIds = new Set<string>(); |
| 66 | + const navigate = vi.fn(); |
| 67 | + const clearLastResponse = vi.fn(() => Promise.resolve()); |
| 68 | + |
| 69 | + await consumeLastNotificationResponse({ |
| 70 | + getLastResponse: () => Promise.resolve(response), |
| 71 | + clearLastResponse, |
| 72 | + handleResponse: (response) => |
| 73 | + routeNotificationResponseOnce({ handledResponseIds, response, navigate }), |
| 74 | + }); |
| 75 | + |
| 76 | + expect(navigate).not.toHaveBeenCalled(); |
| 77 | + expect(clearLastResponse).toHaveBeenCalledOnce(); |
| 78 | + expect(handledResponseIds.has("notification-1")).toBe(true); |
| 79 | + }); |
| 80 | +}); |
0 commit comments