deer-flow/frontend/tests/unit/core/channels/notification-preferences.test.ts
Totoro cb90f5dca4
feat(channels): control IM notifications and add WeChat/QQ delivery (#6850)
* feat(channels): control IM notifications and add WeChat/QQ delivery

* fix(scheduler): skip notification preference locks without recipients

---------

Co-authored-by: Totoro-qaq <279883115+Totoro-qaq@users.noreply.github.com>
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
2026-10-14 11:14:48 +08:00

72 lines
2.5 KiB
TypeScript

import { beforeEach, expect, test, rs } from "@rstest/core";
import { GatewayApiError } from "@/core/api/errors";
import { fetch as fetcher } from "@/core/api/fetcher";
import {
loadChannelNotificationPreferences,
updateChannelNotificationPreference,
} from "@/core/channels/notification-preferences";
rs.mock("@/core/api/fetcher", () => ({ fetch: rs.fn() }));
rs.mock("@/core/config", () => ({ getBackendBaseURL: () => "/backend" }));
const defaults = { feishu: true, wecom: true, wechat: true, qq: true };
beforeEach(() => {
rs.mocked(fetcher).mockReset();
});
test("loads effective preferences with an expected-owner fence and consumed signal", async () => {
rs.mocked(fetcher).mockResolvedValue(new Response(JSON.stringify(defaults)));
const signal = new AbortController().signal;
expect(await loadChannelNotificationPreferences("default", signal)).toEqual(
defaults,
);
expect(rs.mocked(fetcher)).toHaveBeenCalledWith(
"/backend/api/channels/notification-preferences",
expect.objectContaining({
method: "GET",
signal,
headers: expect.objectContaining({ "X-Expected-User-Id": "default" }),
}),
);
});
test("updates exactly one provider, preserving false and the authenticated viewer header", async () => {
rs.mocked(fetcher).mockResolvedValue(
new Response(JSON.stringify({ ...defaults, qq: false })),
);
const signal = new AbortController().signal;
expect(
(await updateChannelNotificationPreference("alice", "qq", false, signal))
.qq,
).toBe(false);
expect(rs.mocked(fetcher)).toHaveBeenCalledWith(
"/backend/api/channels/notification-preferences",
expect.objectContaining({
method: "PATCH",
signal,
body: '{"qq":false}',
headers: expect.objectContaining({ "X-Expected-User-Id": "alice" }),
}),
);
});
test.each([{ feishu: true }, { ...defaults, qq: "false" }, null])(
"rejects malformed effective preferences instead of inventing defaults",
async (body) => {
rs.mocked(fetcher).mockResolvedValue(new Response(JSON.stringify(body)));
await expect(
loadChannelNotificationPreferences("alice", new AbortController().signal),
).rejects.toThrow();
},
);
test("keeps an unavailable endpoint distinguishable from valid default values", async () => {
rs.mocked(fetcher).mockResolvedValue(
new Response('{"detail":"Not found"}', { status: 404 }),
);
await expect(
loadChannelNotificationPreferences("alice", new AbortController().signal),
).rejects.toBeInstanceOf(GatewayApiError);
});