appSettings.test.ts 17 KB

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