| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873 |
- import { describe, expect, it, vi } from "vitest";
- import {
- ZCODE_AGENT_MCP_STATUS_MODE_UNSUPPORTED_ERROR_CODE,
- type IMcpSyncService,
- } from "@zcode/services";
- import {
- buildMcpOAuthAuthorizationStatusRefreshOptions,
- buildPluginMcpServerStatusListKey,
- buildMcpOAuthAuthorizationStatusRefreshServers,
- buildPendingMcpOAuthAuthorizationRefreshKey,
- buildMcpServerStatusListKey,
- createMcpOAuthAuthorizationStatusRefreshDeadline,
- createMcpStatusListRefreshQueue,
- isMcpOAuthAuthorizationStatusRefreshExpired,
- resolveMcpStatusListRefreshSkipReason,
- shouldShowPluginMcpServersInMcpSettings,
- refreshMcpServerStatusList,
- transitionMcpAutoStatusListRefreshKey,
- transitionMcpOAuthAuthorizationPendingRefresh,
- } from "../src/settings/McpSettingsSection.js";
- describe("McpSettingsSection status list", () => {
- it("re-arms automatic refresh after the status key becomes empty", () => {
- const workspaceAKey = "workspace-a\neverything:enabled";
- const first = transitionMcpAutoStatusListRefreshKey("", workspaceAKey);
- expect(first).toEqual({
- lastRefreshKey: workspaceAKey,
- shouldRefresh: true,
- });
- const emptyWorkspace = transitionMcpAutoStatusListRefreshKey(
- first.lastRefreshKey,
- "",
- );
- expect(emptyWorkspace).toEqual({
- lastRefreshKey: "",
- shouldRefresh: false,
- });
- expect(
- transitionMcpAutoStatusListRefreshKey(
- emptyWorkspace.lastRefreshKey,
- workspaceAKey,
- ),
- ).toEqual({
- lastRefreshKey: workspaceAKey,
- shouldRefresh: true,
- });
- });
- it("loads statuses through mcp/list instead of automatic per-server probe", async () => {
- const beginServerStatusListRefresh = vi.fn(() => 7);
- const markServerStatusListRefreshFailed = vi.fn();
- const mergeServerStatusSnapshots = vi.fn();
- const listWorkspaceMcpServerStatuses = vi.fn(async () => ({
- statuses: {
- filesystem: {
- status: "connected",
- transport: "stdio",
- toolCount: 2,
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- }));
- const mcpSyncService = {
- listWorkspaceMcpServerStatuses,
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- await refreshMcpServerStatusList({
- beginServerStatusListRefresh,
- getCurrentWorkspaceKey: () => "ssh://dev/workspace",
- markServerStatusListRefreshFailed,
- mergeServerStatusSnapshots,
- mcpServers: [
- {
- name: "filesystem",
- command: "node",
- args: ["server.mjs"],
- env: [],
- },
- ],
- requestedWorkspaceKey: "ssh://dev/workspace",
- workspaceIdentity: "ssh://dev/workspace",
- workspacePath: "/workspace/app",
- mcpSyncService,
- });
- expect(beginServerStatusListRefresh).toHaveBeenCalledTimes(1);
- expect(beginServerStatusListRefresh).toHaveBeenCalledWith("connect");
- expect(listWorkspaceMcpServerStatuses).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- workspaceIdentity: "ssh://dev/workspace",
- mcpServers: [
- {
- name: "filesystem",
- command: "node",
- args: ["server.mjs"],
- env: [],
- },
- ],
- });
- expect(markServerStatusListRefreshFailed).not.toHaveBeenCalled();
- expect(mergeServerStatusSnapshots).toHaveBeenCalledWith(
- {
- filesystem: {
- status: "connected",
- transport: "stdio",
- toolCount: 2,
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- 7,
- "connect",
- );
- });
- it("marks the current refresh failed when mcp/list rejects", async () => {
- const beginServerStatusListRefresh = vi.fn(() => 9);
- const markServerStatusListRefreshFailed = vi.fn();
- const mergeServerStatusSnapshots = vi.fn();
- const listWorkspaceMcpServerStatuses = vi.fn(async () => {
- throw new Error("agent unavailable");
- });
- const mcpSyncService = {
- listWorkspaceMcpServerStatuses,
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- await expect(
- refreshMcpServerStatusList({
- beginServerStatusListRefresh,
- getCurrentWorkspaceKey: () => "/workspace/app",
- markServerStatusListRefreshFailed,
- mergeServerStatusSnapshots,
- requestedWorkspaceKey: "/workspace/app",
- workspacePath: "/workspace/app",
- mcpSyncService,
- }),
- ).rejects.toThrow("agent unavailable");
- expect(markServerStatusListRefreshFailed).toHaveBeenCalledWith(
- "agent unavailable",
- 9,
- "connect",
- );
- expect(mergeServerStatusSnapshots).not.toHaveBeenCalled();
- });
- it("passes an explicit empty MCP server list to mcp/list", async () => {
- const beginServerStatusListRefresh = vi.fn(() => 11);
- const mergeServerStatusSnapshots = vi.fn();
- const listWorkspaceMcpServerStatuses = vi.fn(async () => ({ statuses: {} }));
- const mcpSyncService = {
- listWorkspaceMcpServerStatuses,
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- await refreshMcpServerStatusList({
- beginServerStatusListRefresh,
- getCurrentWorkspaceKey: () => "/workspace/app",
- mergeServerStatusSnapshots,
- mcpServers: [],
- requestedWorkspaceKey: "/workspace/app",
- workspacePath: "/workspace/app",
- mcpSyncService,
- });
- expect(listWorkspaceMcpServerStatuses).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- mcpServers: [],
- });
- expect(mergeServerStatusSnapshots).toHaveBeenCalledWith({}, 11, "connect");
- });
- it("passes status mode through status-only refreshes", async () => {
- const beginServerStatusListRefresh = vi.fn(() => 12);
- const mergeServerStatusSnapshots = vi.fn();
- const listWorkspaceMcpServerStatuses = vi.fn(async () => ({ statuses: {} }));
- const mcpSyncService = {
- listWorkspaceMcpServerStatuses,
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- await refreshMcpServerStatusList({
- beginServerStatusListRefresh,
- getCurrentWorkspaceKey: () => "/workspace/app",
- mergeServerStatusSnapshots,
- mode: "status",
- mcpServers: [],
- requestedWorkspaceKey: "/workspace/app",
- workspacePath: "/workspace/app",
- mcpSyncService,
- });
- expect(listWorkspaceMcpServerStatuses).toHaveBeenCalledWith({
- workspacePath: "/workspace/app",
- mode: "status",
- mcpServers: [],
- });
- expect(beginServerStatusListRefresh).toHaveBeenCalledWith("status");
- expect(mergeServerStatusSnapshots).toHaveBeenCalledWith({}, 12, "status");
- });
- it("keeps status-only failures isolated from full refresh failure handling", async () => {
- const beginServerStatusListRefresh = vi.fn(() => 13);
- const markServerStatusListRefreshFailed = vi.fn();
- const mergeServerStatusSnapshots = vi.fn();
- const listWorkspaceMcpServerStatuses = vi.fn(async () => {
- throw new Error("temporary status failure");
- });
- const mcpSyncService = {
- listWorkspaceMcpServerStatuses,
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- await expect(
- refreshMcpServerStatusList({
- beginServerStatusListRefresh,
- getCurrentWorkspaceKey: () => "/workspace/app",
- markServerStatusListRefreshFailed,
- mergeServerStatusSnapshots,
- mode: "status",
- mcpServers: [],
- requestedWorkspaceKey: "/workspace/app",
- workspacePath: "/workspace/app",
- mcpSyncService,
- }),
- ).rejects.toThrow("temporary status failure");
- expect(markServerStatusListRefreshFailed).toHaveBeenCalledWith(
- "temporary status failure",
- 13,
- "status",
- );
- expect(mergeServerStatusSnapshots).not.toHaveBeenCalled();
- });
- it("recognizes unsupported status-only capability without recording a refresh failure", async () => {
- const beginServerStatusListRefresh = vi.fn(() => 14);
- const markServerStatusListRefreshFailed = vi.fn();
- const mergeServerStatusSnapshots = vi.fn();
- const listWorkspaceMcpServerStatuses = vi.fn(async () => {
- throw Object.assign(new Error("status-only unsupported"), {
- code: ZCODE_AGENT_MCP_STATUS_MODE_UNSUPPORTED_ERROR_CODE,
- });
- });
- const mcpSyncService = {
- listWorkspaceMcpServerStatuses,
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- await expect(
- refreshMcpServerStatusList({
- beginServerStatusListRefresh,
- getCurrentWorkspaceKey: () => "/workspace/app",
- markServerStatusListRefreshFailed,
- mergeServerStatusSnapshots,
- mode: "status",
- mcpServers: [],
- requestedWorkspaceKey: "/workspace/app",
- workspacePath: "/workspace/app",
- mcpSyncService,
- }),
- ).resolves.toBe("status-mode-unsupported");
- expect(markServerStatusListRefreshFailed).not.toHaveBeenCalled();
- expect(mergeServerStatusSnapshots).not.toHaveBeenCalled();
- });
- it("does not send workspace A credentials through the workspace B service", async () => {
- const listWorkspaceMcpServerStatuses = vi.fn(async () => ({ statuses: {} }));
- const workspaceBService = {
- listWorkspaceMcpServerStatuses,
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- await expect(
- refreshMcpServerStatusList({
- beginServerStatusListRefresh: vi.fn(() => 15),
- getCurrentWorkspaceKey: () => "ssh://dev/remote/workspace-b",
- mergeServerStatusSnapshots: vi.fn(),
- mode: "status",
- mcpServers: [
- {
- headers: [
- { name: "Authorization", value: "Bearer workspace-a-header-secret" },
- ],
- name: "http-a",
- oauth: {
- clientId: "workspace-a-client",
- clientSecret: "workspace-a-oauth-secret",
- type: "authorization_code",
- },
- type: "http",
- url: "https://workspace-a.example.test/mcp",
- },
- ],
- requestedWorkspaceKey: "/local/workspace-a",
- workspacePath: "/local/workspace-a",
- mcpSyncService: workspaceBService,
- }),
- ).resolves.toBe("stale-workspace");
- expect(listWorkspaceMcpServerStatuses).not.toHaveBeenCalled();
- });
- it("discards a legacy-agent error that arrives after switching workspaces", async () => {
- let currentWorkspaceKey = "/local/workspace-a";
- let rejectWorkspaceA!: (error: Error) => void;
- const markServerStatusListRefreshFailed = vi.fn();
- const mergeServerStatusSnapshots = vi.fn();
- const listWorkspaceMcpServerStatuses = vi.fn(
- () =>
- new Promise<never>((_resolve, reject) => {
- rejectWorkspaceA = reject;
- }),
- );
- const mcpSyncService = {
- listWorkspaceMcpServerStatuses,
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- const refresh = refreshMcpServerStatusList({
- beginServerStatusListRefresh: vi.fn(() => 15),
- getCurrentWorkspaceKey: () => currentWorkspaceKey,
- markServerStatusListRefreshFailed,
- mergeServerStatusSnapshots,
- mode: "status",
- mcpServers: [],
- requestedWorkspaceKey: "/local/workspace-a",
- workspacePath: "/local/workspace-a",
- mcpSyncService,
- });
- currentWorkspaceKey = "ssh://dev/remote/workspace-b";
- rejectWorkspaceA(
- Object.assign(new Error("status-only unsupported"), {
- code: ZCODE_AGENT_MCP_STATUS_MODE_UNSUPPORTED_ERROR_CODE,
- }),
- );
- await expect(refresh).resolves.toBe("stale-workspace");
- expect(markServerStatusListRefreshFailed).not.toHaveBeenCalled();
- expect(mergeServerStatusSnapshots).not.toHaveBeenCalled();
- });
- it("dedupes overlapping status refreshes and reruns once with latest input", async () => {
- const queue = createMcpStatusListRefreshQueue();
- let resolveFirst!: () => void;
- const runLatest = vi.fn(async () => {
- if (runLatest.mock.calls.length === 1) {
- await new Promise<void>((resolve) => {
- resolveFirst = resolve;
- });
- }
- });
- const first = queue.request(runLatest);
- const second = queue.request(runLatest);
- const third = queue.request(runLatest);
- await Promise.resolve();
- expect(runLatest).toHaveBeenCalledTimes(1);
- resolveFirst();
- await Promise.all([first, second, third]);
- expect(runLatest).toHaveBeenCalledTimes(2);
- });
- it("reruns overlapping status refresh with the latest runner", async () => {
- const queue = createMcpStatusListRefreshQueue();
- let resolveFirst!: () => void;
- const calls: string[] = [];
- const firstRunner = vi.fn(async () => {
- calls.push("first");
- await new Promise<void>((resolve) => {
- resolveFirst = resolve;
- });
- });
- const secondRunner = vi.fn(async () => {
- calls.push("second");
- });
- const first = queue.request(firstRunner);
- const second = queue.request(secondRunner);
- await Promise.resolve();
- expect(calls).toEqual(["first"]);
- resolveFirst();
- await Promise.all([first, second]);
- expect(calls).toEqual(["first", "second"]);
- expect(firstRunner).toHaveBeenCalledTimes(1);
- expect(secondRunner).toHaveBeenCalledTimes(1);
- });
- it("drops queued and in-flight workspace A refreshes before workspace B can receive secrets", async () => {
- const queue = createMcpStatusListRefreshQueue();
- const workspaceAKey = "/local/workspace-a";
- const workspaceBKey = "ssh://dev/remote/workspace-b";
- let currentWorkspaceKey = workspaceAKey;
- let resolveWorkspaceA!: (value: { statuses: Record<string, never> }) => void;
- const workspaceAService = {
- listWorkspaceMcpServerStatuses: vi.fn(
- () =>
- new Promise<{ statuses: Record<string, never> }>((resolve) => {
- resolveWorkspaceA = resolve;
- }),
- ),
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- const workspaceBService = {
- listWorkspaceMcpServerStatuses: vi.fn(async () => ({
- statuses: {
- "remote-b": {
- status: "connecting" as const,
- toolCount: 0,
- transport: "http" as const,
- updatedAt: "2026-07-10T00:00:00.000Z",
- },
- },
- })),
- } as unknown as Pick<IMcpSyncService, "listWorkspaceMcpServerStatuses">;
- const mergeServerStatusSnapshots = vi.fn();
- const workspaceAServers = [
- {
- args: [],
- command: "local-secret-server",
- env: [{ name: "API_KEY", value: "workspace-a-env-secret" }],
- name: "local-a",
- },
- {
- headers: [{ name: "Authorization", value: "Bearer workspace-a-header-secret" }],
- name: "http-a",
- oauth: {
- clientId: "workspace-a-client",
- clientSecret: "workspace-a-oauth-secret",
- type: "authorization_code" as const,
- },
- type: "http" as const,
- url: "https://workspace-a.example.test/mcp",
- },
- ];
- const workspaceBServers = [
- {
- headers: [{ name: "X-Workspace", value: "workspace-b" }],
- name: "remote-b",
- type: "http" as const,
- url: "https://workspace-b.example.test/mcp",
- },
- ];
- const first = queue.request(async () => {
- await refreshMcpServerStatusList({
- beginServerStatusListRefresh: vi.fn(() => 20),
- getCurrentWorkspaceKey: () => currentWorkspaceKey,
- mergeServerStatusSnapshots,
- mode: "status",
- mcpServers: workspaceAServers,
- requestedWorkspaceKey: workspaceAKey,
- workspacePath: "/local/workspace-a",
- mcpSyncService: workspaceAService,
- });
- });
- expect(workspaceAService.listWorkspaceMcpServerStatuses).toHaveBeenCalledTimes(1);
- currentWorkspaceKey = workspaceBKey;
- const second = queue.request(async () => {
- await refreshMcpServerStatusList({
- beginServerStatusListRefresh: vi.fn(() => 21),
- getCurrentWorkspaceKey: () => currentWorkspaceKey,
- mergeServerStatusSnapshots,
- mode: "status",
- mcpServers: workspaceBServers,
- requestedWorkspaceKey: workspaceBKey,
- workspaceIdentity: workspaceBKey,
- workspacePath: "/remote/workspace-b",
- mcpSyncService: workspaceBService,
- });
- });
- resolveWorkspaceA({ statuses: {} });
- await Promise.all([first, second]);
- expect(workspaceBService.listWorkspaceMcpServerStatuses).toHaveBeenCalledWith({
- mcpServers: workspaceBServers,
- mode: "status",
- workspaceIdentity: workspaceBKey,
- workspacePath: "/remote/workspace-b",
- });
- expect(JSON.stringify(workspaceBService.listWorkspaceMcpServerStatuses.mock.calls)).not.toContain(
- "workspace-a-",
- );
- expect(mergeServerStatusSnapshots).toHaveBeenCalledTimes(1);
- expect(mergeServerStatusSnapshots).toHaveBeenCalledWith(
- {
- "remote-b": {
- status: "connecting",
- toolCount: 0,
- transport: "http",
- updatedAt: "2026-07-10T00:00:00.000Z",
- },
- },
- 21,
- "status",
- );
- });
- it("does not turn workspace A pending state into a workspace B follow-up", () => {
- const workspaceAServer = {
- headers: [{ name: "Authorization", value: "Bearer workspace-a-secret" }],
- name: "http-a",
- type: "http" as const,
- url: "https://workspace-a.example.test/mcp",
- };
- const workspaceBServer = {
- headers: [{ name: "X-Workspace", value: "workspace-b" }],
- name: "http-b",
- type: "http" as const,
- url: "https://workspace-b.example.test/mcp",
- };
- const workspaceAPending = transitionMcpOAuthAuthorizationPendingRefresh({
- activeWorkspaceKey: "/local/workspace-a",
- mcpServers: [workspaceAServer],
- pendingKey: "http-a:started-a",
- previous: null,
- });
- const workspaceBWithoutPending = transitionMcpOAuthAuthorizationPendingRefresh({
- activeWorkspaceKey: "ssh://dev/remote/workspace-b",
- mcpServers: [],
- pendingKey: "",
- previous: workspaceAPending.pending,
- });
- expect(workspaceBWithoutPending).toEqual({ followup: null, pending: null });
- const workspaceBPending = transitionMcpOAuthAuthorizationPendingRefresh({
- activeWorkspaceKey: "ssh://dev/remote/workspace-b",
- mcpServers: [workspaceBServer],
- pendingKey: "http-b:started-b",
- previous: null,
- });
- const workspaceBFollowup = transitionMcpOAuthAuthorizationPendingRefresh({
- activeWorkspaceKey: "ssh://dev/remote/workspace-b",
- mcpServers: [],
- now: () => 123,
- pendingKey: "",
- previous: workspaceBPending.pending,
- });
- expect(workspaceBFollowup.followup).toEqual({
- mcpServers: [workspaceBServer],
- refreshKey: "ssh://dev/remote/workspace-b:http-b:started-b:123",
- workspaceKey: "ssh://dev/remote/workspace-b",
- });
- const workspaceBStatusMerge = transitionMcpOAuthAuthorizationPendingRefresh({
- activeWorkspaceKey: "ssh://dev/remote/workspace-b",
- existingFollowup: workspaceBFollowup.followup,
- mcpServers: [],
- pendingKey: "",
- previous: null,
- });
- expect(workspaceBStatusMerge.followup).toBe(workspaceBFollowup.followup);
- expect(JSON.stringify(workspaceBFollowup)).not.toContain("workspace-a-");
- });
- it("expires pending OAuth status refreshes by wall-clock deadline", () => {
- const startedAt = 1_000;
- const deadline = createMcpOAuthAuthorizationStatusRefreshDeadline(() => startedAt);
- expect(deadline).toBe(startedAt + 5 * 60_000);
- expect(
- isMcpOAuthAuthorizationStatusRefreshExpired(deadline, () => deadline - 1),
- ).toBe(false);
- expect(
- isMcpOAuthAuthorizationStatusRefreshExpired(deadline, () => deadline),
- ).toBe(true);
- expect(
- isMcpOAuthAuthorizationStatusRefreshExpired(deadline, () => deadline + 30_000),
- ).toBe(true);
- });
- it("status list key changes for enable toggles but ignores display status", () => {
- const baseServer = {
- id: "zcodeagentmcp-global-broken",
- name: "broken",
- config: { type: "stdio" as const, command: "missing-mcp-command" },
- enabled: true,
- status: "unknown" as const,
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- };
- const enabledKey = buildMcpServerStatusListKey([baseServer]);
- const connectingKey = buildMcpServerStatusListKey([
- { ...baseServer, status: "connecting" as const },
- ]);
- const disabledKey = buildMcpServerStatusListKey([
- { ...baseServer, enabled: false },
- ]);
- expect(connectingKey).toBe(enabledKey);
- expect(disabledKey).not.toBe(enabledKey);
- });
- it("tracks pending OAuth authorization as a refresh trigger", () => {
- const pendingServer = {
- id: "zcodeagentmcp-global-figma",
- name: "figma",
- config: { type: "http" as const, url: "https://mcp.example.test/mcp" },
- enabled: true,
- status: "connecting" as const,
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=state_test",
- startedAt: "2026-07-08T00:00:00.000Z",
- type: "oauth_authorization_code" as const,
- },
- };
- expect(buildPendingMcpOAuthAuthorizationRefreshKey([pendingServer])).toBe(
- "zcodeagentmcp-global-figma:2026-07-08T00:00:00.000Z",
- );
- expect(
- buildPendingMcpOAuthAuthorizationRefreshKey([
- { ...pendingServer, authorization: undefined },
- ]),
- ).toBe("");
- expect(
- buildPendingMcpOAuthAuthorizationRefreshKey([
- { ...pendingServer, enabled: false },
- ]),
- ).toBe("");
- expect(
- buildPendingMcpOAuthAuthorizationRefreshKey([
- { ...pendingServer, source: "claudeclimcp" as const },
- ]),
- ).toBe("");
- expect(
- buildPendingMcpOAuthAuthorizationRefreshKey([], {
- "plugin:canva:canva": {
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=plugin",
- startedAt: "2026-07-09T00:00:00.000Z",
- type: "oauth_authorization_code",
- },
- status: "connecting",
- toolCount: 0,
- transport: "http",
- updatedAt: "2026-07-09T00:00:00.000Z",
- },
- }),
- ).toBe("plugin:canva:canva:2026-07-09T00:00:00.000Z");
- expect(
- buildPendingMcpOAuthAuthorizationRefreshKey([], {
- canva: {
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=bare",
- startedAt: "2026-07-09T00:00:01.000Z",
- type: "oauth_authorization_code",
- },
- status: "connecting",
- toolCount: 0,
- transport: "http",
- updatedAt: "2026-07-09T00:00:01.000Z",
- },
- }),
- ).toBe("canva:2026-07-09T00:00:01.000Z");
- });
- it("limits OAuth status refresh to local MCP servers that need authorization", () => {
- expect(
- buildMcpOAuthAuthorizationStatusRefreshServers(
- [
- {
- id: "zcodeagentmcp-global-figma",
- name: "figma",
- config: { type: "http" as const, url: "https://mcp.example.test/mcp" },
- enabled: true,
- status: "connecting" as const,
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- },
- {
- id: "zcodeagentmcp-global-everything",
- name: "everything",
- config: {
- type: "stdio" as const,
- command: "npx",
- args: ["@modelcontextprotocol/server-everything"],
- },
- enabled: true,
- status: "connected" as const,
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- },
- ],
- {
- figma: {
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=local",
- startedAt: "2026-07-09T00:00:00.000Z",
- type: "oauth_authorization_code",
- },
- status: "connecting",
- toolCount: 0,
- transport: "http",
- updatedAt: "2026-07-09T00:00:00.000Z",
- },
- "plugin:canva:canva": {
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=plugin",
- startedAt: "2026-07-09T00:00:01.000Z",
- type: "oauth_authorization_code",
- },
- status: "connecting",
- toolCount: 0,
- transport: "http",
- updatedAt: "2026-07-09T00:00:01.000Z",
- },
- },
- ),
- ).toEqual([
- {
- headers: [],
- name: "figma",
- type: "http",
- url: "https://mcp.example.test/mcp",
- },
- ]);
- });
- it("uses an explicit empty local MCP list for plugin-only OAuth refresh", () => {
- expect(
- buildMcpOAuthAuthorizationStatusRefreshServers([], {
- "plugin:canva:canva": {
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=plugin",
- startedAt: "2026-07-09T00:00:01.000Z",
- type: "oauth_authorization_code",
- },
- status: "connecting",
- toolCount: 0,
- transport: "http",
- updatedAt: "2026-07-09T00:00:01.000Z",
- },
- }),
- ).toEqual([]);
- });
- it("builds status-only options for OAuth pending, follow-up and focus refreshes", () => {
- const mcpServers = [
- {
- headers: [],
- name: "canva",
- type: "http" as const,
- url: "https://mcp.canva.example.test/mcp",
- },
- ];
- expect(buildMcpOAuthAuthorizationStatusRefreshOptions(mcpServers)).toEqual({
- mode: "status",
- mcpServers,
- });
- });
- it("includes enabled plugin MCP declarations in the status refresh key", () => {
- expect(
- buildPluginMcpServerStatusListKey([
- {
- commandRootCount: 0,
- declaredMcpServerNames: ["canva"],
- enabled: true,
- id: "canva@zcode-plugins-official",
- marketplace: "zcode-plugins-official",
- mcpServerNames: ["plugin:canva:canva"],
- name: "canva",
- rootPath: "/plugins/canva",
- skillRootCount: 0,
- source: "official",
- },
- ]),
- ).toBe("canva@zcode-plugins-official:enabled:canva:plugin:canva:canva");
- expect(
- buildPluginMcpServerStatusListKey([
- {
- commandRootCount: 0,
- declaredMcpServerNames: ["canva"],
- enabled: false,
- id: "canva@zcode-plugins-official",
- marketplace: "zcode-plugins-official",
- mcpServerNames: ["plugin:canva:canva"],
- name: "canva",
- rootPath: "/plugins/canva",
- skillRootCount: 0,
- source: "official",
- },
- ]),
- ).toBe("");
- });
- it("hides local plugin MCP servers in SSH remote workspace settings", () => {
- expect(shouldShowPluginMcpServersInMcpSettings(true)).toBe(false);
- expect(shouldShowPluginMcpServersInMcpSettings(false)).toBe(true);
- });
- // Bugfix 回归:刷新按钮点了没反应时,必须能区分"没发请求"和"发了但状态没变"。
- // 空 serverStatusListKey 只允许拦住自动刷新,用户手动点击必须真的发出请求。
- describe("refresh skip reasons", () => {
- const readyInput = {
- activeWorkspaceKey: "/workspace",
- activeWorkspacePath: "/workspace",
- configReadyWorkspaceKey: "/workspace",
- isConfigLoaded: true,
- serverStatusListKey: "filesystem:enabled:{}",
- storeWorkspaceKey: "/workspace",
- };
- it("allows a fully aligned refresh from both triggers", () => {
- for (const trigger of ["auto", "manual"] as const) {
- expect(
- resolveMcpStatusListRefreshSkipReason({
- input: readyInput,
- requestedWorkspaceKey: "/workspace",
- trigger,
- }),
- ).toBeUndefined();
- }
- });
- it("only blocks an empty status list key for automatic refreshes", () => {
- const input = { ...readyInput, serverStatusListKey: "" };
- expect(
- resolveMcpStatusListRefreshSkipReason({
- input,
- requestedWorkspaceKey: "/workspace",
- trigger: "auto",
- }),
- ).toBe("empty-status-list-key");
- expect(
- resolveMcpStatusListRefreshSkipReason({
- input,
- requestedWorkspaceKey: "/workspace",
- trigger: "manual",
- }),
- ).toBeUndefined();
- });
- it("keeps workspace alignment guards for manual refreshes", () => {
- expect(
- resolveMcpStatusListRefreshSkipReason({
- input: readyInput,
- requestedWorkspaceKey: "",
- trigger: "manual",
- }),
- ).toBe("no-active-workspace");
- expect(
- resolveMcpStatusListRefreshSkipReason({
- input: { ...readyInput, storeWorkspaceKey: "/other" },
- requestedWorkspaceKey: "/workspace",
- trigger: "manual",
- }),
- ).toBe("mcp-store-not-aligned");
- expect(
- resolveMcpStatusListRefreshSkipReason({
- input: { ...readyInput, configReadyWorkspaceKey: "" },
- requestedWorkspaceKey: "/workspace",
- trigger: "manual",
- }),
- ).toBe("config-not-ready");
- expect(
- resolveMcpStatusListRefreshSkipReason({
- input: { ...readyInput, isConfigLoaded: false },
- requestedWorkspaceKey: "/workspace",
- trigger: "manual",
- }),
- ).toBe("config-not-loaded");
- });
- });
- });
|