| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- import { describe, expect, it } from "vitest";
- import {
- convertToZCodeAgentMcpServer,
- isZCodeCuaMcpCommand,
- isZCodeCuaMcpPackageArg,
- } from "../src/mcp.js";
- describe("zcode-cua MCP matcher (single source of truth for services + CLI bootstrap)", () => {
- it("matches the zcode-cua command by leaf name", () => {
- expect(isZCodeCuaMcpCommand("zcode-cua")).toBe(true);
- expect(isZCodeCuaMcpCommand("/opt/homebrew/bin/zcode-cua")).toBe(true);
- expect(isZCodeCuaMcpCommand("uvx")).toBe(false);
- expect(isZCodeCuaMcpCommand("zcode-cua-server")).toBe(false);
- });
- it("matches uvx/pip package spec forms", () => {
- for (const arg of ["zcode-cua", "zcode-cua[macos]", "zcode-cua@v0.1.3", "zcode-cua[macos]@v0.1.3"]) {
- expect(isZCodeCuaMcpPackageArg(arg)).toBe(true);
- }
- });
- it("matches git and local-path forms (the drift that broke desktop/services parity)", () => {
- expect(isZCodeCuaMcpPackageArg("zcode-cua.git")).toBe(true);
- expect(isZCodeCuaMcpPackageArg("git+https://github.com/x/zcode-cua.git@v0.1.3")).toBe(true);
- expect(isZCodeCuaMcpPackageArg("/Users/me/src/zcode-cua")).toBe(true);
- expect(isZCodeCuaMcpPackageArg("C:\\src\\zcode-cua")).toBe(true);
- });
- it("does not match unrelated args", () => {
- for (const arg of ["--from", "run", "zcode-cua-server", "not-zcode-cua", "server.js"]) {
- expect(isZCodeCuaMcpPackageArg(arg)).toBe(false);
- }
- });
- it("recognizes a package spec placed in COMMAND position (command/arg matchers stay consistent)", () => {
- // 之前 command matcher 只认精确叶子 zcode-cua,会漏判把包规格直接当 command 的配置 → 漏注入 broker。
- for (const command of ["zcode-cua@1.2.3", "zcode-cua[macos]", "zcode-cua.git", "/opt/tools/zcode-cua@1.2.3"]) {
- expect(isZCodeCuaMcpCommand(command), command).toBe(true);
- }
- });
- it("recognizes pip == pins and the zcode_cua underscore distribution name (PyPI treats _ == -)", () => {
- for (const arg of ["zcode-cua==1.2.3", "zcode_cua", "zcode_cua==1.2.3", "zcode_cua@v0.1.3"]) {
- expect(isZCodeCuaMcpPackageArg(arg), arg).toBe(true);
- }
- expect(isZCodeCuaMcpCommand("zcode_cua")).toBe(true);
- });
- it("does not false-positive on sibling packages like zcode-cua-proxy", () => {
- for (const value of ["zcode-cua-proxy", "zcode-cua-proxy@1.0.0", "zcode-cua-helper"]) {
- expect(isZCodeCuaMcpCommand(value), value).toBe(false);
- expect(isZCodeCuaMcpPackageArg(value), value).toBe(false);
- }
- });
- it("recognizes `python -m zcode_cua.server` submodule form (M1 fail-open fix)", () => {
- // `python -m zcode_cua.server` → arg 是 `zcode_cua.server`;旧 matcher 漏判 → 不注入 broker → fail-open。
- for (const arg of ["zcode_cua.server", "zcode-cua.server", "zcode_cua.main", "zcode-cua.__main__"]) {
- expect(isZCodeCuaMcpPackageArg(arg), arg).toBe(true);
- }
- });
- it("recognizes local paths with a trailing separator (M1 fail-open fix)", () => {
- // `.../zcode-cua/` 直接取叶子会得到空串 → 漏判 → fail-open。
- expect(isZCodeCuaMcpPackageArg("/Users/me/src/zcode-cua/")).toBe(true);
- expect(isZCodeCuaMcpPackageArg("C:\\src\\zcode-cua\\")).toBe(true);
- expect(isZCodeCuaMcpCommand("/opt/homebrew/bin/zcode-cua/")).toBe(true);
- // 但带尾斜杠的 sibling 仍不误判。
- expect(isZCodeCuaMcpPackageArg("/Users/me/src/zcode-cua-proxy/")).toBe(false);
- });
- });
- describe("MCP config helpers", () => {
- it("passes legacy http_headers through to ZCode Agent MCP servers", () => {
- expect(
- convertToZCodeAgentMcpServer("web-search-prime", {
- type: "http",
- url: "https://bigmodel.example.test/mcp",
- timeoutMs: 5000,
- http_headers: {
- Authorization: "Bearer token",
- },
- }),
- ).toEqual({
- name: "web-search-prime",
- type: "http",
- url: "https://bigmodel.example.test/mcp",
- headers: [{ name: "Authorization", value: "Bearer token" }],
- timeoutMs: 5000,
- });
- });
- it("preserves timeoutMs when converting stdio MCP servers for ZCode Agent", () => {
- expect(
- convertToZCodeAgentMcpServer("context7", {
- type: "stdio",
- command: "npx",
- args: ["-y", "@upstash/context7-mcp"],
- timeoutMs: 3,
- }),
- ).toEqual({
- name: "context7",
- command: "npx",
- args: ["-y", "@upstash/context7-mcp"],
- env: [],
- timeoutMs: 3,
- });
- });
- it("preserves timeoutMs when converting sse MCP servers for ZCode Agent", () => {
- expect(
- convertToZCodeAgentMcpServer("stream", {
- type: "sse",
- url: "https://bigmodel.example.test/sse",
- timeoutMs: 8000,
- }),
- ).toEqual({
- name: "stream",
- type: "sse",
- url: "https://bigmodel.example.test/sse",
- headers: [],
- timeoutMs: 8000,
- });
- });
- it("preserves HTTP OAuth client credentials when converting MCP servers for ZCode Agent", () => {
- expect(
- convertToZCodeAgentMcpServer("protected", {
- type: "http",
- url: "https://mcp.example.test/mcp",
- oauth: {
- type: "client_credentials",
- clientId: "zcode-client",
- clientSecret: "secret",
- scope: "mcp:tools",
- },
- }),
- ).toMatchObject({
- name: "protected",
- type: "http",
- oauth: {
- type: "client_credentials",
- clientId: "zcode-client",
- clientSecret: "secret",
- scope: "mcp:tools",
- },
- });
- });
- it("preserves HTTP OAuth authorization code config when converting MCP servers for ZCode Agent", () => {
- expect(
- convertToZCodeAgentMcpServer("figma", {
- type: "http",
- url: "https://mcp.figma.com/mcp",
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- redirectPath: "/oauth/callback/mcp/figma",
- scope: "mcp:connect",
- },
- }),
- ).toMatchObject({
- name: "figma",
- type: "http",
- oauth: {
- type: "authorization_code",
- clientName: "ZCode",
- redirectPath: "/oauth/callback/mcp/figma",
- scope: "mcp:connect",
- },
- });
- });
- it("does not persist implicit OAuth defaults when converting bare HTTP MCP servers", () => {
- expect(
- convertToZCodeAgentMcpServer("notion", {
- type: "http",
- url: "https://mcp.notion.com/mcp",
- timeoutMs: 30000,
- }),
- ).toEqual({
- name: "notion",
- type: "http",
- url: "https://mcp.notion.com/mcp",
- headers: [],
- timeoutMs: 30000,
- });
- });
- it("drops invalid timeoutMs when converting MCP servers for ZCode Agent", () => {
- expect(
- convertToZCodeAgentMcpServer("zero", {
- type: "stdio",
- command: "npx",
- timeoutMs: 0,
- }),
- ).not.toHaveProperty("timeoutMs");
- expect(
- convertToZCodeAgentMcpServer("fractional", {
- type: "http",
- url: "https://bigmodel.example.test/mcp",
- timeoutMs: 1.5,
- }),
- ).not.toHaveProperty("timeoutMs");
- });
- it("keeps headers ahead of legacy http_headers when both are present", () => {
- expect(
- convertToZCodeAgentMcpServer("web-reader", {
- type: "sse",
- url: "https://bigmodel.example.test/sse",
- headers: {
- Authorization: "Bearer fresh",
- },
- http_headers: {
- Authorization: "Bearer stale",
- },
- }),
- ).toMatchObject({
- headers: [{ name: "Authorization", value: "Bearer fresh" }],
- });
- });
- });
|