storeOAuthRestoreState.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import type { IBroadcastService } from "@zcode/services";
  2. import { beforeEach, describe, expect, it } from "vitest";
  3. import { createZCodeStore } from "@/store/index.js";
  4. const localStorageState = new Map<string, string>();
  5. Object.defineProperty(globalThis, "localStorage", {
  6. configurable: true,
  7. value: {
  8. getItem: (key: string) => localStorageState.get(key) ?? null,
  9. setItem: (key: string, value: string) => {
  10. localStorageState.set(key, value);
  11. },
  12. removeItem: (key: string) => {
  13. localStorageState.delete(key);
  14. },
  15. clear: () => {
  16. localStorageState.clear();
  17. },
  18. },
  19. });
  20. Object.defineProperty(globalThis, "document", {
  21. configurable: true,
  22. value: {
  23. documentElement: {
  24. classList: {
  25. contains: () => false,
  26. toggle: () => {},
  27. },
  28. },
  29. },
  30. });
  31. const mockBroadcastService: IBroadcastService = {
  32. send: async () => {},
  33. onMessage: () => ({ dispose: () => {} }),
  34. };
  35. describe("createZCodeStore global state", () => {
  36. beforeEach(() => {
  37. localStorageState.clear();
  38. });
  39. it("默认不处于 OAuth 启动恢复中", () => {
  40. const store = createZCodeStore(mockBroadcastService);
  41. expect(store.getState().isRestoringOAuthSession).toBe(false);
  42. });
  43. it("支持在首次渲染前把 OAuth 恢复态初始化为 pending", () => {
  44. const store = createZCodeStore(mockBroadcastService, {
  45. initialIsRestoringOAuthSession: true,
  46. });
  47. expect(store.getState().isRestoringOAuthSession).toBe(true);
  48. });
  49. it("支持请求统一登录入口直接进入指定 provider 的登录流程", () => {
  50. const store = createZCodeStore(mockBroadcastService);
  51. const requestId = store.getState().requestLoginEntry("zai");
  52. const request = store.getState().loginEntryRequest;
  53. expect(request?.providerId).toBe("zai");
  54. expect(requestId).toBe(request?.id);
  55. expect(store.getState().loginEntryAttempt).toEqual({
  56. id: requestId,
  57. providerId: "zai",
  58. purpose: undefined,
  59. status: "requested",
  60. });
  61. store.getState().markLoginEntryAttemptStatus(requestId, "waiting");
  62. expect(store.getState().loginEntryAttempt?.status).toBe("waiting");
  63. store.getState().clearLoginEntryRequest(request?.id);
  64. expect(store.getState().loginEntryRequest).toBe(null);
  65. });
  66. it("登录尝试取消后不会被旧请求或后续新请求误更新", () => {
  67. const store = createZCodeStore(mockBroadcastService);
  68. const cancelledId = store.getState().requestLoginEntry("zai");
  69. store.getState().markLoginEntryAttemptStatus(cancelledId, "cancelled");
  70. const currentId = store.getState().requestLoginEntry("bigmodel");
  71. store.getState().markLoginEntryAttemptStatus(cancelledId, "succeeded");
  72. expect(store.getState().loginEntryAttempt).toMatchObject({
  73. id: currentId,
  74. providerId: "bigmodel",
  75. status: "requested",
  76. });
  77. });
  78. it("只在用户从未登录进入登录态时推进鉴权会话序号", () => {
  79. const store = createZCodeStore(mockBroadcastService);
  80. const user = { id: "user-1", username: "tester", displayName: "Tester" };
  81. store.getState().setUser(user);
  82. expect(store.getState().authSessionSeq).toBe(1);
  83. // 已登录时刷新用户信息或连接额外 provider,不属于重新登录。
  84. store.getState().setUser({ ...user, displayName: "Tester Updated" });
  85. expect(store.getState().authSessionSeq).toBe(1);
  86. store.getState().setUser(null);
  87. store.getState().setUser(user);
  88. expect(store.getState().authSessionSeq).toBe(2);
  89. });
  90. it("同一自动完成历史保留首次鉴权会话且拒绝旧会话回写", () => {
  91. const store = createZCodeStore(mockBroadcastService);
  92. const user = { id: "user-1", username: "tester", displayName: "Tester" };
  93. const firstCompletedAt = Date.now() - 10_000;
  94. const secondCompletedAt = Date.now() - 1_000;
  95. store.getState().setUser(user);
  96. store.getState().setCodingPlanQuotaResetUiEntry(
  97. "composer:test",
  98. "WEEK",
  99. {
  100. status: "completed",
  101. opportunityCount: 0,
  102. opportunityExpiresAt: null,
  103. startedAt: null,
  104. completedAt: firstCompletedAt,
  105. observedAt: firstCompletedAt + 100,
  106. quotaOverridePending: false,
  107. nextResetAt: null,
  108. idempotencyKey: null,
  109. error: null,
  110. },
  111. 1,
  112. );
  113. store.getState().setUser(null);
  114. store.getState().setUser(user);
  115. store.getState().setCodingPlanQuotaResetUiEntry(
  116. "composer:test",
  117. "WEEK",
  118. {
  119. status: "completed",
  120. opportunityCount: 0,
  121. opportunityExpiresAt: null,
  122. startedAt: null,
  123. completedAt: firstCompletedAt,
  124. observedAt: null,
  125. quotaOverridePending: false,
  126. nextResetAt: null,
  127. idempotencyKey: null,
  128. error: null,
  129. },
  130. 2,
  131. );
  132. expect(
  133. store.getState().codingPlanQuotaResetAutomaticObservationsBySource[
  134. "composer:test"
  135. ]?.week,
  136. ).toEqual({ completedAt: firstCompletedAt, authSessionSeq: 1 });
  137. // 上一鉴权会话尚未结束的异步请求不能覆盖当前会话状态。
  138. store.getState().setCodingPlanQuotaResetUiEntry(
  139. "composer:test",
  140. "WEEK",
  141. {
  142. status: "completed",
  143. opportunityCount: 0,
  144. opportunityExpiresAt: null,
  145. startedAt: null,
  146. completedAt: secondCompletedAt,
  147. observedAt: secondCompletedAt + 100,
  148. quotaOverridePending: true,
  149. nextResetAt: null,
  150. idempotencyKey: null,
  151. error: null,
  152. },
  153. 1,
  154. );
  155. expect(
  156. store.getState().codingPlanQuotaResetUiBySource["composer:test"]?.week
  157. ?.completedAt,
  158. ).toBe(firstCompletedAt);
  159. });
  160. it("记录最近一次 OAuth 成功的平台", () => {
  161. const store = createZCodeStore(mockBroadcastService);
  162. store.getState().markOAuthSuccess("zai");
  163. expect(store.getState().oauthSuccessSeq).toBe(1);
  164. expect(store.getState().lastOAuthSuccessProvider).toBe("zai");
  165. });
  166. it("性能模式默认关闭,并持久化用户选择", () => {
  167. const store = createZCodeStore(mockBroadcastService);
  168. expect(store.getState().performanceMode).toBe(false);
  169. store.getState().setPerformanceMode(true);
  170. expect(store.getState().performanceMode).toBe(true);
  171. expect(localStorageState.get("zcode-performance-mode")).toBe("true");
  172. expect(
  173. createZCodeStore(mockBroadcastService).getState().performanceMode,
  174. ).toBe(true);
  175. });
  176. });