appSettings.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import { describe, expect, it } from "vitest";
  2. import { z } from "zod";
  3. import { appSettingsPatchSchema, appSettingsSchema } from "#src/validationAppSettings.js";
  4. describe("app settings", () => {
  5. it.each([undefined, false, true])("旧自动归档选择 %s 一次性关闭,保留天数", (enabled) => {
  6. const migrated = appSettingsSchema.parse({
  7. taskAutoArchiveEnabled: enabled,
  8. taskAutoArchiveOlderThanDays: 14,
  9. });
  10. expect(migrated).toMatchObject({
  11. taskAutoArchiveEnabled: false,
  12. taskAutoArchiveOlderThanDays: 14,
  13. taskAutoArchiveResetVersion: 1,
  14. });
  15. expect(appSettingsSchema.parse(migrated)).toEqual(migrated);
  16. const reopened = appSettingsSchema.parse({
  17. ...migrated,
  18. ...appSettingsPatchSchema.parse({ taskAutoArchiveEnabled: true }),
  19. });
  20. expect(reopened.taskAutoArchiveEnabled).toBe(true);
  21. expect(appSettingsSchema.parse(reopened)).toEqual(reopened);
  22. });
  23. it("defaults ToolSearch off and accepts only the three App modes", () => {
  24. expect(appSettingsSchema.parse({}).toolSearchMode).toBe("off");
  25. expect(appSettingsPatchSchema.parse({})).not.toHaveProperty("toolSearchMode");
  26. for (const mode of ["auto", "local", "off"]) {
  27. expect(appSettingsSchema.parse({ toolSearchMode: mode }).toolSearchMode).toBe(mode);
  28. expect(appSettingsPatchSchema.parse({ toolSearchMode: mode }).toolSearchMode).toBe(mode);
  29. }
  30. for (const mode of ["native", "invalid", true, null]) {
  31. expect(appSettingsPatchSchema.safeParse({ toolSearchMode: mode }).success).toBe(false);
  32. }
  33. });
  34. it("preserves an explicit startup reconnect preference while keeping old settings optional", () => {
  35. // 合同 §8.2:旧设置缺字段不触发迁移,Host/UI 继续按 true 解释;显式 false 必须通过完整 schema。
  36. const parsed = appSettingsSchema.parse({});
  37. expect(parsed.remoteStartupAutoReconnectEnabled).toBeUndefined();
  38. expect(
  39. appSettingsSchema.parse({ remoteStartupAutoReconnectEnabled: false })
  40. .remoteStartupAutoReconnectEnabled,
  41. ).toBe(false);
  42. expect(
  43. appSettingsPatchSchema.parse({ remoteStartupAutoReconnectEnabled: false })
  44. .remoteStartupAutoReconnectEnabled,
  45. ).toBe(false);
  46. expect(
  47. appSettingsPatchSchema.safeParse({
  48. remoteStartupAutoReconnectEnabled: "yes",
  49. }).success,
  50. ).toBe(false);
  51. });
  52. it("drops the removed global asset install mode preference", () => {
  53. const staleFullSettings = appSettingsSchema.parse({
  54. remoteDefaultAssetInstallMode: "remote-download",
  55. });
  56. expect("remoteDefaultAssetInstallMode" in staleFullSettings).toBe(false);
  57. const stalePatch = appSettingsPatchSchema.parse({
  58. remoteDefaultAssetInstallMode: "remote-download",
  59. });
  60. expect("remoteDefaultAssetInstallMode" in stalePatch).toBe(false);
  61. });
  62. it("round-trips Source-scoped conversation backing paths without extending strict project history", () => {
  63. const record = {
  64. "11111111-1111-4111-8111-111111111111": {
  65. workspacePath: "/home/dev/.zcode/workspace/default",
  66. lastResolvedAt: 123,
  67. },
  68. };
  69. expect(
  70. appSettingsSchema.parse({
  71. remoteConversationWorkspaceBySourceServerId: record,
  72. }).remoteConversationWorkspaceBySourceServerId,
  73. ).toEqual(record);
  74. expect(
  75. appSettingsPatchSchema.parse({
  76. remoteConversationWorkspaceBySourceServerId: record,
  77. }).remoteConversationWorkspaceBySourceServerId,
  78. ).toEqual(record);
  79. expect(
  80. appSettingsSchema.safeParse({
  81. remoteConversationWorkspaceBySourceServerId: {
  82. "11111111-1111-4111-8111-111111111111": {
  83. workspacePath: "/home/dev/.zcode/workspace/default",
  84. lastResolvedAt: -1,
  85. },
  86. },
  87. }).success,
  88. ).toBe(false);
  89. });
  90. it("round-trips every structured Provider Family connection selection", () => {
  91. const selections = {
  92. zai: { kind: "individual-coding-plan" },
  93. bigmodel: {
  94. kind: "team-coding-plan",
  95. productId: "product-1",
  96. organizationId: "org-1",
  97. projectId: "project-1",
  98. },
  99. } as const;
  100. expect(
  101. appSettingsSchema.parse({ providerFamilyConnectionSelections: selections })
  102. .providerFamilyConnectionSelections,
  103. ).toEqual(selections);
  104. expect(
  105. appSettingsPatchSchema.parse({ providerFamilyConnectionSelections: selections })
  106. .providerFamilyConnectionSelections,
  107. ).toEqual(selections);
  108. expect(
  109. appSettingsPatchSchema.safeParse({
  110. providerFamilyConnectionSelections: {
  111. zai: { kind: "team-coding-plan", productId: "p", organizationId: "o" },
  112. },
  113. }).success,
  114. ).toBe(false);
  115. });
  116. it("defaults old remote open entries to project and preserves explicit conversation purpose", () => {
  117. const base = {
  118. kind: "remote",
  119. workspacePath: "/workspace/demo",
  120. target: { kind: "docker", container: "demo" },
  121. lastOpenedAt: 1,
  122. lastConnectionStatus: "connected",
  123. } as const;
  124. expect(
  125. appSettingsSchema.parse({ lastWorkspaceSession: [base] }).lastWorkspaceSession[0],
  126. ).toMatchObject({
  127. workspacePurpose: "project",
  128. });
  129. expect(
  130. appSettingsSchema.parse({
  131. lastWorkspaceSession: [{ ...base, workspacePurpose: "conversation" }],
  132. }).lastWorkspaceSession[0],
  133. ).toMatchObject({ workspacePurpose: "conversation" });
  134. });
  135. it("defaults and validates the human embedded Browser viewport preference", () => {
  136. expect(appSettingsSchema.parse({}).embeddedBrowserViewportPreference).toEqual({
  137. mode: "normal",
  138. viewport: { width: 393, height: 852 },
  139. zoom: "fit",
  140. });
  141. const preference = {
  142. mode: "responsive",
  143. viewport: { width: 412, height: 915 },
  144. zoom: "50",
  145. } as const;
  146. expect(
  147. appSettingsSchema.parse({ embeddedBrowserViewportPreference: preference })
  148. .embeddedBrowserViewportPreference,
  149. ).toEqual(preference);
  150. expect(
  151. appSettingsPatchSchema.parse({ embeddedBrowserViewportPreference: preference })
  152. .embeddedBrowserViewportPreference,
  153. ).toEqual(preference);
  154. for (const embeddedBrowserViewportPreference of [
  155. { ...preference, mode: "agent" },
  156. { ...preference, viewport: { width: 319, height: 915 } },
  157. { ...preference, viewport: { width: 412, height: 2161 } },
  158. { ...preference, zoom: "80" },
  159. ]) {
  160. expect(appSettingsPatchSchema.safeParse({ embeddedBrowserViewportPreference }).success).toBe(
  161. false,
  162. );
  163. }
  164. });
  165. it("drops only a damaged human Browser preference from stored settings", () => {
  166. const settings = appSettingsSchema.parse({
  167. locale: "en-US",
  168. recentProjects: ["/workspace/demo"],
  169. embeddedBrowserViewportPreference: {
  170. mode: "responsive",
  171. viewport: { width: "bad", height: 915 },
  172. zoom: "50",
  173. },
  174. });
  175. expect(settings.locale).toBe("en-US");
  176. expect(settings.recentProjects).toEqual(["/workspace/demo"]);
  177. expect(settings.embeddedBrowserViewportPreference).toEqual({
  178. mode: "normal",
  179. viewport: { width: 393, height: 852 },
  180. zoom: "fit",
  181. });
  182. });
  183. it("defaults ModelIO full retention to off and validates explicit boolean patches", () => {
  184. expect(appSettingsSchema.parse({}).modelIoFullRetentionEnabled).toBe(false);
  185. expect(
  186. appSettingsSchema.parse({ modelIoFullRetentionEnabled: true }).modelIoFullRetentionEnabled,
  187. ).toBe(true);
  188. expect(
  189. appSettingsPatchSchema.parse({ modelIoFullRetentionEnabled: false })
  190. .modelIoFullRetentionEnabled,
  191. ).toBe(false);
  192. expect(appSettingsPatchSchema.safeParse({ modelIoFullRetentionEnabled: "true" }).success).toBe(
  193. false,
  194. );
  195. });
  196. it("drops the removed CUA overlay/pip/background settings keys from stored configs", () => {
  197. // 2026-08-17 设置面收敛:三个 key 已从 schema 删除;存量配置里的旧值被 zod strip 静默忽略,
  198. // 不报错也不回流(overlay/PiP 由 services 装配层恒传 true,见 cuaGhostCursorSettingsWiring)。
  199. const stale = appSettingsSchema.parse({
  200. cuaGhostCursorEnabled: false,
  201. cuaPipMode: false,
  202. cuaBackgroundMode: true,
  203. });
  204. expect("cuaGhostCursorEnabled" in stale).toBe(false);
  205. expect("cuaPipMode" in stale).toBe(false);
  206. expect("cuaBackgroundMode" in stale).toBe(false);
  207. expect(appSettingsPatchSchema.parse({ cuaPipMode: true })).toEqual({});
  208. });
  209. it("persists an explicit Windows integrated terminal shell selection", () => {
  210. const selection = {
  211. mode: "shell",
  212. dialect: "git-bash",
  213. id: "git-bash:C:\\Program Files\\Git\\bin\\bash.exe",
  214. label: "Git Bash",
  215. path: "C:\\Program Files\\Git\\bin\\bash.exe",
  216. } as const;
  217. expect(
  218. appSettingsSchema.parse({ integratedTerminalShell: selection }).integratedTerminalShell,
  219. ).toEqual(selection);
  220. expect(
  221. appSettingsPatchSchema.parse({ integratedTerminalShell: selection }).integratedTerminalShell,
  222. ).toEqual(selection);
  223. });
  224. it("accepts auto mode and rejects incomplete shell selections", () => {
  225. expect(
  226. appSettingsPatchSchema.parse({
  227. integratedTerminalShell: { mode: "auto" },
  228. }).integratedTerminalShell,
  229. ).toEqual({ mode: "auto" });
  230. expect(() =>
  231. appSettingsPatchSchema.parse({
  232. integratedTerminalShell: {
  233. mode: "shell",
  234. dialect: "git-bash",
  235. id: "git-bash",
  236. label: "Git Bash",
  237. },
  238. }),
  239. ).toThrow();
  240. });
  241. it("accepts desktop zoom levels within the supported window range", () => {
  242. expect(appSettingsSchema.parse({ desktopZoomLevel: 5 }).desktopZoomLevel).toBe(5);
  243. expect(appSettingsPatchSchema.parse({ desktopZoomLevel: -3 }).desktopZoomLevel).toBe(-3);
  244. expect(() => appSettingsSchema.parse({ desktopZoomLevel: 6 })).toThrow();
  245. expect(() => appSettingsPatchSchema.parse({ desktopZoomLevel: -4 })).toThrow();
  246. });
  247. it("accepts a bounded desktop main window size and maximized state", () => {
  248. const desktopWindowSize = { width: 1440, height: 900, maximized: true };
  249. expect(appSettingsSchema.parse({ desktopWindowSize }).desktopWindowSize).toEqual(
  250. desktopWindowSize,
  251. );
  252. expect(appSettingsPatchSchema.parse({ desktopWindowSize }).desktopWindowSize).toEqual(
  253. desktopWindowSize,
  254. );
  255. expect(() =>
  256. appSettingsPatchSchema.parse({
  257. desktopWindowSize: { width: 479, height: 900, maximized: false },
  258. }),
  259. ).toThrow();
  260. expect(
  261. appSettingsSchema.parse({
  262. desktopWindowSize: { width: 1440, height: 639, maximized: false },
  263. }).desktopWindowSize,
  264. ).toBeUndefined();
  265. });
  266. it("discards a damaged desktop window size without resetting other settings", () => {
  267. const settings = appSettingsSchema.parse({
  268. locale: "en-US",
  269. recentProjects: ["/workspace/demo"],
  270. desktopWindowSize: { width: "bad", height: 900, maximized: false },
  271. });
  272. expect(settings.locale).toBe("en-US");
  273. expect(settings.recentProjects).toEqual(["/workspace/demo"]);
  274. expect(settings.desktopWindowSize).toBeUndefined();
  275. });
  276. it("defaults desktop Chromium hardware acceleration to enabled and accepts explicit patches", () => {
  277. expect(appSettingsSchema.parse({}).desktopChromiumHardwareAccelerationEnabled).toBe(true);
  278. expect(
  279. appSettingsSchema.parse({
  280. desktopChromiumHardwareAccelerationEnabled: false,
  281. }).desktopChromiumHardwareAccelerationEnabled,
  282. ).toBe(false);
  283. expect(
  284. appSettingsPatchSchema.parse({
  285. desktopChromiumHardwareAccelerationEnabled: true,
  286. }).desktopChromiumHardwareAccelerationEnabled,
  287. ).toBe(true);
  288. expect(() =>
  289. appSettingsPatchSchema.parse({
  290. desktopChromiumHardwareAccelerationEnabled: "false",
  291. }),
  292. ).toThrow();
  293. });
  294. it("preserves localized pending post-update release notes", () => {
  295. const payload = {
  296. version: "3.1.4",
  297. title: "Release v3.1.4",
  298. markdown: "## 新功能\n- 中文说明",
  299. releaseDate: "2026-06-23T13:48:54.438Z",
  300. releaseNotesByLocale: {
  301. "zh-CN": {
  302. title: "ZCode v3.1.4",
  303. markdown: "## 新功能\n- 中文说明",
  304. },
  305. "en-US": {
  306. title: "ZCode v3.1.4",
  307. markdown: "## New Features\n- English notes",
  308. },
  309. },
  310. };
  311. expect(
  312. appSettingsSchema.parse({ pendingPostUpdateReleaseNotes: payload })
  313. .pendingPostUpdateReleaseNotes,
  314. ).toEqual(payload);
  315. expect(
  316. appSettingsPatchSchema.parse({ pendingPostUpdateReleaseNotes: payload })
  317. .pendingPostUpdateReleaseNotes,
  318. ).toEqual(payload);
  319. });
  320. it("persists only stable Web remote control startup context fields", () => {
  321. const context = {
  322. workspacePath: "/workspace/demo",
  323. workspaceIdentity: "remote:ssh:host:/workspace/demo",
  324. initialTaskId: "task-1",
  325. };
  326. expect(
  327. appSettingsSchema.parse({ webRemoteControlLastEnabledContext: context })
  328. .webRemoteControlLastEnabledContext,
  329. ).toEqual(context);
  330. expect(
  331. appSettingsPatchSchema.parse({
  332. webRemoteControlLastEnabledContext: context,
  333. }).webRemoteControlLastEnabledContext,
  334. ).toEqual(context);
  335. expect(
  336. appSettingsPatchSchema.parse({
  337. webRemoteControlLastEnabledContext: {
  338. ...context,
  339. remoteSessionId: "ephemeral-session",
  340. },
  341. }).webRemoteControlLastEnabledContext,
  342. ).toEqual(context);
  343. });
  344. });
  345. // DWG-19(docs/dynamic-workflow/launch.md「The user's choice」):空串是删除选择的哨兵,
  346. // 读文件时的未知取值按「跟随」处理,不能把整份设置读坏。
  347. describe("dynamicWorkflowMode 设置(DWG-19)", () => {
  348. it("补丁接受三个模式与空串哨兵,拒绝其它值", () => {
  349. for (const value of ["disabled", "onDemand", "alwaysOn", ""]) {
  350. expect(appSettingsPatchSchema.parse({ dynamicWorkflowMode: value })).toEqual({
  351. dynamicWorkflowMode: value,
  352. });
  353. }
  354. expect(appSettingsPatchSchema.safeParse({ dynamicWorkflowMode: "bogus" }).success).toBe(false);
  355. });
  356. it("文件里的合法值原样读出,缺席保持缺席,未知值按跟随读成缺席", () => {
  357. expect(appSettingsSchema.parse({ dynamicWorkflowMode: "onDemand" }).dynamicWorkflowMode).toBe(
  358. "onDemand",
  359. );
  360. expect(appSettingsSchema.parse({}).dynamicWorkflowMode).toBeUndefined();
  361. expect(appSettingsSchema.parse({ dynamicWorkflowMode: "future" }).dynamicWorkflowMode).toBe(
  362. undefined,
  363. );
  364. });
  365. });
  366. describe("远程介绍回滚兼容", () => {
  367. // 冻结 e147c3b79a 的相关字段合同;不能复用新版预处理,否则会掩盖旧版拒绝 3 的问题。
  368. const previousSettingsSchema = z.object({
  369. remoteBoundaryOnboardingVersion: z.literal(2).optional(),
  370. recentProjects: z.array(z.string()).default([]),
  371. memoryEnabled: z.boolean().default(false),
  372. });
  373. it.each([undefined, 2])("新确认不扩展旧字段(原值 %s)", (legacyVersion) => {
  374. const patch = appSettingsPatchSchema.parse({ remoteBoundaryIntroAcknowledgedVersion: 1 });
  375. expect(patch).toEqual({ remoteBoundaryIntroAcknowledgedVersion: 1 });
  376. const written = appSettingsSchema.parse({
  377. recentProjects: ["/example/project"],
  378. memoryEnabled: true,
  379. remoteBoundaryOnboardingVersion: legacyVersion,
  380. ...patch,
  381. });
  382. const rollback = previousSettingsSchema.safeParse(written);
  383. expect(rollback.success).toBe(true);
  384. expect(rollback.data).toEqual({
  385. recentProjects: ["/example/project"],
  386. memoryEnabled: true,
  387. remoteBoundaryOnboardingVersion: legacyVersion,
  388. });
  389. });
  390. it("迁移已落盘的 3,同时保留原设置和明确确认事实", () => {
  391. const written = appSettingsSchema.parse({
  392. remoteBoundaryOnboardingVersion: 3,
  393. recentProjects: ["/example/project"],
  394. memoryEnabled: true,
  395. });
  396. expect(written).toMatchObject({
  397. remoteBoundaryOnboardingVersion: 2,
  398. remoteBoundaryIntroAcknowledgedVersion: 1,
  399. recentProjects: ["/example/project"],
  400. memoryEnabled: true,
  401. });
  402. expect(previousSettingsSchema.safeParse(written).success).toBe(true);
  403. expect(appSettingsSchema.parse(written)).toEqual(written);
  404. });
  405. it("旧 2 不代表新版已确认,补丁不能继续写 3", () => {
  406. expect(appSettingsSchema.parse({ remoteBoundaryOnboardingVersion: 2 })).not.toHaveProperty(
  407. "remoteBoundaryIntroAcknowledgedVersion",
  408. );
  409. expect(appSettingsPatchSchema.safeParse({ remoteBoundaryOnboardingVersion: 3 }).success).toBe(
  410. false,
  411. );
  412. });
  413. });