mirror of
https://github.com/aiclientproxy/proxycast.git
synced 2026-10-16 15:34:08 +08:00
125 lines
3.3 KiB
TypeScript
125 lines
3.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const {
|
|
notificationCtorMock,
|
|
notificationIsSupportedMock,
|
|
notificationOptions,
|
|
notificationShowMock,
|
|
} = vi.hoisted(() => {
|
|
const notificationOptions: unknown[] = [];
|
|
const notificationShowMock = vi.fn();
|
|
const notificationCtorMock = vi.fn((options: unknown) => {
|
|
notificationOptions.push(options);
|
|
return {
|
|
show: notificationShowMock,
|
|
};
|
|
});
|
|
const notificationIsSupportedMock = vi.fn(() => true);
|
|
return {
|
|
notificationCtorMock,
|
|
notificationIsSupportedMock,
|
|
notificationOptions,
|
|
notificationShowMock,
|
|
};
|
|
});
|
|
|
|
vi.mock("./electronRuntime", () => ({
|
|
Notification: Object.assign(notificationCtorMock, {
|
|
isSupported: notificationIsSupportedMock,
|
|
}),
|
|
}));
|
|
|
|
import { showDesktopNotification } from "./desktopNotificationHost";
|
|
|
|
describe("showDesktopNotification", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
notificationCtorMock.mockImplementation((options: unknown) => {
|
|
notificationOptions.push(options);
|
|
return {
|
|
show: notificationShowMock,
|
|
};
|
|
});
|
|
notificationIsSupportedMock.mockReturnValue(true);
|
|
notificationOptions.length = 0;
|
|
});
|
|
|
|
it("通过 Electron 原生 Notification 发送 summary-only 桌面通知", () => {
|
|
expect(
|
|
showDesktopNotification({
|
|
request: {
|
|
body: " Lime local output · +520 ms ",
|
|
silent: true,
|
|
tag: "claw-trace-regression-alert-123",
|
|
title: " Regression alert: Critical ",
|
|
},
|
|
}),
|
|
).toEqual({ status: "sent" });
|
|
|
|
expect(notificationIsSupportedMock).toHaveBeenCalledOnce();
|
|
expect(notificationCtorMock).toHaveBeenCalledOnce();
|
|
expect(notificationOptions).toEqual([
|
|
{
|
|
body: "Lime local output · +520 ms",
|
|
id: "claw-trace-regression-alert-123",
|
|
silent: true,
|
|
title: "Regression alert: Critical",
|
|
},
|
|
]);
|
|
expect(notificationShowMock).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it("桌面通知不支持时返回 unsupported 且不构造通知", () => {
|
|
notificationIsSupportedMock.mockReturnValueOnce(false);
|
|
|
|
expect(
|
|
showDesktopNotification({
|
|
request: {
|
|
body: "Lime local output · +520 ms",
|
|
tag: "claw-trace-regression-alert-123",
|
|
title: "Regression alert: Warning",
|
|
},
|
|
}),
|
|
).toEqual({
|
|
reason: "electron_notification_unsupported",
|
|
status: "unsupported",
|
|
});
|
|
|
|
expect(notificationCtorMock).not.toHaveBeenCalled();
|
|
expect(notificationShowMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("拒绝 raw trace 和未声明字段", () => {
|
|
expect(() =>
|
|
showDesktopNotification({
|
|
request: {
|
|
body: "summary",
|
|
raw_trace_jsonl: "{}",
|
|
title: "alert",
|
|
},
|
|
}),
|
|
).toThrow("桌面通知请求包含不支持字段: raw_trace_jsonl");
|
|
|
|
expect(notificationCtorMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("通知构造或 show 失败时返回 failed", () => {
|
|
notificationCtorMock.mockImplementationOnce(() => {
|
|
throw new Error("notification blocked");
|
|
});
|
|
|
|
expect(
|
|
showDesktopNotification({
|
|
request: {
|
|
body: "summary",
|
|
tag: "claw-trace-regression-alert-123",
|
|
title: "alert",
|
|
},
|
|
}),
|
|
).toEqual({
|
|
reason: "notification blocked",
|
|
status: "failed",
|
|
});
|
|
});
|
|
});
|