migration.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { describe, expect, test, vi } from "vitest";
  2. const createWebhookTargetActionMock = vi.hoisted(() => vi.fn());
  3. const updateBindingsActionMock = vi.hoisted(() => vi.fn());
  4. vi.mock("@/actions/webhook-targets", () => {
  5. return {
  6. createWebhookTargetAction: createWebhookTargetActionMock,
  7. };
  8. });
  9. vi.mock("@/actions/notification-bindings", () => {
  10. return {
  11. updateBindingsAction: updateBindingsActionMock,
  12. };
  13. });
  14. describe("migrateToNewWebhookSystem - 异常计数返回", () => {
  15. test("发生异常时,应返回已完成的 createdTargets/createdBindings", async () => {
  16. createWebhookTargetActionMock
  17. .mockResolvedValueOnce({ ok: true, data: { id: 1 } })
  18. .mockRejectedValueOnce(new Error("boom"));
  19. const { migrateToNewWebhookSystem } = await import("@/lib/webhook/migration");
  20. const settings = {
  21. useLegacyMode: true,
  22. circuitBreakerEnabled: true,
  23. circuitBreakerWebhook: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx",
  24. dailyLeaderboardEnabled: true,
  25. dailyLeaderboardWebhook: "https://open.feishu.cn/open-apis/bot/v2/hook/xxx",
  26. costAlertEnabled: false,
  27. costAlertWebhook: null,
  28. } as any;
  29. const result = await migrateToNewWebhookSystem(settings, new Map());
  30. expect(result.success).toBe(false);
  31. expect(result.error).toBe("boom");
  32. expect(result.createdTargets).toBe(1);
  33. expect(result.createdBindings).toBe(0);
  34. });
  35. });