accountConnectionLossNotification.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // @vitest-environment jsdom
  2. import { act, cleanup, renderHook, waitFor } from "@testing-library/react";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { useAccountConnectionLossNotification } from "@/root/useAccountConnectionLossNotification.js";
  5. const mocks = vi.hoisted(() => ({
  6. toast: vi.fn((_message: string, _options?: { onAction?: () => void; actionLabel?: string }) => 1),
  7. dismissToast: vi.fn(),
  8. prepare: vi.fn(),
  9. apply: vi.fn(async () => "switched"),
  10. }));
  11. vi.mock("@/components/ui/toast.js", () => ({
  12. toast: mocks.toast,
  13. dismissToast: mocks.dismissToast,
  14. }));
  15. vi.mock("@/root/accountConnectionLossSuggestion.js", () => ({
  16. prepareAccountConnectionSwitch: mocks.prepare,
  17. }));
  18. vi.mock("@/i18n/IntlProvider.js", () => ({
  19. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  20. }));
  21. afterEach(() => {
  22. cleanup();
  23. vi.clearAllMocks();
  24. });
  25. function fixture() {
  26. let revision = 0;
  27. let accept!: (view: unknown) => void;
  28. const snapshot = (availability: string) => ({
  29. revision: ++revision,
  30. providers: [
  31. {
  32. providerId: "account:bigmodel-team-coding-plan",
  33. accountState: {
  34. availability,
  35. current: true,
  36. connectionKey: "account/start",
  37. },
  38. },
  39. ],
  40. });
  41. const services = {
  42. providerSettingsService: {
  43. getView: vi.fn(async () => snapshot("available")),
  44. onDidChange: vi.fn((listener) => {
  45. accept = listener;
  46. return { dispose: vi.fn() };
  47. }),
  48. },
  49. };
  50. mocks.prepare.mockResolvedValue({
  51. selection: { kind: "individual-coding-plan" },
  52. apply: mocks.apply,
  53. });
  54. return { services, emit: (availability: string) => act(() => accept(snapshot(availability))) };
  55. }
  56. describe("根层套餐失效提示", () => {
  57. it("点击保存失败后提供同一目标的手动重试,不重新查询候选", async () => {
  58. const { services, emit } = fixture();
  59. mocks.apply.mockRejectedValueOnce(new Error("disk"));
  60. renderHook(() => useAccountConnectionLossNotification(services as never, "intent"));
  61. await act(async () => {});
  62. emit("unavailable");
  63. await waitFor(() => expect(mocks.toast).toHaveBeenCalledOnce());
  64. await act(async () => mocks.toast.mock.calls[0]![1]!.onAction!());
  65. expect(mocks.toast.mock.calls[1]![1]?.actionLabel).toBe("common.retry");
  66. await act(async () => mocks.toast.mock.calls[1]![1]!.onAction!());
  67. expect(mocks.apply).toHaveBeenCalledTimes(2);
  68. expect(mocks.prepare).toHaveBeenCalledOnce();
  69. });
  70. it("普通重新渲染不重置基线;重复不可用只提示一次,点击才提交", async () => {
  71. const { services, emit } = fixture();
  72. const refresh = vi.fn();
  73. const { rerender } = renderHook(() =>
  74. useAccountConnectionLossNotification(services as never, "intent", refresh),
  75. );
  76. await act(async () => {});
  77. emit("unavailable");
  78. await waitFor(() => expect(mocks.toast).toHaveBeenCalledOnce());
  79. expect(mocks.apply).not.toHaveBeenCalled();
  80. rerender();
  81. emit("unavailable");
  82. await act(async () => {});
  83. expect(mocks.toast).toHaveBeenCalledOnce();
  84. expect(services.providerSettingsService.onDidChange).toHaveBeenCalledOnce();
  85. const options = mocks.toast.mock.calls[0]![1] as { onAction(): void };
  86. await act(async () => options.onAction());
  87. expect(mocks.apply).toHaveBeenCalledOnce();
  88. expect(refresh).toHaveBeenCalledOnce();
  89. });
  90. it("切换意图立即关闭旧提示,新不可用是基线而非再次失效", async () => {
  91. const { services, emit } = fixture();
  92. const { rerender } = renderHook(
  93. ({ intent }) => useAccountConnectionLossNotification(services as never, intent),
  94. { initialProps: { intent: "old" } },
  95. );
  96. await act(async () => {});
  97. emit("unavailable");
  98. await waitFor(() => expect(mocks.toast).toHaveBeenCalledOnce());
  99. const event = mocks.prepare.mock.calls[0]![1];
  100. rerender({ intent: "new" });
  101. expect(event.isCurrent()).toBe(false);
  102. expect(mocks.dismissToast).toHaveBeenCalledWith(1);
  103. emit("unavailable");
  104. await act(async () => {});
  105. expect(mocks.toast).toHaveBeenCalledOnce();
  106. });
  107. it("备选查询失败只显示状态,不生成可点击的假目标", async () => {
  108. const { services, emit } = fixture();
  109. mocks.prepare.mockRejectedValueOnce(new Error("offline"));
  110. renderHook(() => useAccountConnectionLossNotification(services as never, "intent"));
  111. await act(async () => {});
  112. emit("unavailable");
  113. await waitFor(() => expect(mocks.toast).toHaveBeenCalledOnce());
  114. expect(mocks.toast.mock.calls[0]![1]).toMatchObject({
  115. actionLabel: undefined,
  116. onAction: undefined,
  117. });
  118. expect(mocks.apply).not.toHaveBeenCalled();
  119. });
  120. });