useSettingServiceAutoResolution.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // @vitest-environment jsdom
  2. import { act, renderHook, waitFor } from "@testing-library/react";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. import { APP_RUNTIME_PREFERENCES_CHANGED_BROADCAST_CHANNEL, type AppSettings } from "@zcode/shared";
  5. import type { IServiceAccessor } from "@zcode/services";
  6. const settingService = {
  7. get: vi.fn<() => Promise<AppSettings>>(),
  8. update: vi.fn<(patch: Partial<AppSettings>) => Promise<void>>(),
  9. };
  10. const broadcastService = {
  11. send: vi.fn<(message: { channel: string; payload: unknown }) => Promise<void>>(),
  12. };
  13. const zcodeAgentService = {
  14. syncAppRuntimePreferences:
  15. vi.fn<
  16. (preferences: {
  17. askUserQuestionAutoResolutionEnabled: boolean;
  18. modelIoFullRetentionEnabled: boolean;
  19. }) => Promise<void>
  20. >(),
  21. };
  22. const botsService = {
  23. syncAppRuntimePreferences:
  24. vi.fn<
  25. (preferences: {
  26. askUserQuestionAutoResolutionEnabled: boolean;
  27. modelIoFullRetentionEnabled: boolean;
  28. }) => Promise<void>
  29. >(),
  30. };
  31. vi.mock("@/hooks/useServices.js", () => ({
  32. useServices: () => ({
  33. broadcastService,
  34. settingService,
  35. zcodeAgentService,
  36. botsService,
  37. }),
  38. }));
  39. vi.mock("@/hooks/usePlatform.js", () => ({
  40. usePlatform: () => ({}),
  41. }));
  42. describe("useSettings AskUserQuestion runtime preference", () => {
  43. beforeEach(() => {
  44. vi.clearAllMocks();
  45. settingService.get.mockResolvedValue({
  46. askUserQuestionAutoResolutionEnabled: true,
  47. modelIoFullRetentionEnabled: false,
  48. } as AppSettings);
  49. settingService.update.mockResolvedValue(undefined);
  50. broadcastService.send.mockResolvedValue(undefined);
  51. zcodeAgentService.syncAppRuntimePreferences.mockResolvedValue(undefined);
  52. botsService.syncAppRuntimePreferences.mockResolvedValue(undefined);
  53. });
  54. it("账号 CAS 条件透传到设置 owner,冲突拒绝更新", async () => {
  55. const { useSettings } = await import("@/hooks/useSettingService.js");
  56. const { result } = renderHook(() => useSettings());
  57. const expected = {
  58. providerFamilyDomain: "bigmodel" as const,
  59. providerFamilyConnectionSelections: {},
  60. };
  61. const patch = {
  62. providerFamilyConnectionSelections: { bigmodel: { kind: "individual-coding-plan" as const } },
  63. };
  64. await act(async () => {
  65. await result.current.update(patch, expected);
  66. });
  67. expect(settingService.update).toHaveBeenCalledWith(patch, expected);
  68. settingService.get.mockClear();
  69. settingService.update.mockRejectedValueOnce(new Error("Account connection settings changed"));
  70. await expect(result.current.update(patch, expected)).rejects.toThrow(
  71. "Account connection settings changed",
  72. );
  73. expect(settingService.get).toHaveBeenCalledOnce();
  74. });
  75. it("persists, waits for the current Host ACK, then broadcasts the strict preference", async () => {
  76. const { useSettings } = await import("@/hooks/useSettingService.js");
  77. const hook = renderHook(() => useSettings());
  78. await waitFor(() => expect(hook.result.current.loading).toBe(false));
  79. let releaseBotSync: (() => void) | undefined;
  80. botsService.syncAppRuntimePreferences.mockReturnValueOnce(
  81. new Promise<void>((resolve) => {
  82. releaseBotSync = resolve;
  83. }),
  84. );
  85. let updatePromise: Promise<void> | undefined;
  86. act(() => {
  87. updatePromise = hook.result.current.update({
  88. askUserQuestionAutoResolutionEnabled: false,
  89. });
  90. });
  91. await waitFor(() => expect(botsService.syncAppRuntimePreferences).toHaveBeenCalledOnce());
  92. expect(broadcastService.send).not.toHaveBeenCalled();
  93. releaseBotSync?.();
  94. await act(async () => {
  95. await updatePromise;
  96. });
  97. expect(settingService.update).toHaveBeenCalledWith({
  98. askUserQuestionAutoResolutionEnabled: false,
  99. });
  100. expect(zcodeAgentService.syncAppRuntimePreferences).toHaveBeenCalledWith({
  101. askUserQuestionAutoResolutionEnabled: false,
  102. modelIoFullRetentionEnabled: false,
  103. });
  104. expect(botsService.syncAppRuntimePreferences).toHaveBeenCalledWith({
  105. askUserQuestionAutoResolutionEnabled: false,
  106. modelIoFullRetentionEnabled: false,
  107. });
  108. expect(broadcastService.send).toHaveBeenCalledWith({
  109. channel: APP_RUNTIME_PREFERENCES_CHANGED_BROADCAST_CHANNEL,
  110. payload: {
  111. askUserQuestionAutoResolutionEnabled: false,
  112. modelIoFullRetentionEnabled: false,
  113. },
  114. });
  115. expect(zcodeAgentService.syncAppRuntimePreferences.mock.invocationCallOrder[0]).toBeLessThan(
  116. broadcastService.send.mock.invocationCallOrder[0] ?? 0,
  117. );
  118. expect(botsService.syncAppRuntimePreferences.mock.invocationCallOrder[0]).toBeLessThan(
  119. broadcastService.send.mock.invocationCallOrder[0] ?? 0,
  120. );
  121. hook.unmount();
  122. });
  123. it("persists and synchronizes ModelIO full retention with the complete preference snapshot", async () => {
  124. const { useSettings } = await import("@/hooks/useSettingService.js");
  125. const hook = renderHook(() => useSettings());
  126. await waitFor(() => expect(hook.result.current.loading).toBe(false));
  127. await act(async () => {
  128. await hook.result.current.update({ modelIoFullRetentionEnabled: true });
  129. });
  130. expect(settingService.update).toHaveBeenCalledWith({
  131. modelIoFullRetentionEnabled: true,
  132. });
  133. const preferences = {
  134. askUserQuestionAutoResolutionEnabled: true,
  135. modelIoFullRetentionEnabled: true,
  136. };
  137. expect(zcodeAgentService.syncAppRuntimePreferences).toHaveBeenCalledWith(preferences);
  138. expect(botsService.syncAppRuntimePreferences).toHaveBeenCalledWith(preferences);
  139. expect(broadcastService.send).toHaveBeenCalledWith({
  140. channel: APP_RUNTIME_PREFERENCES_CHANGED_BROADCAST_CHANNEL,
  141. payload: preferences,
  142. });
  143. hook.unmount();
  144. });
  145. it("isolates Source settings snapshots from the local renderer snapshot", async () => {
  146. const { useSettings } = await import("@/hooks/useSettingService.js");
  147. settingService.get.mockResolvedValueOnce({ locale: "zh-CN" } as AppSettings);
  148. const local = renderHook(() => useSettings({ cacheKey: "test-local" }));
  149. await waitFor(() => expect(local.result.current.loading).toBe(false));
  150. settingService.get.mockResolvedValueOnce({ locale: "en-US" } as AppSettings);
  151. const source = renderHook(() => useSettings({ cacheKey: "source:server-a" }));
  152. await waitFor(() => expect(source.result.current.loading).toBe(false));
  153. expect(local.result.current.settings?.locale).toBe("zh-CN");
  154. expect(source.result.current.settings?.locale).toBe("en-US");
  155. local.unmount();
  156. source.unmount();
  157. });
  158. it("isolates default snapshots by service accessor without requiring every caller to name a cache", async () => {
  159. const createServices = (locale: "zh-CN" | "en-US") =>
  160. ({
  161. broadcastService,
  162. settingService: {
  163. get: vi.fn(async () => ({ locale }) as AppSettings),
  164. update: vi.fn(async () => undefined),
  165. },
  166. zcodeAgentService,
  167. botsService,
  168. }) as unknown as IServiceAccessor;
  169. const controllerServices = createServices("zh-CN");
  170. const sourceServices = createServices("en-US");
  171. const { useSettings } = await import("@/hooks/useSettingService.js");
  172. const controller = renderHook(() => useSettings({ services: controllerServices }));
  173. const source = renderHook(() => useSettings({ services: sourceServices }));
  174. await waitFor(() => {
  175. expect(controller.result.current.loading).toBe(false);
  176. expect(source.result.current.loading).toBe(false);
  177. });
  178. expect(controller.result.current.settings?.locale).toBe("zh-CN");
  179. expect(source.result.current.settings?.locale).toBe("en-US");
  180. controller.unmount();
  181. source.unmount();
  182. });
  183. it("uses an explicitly injected Controller service instead of the active remote context", async () => {
  184. const controllerSettingService = {
  185. get: vi.fn(
  186. async () =>
  187. ({
  188. locale: "zh-CN",
  189. }) as AppSettings,
  190. ),
  191. update: vi.fn(async () => undefined),
  192. };
  193. const controllerServices = {
  194. broadcastService,
  195. settingService: controllerSettingService,
  196. zcodeAgentService,
  197. botsService,
  198. } as unknown as IServiceAccessor;
  199. const { useSettings } = await import("@/hooks/useSettingService.js");
  200. const hook = renderHook(() =>
  201. useSettings({ cacheKey: "controller-local-test", services: controllerServices }),
  202. );
  203. await waitFor(() => expect(hook.result.current.loading).toBe(false));
  204. await act(async () => {
  205. await hook.result.current.update({ locale: "en-US" });
  206. });
  207. expect(controllerSettingService.update).toHaveBeenCalledWith({
  208. locale: "en-US",
  209. });
  210. expect(settingService.update).not.toHaveBeenCalled();
  211. hook.unmount();
  212. });
  213. it("publishes an external Controller update to mounted settings consumers", async () => {
  214. let current = {
  215. locale: undefined,
  216. } as AppSettings;
  217. settingService.get.mockImplementation(async () => current);
  218. settingService.update.mockImplementation(async (patch) => {
  219. current = { ...current, ...patch };
  220. });
  221. const { updateSettingsAndRefreshSnapshot, useSettings } =
  222. await import("@/hooks/useSettingService.js");
  223. const hook = renderHook(() => useSettings());
  224. await waitFor(() => expect(hook.result.current.loading).toBe(false));
  225. await act(async () => {
  226. await updateSettingsAndRefreshSnapshot({
  227. settingService,
  228. patch: { locale: "zh-CN" },
  229. });
  230. });
  231. expect(hook.result.current.settings?.locale).toBe("zh-CN");
  232. hook.unmount();
  233. });
  234. it("persists ToolSearch mode without synchronizing or broadcasting to active runtimes", async () => {
  235. const { useSettings } = await import("@/hooks/useSettingService.js");
  236. const hook = renderHook(() => useSettings());
  237. await waitFor(() => expect(hook.result.current.loading).toBe(false));
  238. await act(async () => {
  239. await hook.result.current.update({ toolSearchMode: "local" });
  240. });
  241. expect(settingService.update).toHaveBeenCalledWith({ toolSearchMode: "local" });
  242. expect(zcodeAgentService.syncAppRuntimePreferences).not.toHaveBeenCalled();
  243. expect(botsService.syncAppRuntimePreferences).not.toHaveBeenCalled();
  244. expect(broadcastService.send).not.toHaveBeenCalled();
  245. hook.unmount();
  246. });
  247. });