settingsNavigation.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. async function loadSettingsNavigation(env: "test" | "production") {
  3. vi.resetModules();
  4. vi.doMock("@zcode/shared", async () => {
  5. const actual = await vi.importActual<typeof import("@zcode/shared")>("@zcode/shared");
  6. return {
  7. ...actual,
  8. ZCODE_ENV: env,
  9. };
  10. });
  11. return import("../src/lib/settingsNavigation.js");
  12. }
  13. afterEach(() => {
  14. vi.resetModules();
  15. vi.unstubAllGlobals();
  16. });
  17. describe("settingsNavigation", () => {
  18. it("保留 usage、hooks 和 memory 分区", async () => {
  19. const navigation = await loadSettingsNavigation("test");
  20. expect(navigation.isSettingsSectionEnabled("usage")).toBe(true);
  21. expect(navigation.resolveSettingsSection("usage", "modelProvider")).toBe("usage");
  22. expect(navigation.isSettingsSectionEnabled("hooks")).toBe(true);
  23. expect(navigation.resolveSettingsSection("hooks", "modelProvider")).toBe("hooks");
  24. expect(navigation.isSettingsSectionEnabled("memory")).toBe(true);
  25. expect(navigation.resolveSettingsSection("memory", "general")).toBe("memory");
  26. });
  27. it("通知已打开的设置页切换到新的分区意图", async () => {
  28. const eventTarget = new EventTarget();
  29. const storage = new Map<string, string>();
  30. vi.stubGlobal("window", {
  31. addEventListener: eventTarget.addEventListener.bind(eventTarget),
  32. removeEventListener: eventTarget.removeEventListener.bind(eventTarget),
  33. dispatchEvent: eventTarget.dispatchEvent.bind(eventTarget),
  34. sessionStorage: {
  35. getItem: (key: string) => storage.get(key) ?? null,
  36. removeItem: (key: string) => storage.delete(key),
  37. setItem: (key: string, value: string) => {
  38. storage.set(key, value);
  39. },
  40. },
  41. });
  42. const navigation = await loadSettingsNavigation("test");
  43. const listener = vi.fn();
  44. const dispose = navigation.addPendingSettingsSectionListener(listener);
  45. navigation.setPendingSettingsSection("general");
  46. expect(listener).toHaveBeenCalledWith("general", {
  47. section: "general",
  48. usageTab: undefined,
  49. modelProviderId: undefined,
  50. });
  51. expect(storage.has("zcode-settings-section-intent")).toBe(false);
  52. dispose();
  53. });
  54. it("Quick Pick 的 MCP、Skill 与 Command 意图直达独立分区", async () => {
  55. const eventTarget = new EventTarget();
  56. const storage = new Map<string, string>();
  57. vi.stubGlobal("window", {
  58. addEventListener: eventTarget.addEventListener.bind(eventTarget),
  59. removeEventListener: eventTarget.removeEventListener.bind(eventTarget),
  60. dispatchEvent: eventTarget.dispatchEvent.bind(eventTarget),
  61. sessionStorage: {
  62. getItem: (key: string) => storage.get(key) ?? null,
  63. removeItem: (key: string) => storage.delete(key),
  64. setItem: (key: string, value: string) => storage.set(key, value),
  65. },
  66. });
  67. const navigation = await loadSettingsNavigation("test");
  68. const listener = vi.fn();
  69. const dispose = navigation.addPendingSettingsSectionListener(listener);
  70. navigation.setPendingSettingsPluginIntent("mcps");
  71. expect(listener).toHaveBeenLastCalledWith(
  72. "mcp",
  73. expect.objectContaining({
  74. section: "mcp",
  75. }),
  76. );
  77. navigation.setPendingSettingsPluginIntent("skills");
  78. expect(listener).toHaveBeenLastCalledWith(
  79. "skill",
  80. expect.objectContaining({ section: "skill" }),
  81. );
  82. navigation.setPendingSettingsPluginIntent("commands");
  83. expect(listener).toHaveBeenLastCalledWith(
  84. "commands",
  85. expect.objectContaining({ section: "commands" }),
  86. );
  87. dispose();
  88. });
  89. it("插件市场管理入口携带一次性的父级来源", async () => {
  90. const eventTarget = new EventTarget();
  91. const storage = new Map<string, string>();
  92. vi.stubGlobal("window", {
  93. addEventListener: eventTarget.addEventListener.bind(eventTarget),
  94. removeEventListener: eventTarget.removeEventListener.bind(eventTarget),
  95. dispatchEvent: eventTarget.dispatchEvent.bind(eventTarget),
  96. sessionStorage: {
  97. getItem: (key: string) => storage.get(key) ?? null,
  98. removeItem: (key: string) => storage.delete(key),
  99. setItem: (key: string, value: string) => storage.set(key, value),
  100. },
  101. });
  102. const navigation = await loadSettingsNavigation("plugin-store-origin");
  103. const workspaceScopeKey = "remote:ssh:host-a:22:user:/workspace";
  104. navigation.setPendingSettingsPluginIntent("plugins", {
  105. origin: "plugin-store",
  106. scopeKey: workspaceScopeKey,
  107. });
  108. expect(navigation.consumePendingSettingsPluginOrigin()).toBe("plugin-store");
  109. expect(navigation.consumePendingSettingsPluginScopeKey()).toBe(workspaceScopeKey);
  110. expect(navigation.consumePendingSettingsPluginScopeKey()).toBe(workspaceScopeKey);
  111. navigation.clearPendingSettingsPluginScopeKey();
  112. expect(navigation.consumePendingSettingsPluginScopeKey()).toBeUndefined();
  113. expect(navigation.consumePendingSettingsPluginOrigin()).toBe("plugin-store");
  114. navigation.clearPendingSettingsPluginOrigin();
  115. expect(navigation.consumePendingSettingsPluginOrigin()).toBeUndefined();
  116. });
  117. it("已打开的设置页收到具体 Workspace scopeKey,事件消费后不会残留到下次挂载", async () => {
  118. const eventTarget = new EventTarget();
  119. const storage = new Map<string, string>();
  120. vi.stubGlobal("window", {
  121. addEventListener: eventTarget.addEventListener.bind(eventTarget),
  122. removeEventListener: eventTarget.removeEventListener.bind(eventTarget),
  123. dispatchEvent: eventTarget.dispatchEvent.bind(eventTarget),
  124. sessionStorage: {
  125. getItem: (key: string) => storage.get(key) ?? null,
  126. removeItem: (key: string) => storage.delete(key),
  127. setItem: (key: string, value: string) => storage.set(key, value),
  128. },
  129. });
  130. const navigation = await loadSettingsNavigation("test");
  131. const listener = vi.fn();
  132. const dispose = navigation.addPendingSettingsSectionListener(listener);
  133. const scopeKey = "remote:ssh:host-b:22:user:/workspace";
  134. navigation.setPendingSettingsPluginIntent("plugins", {
  135. origin: "plugin-store",
  136. scopeKey,
  137. });
  138. expect(listener).toHaveBeenCalledWith(
  139. "plugin",
  140. expect.objectContaining({
  141. pluginOrigin: "plugin-store",
  142. pluginScopeKey: scopeKey,
  143. pluginTab: "plugins",
  144. }),
  145. );
  146. expect(storage.has("zcode-settings-plugin-scope-key-intent")).toBe(false);
  147. expect(navigation.consumePendingSettingsPluginScopeKey()).toBeUndefined();
  148. dispose();
  149. });
  150. it("剩余额度详情入口写入 usage coding plan 跳转意图", async () => {
  151. const eventTarget = new EventTarget();
  152. const storage = new Map<string, string>();
  153. vi.stubGlobal("window", {
  154. addEventListener: eventTarget.addEventListener.bind(eventTarget),
  155. removeEventListener: eventTarget.removeEventListener.bind(eventTarget),
  156. dispatchEvent: eventTarget.dispatchEvent.bind(eventTarget),
  157. sessionStorage: {
  158. getItem: (key: string) => storage.get(key) ?? null,
  159. removeItem: (key: string) => storage.delete(key),
  160. setItem: (key: string, value: string) => {
  161. storage.set(key, value);
  162. },
  163. },
  164. localStorage: {
  165. getItem: () => null,
  166. removeItem: () => undefined,
  167. setItem: () => undefined,
  168. },
  169. });
  170. const navigation = await loadSettingsNavigation("test");
  171. const listener = vi.fn();
  172. const dispose = navigation.addPendingSettingsSectionListener(listener);
  173. navigation.setPendingSettingsUsageCodingPlanIntent();
  174. expect(listener).toHaveBeenCalledWith("usage", {
  175. section: "usage",
  176. usageTab: "codingPlan",
  177. modelProviderId: undefined,
  178. });
  179. expect(navigation.consumePendingSettingsUsageTab()).toBeUndefined();
  180. dispose();
  181. navigation.setPendingSettingsUsageCodingPlanIntent();
  182. expect(navigation.consumeInitialSettingsSection("general")).toBe("usage");
  183. expect(navigation.consumePendingSettingsUsageTab()).toBe("codingPlan");
  184. });
  185. it("provider loading 期间保留 coding plan 使用统计 tab 意图", async () => {
  186. const navigation = await loadSettingsNavigation("test");
  187. expect(
  188. navigation.shouldFallbackSettingsUsageTabToApp({
  189. activeTab: "codingPlan",
  190. checkingCodingPlanTab: false,
  191. loadingModelProviders: true,
  192. showCodingPlanTab: false,
  193. }),
  194. ).toBe(false);
  195. expect(
  196. navigation.shouldFallbackSettingsUsageTabToApp({
  197. activeTab: "codingPlan",
  198. checkingCodingPlanTab: true,
  199. loadingModelProviders: false,
  200. showCodingPlanTab: false,
  201. }),
  202. ).toBe(false);
  203. expect(
  204. navigation.shouldFallbackSettingsUsageTabToApp({
  205. activeTab: "codingPlan",
  206. checkingCodingPlanTab: false,
  207. loadingModelProviders: false,
  208. showCodingPlanTab: false,
  209. }),
  210. ).toBe(true);
  211. });
  212. it("旧版已删除分区意图会回落到安全分区", async () => {
  213. const storage = new Map<string, string>([
  214. ["zcode-settings-section-intent", "legacy-removed-section"],
  215. ]);
  216. vi.stubGlobal("window", {
  217. sessionStorage: {
  218. getItem: (key: string) => storage.get(key) ?? null,
  219. removeItem: (key: string) => storage.delete(key),
  220. setItem: (key: string, value: string) => {
  221. storage.set(key, value);
  222. },
  223. },
  224. });
  225. const navigation = await loadSettingsNavigation("test");
  226. expect(navigation.consumePendingSettingsSection("modelProvider")).toBe("modelProvider");
  227. expect(storage.has("zcode-settings-section-intent")).toBe(false);
  228. });
  229. it("保留 MCP 设置分区为独立入口", async () => {
  230. const sessionStorage = new Map<string, string>();
  231. const localStorage = new Map<string, string>([["zcode-settings-last-section", "mcp"]]);
  232. vi.stubGlobal("window", {
  233. sessionStorage: {
  234. getItem: (key: string) => sessionStorage.get(key) ?? null,
  235. removeItem: (key: string) => sessionStorage.delete(key),
  236. setItem: (key: string, value: string) => {
  237. sessionStorage.set(key, value);
  238. },
  239. },
  240. localStorage: {
  241. getItem: (key: string) => localStorage.get(key) ?? null,
  242. removeItem: (key: string) => localStorage.delete(key),
  243. setItem: (key: string, value: string) => {
  244. localStorage.set(key, value);
  245. },
  246. },
  247. });
  248. const navigation = await loadSettingsNavigation("test");
  249. expect(navigation.consumeInitialSettingsSection("general")).toBe("mcp");
  250. expect(localStorage.get("zcode-settings-last-section")).toBe("mcp");
  251. });
  252. it("把旧 Skills 一次性跳转意图迁移到独立 skill 入口", async () => {
  253. const sessionStorage = new Map<string, string>([["zcode-settings-section-intent", "skills"]]);
  254. vi.stubGlobal("window", {
  255. sessionStorage: {
  256. getItem: (key: string) => sessionStorage.get(key) ?? null,
  257. removeItem: (key: string) => sessionStorage.delete(key),
  258. setItem: (key: string, value: string) => sessionStorage.set(key, value),
  259. },
  260. localStorage: {
  261. getItem: () => null,
  262. removeItem: () => undefined,
  263. setItem: () => undefined,
  264. },
  265. });
  266. const navigation = await loadSettingsNavigation("test");
  267. expect(navigation.consumeInitialSettingsSection("general")).toBe("skill");
  268. });
  269. it("清理不再保留的 customize 分区", async () => {
  270. const localStorage = new Map<string, string>([["zcode-settings-last-section", "customize"]]);
  271. vi.stubGlobal("window", {
  272. localStorage: {
  273. getItem: (key: string) => localStorage.get(key) ?? null,
  274. removeItem: (key: string) => localStorage.delete(key),
  275. setItem: (key: string, value: string) => localStorage.set(key, value),
  276. },
  277. sessionStorage: {
  278. getItem: () => null,
  279. removeItem: () => undefined,
  280. setItem: () => undefined,
  281. },
  282. });
  283. const navigation = await loadSettingsNavigation("test");
  284. expect(navigation.readLastSettingsSectionPreference("general")).toBe("general");
  285. expect(localStorage.has("zcode-settings-last-section")).toBe(false);
  286. });
  287. it("把旧代码预览分区迁移到外观", async () => {
  288. const localStorage = new Map<string, string>([["zcode-settings-last-section", "codePreview"]]);
  289. vi.stubGlobal("window", {
  290. localStorage: {
  291. getItem: (key: string) => localStorage.get(key) ?? null,
  292. removeItem: (key: string) => localStorage.delete(key),
  293. setItem: (key: string, value: string) => localStorage.set(key, value),
  294. },
  295. });
  296. const navigation = await loadSettingsNavigation("test");
  297. expect(navigation.readLastSettingsSectionPreference()).toBe("appearance");
  298. expect(localStorage.get("zcode-settings-last-section")).toBe("appearance");
  299. });
  300. it("显式设置分区意图优先于上次停留分区", async () => {
  301. const sessionStorage = new Map<string, string>([
  302. ["zcode-settings-section-intent", "modelProvider"],
  303. ]);
  304. const localStorage = new Map<string, string>([["zcode-settings-last-section", "mcp"]]);
  305. vi.stubGlobal("window", {
  306. sessionStorage: {
  307. getItem: (key: string) => sessionStorage.get(key) ?? null,
  308. removeItem: (key: string) => sessionStorage.delete(key),
  309. setItem: (key: string, value: string) => {
  310. sessionStorage.set(key, value);
  311. },
  312. },
  313. localStorage: {
  314. getItem: (key: string) => localStorage.get(key) ?? null,
  315. removeItem: (key: string) => localStorage.delete(key),
  316. setItem: (key: string, value: string) => {
  317. localStorage.set(key, value);
  318. },
  319. },
  320. });
  321. const navigation = await loadSettingsNavigation("test");
  322. expect(navigation.consumeInitialSettingsSection("general")).toBe("modelProvider");
  323. expect(sessionStorage.has("zcode-settings-section-intent")).toBe(false);
  324. });
  325. it("记录设置页最后停留分区并清理非法旧值", async () => {
  326. const localStorage = new Map<string, string>([
  327. ["zcode-settings-last-section", "legacy-removed-section"],
  328. ]);
  329. vi.stubGlobal("window", {
  330. localStorage: {
  331. getItem: (key: string) => localStorage.get(key) ?? null,
  332. removeItem: (key: string) => localStorage.delete(key),
  333. setItem: (key: string, value: string) => {
  334. localStorage.set(key, value);
  335. },
  336. },
  337. sessionStorage: {
  338. getItem: () => null,
  339. removeItem: () => undefined,
  340. setItem: () => undefined,
  341. },
  342. });
  343. const navigation = await loadSettingsNavigation("test");
  344. expect(navigation.readLastSettingsSectionPreference("general")).toBe("general");
  345. expect(localStorage.has("zcode-settings-last-section")).toBe(false);
  346. navigation.writeLastSettingsSectionPreference("plugin");
  347. expect(localStorage.get("zcode-settings-last-section")).toBe("plugin");
  348. });
  349. it("保留 hooks 分区的历史偏好", async () => {
  350. const localStorage = new Map<string, string>([["zcode-settings-last-section", "hooks"]]);
  351. vi.stubGlobal("window", {
  352. localStorage: {
  353. getItem: (key: string) => localStorage.get(key) ?? null,
  354. removeItem: (key: string) => localStorage.delete(key),
  355. setItem: (key: string, value: string) => {
  356. localStorage.set(key, value);
  357. },
  358. },
  359. sessionStorage: {
  360. getItem: () => null,
  361. removeItem: () => undefined,
  362. setItem: () => undefined,
  363. },
  364. });
  365. const navigation = await loadSettingsNavigation("test");
  366. expect(navigation.readLastSettingsSectionPreference("general")).toBe("hooks");
  367. navigation.writeLastSettingsSectionPreference("hooks");
  368. expect(localStorage.get("zcode-settings-last-section")).toBe("hooks");
  369. });
  370. });
  371. it("设备卡定位意图支持首次挂载和已打开设置页,并在消费后清理", async () => {
  372. const events = new EventTarget();
  373. const storage = new Map<string, string>();
  374. vi.stubGlobal("window", {
  375. addEventListener: events.addEventListener.bind(events),
  376. removeEventListener: events.removeEventListener.bind(events),
  377. dispatchEvent: events.dispatchEvent.bind(events),
  378. sessionStorage: {
  379. getItem: (key: string) => storage.get(key) ?? null,
  380. setItem: (key: string, value: string) => storage.set(key, value),
  381. removeItem: (key: string) => storage.delete(key),
  382. },
  383. });
  384. const navigation = await loadSettingsNavigation("test");
  385. navigation.setPendingSettingsSectionIntent("remoteDevices", { sourceServerId: "device-a" });
  386. expect(navigation.consumePendingSettingsSourceServerId()).toBe("device-a");
  387. expect(navigation.consumePendingSettingsSourceServerId()).toBeUndefined();
  388. const listener = vi.fn();
  389. const dispose = navigation.addPendingSettingsSectionListener(listener);
  390. navigation.setPendingSettingsSectionIntent("remoteDevices", { sourceServerId: "device-b" });
  391. expect(listener).toHaveBeenCalledWith(
  392. "remoteDevices",
  393. expect.objectContaining({ sourceServerId: "device-b" }),
  394. );
  395. expect(navigation.consumePendingSettingsSourceServerId()).toBeUndefined();
  396. dispose();
  397. });
  398. it("设备定位与失败详情保持独立意图,已挂载事件消费清除存储", async () => {
  399. const eventTarget = new EventTarget();
  400. const storage = new Map<string, string>();
  401. vi.stubGlobal("window", {
  402. addEventListener: eventTarget.addEventListener.bind(eventTarget),
  403. removeEventListener: eventTarget.removeEventListener.bind(eventTarget),
  404. dispatchEvent: eventTarget.dispatchEvent.bind(eventTarget),
  405. sessionStorage: {
  406. getItem: (key: string) => storage.get(key) ?? null,
  407. removeItem: (key: string) => storage.delete(key),
  408. setItem: (key: string, value: string) => storage.set(key, value),
  409. },
  410. });
  411. const navigation = await loadSettingsNavigation("test");
  412. const listener = vi.fn();
  413. const dispose = navigation.addPendingSettingsSectionListener(listener);
  414. navigation.setPendingSettingsSectionIntent("remoteDevices", {
  415. sourceServerId: " source ",
  416. sourceServerDetails: true,
  417. });
  418. expect(listener).toHaveBeenLastCalledWith(
  419. "remoteDevices",
  420. expect.objectContaining({ sourceServerId: "source", sourceServerDetails: true }),
  421. );
  422. expect(storage.size).toBe(0);
  423. navigation.setPendingSettingsSectionIntent("remoteDevices", { sourceServerId: "source" });
  424. expect(listener.mock.calls.at(-1)?.[1].sourceServerDetails).toBeUndefined();
  425. dispose();
  426. navigation.setPendingSettingsSectionIntent("remoteDevices", {
  427. sourceServerId: "source",
  428. sourceServerDetails: true,
  429. });
  430. expect(navigation.consumePendingSettingsSourceServerId()).toBe("source");
  431. expect(navigation.consumePendingSettingsSourceServerDetails()).toBe(true);
  432. expect(navigation.consumePendingSettingsSourceServerDetails()).toBe(false);
  433. navigation.setPendingSettingsSectionIntent("remoteDevices", {
  434. sourceServerId: "source",
  435. sourceServerDetails: true,
  436. });
  437. navigation.setPendingSettingsSectionIntent("general");
  438. expect(navigation.consumePendingSettingsSourceServerDetails()).toBe(false);
  439. });