| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035 |
- import { beforeEach, describe, expect, it, vi } from "vitest";
- import {
- setMcpStoreDirectoryService,
- setMcpStorePlatform,
- useMcpStore,
- } from "@/store/mcpStore.js";
- import { DEFAULT_MCP_CONFIG, MCP_CONFIG_KEY } from "@/store/mcpStoreHelpers.js";
- import { logger } from "@/logger.js";
- function createLocalStorageStub(initial: Record<string, string> = {}) {
- const values = new Map(Object.entries(initial));
- return {
- getItem: vi.fn((key: string) => values.get(key) ?? null),
- setItem: vi.fn((key: string, value: string) => {
- values.set(key, value);
- }),
- removeItem: vi.fn((key: string) => {
- values.delete(key);
- }),
- };
- }
- describe("mcpStore", () => {
- beforeEach(() => {
- vi.unstubAllGlobals();
- setMcpStoreDirectoryService(null);
- setMcpStorePlatform(null);
- useMcpStore.setState((state) => ({
- ...state,
- config: { ...DEFAULT_MCP_CONFIG },
- nativeServers: [],
- servers: [],
- statusSnapshots: {},
- currentProjectPath: "",
- currentWorkspaceIdentity: undefined,
- enabledStates: {},
- deletedPreloadMcpServers: new Set(),
- isConfigLoaded: false,
- currentSessionId: null,
- }));
- });
- it("无 window 环境时,ensureLoadedForWorkspace 也不会抛错", async () => {
- const globalLike = globalThis as Record<string, unknown>;
- delete globalLike.window;
- await expect(
- useMcpStore.getState().ensureLoadedForWorkspace("/tmp/mcp-workspace"),
- ).resolves.toBe(false);
- });
- it("目录配置读取失败时保持现有 MCP,并返回未加载", async () => {
- vi.stubGlobal("window", {});
- const existingServer = {
- id: "zcodeagentmcp-global-everything",
- name: "everything",
- config: { type: "stdio" as const, command: "node", args: ["server.mjs"] },
- enabled: true,
- status: "connected" as const,
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- toolCount: 1,
- };
- setMcpStorePlatform({
- loadMcpFromUserDirectory: vi.fn(async () => {
- throw new SyntaxError("invalid MCP config");
- }),
- migrateLegacyCommonMcp: vi.fn(async () => ({
- importedCount: 0,
- servers: {},
- skippedCount: 0,
- totalCount: 0,
- })),
- saveMcpToUserDirectory: vi.fn(async () => ({ success: true })),
- });
- useMcpStore.setState({
- currentProjectPath: "/local/workspace",
- isConfigLoaded: true,
- servers: [existingServer],
- });
- await expect(
- useMcpStore.getState().ensureLoadedForWorkspace("/local/workspace"),
- ).resolves.toBe(false);
- expect(useMcpStore.getState().servers).toEqual([existingServer]);
- });
- it("传入目录服务时按当前 workspace 服务加载 MCP", async () => {
- vi.stubGlobal("window", {});
- const platformLoad = vi.fn(async () => ({
- servers: [
- {
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- name: "local-http",
- config: { type: "http" as const, url: "https://local.example.test/mcp" },
- },
- ],
- }));
- setMcpStorePlatform({
- loadMcpFromUserDirectory: platformLoad,
- migrateLegacyCommonMcp: vi.fn(async () => ({
- importedCount: 0,
- servers: {},
- skippedCount: 0,
- totalCount: 0,
- })),
- saveMcpToUserDirectory: vi.fn(async () => ({ success: true })),
- });
- const directoryService = {
- loadMcpFromUserDirectory: vi.fn(async () => ({
- servers: [
- {
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- name: "remote-http",
- config: { type: "http" as const, url: "https://remote.example.test/mcp" },
- },
- ],
- })),
- saveMcpToUserDirectory: vi.fn(async () => {}),
- };
- await useMcpStore
- .getState()
- .ensureLoadedForWorkspace("/remote/workspace", directoryService, "ssh:host:/remote/workspace");
- expect(directoryService.loadMcpFromUserDirectory).toHaveBeenCalledWith({
- workspacePath: "/remote/workspace",
- });
- expect(platformLoad).not.toHaveBeenCalled();
- expect(useMcpStore.getState().servers.map((server) => server.name)).toEqual([
- "remote-http",
- ]);
- });
- it("切换到远程 workspace 时立即清空本地配置并丢弃晚到的旧加载结果", async () => {
- vi.stubGlobal("window", {});
- let resolveLocalLoad!: (value: {
- servers: Array<{
- config: {
- command: string;
- env: Record<string, string>;
- type: "stdio";
- };
- name: string;
- scope: "user";
- source: "zcodeagentmcp";
- }>;
- }) => void;
- const platformLoad = vi.fn(
- () =>
- new Promise<{
- servers: Array<{
- config: {
- command: string;
- env: Record<string, string>;
- type: "stdio";
- };
- name: string;
- scope: "user";
- source: "zcodeagentmcp";
- }>;
- }>((resolve) => {
- resolveLocalLoad = resolve;
- }),
- );
- setMcpStorePlatform({
- loadMcpFromUserDirectory: platformLoad,
- migrateLegacyCommonMcp: vi.fn(async () => ({
- importedCount: 0,
- servers: {},
- skippedCount: 0,
- totalCount: 0,
- })),
- saveMcpToUserDirectory: vi.fn(async () => ({ success: true })),
- });
- const directoryService = {
- loadMcpFromUserDirectory: vi.fn(async () => ({
- servers: [
- {
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- name: "remote-b",
- config: {
- type: "http" as const,
- url: "https://workspace-b.example.test/mcp",
- headers: { "X-Workspace": "workspace-b" },
- },
- },
- ],
- })),
- saveMcpToUserDirectory: vi.fn(async () => {}),
- };
- const loadWorkspaceA = useMcpStore
- .getState()
- .ensureLoadedForWorkspace("/local/workspace-a");
- expect(platformLoad).toHaveBeenCalledTimes(1);
- const loadWorkspaceB = useMcpStore
- .getState()
- .ensureLoadedForWorkspace(
- "/remote/workspace-b",
- directoryService,
- "ssh://dev/remote/workspace-b",
- );
- expect(useMcpStore.getState().servers).toEqual([]);
- resolveLocalLoad({
- servers: [
- {
- source: "zcodeagentmcp",
- scope: "user",
- name: "local-a-secret",
- config: {
- type: "stdio",
- command: "local-secret-server",
- env: { API_KEY: "workspace-a-env-secret" },
- },
- },
- ],
- });
- await Promise.all([loadWorkspaceA, loadWorkspaceB]);
- expect(directoryService.loadMcpFromUserDirectory).toHaveBeenCalledWith({
- workspacePath: "/remote/workspace-b",
- });
- expect(useMcpStore.getState().servers.map((server) => server.name)).toEqual([
- "remote-b",
- ]);
- expect(JSON.stringify(useMcpStore.getState().servers)).not.toContain("workspace-a-");
- });
- it("远端 workspace attachment 绑定前不把预期断连记录成 MCP 警告", async () => {
- vi.stubGlobal("window", {});
- const disconnectedError = Object.assign(
- new Error("ZCODE_REMOTE_WORKSPACE_DISCONNECTED"),
- { code: "ZCODE_REMOTE_WORKSPACE_DISCONNECTED" },
- );
- const directoryService = {
- loadMcpFromUserDirectory: vi.fn(async () => {
- throw disconnectedError;
- }),
- saveMcpToUserDirectory: vi.fn(async () => {}),
- };
- const warn = vi.spyOn(logger, "warn").mockImplementation(() => undefined);
- await useMcpStore
- .getState()
- .ensureLoadedForWorkspace(
- "/remote/workspace",
- directoryService,
- "remote:ssh:host:/remote/workspace",
- );
- expect(warn).not.toHaveBeenCalled();
- warn.mockRestore();
- });
- it("MCP 真实目录读取失败仍保留警告", async () => {
- vi.stubGlobal("window", {});
- const directoryService = {
- loadMcpFromUserDirectory: vi.fn(async () => {
- throw new Error("EACCES: permission denied");
- }),
- saveMcpToUserDirectory: vi.fn(async () => {}),
- };
- const warn = vi.spyOn(logger, "warn").mockImplementation(() => undefined);
- await useMcpStore
- .getState()
- .ensureLoadedForWorkspace(
- "/remote/workspace",
- directoryService,
- "remote:ssh:host:/remote/workspace",
- );
- expect(warn).toHaveBeenCalledWith(
- "[mcpStore] loadMcpFromUserDirectory failed",
- "Error: EACCES: permission denied",
- );
- warn.mockRestore();
- });
- it("本地 workspace 即使存在目录服务也保留 platform 迁移 legacy common MCP", async () => {
- vi.stubGlobal("window", {});
- vi.stubGlobal(
- "localStorage",
- createLocalStorageStub({
- [MCP_CONFIG_KEY]: JSON.stringify({
- mcp: {
- mcpServers: {
- legacyHttp: {
- type: "http",
- url: "https://legacy.example.test/mcp",
- },
- },
- },
- }),
- }),
- );
- const platformLoad = vi
- .fn()
- .mockResolvedValueOnce({ servers: [] })
- .mockResolvedValueOnce({
- servers: [
- {
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- name: "legacyHttp",
- config: {
- type: "http" as const,
- url: "https://legacy.example.test/mcp",
- },
- },
- ],
- });
- const platformSave = vi.fn(async () => ({ success: true }));
- setMcpStorePlatform({
- loadMcpFromUserDirectory: platformLoad,
- migrateLegacyCommonMcp: vi.fn(async () => ({
- importedCount: 0,
- servers: {},
- skippedCount: 0,
- totalCount: 0,
- })),
- saveMcpToUserDirectory: platformSave,
- });
- const directoryService = {
- loadMcpFromUserDirectory: vi.fn(async () => ({
- servers: [
- {
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- name: "remote-http",
- config: { type: "http" as const, url: "https://remote.example.test/mcp" },
- },
- ],
- })),
- saveMcpToUserDirectory: vi.fn(async () => {}),
- };
- setMcpStoreDirectoryService(directoryService);
- await useMcpStore
- .getState()
- .ensureLoadedForWorkspace("/local/workspace", directoryService);
- expect(directoryService.loadMcpFromUserDirectory).not.toHaveBeenCalled();
- expect(platformLoad).toHaveBeenCalledWith({ workspacePath: "/local/workspace" });
- expect(platformSave).toHaveBeenCalledWith({
- action: "upsert",
- source: "zcodeagentmcp",
- name: "legacyHttp",
- config: {
- type: "http",
- url: "https://legacy.example.test/mcp",
- },
- });
- expect(useMcpStore.getState().servers.map((server) => server.name)).toEqual([
- "legacyHttp",
- ]);
- });
- it("批量合并只更新 ZCode Agent MCP 行且清理 changed", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-filesystem",
- name: "filesystem",
- config: { type: "stdio", command: "node", args: ["server.js"] },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "zcodeagentmcp",
- scope: "user",
- },
- {
- id: "claudeclimcp-global-filesystem",
- name: "filesystem",
- config: { type: "stdio", command: "node", args: ["server.js"] },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "claudeclimcp",
- scope: "user",
- },
- ],
- });
- const epoch = useMcpStore.getState().beginServerStatusListRefresh();
- useMcpStore.getState().mergeServerStatusSnapshots(
- {
- filesystem: {
- status: "connected",
- transport: "stdio",
- toolCount: 2,
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- epoch,
- );
- const [zcodeServer, claudeServer] = useMcpStore.getState().servers;
- expect(zcodeServer).toMatchObject({
- status: "connected",
- toolCount: 2,
- changed: false,
- });
- expect(claudeServer).toMatchObject({
- status: "unknown",
- changed: true,
- });
- });
- it("批量状态结果会保留待授权 OAuth URL", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-notion",
- name: "notion",
- config: {
- type: "http",
- url: "https://mcp.notion.example.test/mcp",
- oauth: { type: "authorization_code" },
- },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- const epoch = useMcpStore.getState().beginServerStatusListRefresh();
- useMcpStore.getState().mergeServerStatusSnapshots(
- {
- notion: {
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=state_test",
- startedAt: "2026-06-23T00:00:00.000Z",
- type: "oauth_authorization_code",
- },
- status: "connecting",
- transport: "http",
- toolCount: 0,
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- epoch,
- );
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- authorization: {
- authorizationUrl: "https://auth.example.test/authorize?state=state_test",
- type: "oauth_authorization_code",
- },
- status: "connecting",
- });
- });
- it("status-only 状态结果会 patch merge 并保留其他 MCP snapshot", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-everything",
- name: "everything",
- config: { type: "stdio", command: "npx", args: ["everything"] },
- enabled: true,
- changed: false,
- status: "connected",
- source: "zcodeagentmcp",
- scope: "user",
- toolCount: 9,
- },
- {
- id: "zcodeagentmcp-global-plugin-canva",
- name: "plugin:canva:canva",
- config: { type: "http", url: "https://mcp.canva.example.test/mcp" },
- enabled: true,
- changed: false,
- status: "connecting",
- source: "zcodeagentmcp",
- scope: "user",
- authorization: {
- authorizationUrl: "https://auth.canva.example.test/authorize",
- startedAt: "2026-07-09T00:00:00.000Z",
- type: "oauth_authorization_code",
- },
- },
- ],
- statusSnapshots: {
- everything: {
- status: "connected",
- transport: "stdio",
- toolCount: 9,
- updatedAt: "2026-07-09T00:00:00.000Z",
- },
- "plugin:canva:canva": {
- authorization: {
- authorizationUrl: "https://auth.canva.example.test/authorize",
- startedAt: "2026-07-09T00:00:00.000Z",
- type: "oauth_authorization_code",
- },
- status: "connecting",
- transport: "http",
- toolCount: 0,
- updatedAt: "2026-07-09T00:00:00.000Z",
- },
- },
- });
- const epoch = useMcpStore.getState().beginServerStatusListRefresh("status");
- useMcpStore.getState().mergeServerStatusSnapshots(
- {
- "plugin:canva:canva": {
- status: "connected",
- transport: "http",
- toolCount: 33,
- updatedAt: "2026-07-09T00:00:01.000Z",
- },
- },
- epoch,
- "status",
- );
- expect(useMcpStore.getState().statusSnapshots).toMatchObject({
- everything: {
- status: "connected",
- toolCount: 9,
- },
- "plugin:canva:canva": {
- status: "connected",
- toolCount: 33,
- },
- });
- expect(useMcpStore.getState().servers).toEqual([
- expect.objectContaining({
- name: "everything",
- status: "connected",
- toolCount: 9,
- }),
- expect.objectContaining({
- authorization: undefined,
- name: "plugin:canva:canva",
- status: "connected",
- toolCount: 33,
- }),
- ]);
- });
- it("connect 状态结果仍按全量 replace 处理缺失的连接中 server", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-canva",
- name: "canva",
- config: { type: "http", url: "https://mcp.canva.example.test/mcp" },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- statusSnapshots: {
- canva: {
- status: "connected",
- transport: "http",
- toolCount: 33,
- updatedAt: "2026-07-09T00:00:00.000Z",
- },
- },
- });
- const epoch = useMcpStore.getState().beginServerStatusListRefresh();
- useMcpStore.getState().mergeServerStatusSnapshots({}, epoch, "connect");
- expect(useMcpStore.getState().statusSnapshots).toEqual({});
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- status: "error",
- error: undefined,
- failureKind: "status_unavailable",
- toolCount: undefined,
- });
- });
- it("迟到的批量状态不会覆盖手动检查结果", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-filesystem",
- name: "filesystem",
- config: { type: "stdio", command: "node", args: ["server.js"] },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- const epoch = useMcpStore.getState().beginServerStatusListRefresh();
- useMcpStore.getState().updateServerStatus("zcodeagentmcp-global-filesystem", "error", "manual");
- useMcpStore.getState().mergeServerStatusSnapshots(
- {
- filesystem: {
- status: "connected",
- transport: "stdio",
- toolCount: 2,
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- epoch,
- );
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- status: "error",
- error: "manual",
- });
- });
- it("临时展示态更新不会作废当前批量状态结果", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-filesystem",
- name: "filesystem",
- config: { type: "stdio", command: "node", args: ["server.js"] },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- const epoch = useMcpStore.getState().beginServerStatusListRefresh();
- useMcpStore.getState().updateServerStatus(
- "zcodeagentmcp-global-filesystem",
- "connecting",
- undefined,
- { invalidateStatusListRequests: false },
- );
- useMcpStore.getState().mergeServerStatusSnapshots(
- {
- filesystem: {
- status: "failed",
- transport: "stdio",
- error: "spawn missing ENOENT",
- updatedAt: "2026-06-23T00:00:00.000Z",
- },
- },
- epoch,
- );
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- status: "error",
- error: "spawn missing ENOENT",
- });
- });
- it("批量状态刷新开始时会立即标记待检查的 ZCode Agent MCP 为连接中", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-context7",
- name: "context7",
- config: { type: "stdio", command: "npx", args: ["-y", "@upstash/context7-mcp"], timeoutMs: 3 },
- enabled: true,
- changed: true,
- status: "unknown",
- error: "old error",
- toolCount: 2,
- source: "zcodeagentmcp",
- scope: "user",
- },
- {
- id: "zcodeagentmcp-global-exa",
- name: "exa",
- config: { type: "http", url: "https://mcp.exa.ai/mcp" },
- enabled: true,
- changed: false,
- status: "connected",
- toolCount: 2,
- source: "zcodeagentmcp",
- scope: "user",
- },
- {
- id: "claudeclimcp-global-context7",
- name: "context7",
- config: { type: "stdio", command: "npx", args: ["-y", "@upstash/context7-mcp"], timeoutMs: 3 },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "claudeclimcp",
- scope: "user",
- },
- ],
- });
- useMcpStore.getState().beginServerStatusListRefresh();
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- status: "connecting",
- error: undefined,
- toolCount: undefined,
- });
- expect(useMcpStore.getState().servers[1]).toMatchObject({
- status: "connected",
- toolCount: 2,
- });
- expect(useMcpStore.getState().servers[2]).toMatchObject({
- status: "unknown",
- source: "claudeclimcp",
- });
- });
- it("status-only 刷新开始时只推进 epoch,不改写现有展示态", () => {
- const authorization = {
- authorizationUrl: "https://oauth.example.test/authorize",
- startedAt: "2026-07-10T00:00:00.000Z",
- };
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-pending",
- name: "pending",
- config: { type: "http", url: "https://mcp.example.test/pending" },
- enabled: true,
- changed: true,
- status: "unknown",
- authorization,
- error: "keep display state",
- toolCount: 3,
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- useMcpStore.getState().beginServerStatusListRefresh("status");
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- authorization,
- changed: true,
- error: "keep display state",
- status: "unknown",
- toolCount: 3,
- });
- });
- it("status-only epoch 不会作废同时进行的 connect 结果", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-new-server",
- name: "new-server",
- config: { type: "stdio", command: "new-server" },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- const connectEpoch = useMcpStore.getState().beginServerStatusListRefresh("connect");
- useMcpStore.getState().beginServerStatusListRefresh("status");
- useMcpStore.getState().mergeServerStatusSnapshots(
- {
- "new-server": {
- status: "connected",
- transport: "stdio",
- toolCount: 1,
- updatedAt: "2026-07-10T00:00:00.000Z",
- },
- },
- connectEpoch,
- "connect",
- );
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- changed: false,
- status: "connected",
- toolCount: 1,
- });
- });
- it("批量状态刷新失败时会把当前连接中的 ZCode Agent MCP 收口为错误", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-broken",
- name: "broken",
- config: { type: "stdio", command: "missing-mcp-command" },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- const epoch = useMcpStore.getState().beginServerStatusListRefresh();
- useMcpStore.getState().markServerStatusListRefreshFailed("agent unavailable", epoch);
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- status: "error",
- error: "agent unavailable",
- toolCount: undefined,
- });
- });
- it("status-only 刷新失败时保留 pending snapshot 和现有连接态", () => {
- useMcpStore.setState({
- statusSnapshots: {
- local: {
- status: "connecting",
- transport: "http",
- toolCount: 0,
- updatedAt: "2026-07-10T00:00:00.000Z",
- authorization: {
- authorizationUrl: "https://oauth.example.test/local",
- startedAt: "2026-07-10T00:00:00.000Z",
- },
- },
- "plugin:canva:canva": {
- status: "connecting",
- transport: "http",
- toolCount: 0,
- updatedAt: "2026-07-10T00:00:00.000Z",
- authorization: {
- authorizationUrl: "https://oauth.example.test/plugin",
- startedAt: "2026-07-10T00:00:00.000Z",
- },
- },
- },
- servers: [
- {
- id: "zcodeagentmcp-global-local",
- name: "local",
- config: { type: "http", url: "https://mcp.example.test/local" },
- enabled: true,
- changed: false,
- status: "connecting",
- authorization: {
- authorizationUrl: "https://oauth.example.test/local",
- startedAt: "2026-07-10T00:00:00.000Z",
- },
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- const epoch = useMcpStore.getState().beginServerStatusListRefresh("status");
- useMcpStore
- .getState()
- .markServerStatusListRefreshFailed("temporary status failure", epoch, "status");
- expect(useMcpStore.getState().statusSnapshots).toMatchObject({
- local: {
- authorization: {
- authorizationUrl: "https://oauth.example.test/local",
- },
- },
- "plugin:canva:canva": {
- authorization: {
- authorizationUrl: "https://oauth.example.test/plugin",
- },
- },
- });
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- status: "connecting",
- authorization: {
- authorizationUrl: "https://oauth.example.test/local",
- },
- });
- expect(useMcpStore.getState().servers[0]?.error).toBeUndefined();
- });
- it("批量状态结果缺失当前连接中的 server 时不会一直停在连接中", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-broken",
- name: "broken",
- config: { type: "stdio", command: "missing-mcp-command" },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- const epoch = useMcpStore.getState().beginServerStatusListRefresh();
- useMcpStore.getState().mergeServerStatusSnapshots({}, epoch);
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- status: "error",
- error: undefined,
- failureKind: "status_unavailable",
- toolCount: undefined,
- });
- });
- it("迟到的批量状态刷新失败不会覆盖更新的连接状态", () => {
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-broken",
- name: "broken",
- config: { type: "stdio", command: "missing-mcp-command" },
- enabled: true,
- changed: true,
- status: "unknown",
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- const oldEpoch = useMcpStore.getState().beginServerStatusListRefresh();
- useMcpStore.getState().beginServerStatusListRefresh();
- useMcpStore.getState().markServerStatusListRefreshFailed("old failure", oldEpoch);
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- status: "connecting",
- error: undefined,
- });
- });
- it("更新 MCP 配置后会清理旧健康状态并重新标记 changed", async () => {
- const server = {
- source: "zcodeagentmcp" as const,
- scope: "user" as const,
- name: "context7",
- config: {
- type: "stdio" as const,
- command: "npx",
- args: ["-y", "@upstash/context7-mcp"],
- timeoutMs: 3,
- },
- enabled: true,
- };
- useMcpStore.setState({
- nativeServers: [server],
- servers: [
- {
- id: "zcodeagentmcp-global-context7",
- name: "context7",
- config: server.config,
- enabled: true,
- changed: false,
- status: "error",
- error: "MCP server context7 connection timed out after 3ms",
- source: "zcodeagentmcp",
- scope: "user",
- },
- ],
- });
- await useMcpStore.getState().updateScopedMcpServer("zcodeagentmcp", "context7", {
- ...server.config,
- timeoutMs: 30000,
- });
- expect(useMcpStore.getState().servers[0]).toMatchObject({
- config: expect.objectContaining({ timeoutMs: 30000 }),
- changed: true,
- status: "unknown",
- error: undefined,
- });
- });
- it("切换启用状态会先落盘再更新本地列表", async () => {
- let resolveSave!: () => void;
- const saveMcpToUserDirectory = vi.fn(
- () =>
- new Promise<{ success: true }>((resolve) => {
- resolveSave = () => resolve({ success: true });
- }),
- );
- setMcpStorePlatform({
- loadMcpFromUserDirectory: vi.fn(async () => ({ servers: [] })),
- migrateLegacyCommonMcp: vi.fn(async () => ({
- importedCount: 0,
- servers: {},
- skippedCount: 0,
- totalCount: 0,
- })),
- saveMcpToUserDirectory,
- });
- useMcpStore.setState({
- servers: [
- {
- id: "zcodeagentmcp-global-broken",
- name: "broken",
- config: { type: "stdio", command: "missing-mcp-command" },
- enabled: false,
- source: "zcodeagentmcp",
- scope: "user",
- status: "unknown",
- },
- ],
- });
- const toggle = useMcpStore.getState().toggleServer("zcodeagentmcp-global-broken", true);
- await Promise.resolve();
- expect(useMcpStore.getState().servers[0]?.enabled).toBe(false);
- expect(saveMcpToUserDirectory).toHaveBeenCalledWith(
- expect.objectContaining({
- action: "set-enabled",
- enabled: true,
- name: "broken",
- source: "zcodeagentmcp",
- }),
- );
- resolveSave();
- await toggle;
- expect(useMcpStore.getState().servers[0]?.enabled).toBe(true);
- });
- });
|