appSettings.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { describe, expect, it } from "vitest";
  2. import { appSettingsPatchSchema, appSettingsSchema } from "../src/validationAppSettings.js";
  3. describe("app settings", () => {
  4. it("round-trips every structured Provider Family connection selection", () => {
  5. const selections = {
  6. zai: { kind: "individual-coding-plan" },
  7. bigmodel: {
  8. kind: "team-coding-plan",
  9. productId: "product-1",
  10. organizationId: "org-1",
  11. projectId: "project-1",
  12. },
  13. } as const;
  14. expect(
  15. appSettingsSchema.parse({ providerFamilyConnectionSelections: selections })
  16. .providerFamilyConnectionSelections,
  17. ).toEqual(selections);
  18. expect(
  19. appSettingsPatchSchema.parse({ providerFamilyConnectionSelections: selections })
  20. .providerFamilyConnectionSelections,
  21. ).toEqual(selections);
  22. expect(
  23. appSettingsPatchSchema.safeParse({
  24. providerFamilyConnectionSelections: {
  25. zai: { kind: "team-coding-plan", productId: "p", organizationId: "o" },
  26. },
  27. }).success,
  28. ).toBe(false);
  29. });
  30. it("defaults and validates the human embedded Browser viewport preference", () => {
  31. expect(appSettingsSchema.parse({}).embeddedBrowserViewportPreference).toEqual({
  32. mode: "normal",
  33. viewport: { width: 393, height: 852 },
  34. zoom: "fit",
  35. });
  36. const preference = {
  37. mode: "responsive",
  38. viewport: { width: 412, height: 915 },
  39. zoom: "50",
  40. } as const;
  41. expect(
  42. appSettingsSchema.parse({ embeddedBrowserViewportPreference: preference })
  43. .embeddedBrowserViewportPreference,
  44. ).toEqual(preference);
  45. expect(
  46. appSettingsPatchSchema.parse({ embeddedBrowserViewportPreference: preference })
  47. .embeddedBrowserViewportPreference,
  48. ).toEqual(preference);
  49. for (const embeddedBrowserViewportPreference of [
  50. { ...preference, mode: "agent" },
  51. { ...preference, viewport: { width: 319, height: 915 } },
  52. { ...preference, viewport: { width: 412, height: 2161 } },
  53. { ...preference, zoom: "80" },
  54. ]) {
  55. expect(appSettingsPatchSchema.safeParse({ embeddedBrowserViewportPreference }).success).toBe(
  56. false,
  57. );
  58. }
  59. });
  60. it("drops only a damaged human Browser preference from stored settings", () => {
  61. const settings = appSettingsSchema.parse({
  62. locale: "en-US",
  63. recentProjects: ["/workspace/demo"],
  64. embeddedBrowserViewportPreference: {
  65. mode: "responsive",
  66. viewport: { width: "bad", height: 915 },
  67. zoom: "50",
  68. },
  69. });
  70. expect(settings.locale).toBe("en-US");
  71. expect(settings.recentProjects).toEqual(["/workspace/demo"]);
  72. expect(settings.embeddedBrowserViewportPreference).toEqual({
  73. mode: "normal",
  74. viewport: { width: 393, height: 852 },
  75. zoom: "fit",
  76. });
  77. });
  78. it("defaults ModelIO full retention to off and validates explicit boolean patches", () => {
  79. expect(appSettingsSchema.parse({}).modelIoFullRetentionEnabled).toBe(false);
  80. expect(
  81. appSettingsSchema.parse({ modelIoFullRetentionEnabled: true }).modelIoFullRetentionEnabled,
  82. ).toBe(true);
  83. expect(
  84. appSettingsPatchSchema.parse({ modelIoFullRetentionEnabled: false })
  85. .modelIoFullRetentionEnabled,
  86. ).toBe(false);
  87. expect(appSettingsPatchSchema.safeParse({ modelIoFullRetentionEnabled: "true" }).success).toBe(
  88. false,
  89. );
  90. });
  91. it("drops the removed CUA overlay/pip/background settings keys from stored configs", () => {
  92. // 2026-08-17 设置面收敛:三个 key 已从 schema 删除;存量配置里的旧值被 zod strip 静默忽略,
  93. // 不报错也不回流(overlay/PiP 由 services 装配层恒传 true,见 cuaGhostCursorSettingsWiring)。
  94. const stale = appSettingsSchema.parse({
  95. cuaGhostCursorEnabled: false,
  96. cuaPipMode: false,
  97. cuaBackgroundMode: true,
  98. });
  99. expect("cuaGhostCursorEnabled" in stale).toBe(false);
  100. expect("cuaPipMode" in stale).toBe(false);
  101. expect("cuaBackgroundMode" in stale).toBe(false);
  102. expect(appSettingsPatchSchema.parse({ cuaPipMode: true })).toEqual({});
  103. });
  104. it("persists an explicit Windows integrated terminal shell selection", () => {
  105. const selection = {
  106. mode: "shell",
  107. dialect: "git-bash",
  108. id: "git-bash:C:\\Program Files\\Git\\bin\\bash.exe",
  109. label: "Git Bash",
  110. path: "C:\\Program Files\\Git\\bin\\bash.exe",
  111. } as const;
  112. expect(
  113. appSettingsSchema.parse({ integratedTerminalShell: selection }).integratedTerminalShell,
  114. ).toEqual(selection);
  115. expect(
  116. appSettingsPatchSchema.parse({ integratedTerminalShell: selection }).integratedTerminalShell,
  117. ).toEqual(selection);
  118. });
  119. it("accepts auto mode and rejects incomplete shell selections", () => {
  120. expect(
  121. appSettingsPatchSchema.parse({ integratedTerminalShell: { mode: "auto" } })
  122. .integratedTerminalShell,
  123. ).toEqual({ mode: "auto" });
  124. expect(() =>
  125. appSettingsPatchSchema.parse({
  126. integratedTerminalShell: {
  127. mode: "shell",
  128. dialect: "git-bash",
  129. id: "git-bash",
  130. label: "Git Bash",
  131. },
  132. }),
  133. ).toThrow();
  134. });
  135. it("accepts desktop zoom levels within the supported window range", () => {
  136. expect(appSettingsSchema.parse({ desktopZoomLevel: 5 }).desktopZoomLevel).toBe(5);
  137. expect(appSettingsPatchSchema.parse({ desktopZoomLevel: -3 }).desktopZoomLevel).toBe(-3);
  138. expect(() => appSettingsSchema.parse({ desktopZoomLevel: 6 })).toThrow();
  139. expect(() => appSettingsPatchSchema.parse({ desktopZoomLevel: -4 })).toThrow();
  140. });
  141. it("accepts a bounded desktop main window size and maximized state", () => {
  142. const desktopWindowSize = { width: 1440, height: 900, maximized: true };
  143. expect(appSettingsSchema.parse({ desktopWindowSize }).desktopWindowSize).toEqual(
  144. desktopWindowSize,
  145. );
  146. expect(appSettingsPatchSchema.parse({ desktopWindowSize }).desktopWindowSize).toEqual(
  147. desktopWindowSize,
  148. );
  149. expect(() =>
  150. appSettingsPatchSchema.parse({
  151. desktopWindowSize: { width: 479, height: 900, maximized: false },
  152. }),
  153. ).toThrow();
  154. expect(
  155. appSettingsSchema.parse({
  156. desktopWindowSize: { width: 1440, height: 639, maximized: false },
  157. }).desktopWindowSize,
  158. ).toBeUndefined();
  159. });
  160. it("discards a damaged desktop window size without resetting other settings", () => {
  161. const settings = appSettingsSchema.parse({
  162. locale: "en-US",
  163. recentProjects: ["/workspace/demo"],
  164. desktopWindowSize: { width: "bad", height: 900, maximized: false },
  165. });
  166. expect(settings.locale).toBe("en-US");
  167. expect(settings.recentProjects).toEqual(["/workspace/demo"]);
  168. expect(settings.desktopWindowSize).toBeUndefined();
  169. });
  170. it("defaults desktop Chromium hardware acceleration to enabled and accepts explicit patches", () => {
  171. expect(appSettingsSchema.parse({}).desktopChromiumHardwareAccelerationEnabled).toBe(true);
  172. expect(
  173. appSettingsSchema.parse({
  174. desktopChromiumHardwareAccelerationEnabled: false,
  175. }).desktopChromiumHardwareAccelerationEnabled,
  176. ).toBe(false);
  177. expect(
  178. appSettingsPatchSchema.parse({
  179. desktopChromiumHardwareAccelerationEnabled: true,
  180. }).desktopChromiumHardwareAccelerationEnabled,
  181. ).toBe(true);
  182. expect(() =>
  183. appSettingsPatchSchema.parse({
  184. desktopChromiumHardwareAccelerationEnabled: "false",
  185. }),
  186. ).toThrow();
  187. });
  188. it("preserves localized pending post-update release notes", () => {
  189. const payload = {
  190. version: "3.1.4",
  191. title: "Release v3.1.4",
  192. markdown: "## 新功能\n- 中文说明",
  193. releaseDate: "2026-06-23T13:48:54.438Z",
  194. releaseNotesByLocale: {
  195. "zh-CN": {
  196. title: "ZCode v3.1.4",
  197. markdown: "## 新功能\n- 中文说明",
  198. },
  199. "en-US": {
  200. title: "ZCode v3.1.4",
  201. markdown: "## New Features\n- English notes",
  202. },
  203. },
  204. };
  205. expect(
  206. appSettingsSchema.parse({ pendingPostUpdateReleaseNotes: payload })
  207. .pendingPostUpdateReleaseNotes,
  208. ).toEqual(payload);
  209. expect(
  210. appSettingsPatchSchema.parse({ pendingPostUpdateReleaseNotes: payload })
  211. .pendingPostUpdateReleaseNotes,
  212. ).toEqual(payload);
  213. });
  214. it("persists only stable Web remote control startup context fields", () => {
  215. const context = {
  216. workspacePath: "/workspace/demo",
  217. workspaceIdentity: "remote:ssh:host:/workspace/demo",
  218. initialTaskId: "task-1",
  219. };
  220. expect(
  221. appSettingsSchema.parse({ webRemoteControlLastEnabledContext: context })
  222. .webRemoteControlLastEnabledContext,
  223. ).toEqual(context);
  224. expect(
  225. appSettingsPatchSchema.parse({ webRemoteControlLastEnabledContext: context })
  226. .webRemoteControlLastEnabledContext,
  227. ).toEqual(context);
  228. expect(
  229. appSettingsPatchSchema.parse({
  230. webRemoteControlLastEnabledContext: {
  231. ...context,
  232. remoteSessionId: "ephemeral-session",
  233. },
  234. }).webRemoteControlLastEnabledContext,
  235. ).toEqual(context);
  236. });
  237. });
  238. // DWG-19(docs/dynamic-workflow/launch.md「The user's choice」):空串是删除选择的哨兵,
  239. // 读文件时的未知取值按「跟随」处理,不能把整份设置读坏。
  240. describe("dynamicWorkflowMode 设置(DWG-19)", () => {
  241. it("补丁接受三个模式与空串哨兵,拒绝其它值", () => {
  242. for (const value of ["disabled", "onDemand", "alwaysOn", ""]) {
  243. expect(appSettingsPatchSchema.parse({ dynamicWorkflowMode: value })).toEqual({
  244. dynamicWorkflowMode: value,
  245. });
  246. }
  247. expect(appSettingsPatchSchema.safeParse({ dynamicWorkflowMode: "bogus" }).success).toBe(false);
  248. });
  249. it("文件里的合法值原样读出,缺席保持缺席,未知值按跟随读成缺席", () => {
  250. expect(appSettingsSchema.parse({ dynamicWorkflowMode: "onDemand" }).dynamicWorkflowMode).toBe(
  251. "onDemand",
  252. );
  253. expect(appSettingsSchema.parse({}).dynamicWorkflowMode).toBeUndefined();
  254. expect(appSettingsSchema.parse({ dynamicWorkflowMode: "future" }).dynamicWorkflowMode).toBe(
  255. undefined,
  256. );
  257. });
  258. });