| 1 | import type { Notification } from "expo-notifications"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { foregroundNotificationBehavior } from "./foregroundNotificationBehavior"; |
| 5 | import { threadDeepLinkOnScreen } from "./notificationPayload"; |
| 6 | |
| 7 | function notificationWithData(data: Record<string, unknown>): Notification { |
| 8 | return { |
| 9 | date: 0, |
| 10 | request: { identifier: "notification-1", content: { data }, trigger: null }, |
| 11 | } as unknown as Notification; |
| 12 | } |
| 13 | |
| 14 | describe("threadDeepLinkOnScreen", () => { |
| 15 | it("maps a thread route and its nested screens to the thread deep link", () => { |
| 16 | expect(threadDeepLinkOnScreen("/threads/env-1/thread-1")).toBe("/threads/env-1/thread-1"); |
| 17 | expect(threadDeepLinkOnScreen("/threads/env-1/thread-1/files/src")).toBe( |
| 18 | "/threads/env-1/thread-1", |
| 19 | ); |
| 20 | }); |
| 21 | |
| 22 | it("returns null outside a thread", () => { |
| 23 | expect(threadDeepLinkOnScreen("/")).toBeNull(); |
| 24 | expect(threadDeepLinkOnScreen("/settings")).toBeNull(); |
| 25 | expect(threadDeepLinkOnScreen("/threads/env-1")).toBeNull(); |
| 26 | }); |
| 27 | }); |
| 28 | |
| 29 | describe("foregroundNotificationBehavior", () => { |
| 30 | it("suppresses a notification for the thread already on screen", () => { |
| 31 | const behavior = foregroundNotificationBehavior( |
| 32 | notificationWithData({ environmentId: "env-1", threadId: "thread-1" }), |
| 33 | "/threads/env-1/thread-1", |
| 34 | ); |
| 35 | expect(behavior.shouldShowBanner).toBe(false); |
| 36 | expect(behavior.shouldShowList).toBe(false); |
| 37 | expect(behavior.shouldPlaySound).toBe(false); |
| 38 | }); |
| 39 | |
| 40 | it("shows a notification for another thread", () => { |
| 41 | const behavior = foregroundNotificationBehavior( |
| 42 | notificationWithData({ deepLink: "/threads/env-1/thread-2" }), |
| 43 | "/threads/env-1/thread-1", |
| 44 | ); |
| 45 | expect(behavior.shouldShowBanner).toBe(true); |
| 46 | expect(behavior.shouldShowList).toBe(true); |
| 47 | }); |
| 48 | |
| 49 | it("shows a notification when no thread is open or the payload has no target", () => { |
| 50 | expect( |
| 51 | foregroundNotificationBehavior( |
| 52 | notificationWithData({ environmentId: "env-1", threadId: "thread-1" }), |
| 53 | null, |
| 54 | ).shouldShowBanner, |
| 55 | ).toBe(true); |
| 56 | expect( |
| 57 | foregroundNotificationBehavior(notificationWithData({}), "/threads/env-1/thread-1") |
| 58 | .shouldShowBanner, |
| 59 | ).toBe(true); |
| 60 | }); |
| 61 | }); |