useStartPlanRecommendation.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // @vitest-environment jsdom
  2. import { act, cleanup, renderHook } from "@testing-library/react";
  3. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  4. import type { ModelSelectionView } from "@zcode/provider";
  5. import { useStartPlanRecommendation } from "@/hooks/useStartPlanRecommendation.js";
  6. import { useConfirmDialogStore } from "@/store/confirmDialogStore.js";
  7. const fixture = vi.hoisted(() => ({
  8. dismissed: false,
  9. update: vi.fn(),
  10. toast: vi.fn(),
  11. readError: false,
  12. error: null as string | null,
  13. }));
  14. vi.mock("@/hooks/useProviderSettingsView.js", () => ({
  15. useProviderSettingsView: () => ({
  16. state: {
  17. status: "ready",
  18. view: {
  19. revision: 1,
  20. providers: [
  21. {
  22. providerId: "account:bigmodel-start-plan",
  23. effectiveConfig: {
  24. access: {
  25. type: "zhipu-account",
  26. accountType: "bigmodel",
  27. mode: "start-plan",
  28. entitled: true,
  29. },
  30. },
  31. },
  32. ],
  33. },
  34. },
  35. }),
  36. }));
  37. vi.mock("@/components/ui/toast.js", () => ({ toast: fixture.toast }));
  38. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  39. useOptionalBaseWorkspaceServices: () => ({
  40. settingService: {
  41. get: async () => {
  42. if (fixture.readError) throw new Error("settings unavailable");
  43. return { startPlanRecommendationDismissed: fixture.dismissed };
  44. },
  45. update: fixture.update,
  46. },
  47. usageStatsService: {},
  48. }),
  49. }));
  50. vi.mock("@/hooks/useUsageEntitlement.js", () => ({
  51. useUsageEntitlementWithService: (_service: unknown, options: { accountAccess: unknown }) => {
  52. expect(options.accountAccess).toEqual({
  53. type: "zhipu-account",
  54. family: "bigmodel",
  55. planKind: "start-plan",
  56. });
  57. return {
  58. error: fixture.error,
  59. refresh: vi.fn(),
  60. snapshot: {
  61. provider: { id: "account:bigmodel-start-plan" },
  62. generatedAt: Date.now(),
  63. quota: { limits: [{ remaining: 10, usageDetails: [{ modelCode: "glm-5" }] }] },
  64. },
  65. };
  66. },
  67. }));
  68. vi.mock("@/i18n/IntlProvider.js", () => ({
  69. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  70. }));
  71. const paid = {
  72. providerId: "account:bigmodel-individual-coding-plan",
  73. modelId: "glm-5",
  74. options: { reasoningLevel: "high" },
  75. };
  76. const start = { ...paid, providerId: "account:bigmodel-start-plan" };
  77. const view = {
  78. revision: 1,
  79. providers: [
  80. {
  81. providerId: start.providerId,
  82. config: {},
  83. models: [
  84. {
  85. modelId: start.modelId,
  86. config: { optionSpecs: { reasoningLevel: { values: ["high"] } } },
  87. },
  88. ],
  89. },
  90. ],
  91. } as unknown as ModelSelectionView;
  92. beforeEach(() => {
  93. fixture.dismissed = false;
  94. fixture.readError = false;
  95. fixture.toast.mockReset();
  96. fixture.error = null;
  97. fixture.update.mockReset().mockImplementation(async (patch) => {
  98. fixture.dismissed = patch.startPlanRecommendationDismissed;
  99. });
  100. });
  101. afterEach(() => {
  102. act(() => useConfirmDialogStore.getState().settleChoice("dismiss"));
  103. cleanup();
  104. });
  105. describe("提交推荐确认与 Host 偏好", () => {
  106. it.each(["confirm", "cancel"] as const)(
  107. "%s + 不再提示:只保存抑制偏好,下次仍用当前入口的选择",
  108. async (choice) => {
  109. const first = renderHook(() => useStartPlanRecommendation(view));
  110. let pending!: ReturnType<typeof first.result.current>;
  111. await act(async () => {
  112. pending = first.result.current(paid);
  113. });
  114. expect(useConfirmDialogStore.getState().pendingRequest).toBeDefined();
  115. act(() => {
  116. useConfirmDialogStore.getState().pendingRequest?.checkbox?.onCheckedChange(true);
  117. useConfirmDialogStore.getState().settleChoice(choice);
  118. });
  119. const chosen = await pending;
  120. expect(chosen).toEqual(choice === "confirm" ? start : paid);
  121. expect(fixture.update).toHaveBeenCalledExactlyOnceWith({
  122. startPlanRecommendationDismissed: true,
  123. });
  124. first.unmount();
  125. const next = renderHook(() => useStartPlanRecommendation(view));
  126. await expect(next.result.current(paid)).resolves.toEqual(paid);
  127. await expect(next.result.current(start)).resolves.toEqual(start);
  128. expect(useConfirmDialogStore.getState().pendingRequest).toBeUndefined();
  129. },
  130. );
  131. it("关闭弹窗取消本次提交,即使勾选也不保存偏好", async () => {
  132. const { result } = renderHook(() => useStartPlanRecommendation(view));
  133. let pending!: ReturnType<typeof result.current>;
  134. await act(async () => {
  135. pending = result.current(paid);
  136. });
  137. act(() => {
  138. useConfirmDialogStore.getState().pendingRequest?.checkbox?.onCheckedChange(true);
  139. useConfirmDialogStore.getState().settleChoice("dismiss");
  140. });
  141. await expect(pending).resolves.toBeNull();
  142. expect(fixture.update).not.toHaveBeenCalled();
  143. });
  144. it("不勾选时保留后续推荐,另一弹窗占用时不误当成“不了”提交", async () => {
  145. const { result } = renderHook(() => useStartPlanRecommendation(view));
  146. let pending!: ReturnType<typeof result.current>;
  147. await act(async () => {
  148. pending = result.current(paid);
  149. });
  150. await expect(result.current(paid)).resolves.toBeNull();
  151. act(() => useConfirmDialogStore.getState().settleChoice("cancel"));
  152. await expect(pending).resolves.toEqual(paid);
  153. expect(fixture.update).not.toHaveBeenCalled();
  154. });
  155. it("额度查询失败不会阻断原提交", async () => {
  156. fixture.error = "offline";
  157. const { result } = renderHook(() => useStartPlanRecommendation(view));
  158. await expect(result.current(paid)).resolves.toEqual(paid);
  159. expect(useConfirmDialogStore.getState().pendingRequest).toBeUndefined();
  160. });
  161. it("偏好读取失败跳过推荐,保留原付费提交", async () => {
  162. fixture.readError = true;
  163. const { result } = renderHook(() => useStartPlanRecommendation(view));
  164. await expect(result.current(paid)).resolves.toEqual(paid);
  165. expect(useConfirmDialogStore.getState().pendingRequest).toBeUndefined();
  166. });
  167. it.each(["confirm", "cancel"] as const)(
  168. "保存偏好失败仍遵守本次 %s,并提示没有保存成功",
  169. async (choice) => {
  170. fixture.update.mockRejectedValue(new Error("disk unavailable"));
  171. const { result } = renderHook(() => useStartPlanRecommendation(view));
  172. let pending!: ReturnType<typeof result.current>;
  173. await act(async () => {
  174. pending = result.current(paid);
  175. });
  176. act(() => {
  177. useConfirmDialogStore.getState().pendingRequest?.checkbox?.onCheckedChange(true);
  178. useConfirmDialogStore.getState().settleChoice(choice);
  179. });
  180. await expect(pending).resolves.toEqual(choice === "confirm" ? start : paid);
  181. expect(fixture.dismissed).toBe(false);
  182. expect(fixture.toast).toHaveBeenCalledExactlyOnceWith(
  183. "startPlan.recommendation.preferenceSaveFailed",
  184. );
  185. },
  186. );
  187. it("原二元确认兼容关闭行为", async () => {
  188. const pending = useConfirmDialogStore.getState().requestConfirmation({ title: "delete" });
  189. useConfirmDialogStore.getState().settleChoice("dismiss");
  190. await expect(pending).resolves.toBe(false);
  191. });
  192. });