| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- import { beforeEach, describe, expect, it, vi } from "vitest";
- import { PlatformChannels } from "@zcode/shared";
- const electronMocks = vi.hoisted(() => {
- const notificationInstances: NotificationMock[] = [];
- class NotificationMock {
- static isSupported = vi.fn(() => true);
- handlers = new Map<string, Array<() => void>>();
- once = vi.fn((event: string, handler: () => void) => {
- const handlers = this.handlers.get(event) ?? [];
- handlers.push(handler);
- this.handlers.set(event, handlers);
- return this;
- });
- show = vi.fn();
- constructor(public options: unknown) {
- notificationInstances.push(this);
- }
- emit(event: string) {
- const handlers = this.handlers.get(event) ?? [];
- this.handlers.delete(event);
- for (const handler of handlers) {
- handler();
- }
- }
- }
- return {
- appDockShow: vi.fn(),
- appFocus: vi.fn(),
- appShow: vi.fn(),
- browserWindowFromWebContents: vi.fn(),
- browserWindowGetAllWindows: vi.fn(() => []),
- NotificationMock,
- notificationInstances,
- };
- });
- vi.mock("electron", () => ({
- app: {
- dock: {
- show: electronMocks.appDockShow,
- },
- focus: electronMocks.appFocus,
- show: electronMocks.appShow,
- },
- BrowserWindow: {
- fromWebContents: electronMocks.browserWindowFromWebContents,
- getAllWindows: electronMocks.browserWindowGetAllWindows,
- },
- Notification: electronMocks.NotificationMock,
- }));
- import {
- dispatchTaskNotification,
- getActiveTaskNotificationCountForTest,
- resetActiveTaskNotificationsForTest,
- } from "../src/main/desktopNotifications.js";
- function createTaskNotificationPayload(taskId = "task-1") {
- return {
- taskId,
- status: "completed" as const,
- title: "测试任务完成",
- body: "可以回来看结果了",
- };
- }
- describe("desktop task notifications", () => {
- beforeEach(() => {
- electronMocks.NotificationMock.isSupported.mockReturnValue(true);
- electronMocks.notificationInstances.length = 0;
- electronMocks.appDockShow.mockClear();
- electronMocks.appFocus.mockClear();
- electronMocks.appShow.mockClear();
- electronMocks.browserWindowFromWebContents.mockReset();
- electronMocks.browserWindowGetAllWindows.mockReset();
- electronMocks.browserWindowGetAllWindows.mockReturnValue([]);
- resetActiveTaskNotificationsForTest();
- });
- it("does not generate fallback copy in main process when localized notification copy is missing", () => {
- const eventSender = {
- send: vi.fn(),
- };
- const logger = {
- info: vi.fn(),
- warn: vi.fn(),
- };
- const shown = dispatchTaskNotification({
- event: { sender: eventSender } as never,
- payload: {
- ...createTaskNotificationPayload("task-missing-copy"),
- title: " ",
- body: "",
- },
- logger,
- });
- expect(shown).toBe(false);
- expect(electronMocks.notificationInstances).toHaveLength(0);
- expect(eventSender.send).not.toHaveBeenCalledWith(PlatformChannels.TaskNotificationSound);
- expect(logger.warn).toHaveBeenCalledWith(
- "[show-task-notification] missing localized notification copy",
- {
- taskId: "task-missing-copy",
- status: "completed",
- hasTitle: false,
- hasBody: false,
- },
- );
- });
- it("keeps native notifications alive until the click handler can focus the task window", () => {
- const eventSender = {
- send: vi.fn(),
- };
- const senderWindow = {
- id: 7,
- isDestroyed: vi.fn(() => false),
- isMinimized: vi.fn(() => true),
- restore: vi.fn(),
- isVisible: vi.fn(() => false),
- show: vi.fn(),
- focus: vi.fn(),
- webContents: {
- send: vi.fn(),
- },
- };
- const logger = {
- info: vi.fn(),
- warn: vi.fn(),
- };
- electronMocks.browserWindowFromWebContents.mockReturnValue(senderWindow);
- const shown = dispatchTaskNotification({
- event: { sender: eventSender } as never,
- payload: createTaskNotificationPayload(),
- logger,
- });
- expect(shown).toBe(true);
- expect(getActiveTaskNotificationCountForTest()).toBe(1);
- expect(eventSender.send).toHaveBeenCalledWith(PlatformChannels.TaskNotificationSound);
- electronMocks.notificationInstances[0]?.emit("click");
- expect(getActiveTaskNotificationCountForTest()).toBe(0);
- expect(senderWindow.restore).toHaveBeenCalledTimes(1);
- expect(senderWindow.show).toHaveBeenCalledTimes(1);
- expect(senderWindow.focus).toHaveBeenCalledTimes(1);
- expect(senderWindow.webContents.send).toHaveBeenCalledWith(
- PlatformChannels.TaskNotificationClick,
- "task-1",
- );
- expect(logger.info).toHaveBeenCalledWith(
- "[show-task-notification] notification click handled",
- { taskId: "task-1", windowId: 7 },
- );
- expect(senderWindow.restore.mock.invocationCallOrder[0]).toBeLessThan(
- senderWindow.focus.mock.invocationCallOrder[0] ?? Number.MAX_SAFE_INTEGER,
- );
- if (process.platform === "darwin") {
- expect(electronMocks.appDockShow).toHaveBeenCalledTimes(1);
- expect(electronMocks.appShow).toHaveBeenCalledTimes(1);
- expect(electronMocks.appFocus).toHaveBeenCalledWith({ steal: true });
- expect(senderWindow.show.mock.invocationCallOrder[0]).toBeLessThan(
- electronMocks.appFocus.mock.invocationCallOrder[0] ?? Number.MAX_SAFE_INTEGER,
- );
- }
- });
- it("releases retained notifications when the OS closes them", () => {
- const eventSender = {
- send: vi.fn(),
- };
- electronMocks.browserWindowFromWebContents.mockReturnValue({
- webContents: {
- send: vi.fn(),
- },
- });
- dispatchTaskNotification({
- event: { sender: eventSender } as never,
- payload: createTaskNotificationPayload("task-closed-by-os"),
- logger: { info: vi.fn(), warn: vi.fn() },
- });
- expect(getActiveTaskNotificationCountForTest()).toBe(1);
- electronMocks.notificationInstances[0]?.emit("close");
- expect(getActiveTaskNotificationCountForTest()).toBe(0);
- });
- it("deduplicates elicitation notifications by request id instead of task id", () => {
- const eventSender = {
- send: vi.fn(),
- };
- const logger = {
- info: vi.fn(),
- warn: vi.fn(),
- };
- const firstShown = dispatchTaskNotification({
- event: { sender: eventSender } as never,
- payload: {
- taskId: "task-awaiting-user",
- status: "elicitation_request",
- requestId: "question-1",
- title: "需要你确认",
- body: "请回答当前问题",
- },
- logger,
- });
- const duplicateShown = dispatchTaskNotification({
- event: { sender: eventSender } as never,
- payload: {
- taskId: "task-awaiting-user",
- status: "elicitation_request",
- requestId: "question-1",
- title: "需要你确认",
- body: "请回答当前问题",
- },
- logger,
- });
- const nextRequestShown = dispatchTaskNotification({
- event: { sender: eventSender } as never,
- payload: {
- taskId: "task-awaiting-user",
- status: "elicitation_request",
- requestId: "question-2",
- title: "需要你确认",
- body: "请回答下一个问题",
- },
- logger,
- });
- expect(firstShown).toBe(true);
- expect(duplicateShown).toBe(false);
- expect(nextRequestShown).toBe(true);
- expect(electronMocks.notificationInstances).toHaveLength(2);
- expect(eventSender.send).toHaveBeenCalledTimes(2);
- expect(eventSender.send).toHaveBeenCalledWith(PlatformChannels.TaskNotificationSound);
- });
- });
|