mcpSettingsSectionStatusList.test.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. ZCODE_AGENT_MCP_STATUS_MODE_UNSUPPORTED_ERROR_CODE,
  4. type IMcpSyncService,
  5. } from "@zcode/services";
  6. import {
  7. buildMcpOAuthAuthorizationStatusRefreshOptions,
  8. buildPluginMcpServerStatusListKey,
  9. buildMcpOAuthAuthorizationStatusRefreshServers,
  10. buildPendingMcpOAuthAuthorizationRefreshKey,
  11. buildMcpServerStatusListKey,
  12. createMcpOAuthAuthorizationStatusRefreshDeadline,
  13. createMcpStatusListRefreshQueue,
  14. isMcpOAuthAuthorizationStatusRefreshExpired,
  15. resolveMcpStatusListRefreshSkipReason,
  16. shouldShowPluginMcpServersInMcpSettings,
  17. refreshMcpServerStatusList,
  18. transitionMcpAutoStatusListRefreshKey,
  19. transitionMcpOAuthAuthorizationPendingRefresh,
  20. } from "../src/settings/McpSettingsSection.js";
  21. describe("McpSettingsSection status list", () => {
  22. it("re-arms automatic refresh after the status key becomes empty", () => {
  23. const workspaceAKey = "workspace-a\neverything:enabled";
  24. const first = transitionMcpAutoStatusListRefreshKey("", workspaceAKey);
  25. expect(first).toEqual({
  26. lastRefreshKey: workspaceAKey,
  27. shouldRefresh: true,
  28. });
  29. const emptyWorkspace = transitionMcpAutoStatusListRefreshKey(
  30. first.lastRefreshKey,
  31. "",
  32. );
  33. expect(emptyWorkspace).toEqual({
  34. lastRefreshKey: "",
  35. shouldRefresh: false,
  36. });
  37. expect(
  38. transitionMcpAutoStatusListRefreshKey(
  39. emptyWorkspace.lastRefreshKey,
  40. workspaceAKey,
  41. ),
  42. ).toEqual({
  43. lastRefreshKey: workspaceAKey,
  44. shouldRefresh: true,
  45. });
  46. });
  47. it("loads statuses through mcp/list instead of automatic per-server probe", async () => {
  48. const beginServerStatusListRefresh = vi.fn(() => 7);
  49. const markServerStatusListRefreshFailed = vi.fn();
  50. const mergeServerStatusSnapshots = vi.fn();
  51. const listWorkspaceMcpServerStatuses = vi.fn(async () => ({
  52. statuses: {
  53. filesystem: {
  54. status: "connected",
  55. transport: "stdio",
  56. toolCount: 2,
  57. updatedAt: "2026-06-23T00:00:00.000Z",
  58. },
  59. },
  60. }));
  61. const mcpSyncService = {
  62. listWorkspaceMcpServerStatuses,
  63. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  64. await refreshMcpServerStatusList({
  65. beginServerStatusListRefresh,
  66. getCurrentWorkspaceKey: () => "ssh://dev/workspace",
  67. markServerStatusListRefreshFailed,
  68. mergeServerStatusSnapshots,
  69. mcpServers: [
  70. {
  71. name: "filesystem",
  72. command: "node",
  73. args: ["server.mjs"],
  74. env: [],
  75. },
  76. ],
  77. requestedWorkspaceKey: "ssh://dev/workspace",
  78. workspaceIdentity: "ssh://dev/workspace",
  79. workspacePath: "/workspace/app",
  80. mcpSyncService,
  81. });
  82. expect(beginServerStatusListRefresh).toHaveBeenCalledTimes(1);
  83. expect(beginServerStatusListRefresh).toHaveBeenCalledWith("connect");
  84. expect(listWorkspaceMcpServerStatuses).toHaveBeenCalledWith({
  85. workspacePath: "/workspace/app",
  86. workspaceIdentity: "ssh://dev/workspace",
  87. mcpServers: [
  88. {
  89. name: "filesystem",
  90. command: "node",
  91. args: ["server.mjs"],
  92. env: [],
  93. },
  94. ],
  95. });
  96. expect(markServerStatusListRefreshFailed).not.toHaveBeenCalled();
  97. expect(mergeServerStatusSnapshots).toHaveBeenCalledWith(
  98. {
  99. filesystem: {
  100. status: "connected",
  101. transport: "stdio",
  102. toolCount: 2,
  103. updatedAt: "2026-06-23T00:00:00.000Z",
  104. },
  105. },
  106. 7,
  107. "connect",
  108. );
  109. });
  110. it("marks the current refresh failed when mcp/list rejects", async () => {
  111. const beginServerStatusListRefresh = vi.fn(() => 9);
  112. const markServerStatusListRefreshFailed = vi.fn();
  113. const mergeServerStatusSnapshots = vi.fn();
  114. const listWorkspaceMcpServerStatuses = vi.fn(async () => {
  115. throw new Error("agent unavailable");
  116. });
  117. const mcpSyncService = {
  118. listWorkspaceMcpServerStatuses,
  119. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  120. await expect(
  121. refreshMcpServerStatusList({
  122. beginServerStatusListRefresh,
  123. getCurrentWorkspaceKey: () => "/workspace/app",
  124. markServerStatusListRefreshFailed,
  125. mergeServerStatusSnapshots,
  126. requestedWorkspaceKey: "/workspace/app",
  127. workspacePath: "/workspace/app",
  128. mcpSyncService,
  129. }),
  130. ).rejects.toThrow("agent unavailable");
  131. expect(markServerStatusListRefreshFailed).toHaveBeenCalledWith(
  132. "agent unavailable",
  133. 9,
  134. "connect",
  135. );
  136. expect(mergeServerStatusSnapshots).not.toHaveBeenCalled();
  137. });
  138. it("passes an explicit empty MCP server list to mcp/list", async () => {
  139. const beginServerStatusListRefresh = vi.fn(() => 11);
  140. const mergeServerStatusSnapshots = vi.fn();
  141. const listWorkspaceMcpServerStatuses = vi.fn(async () => ({ statuses: {} }));
  142. const mcpSyncService = {
  143. listWorkspaceMcpServerStatuses,
  144. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  145. await refreshMcpServerStatusList({
  146. beginServerStatusListRefresh,
  147. getCurrentWorkspaceKey: () => "/workspace/app",
  148. mergeServerStatusSnapshots,
  149. mcpServers: [],
  150. requestedWorkspaceKey: "/workspace/app",
  151. workspacePath: "/workspace/app",
  152. mcpSyncService,
  153. });
  154. expect(listWorkspaceMcpServerStatuses).toHaveBeenCalledWith({
  155. workspacePath: "/workspace/app",
  156. mcpServers: [],
  157. });
  158. expect(mergeServerStatusSnapshots).toHaveBeenCalledWith({}, 11, "connect");
  159. });
  160. it("passes status mode through status-only refreshes", async () => {
  161. const beginServerStatusListRefresh = vi.fn(() => 12);
  162. const mergeServerStatusSnapshots = vi.fn();
  163. const listWorkspaceMcpServerStatuses = vi.fn(async () => ({ statuses: {} }));
  164. const mcpSyncService = {
  165. listWorkspaceMcpServerStatuses,
  166. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  167. await refreshMcpServerStatusList({
  168. beginServerStatusListRefresh,
  169. getCurrentWorkspaceKey: () => "/workspace/app",
  170. mergeServerStatusSnapshots,
  171. mode: "status",
  172. mcpServers: [],
  173. requestedWorkspaceKey: "/workspace/app",
  174. workspacePath: "/workspace/app",
  175. mcpSyncService,
  176. });
  177. expect(listWorkspaceMcpServerStatuses).toHaveBeenCalledWith({
  178. workspacePath: "/workspace/app",
  179. mode: "status",
  180. mcpServers: [],
  181. });
  182. expect(beginServerStatusListRefresh).toHaveBeenCalledWith("status");
  183. expect(mergeServerStatusSnapshots).toHaveBeenCalledWith({}, 12, "status");
  184. });
  185. it("keeps status-only failures isolated from full refresh failure handling", async () => {
  186. const beginServerStatusListRefresh = vi.fn(() => 13);
  187. const markServerStatusListRefreshFailed = vi.fn();
  188. const mergeServerStatusSnapshots = vi.fn();
  189. const listWorkspaceMcpServerStatuses = vi.fn(async () => {
  190. throw new Error("temporary status failure");
  191. });
  192. const mcpSyncService = {
  193. listWorkspaceMcpServerStatuses,
  194. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  195. await expect(
  196. refreshMcpServerStatusList({
  197. beginServerStatusListRefresh,
  198. getCurrentWorkspaceKey: () => "/workspace/app",
  199. markServerStatusListRefreshFailed,
  200. mergeServerStatusSnapshots,
  201. mode: "status",
  202. mcpServers: [],
  203. requestedWorkspaceKey: "/workspace/app",
  204. workspacePath: "/workspace/app",
  205. mcpSyncService,
  206. }),
  207. ).rejects.toThrow("temporary status failure");
  208. expect(markServerStatusListRefreshFailed).toHaveBeenCalledWith(
  209. "temporary status failure",
  210. 13,
  211. "status",
  212. );
  213. expect(mergeServerStatusSnapshots).not.toHaveBeenCalled();
  214. });
  215. it("recognizes unsupported status-only capability without recording a refresh failure", async () => {
  216. const beginServerStatusListRefresh = vi.fn(() => 14);
  217. const markServerStatusListRefreshFailed = vi.fn();
  218. const mergeServerStatusSnapshots = vi.fn();
  219. const listWorkspaceMcpServerStatuses = vi.fn(async () => {
  220. throw Object.assign(new Error("status-only unsupported"), {
  221. code: ZCODE_AGENT_MCP_STATUS_MODE_UNSUPPORTED_ERROR_CODE,
  222. });
  223. });
  224. const mcpSyncService = {
  225. listWorkspaceMcpServerStatuses,
  226. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  227. await expect(
  228. refreshMcpServerStatusList({
  229. beginServerStatusListRefresh,
  230. getCurrentWorkspaceKey: () => "/workspace/app",
  231. markServerStatusListRefreshFailed,
  232. mergeServerStatusSnapshots,
  233. mode: "status",
  234. mcpServers: [],
  235. requestedWorkspaceKey: "/workspace/app",
  236. workspacePath: "/workspace/app",
  237. mcpSyncService,
  238. }),
  239. ).resolves.toBe("status-mode-unsupported");
  240. expect(markServerStatusListRefreshFailed).not.toHaveBeenCalled();
  241. expect(mergeServerStatusSnapshots).not.toHaveBeenCalled();
  242. });
  243. it("does not send workspace A credentials through the workspace B service", async () => {
  244. const listWorkspaceMcpServerStatuses = vi.fn(async () => ({ statuses: {} }));
  245. const workspaceBService = {
  246. listWorkspaceMcpServerStatuses,
  247. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  248. await expect(
  249. refreshMcpServerStatusList({
  250. beginServerStatusListRefresh: vi.fn(() => 15),
  251. getCurrentWorkspaceKey: () => "ssh://dev/remote/workspace-b",
  252. mergeServerStatusSnapshots: vi.fn(),
  253. mode: "status",
  254. mcpServers: [
  255. {
  256. headers: [
  257. { name: "Authorization", value: "Bearer workspace-a-header-secret" },
  258. ],
  259. name: "http-a",
  260. oauth: {
  261. clientId: "workspace-a-client",
  262. clientSecret: "workspace-a-oauth-secret",
  263. type: "authorization_code",
  264. },
  265. type: "http",
  266. url: "https://workspace-a.example.test/mcp",
  267. },
  268. ],
  269. requestedWorkspaceKey: "/local/workspace-a",
  270. workspacePath: "/local/workspace-a",
  271. mcpSyncService: workspaceBService,
  272. }),
  273. ).resolves.toBe("stale-workspace");
  274. expect(listWorkspaceMcpServerStatuses).not.toHaveBeenCalled();
  275. });
  276. it("discards a legacy-agent error that arrives after switching workspaces", async () => {
  277. let currentWorkspaceKey = "/local/workspace-a";
  278. let rejectWorkspaceA!: (error: Error) => void;
  279. const markServerStatusListRefreshFailed = vi.fn();
  280. const mergeServerStatusSnapshots = vi.fn();
  281. const listWorkspaceMcpServerStatuses = vi.fn(
  282. () =>
  283. new Promise<never>((_resolve, reject) => {
  284. rejectWorkspaceA = reject;
  285. }),
  286. );
  287. const mcpSyncService = {
  288. listWorkspaceMcpServerStatuses,
  289. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  290. const refresh = refreshMcpServerStatusList({
  291. beginServerStatusListRefresh: vi.fn(() => 15),
  292. getCurrentWorkspaceKey: () => currentWorkspaceKey,
  293. markServerStatusListRefreshFailed,
  294. mergeServerStatusSnapshots,
  295. mode: "status",
  296. mcpServers: [],
  297. requestedWorkspaceKey: "/local/workspace-a",
  298. workspacePath: "/local/workspace-a",
  299. mcpSyncService,
  300. });
  301. currentWorkspaceKey = "ssh://dev/remote/workspace-b";
  302. rejectWorkspaceA(
  303. Object.assign(new Error("status-only unsupported"), {
  304. code: ZCODE_AGENT_MCP_STATUS_MODE_UNSUPPORTED_ERROR_CODE,
  305. }),
  306. );
  307. await expect(refresh).resolves.toBe("stale-workspace");
  308. expect(markServerStatusListRefreshFailed).not.toHaveBeenCalled();
  309. expect(mergeServerStatusSnapshots).not.toHaveBeenCalled();
  310. });
  311. it("dedupes overlapping status refreshes and reruns once with latest input", async () => {
  312. const queue = createMcpStatusListRefreshQueue();
  313. let resolveFirst!: () => void;
  314. const runLatest = vi.fn(async () => {
  315. if (runLatest.mock.calls.length === 1) {
  316. await new Promise<void>((resolve) => {
  317. resolveFirst = resolve;
  318. });
  319. }
  320. });
  321. const first = queue.request(runLatest);
  322. const second = queue.request(runLatest);
  323. const third = queue.request(runLatest);
  324. await Promise.resolve();
  325. expect(runLatest).toHaveBeenCalledTimes(1);
  326. resolveFirst();
  327. await Promise.all([first, second, third]);
  328. expect(runLatest).toHaveBeenCalledTimes(2);
  329. });
  330. it("reruns overlapping status refresh with the latest runner", async () => {
  331. const queue = createMcpStatusListRefreshQueue();
  332. let resolveFirst!: () => void;
  333. const calls: string[] = [];
  334. const firstRunner = vi.fn(async () => {
  335. calls.push("first");
  336. await new Promise<void>((resolve) => {
  337. resolveFirst = resolve;
  338. });
  339. });
  340. const secondRunner = vi.fn(async () => {
  341. calls.push("second");
  342. });
  343. const first = queue.request(firstRunner);
  344. const second = queue.request(secondRunner);
  345. await Promise.resolve();
  346. expect(calls).toEqual(["first"]);
  347. resolveFirst();
  348. await Promise.all([first, second]);
  349. expect(calls).toEqual(["first", "second"]);
  350. expect(firstRunner).toHaveBeenCalledTimes(1);
  351. expect(secondRunner).toHaveBeenCalledTimes(1);
  352. });
  353. it("drops queued and in-flight workspace A refreshes before workspace B can receive secrets", async () => {
  354. const queue = createMcpStatusListRefreshQueue();
  355. const workspaceAKey = "/local/workspace-a";
  356. const workspaceBKey = "ssh://dev/remote/workspace-b";
  357. let currentWorkspaceKey = workspaceAKey;
  358. let resolveWorkspaceA!: (value: { statuses: Record<string, never> }) => void;
  359. const workspaceAService = {
  360. listWorkspaceMcpServerStatuses: vi.fn(
  361. () =>
  362. new Promise<{ statuses: Record<string, never> }>((resolve) => {
  363. resolveWorkspaceA = resolve;
  364. }),
  365. ),
  366. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  367. const workspaceBService = {
  368. listWorkspaceMcpServerStatuses: vi.fn(async () => ({
  369. statuses: {
  370. "remote-b": {
  371. status: "connecting" as const,
  372. toolCount: 0,
  373. transport: "http" as const,
  374. updatedAt: "2026-07-10T00:00:00.000Z",
  375. },
  376. },
  377. })),
  378. } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
  379. const mergeServerStatusSnapshots = vi.fn();
  380. const workspaceAServers = [
  381. {
  382. args: [],
  383. command: "local-secret-server",
  384. env: [{ name: "API_KEY", value: "workspace-a-env-secret" }],
  385. name: "local-a",
  386. },
  387. {
  388. headers: [{ name: "Authorization", value: "Bearer workspace-a-header-secret" }],
  389. name: "http-a",
  390. oauth: {
  391. clientId: "workspace-a-client",
  392. clientSecret: "workspace-a-oauth-secret",
  393. type: "authorization_code" as const,
  394. },
  395. type: "http" as const,
  396. url: "https://workspace-a.example.test/mcp",
  397. },
  398. ];
  399. const workspaceBServers = [
  400. {
  401. headers: [{ name: "X-Workspace", value: "workspace-b" }],
  402. name: "remote-b",
  403. type: "http" as const,
  404. url: "https://workspace-b.example.test/mcp",
  405. },
  406. ];
  407. const first = queue.request(async () => {
  408. await refreshMcpServerStatusList({
  409. beginServerStatusListRefresh: vi.fn(() => 20),
  410. getCurrentWorkspaceKey: () => currentWorkspaceKey,
  411. mergeServerStatusSnapshots,
  412. mode: "status",
  413. mcpServers: workspaceAServers,
  414. requestedWorkspaceKey: workspaceAKey,
  415. workspacePath: "/local/workspace-a",
  416. mcpSyncService: workspaceAService,
  417. });
  418. });
  419. expect(workspaceAService.listWorkspaceMcpServerStatuses).toHaveBeenCalledTimes(1);
  420. currentWorkspaceKey = workspaceBKey;
  421. const second = queue.request(async () => {
  422. await refreshMcpServerStatusList({
  423. beginServerStatusListRefresh: vi.fn(() => 21),
  424. getCurrentWorkspaceKey: () => currentWorkspaceKey,
  425. mergeServerStatusSnapshots,
  426. mode: "status",
  427. mcpServers: workspaceBServers,
  428. requestedWorkspaceKey: workspaceBKey,
  429. workspaceIdentity: workspaceBKey,
  430. workspacePath: "/remote/workspace-b",
  431. mcpSyncService: workspaceBService,
  432. });
  433. });
  434. resolveWorkspaceA({ statuses: {} });
  435. await Promise.all([first, second]);
  436. expect(workspaceBService.listWorkspaceMcpServerStatuses).toHaveBeenCalledWith({
  437. mcpServers: workspaceBServers,
  438. mode: "status",
  439. workspaceIdentity: workspaceBKey,
  440. workspacePath: "/remote/workspace-b",
  441. });
  442. expect(JSON.stringify(workspaceBService.listWorkspaceMcpServerStatuses.mock.calls)).not.toContain(
  443. "workspace-a-",
  444. );
  445. expect(mergeServerStatusSnapshots).toHaveBeenCalledTimes(1);
  446. expect(mergeServerStatusSnapshots).toHaveBeenCalledWith(
  447. {
  448. "remote-b": {
  449. status: "connecting",
  450. toolCount: 0,
  451. transport: "http",
  452. updatedAt: "2026-07-10T00:00:00.000Z",
  453. },
  454. },
  455. 21,
  456. "status",
  457. );
  458. });
  459. it("does not turn workspace A pending state into a workspace B follow-up", () => {
  460. const workspaceAServer = {
  461. headers: [{ name: "Authorization", value: "Bearer workspace-a-secret" }],
  462. name: "http-a",
  463. type: "http" as const,
  464. url: "https://workspace-a.example.test/mcp",
  465. };
  466. const workspaceBServer = {
  467. headers: [{ name: "X-Workspace", value: "workspace-b" }],
  468. name: "http-b",
  469. type: "http" as const,
  470. url: "https://workspace-b.example.test/mcp",
  471. };
  472. const workspaceAPending = transitionMcpOAuthAuthorizationPendingRefresh({
  473. activeWorkspaceKey: "/local/workspace-a",
  474. mcpServers: [workspaceAServer],
  475. pendingKey: "http-a:started-a",
  476. previous: null,
  477. });
  478. const workspaceBWithoutPending = transitionMcpOAuthAuthorizationPendingRefresh({
  479. activeWorkspaceKey: "ssh://dev/remote/workspace-b",
  480. mcpServers: [],
  481. pendingKey: "",
  482. previous: workspaceAPending.pending,
  483. });
  484. expect(workspaceBWithoutPending).toEqual({ followup: null, pending: null });
  485. const workspaceBPending = transitionMcpOAuthAuthorizationPendingRefresh({
  486. activeWorkspaceKey: "ssh://dev/remote/workspace-b",
  487. mcpServers: [workspaceBServer],
  488. pendingKey: "http-b:started-b",
  489. previous: null,
  490. });
  491. const workspaceBFollowup = transitionMcpOAuthAuthorizationPendingRefresh({
  492. activeWorkspaceKey: "ssh://dev/remote/workspace-b",
  493. mcpServers: [],
  494. now: () => 123,
  495. pendingKey: "",
  496. previous: workspaceBPending.pending,
  497. });
  498. expect(workspaceBFollowup.followup).toEqual({
  499. mcpServers: [workspaceBServer],
  500. refreshKey: "ssh://dev/remote/workspace-b:http-b:started-b:123",
  501. workspaceKey: "ssh://dev/remote/workspace-b",
  502. });
  503. const workspaceBStatusMerge = transitionMcpOAuthAuthorizationPendingRefresh({
  504. activeWorkspaceKey: "ssh://dev/remote/workspace-b",
  505. existingFollowup: workspaceBFollowup.followup,
  506. mcpServers: [],
  507. pendingKey: "",
  508. previous: null,
  509. });
  510. expect(workspaceBStatusMerge.followup).toBe(workspaceBFollowup.followup);
  511. expect(JSON.stringify(workspaceBFollowup)).not.toContain("workspace-a-");
  512. });
  513. it("expires pending OAuth status refreshes by wall-clock deadline", () => {
  514. const startedAt = 1_000;
  515. const deadline = createMcpOAuthAuthorizationStatusRefreshDeadline(() => startedAt);
  516. expect(deadline).toBe(startedAt + 5 * 60_000);
  517. expect(
  518. isMcpOAuthAuthorizationStatusRefreshExpired(deadline, () => deadline - 1),
  519. ).toBe(false);
  520. expect(
  521. isMcpOAuthAuthorizationStatusRefreshExpired(deadline, () => deadline),
  522. ).toBe(true);
  523. expect(
  524. isMcpOAuthAuthorizationStatusRefreshExpired(deadline, () => deadline + 30_000),
  525. ).toBe(true);
  526. });
  527. it("status list key changes for enable toggles but ignores display status", () => {
  528. const baseServer = {
  529. id: "zcodeagentmcp-global-broken",
  530. name: "broken",
  531. config: { type: "stdio" as const, command: "missing-mcp-command" },
  532. enabled: true,
  533. status: "unknown" as const,
  534. source: "zcodeagentmcp" as const,
  535. scope: "user" as const,
  536. };
  537. const enabledKey = buildMcpServerStatusListKey([baseServer]);
  538. const connectingKey = buildMcpServerStatusListKey([
  539. { ...baseServer, status: "connecting" as const },
  540. ]);
  541. const disabledKey = buildMcpServerStatusListKey([
  542. { ...baseServer, enabled: false },
  543. ]);
  544. expect(connectingKey).toBe(enabledKey);
  545. expect(disabledKey).not.toBe(enabledKey);
  546. });
  547. it("tracks pending OAuth authorization as a refresh trigger", () => {
  548. const pendingServer = {
  549. id: "zcodeagentmcp-global-figma",
  550. name: "figma",
  551. config: { type: "http" as const, url: "https://mcp.example.test/mcp" },
  552. enabled: true,
  553. status: "connecting" as const,
  554. source: "zcodeagentmcp" as const,
  555. scope: "user" as const,
  556. authorization: {
  557. authorizationUrl: "https://auth.example.test/authorize?state=state_test",
  558. startedAt: "2026-07-08T00:00:00.000Z",
  559. type: "oauth_authorization_code" as const,
  560. },
  561. };
  562. expect(buildPendingMcpOAuthAuthorizationRefreshKey([pendingServer])).toBe(
  563. "zcodeagentmcp-global-figma:2026-07-08T00:00:00.000Z",
  564. );
  565. expect(
  566. buildPendingMcpOAuthAuthorizationRefreshKey([
  567. { ...pendingServer, authorization: undefined },
  568. ]),
  569. ).toBe("");
  570. expect(
  571. buildPendingMcpOAuthAuthorizationRefreshKey([
  572. { ...pendingServer, enabled: false },
  573. ]),
  574. ).toBe("");
  575. expect(
  576. buildPendingMcpOAuthAuthorizationRefreshKey([
  577. { ...pendingServer, source: "claudeclimcp" as const },
  578. ]),
  579. ).toBe("");
  580. expect(
  581. buildPendingMcpOAuthAuthorizationRefreshKey([], {
  582. "plugin:canva:canva": {
  583. authorization: {
  584. authorizationUrl: "https://auth.example.test/authorize?state=plugin",
  585. startedAt: "2026-07-09T00:00:00.000Z",
  586. type: "oauth_authorization_code",
  587. },
  588. status: "connecting",
  589. toolCount: 0,
  590. transport: "http",
  591. updatedAt: "2026-07-09T00:00:00.000Z",
  592. },
  593. }),
  594. ).toBe("plugin:canva:canva:2026-07-09T00:00:00.000Z");
  595. expect(
  596. buildPendingMcpOAuthAuthorizationRefreshKey([], {
  597. canva: {
  598. authorization: {
  599. authorizationUrl: "https://auth.example.test/authorize?state=bare",
  600. startedAt: "2026-07-09T00:00:01.000Z",
  601. type: "oauth_authorization_code",
  602. },
  603. status: "connecting",
  604. toolCount: 0,
  605. transport: "http",
  606. updatedAt: "2026-07-09T00:00:01.000Z",
  607. },
  608. }),
  609. ).toBe("canva:2026-07-09T00:00:01.000Z");
  610. });
  611. it("limits OAuth status refresh to local MCP servers that need authorization", () => {
  612. expect(
  613. buildMcpOAuthAuthorizationStatusRefreshServers(
  614. [
  615. {
  616. id: "zcodeagentmcp-global-figma",
  617. name: "figma",
  618. config: { type: "http" as const, url: "https://mcp.example.test/mcp" },
  619. enabled: true,
  620. status: "connecting" as const,
  621. source: "zcodeagentmcp" as const,
  622. scope: "user" as const,
  623. },
  624. {
  625. id: "zcodeagentmcp-global-everything",
  626. name: "everything",
  627. config: {
  628. type: "stdio" as const,
  629. command: "npx",
  630. args: ["@modelcontextprotocol/server-everything"],
  631. },
  632. enabled: true,
  633. status: "connected" as const,
  634. source: "zcodeagentmcp" as const,
  635. scope: "user" as const,
  636. },
  637. ],
  638. {
  639. figma: {
  640. authorization: {
  641. authorizationUrl: "https://auth.example.test/authorize?state=local",
  642. startedAt: "2026-07-09T00:00:00.000Z",
  643. type: "oauth_authorization_code",
  644. },
  645. status: "connecting",
  646. toolCount: 0,
  647. transport: "http",
  648. updatedAt: "2026-07-09T00:00:00.000Z",
  649. },
  650. "plugin:canva:canva": {
  651. authorization: {
  652. authorizationUrl: "https://auth.example.test/authorize?state=plugin",
  653. startedAt: "2026-07-09T00:00:01.000Z",
  654. type: "oauth_authorization_code",
  655. },
  656. status: "connecting",
  657. toolCount: 0,
  658. transport: "http",
  659. updatedAt: "2026-07-09T00:00:01.000Z",
  660. },
  661. },
  662. ),
  663. ).toEqual([
  664. {
  665. headers: [],
  666. name: "figma",
  667. type: "http",
  668. url: "https://mcp.example.test/mcp",
  669. },
  670. ]);
  671. });
  672. it("uses an explicit empty local MCP list for plugin-only OAuth refresh", () => {
  673. expect(
  674. buildMcpOAuthAuthorizationStatusRefreshServers([], {
  675. "plugin:canva:canva": {
  676. authorization: {
  677. authorizationUrl: "https://auth.example.test/authorize?state=plugin",
  678. startedAt: "2026-07-09T00:00:01.000Z",
  679. type: "oauth_authorization_code",
  680. },
  681. status: "connecting",
  682. toolCount: 0,
  683. transport: "http",
  684. updatedAt: "2026-07-09T00:00:01.000Z",
  685. },
  686. }),
  687. ).toEqual([]);
  688. });
  689. it("builds status-only options for OAuth pending, follow-up and focus refreshes", () => {
  690. const mcpServers = [
  691. {
  692. headers: [],
  693. name: "canva",
  694. type: "http" as const,
  695. url: "https://mcp.canva.example.test/mcp",
  696. },
  697. ];
  698. expect(buildMcpOAuthAuthorizationStatusRefreshOptions(mcpServers)).toEqual({
  699. mode: "status",
  700. mcpServers,
  701. });
  702. });
  703. it("includes enabled plugin MCP declarations in the status refresh key", () => {
  704. expect(
  705. buildPluginMcpServerStatusListKey([
  706. {
  707. commandRootCount: 0,
  708. declaredMcpServerNames: ["canva"],
  709. enabled: true,
  710. id: "canva@zcode-plugins-official",
  711. marketplace: "zcode-plugins-official",
  712. mcpServerNames: ["plugin:canva:canva"],
  713. name: "canva",
  714. rootPath: "/plugins/canva",
  715. skillRootCount: 0,
  716. source: "official",
  717. },
  718. ]),
  719. ).toBe("canva@zcode-plugins-official:enabled:canva:plugin:canva:canva");
  720. expect(
  721. buildPluginMcpServerStatusListKey([
  722. {
  723. commandRootCount: 0,
  724. declaredMcpServerNames: ["canva"],
  725. enabled: false,
  726. id: "canva@zcode-plugins-official",
  727. marketplace: "zcode-plugins-official",
  728. mcpServerNames: ["plugin:canva:canva"],
  729. name: "canva",
  730. rootPath: "/plugins/canva",
  731. skillRootCount: 0,
  732. source: "official",
  733. },
  734. ]),
  735. ).toBe("");
  736. });
  737. it("hides local plugin MCP servers in SSH remote workspace settings", () => {
  738. expect(shouldShowPluginMcpServersInMcpSettings(true)).toBe(false);
  739. expect(shouldShowPluginMcpServersInMcpSettings(false)).toBe(true);
  740. });
  741. // Bugfix 回归:刷新按钮点了没反应时,必须能区分"没发请求"和"发了但状态没变"。
  742. // 空 serverStatusListKey 只允许拦住自动刷新,用户手动点击必须真的发出请求。
  743. describe("refresh skip reasons", () => {
  744. const readyInput = {
  745. activeWorkspaceKey: "/workspace",
  746. activeWorkspacePath: "/workspace",
  747. configReadyWorkspaceKey: "/workspace",
  748. isConfigLoaded: true,
  749. serverStatusListKey: "filesystem:enabled:{}",
  750. storeWorkspaceKey: "/workspace",
  751. };
  752. it("allows a fully aligned refresh from both triggers", () => {
  753. for (const trigger of ["auto", "manual"] as const) {
  754. expect(
  755. resolveMcpStatusListRefreshSkipReason({
  756. input: readyInput,
  757. requestedWorkspaceKey: "/workspace",
  758. trigger,
  759. }),
  760. ).toBeUndefined();
  761. }
  762. });
  763. it("only blocks an empty status list key for automatic refreshes", () => {
  764. const input = { ...readyInput, serverStatusListKey: "" };
  765. expect(
  766. resolveMcpStatusListRefreshSkipReason({
  767. input,
  768. requestedWorkspaceKey: "/workspace",
  769. trigger: "auto",
  770. }),
  771. ).toBe("empty-status-list-key");
  772. expect(
  773. resolveMcpStatusListRefreshSkipReason({
  774. input,
  775. requestedWorkspaceKey: "/workspace",
  776. trigger: "manual",
  777. }),
  778. ).toBeUndefined();
  779. });
  780. it("keeps workspace alignment guards for manual refreshes", () => {
  781. expect(
  782. resolveMcpStatusListRefreshSkipReason({
  783. input: readyInput,
  784. requestedWorkspaceKey: "",
  785. trigger: "manual",
  786. }),
  787. ).toBe("no-active-workspace");
  788. expect(
  789. resolveMcpStatusListRefreshSkipReason({
  790. input: { ...readyInput, storeWorkspaceKey: "/other" },
  791. requestedWorkspaceKey: "/workspace",
  792. trigger: "manual",
  793. }),
  794. ).toBe("mcp-store-not-aligned");
  795. expect(
  796. resolveMcpStatusListRefreshSkipReason({
  797. input: { ...readyInput, configReadyWorkspaceKey: "" },
  798. requestedWorkspaceKey: "/workspace",
  799. trigger: "manual",
  800. }),
  801. ).toBe("config-not-ready");
  802. expect(
  803. resolveMcpStatusListRefreshSkipReason({
  804. input: { ...readyInput, isConfigLoaded: false },
  805. requestedWorkspaceKey: "/workspace",
  806. trigger: "manual",
  807. }),
  808. ).toBe("config-not-loaded");
  809. });
  810. });
  811. });