BangSo/apps/mobile/lib/live-notifications.test.ts

58 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2026-09-01 16:51:19 +00:00
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("expo-modules-core", () => ({ requireNativeModule: vi.fn() }));
vi.mock("expo-notifications", () => ({
setNotificationHandler: vi.fn(),
getPresentedNotificationsAsync: vi.fn(async () => []),
}));
vi.mock("react-native", () => ({ Platform: { OS: "ios" } }));
import * as Notifications from "expo-notifications";
import {
configureForegroundNotifications,
notificationTargetsThread,
setOpenNotificationThread,
} from "./live-notifications";
beforeEach(() => vi.clearAllMocks());
describe("notification thread targeting", () => {
it("matches Expo and native notification keys for the open agent", () => {
expect(notificationTargetsThread({ botId: "bot-1" }, { botId: "bot-1" })).toBe(true);
expect(
notificationTargetsThread(
{ "rakazo.threadId": "thread-1" },
{ botId: "bot-1", threadId: "thread-1" },
),
).toBe(true);
});
it("keeps other agents visible", () => {
expect(notificationTargetsThread({ botId: "bot-2" }, { botId: "bot-1" })).toBe(false);
expect(notificationTargetsThread({ botId: "bot-1" }, null)).toBe(false);
});
it("does not confuse an open DM with a group notification from the same agent", () => {
expect(
notificationTargetsThread(
{ botId: "bot-1", threadId: "group-thread" },
{ botId: "bot-1", threadId: "dm-thread" },
),
).toBe(false);
});
it("hides only foreground notifications for the open agent", async () => {
configureForegroundNotifications();
await setOpenNotificationThread({ botId: "bot-1" });
const handler = vi.mocked(Notifications.setNotificationHandler).mock.calls[0]?.[0];
expect(handler).toBeTruthy();
await expect(
handler?.handleNotification({ request: { content: { data: { botId: "bot-1" } } } } as never),
).resolves.toMatchObject({ shouldShowBanner: false, shouldShowList: false });
await expect(
handler?.handleNotification({ request: { content: { data: { botId: "bot-2" } } } } as never),
).resolves.toMatchObject({ shouldShowBanner: true, shouldShowList: true });
});
});