Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
15.6 KB
Raw Blame
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import { once } from "node:events";
import { createServer } from "node:http";
import test from "node:test";
import { fileURLToPath } from "node:url";

const serverPath = fileURLToPath(new URL("./mock-oauth-mcp-server.mjs", import.meta.url));

async function reservePort() {
  const server = createServer();
  server.listen(0, "127.0.0.1");
  await once(server, "listening");
  const address = server.address();
  assert.ok(address && typeof address === "object");
  const closed = once(server, "close");
  server.close();
  await closed;
  return address.port;
}

async function waitFor(check) {
  for (let attempt = 0; attempt < 100; attempt += 1) {
    if (await check()) return;
    await new Promise((resolve) => setTimeout(resolve, 20));
  }
  throw new Error("Timed out waiting for mock OAuth MCP server");
}

async function stop(child) {
  if (child.exitCode !== null) return;
  const exited = once(child, "exit");
  child.kill();
  await exited;
}

test("mock OAuth HTML, Basic auth, and errors keep security boundaries", { timeout: 10_000 }, async (context) => {
  const port = await reservePort();
  const origin = `http://127.0.0.1:${port}`;
  const child = spawn(process.execPath, [serverPath], {
    env: {
      ...process.env,
      AUTO_APPROVE: "0",
      DISABLE_DCR: "1",
      HOST: "127.0.0.1",
      ISSUER: origin,
      MOCK_CLIENT_ID: "test-client",
      MOCK_CLIENT_SECRET: "test-secret",
      PORT: String(port),
    },
    stdio: ["ignore", "pipe", "pipe"],
  });
  context.after(() => stop(child));

  let stderr = "";
  child.stderr.setEncoding("utf8");
  child.stderr.on("data", (chunk) => {
    stderr += chunk;
  });
  await waitFor(async () => {
    try {
      return (await fetch(`${origin}/health`)).ok;
    } catch {
      return false;
    }
  });

  const authorizeUrl = new URL(`${origin}/authorize`);
  authorizeUrl.searchParams.set("client_id", "test-client");
  authorizeUrl.searchParams.set("redirect_uri", `${origin}/callback`);
  authorizeUrl.searchParams.set("scope", "mcp:read");
  authorizeUrl.searchParams.set("state", `"><script>alert("unsafe")</script>`);
  const authorizeResponse = await fetch(authorizeUrl);
  const authorizeHtml = await authorizeResponse.text();
  assert.equal(authorizeResponse.status, 200);
  assert.equal(
    authorizeResponse.headers.get("content-security-policy"),
    "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'",
  );
  const action = authorizeHtml.match(/<form method="post" action="([^"]+)">/)?.[1];
  assert.ok(action);
  assert.match(action, /^\/approve\?/);
  assert.match(action, /&amp;redirect_uri=/);
  assert.doesNotMatch(action, /&redirect_uri=/);

  const credentials = Buffer.from("test-client:test-secret").toString("base64");
  const tokenResponse = await fetch(`${origin}/token`, {
    method: "POST",
    headers: {
      authorization: `Basic ${" ".repeat(4_096)}${credentials}`,
      "content-type": "application/x-www-form-urlencoded",
    },
    body: "grant_type=refresh_token",
  });
  assert.equal(tokenResponse.status, 200);
  const accessToken = (await tokenResponse.json()).access_token;
  assert.equal(typeof accessToken, "string");

  const configured = await fetch(`${origin}/admin/tools`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ tools: [{
      name: "execute_capability",
      description: "A deterministic handoff witness",
      inputSchema: { type: "object", properties: { target: { type: "string" } } },
      result: { content: [{ type: "text", text: "queued" }] },
    }] }),
  });
  assert.equal(configured.status, 200);
  const rpc = async (method, params) => {
    const response = await fetch(`${origin}/mcp`, {
      method: "POST",
      headers: { authorization: `Bearer ${accessToken}`, "content-type": "application/json" },
      body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
    });
    assert.equal(response.status, 200);
    return response.json();
  };
  const discovery = await rpc("server/discover", {});
  assert.equal(discovery.error.code, -32601);
  assert.equal("result" in discovery, false);
  const initialized = await rpc("initialize", { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "test", version: "1" } });
  assert.equal(initialized.result.protocolVersion, "2025-06-18");
  const listed = await rpc("tools/list", {});
  assert.deepEqual(listed.result.tools.map((tool) => tool.name), ["execute_capability"]);
  assert.equal("result" in listed.result.tools[0], false);
  const invoked = await rpc("tools/call", { name: "execute_capability", arguments: { target: "desktop" } });
  assert.equal(invoked.result.content[0].text, "queued");
  const log = await (await fetch(`${origin}/requests`)).json();
  assert.deepEqual(log.requests.flatMap((entry) => entry.toolCalls ?? []).map(({ name, args }) => ({ name, args })), [
    { name: "execute_capability", args: { target: "desktop" } },
  ]);

  const appTool = {
    name: "get_page",
    title: "Get page",
    inputSchema: { type: "object", properties: { cloudId: { type: "string" } }, required: ["cloudId"] },
    _meta: { ui: { resourceUri: "ui://mock/page" } },
    appHtml: "<!doctype html><html><body>Page</body></html>",
    validateRequiredArguments: true,
    result: { content: [{ type: "text", text: "page loaded" }] },
  };
  const appConfigured = await fetch(`${origin}/admin/tools`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ tools: [appTool] }),
  });
  assert.equal(appConfigured.status, 200);
  const appCatalog = await rpc("tools/list", {});
  assert.equal(appCatalog.result.tools[0]._meta.ui.resourceUri, "ui://mock/page");
  assert.equal("appHtml" in appCatalog.result.tools[0], false);
  assert.equal("validateRequiredArguments" in appCatalog.result.tools[0], false);
  const appInitialized = await rpc("initialize", {});
  assert.deepEqual(appInitialized.result.capabilities.resources, {});
  const resource = await rpc("resources/read", { uri: "ui://mock/page" });
  assert.equal(resource.result.contents[0].text, appTool.appHtml);
  assert.equal(resource.result.contents[0].mimeType, "text/html;profile=mcp-app");
  const rejected = await rpc("tools/call", { name: "get_page", arguments: {} });
  assert.equal(rejected.error.code, -32602);
  assert.match(rejected.error.message, /cloudId/);
  assert.equal("result" in rejected, false);
  const recovered = await rpc("tools/call", { name: "get_page", arguments: { cloudId: "workspace" } });
  assert.equal(recovered.result.content[0].text, "page loaded");
  assert.equal("error" in recovered, false);
  const appLog = await (await fetch(`${origin}/requests`)).json();
  assert.deepEqual(appLog.requests.flatMap((entry) => entry.toolCalls ?? [])
    .filter((call) => call.name === "get_page").map((call) => call.args), [{}, { cloudId: "workspace" }]);

  const workload = await fetch(`${origin}/admin/agent-workloads`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ workloads: [{
      promptMarker: "[The user selected @",
      finalReply: "Handoff received",
      finalReasoning: "Checking the handoff result.",
      steps: [{ tool: "execute_capability", arguments: { ignored: true }, argumentsFrom: "computer-mention" }],
    }] }),
  });
  assert.equal(workload.status, 200);
  for (const [target, task] of [["cloud", "Summarize today's notes."], ["desktop", "Review the changed draft."]]) {
    const completion = await fetch(`${origin}/v1/chat/completions`, {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({
        model: "handoff-model",
        messages: [{ role: "user", content: [
          { type: "text", text: `@${target} ${task}` },
          { type: "text", text: `[The user selected @${target}: execute it with target "${target}" and the user's task as prompt.]` },
        ] }],
        tools: [{ type: "function", function: { name: "execute_capability" } }],
      }),
    });
    assert.equal(completion.status, 200);
    const frames = (await completion.text()).split("\n")
      .filter((line) => line.startsWith("data: {")).map((line) => JSON.parse(line.slice(6)));
    const call = frames.flatMap((frame) => frame.choices[0].delta.tool_calls ?? [])[0];
    assert.deepEqual(JSON.parse(call.function.arguments), { name: "remote-session:create", body: { target, prompt: task } });
  }

  const finalCompletion = await fetch(`${origin}/v1/chat/completions`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({
      model: "handoff-model",
      messages: [
        { role: "user", content: "[The user selected @cloud: complete the handoff.]" },
        { role: "tool", content: "queued", tool_call_id: "call_handoff" },
      ],
      tools: [{ type: "function", function: { name: "execute_capability" } }],
    }),
  });
  assert.equal(finalCompletion.status, 200);
  const finalFrames = (await finalCompletion.text()).split("\n")
    .filter((line) => line.startsWith("data: {")).map((line) => JSON.parse(line.slice(6)));
  const deltas = finalFrames.map((frame) => frame.choices[0].delta);
  assert.deepEqual(deltas.filter((delta) => delta.reasoning_content), [{ reasoning_content: "Checking the handoff result." }]);
  assert.deepEqual(deltas.filter((delta) => delta.content), [{ content: "Handoff received" }]);
  assert.ok(deltas.findIndex((delta) => delta.reasoning_content) < deltas.findIndex((delta) => delta.content));

  const discoveryWorkload = await fetch(`${origin}/admin/agent-workloads`, {
    method: "POST", headers: { "content-type": "application/json" },
    body: JSON.stringify({ workloads: [{ promptMarker: "Find the assigned skill", finalReply: "unused fixture reply",
      finalReplyFrom: "last-tool-text", steps: [
        { tool: "search_capabilities", arguments: { query: "Assigned skill" } },
        { tool: "execute_capability", arguments: { body: { limit: 3 } }, argumentsFrom: "capability-search" },
      ],
    }] }),
  });
  assert.equal(discoveryWorkload.status, 200);
  const modelRequest = async (toolResults) => {
    const response = await fetch(`${origin}/v1/chat/completions`, {
      method: "POST", headers: { "content-type": "application/json" },
      body: JSON.stringify({ model: "discovery-model", messages: [
        { role: "user", content: "Find the assigned skill" },
        ...toolResults.map(content => ({ role: "tool", content })),
      ], tools: ["search_capabilities", "execute_capability"].map(name => ({ type: "function", function: { name } })) }),
    });
    const body = await response.text();
    return { status: response.status, frames: body.split("\n").filter(line => line.startsWith("data: {")).map(line => JSON.parse(line.slice(6))) };
  };
  for (const name of ["plugin:first:skill", "plugin:second:skill"]) {
    const result = await modelRequest([JSON.stringify({ matches: [{ name }] })]);
    assert.equal(result.status, 200);
    const call = result.frames.flatMap(frame => frame.choices[0].delta.tool_calls ?? [])[0];
    assert.deepEqual(JSON.parse(call.function.arguments), { name, body: { limit: 3 } });
  }
  const missing = await modelRequest([JSON.stringify({ matches: [] })]);
  assert.equal(missing.status, 500);
  const final = await modelRequest([JSON.stringify({ matches: [{ name: "plugin:first:skill" }] }), "unique text returned by the real tool"]);
  assert.equal(final.status, 200);
  assert.equal(final.frames.map(frame => frame.choices[0].delta.content ?? "").join(""), "unique text returned by the real tool");

  // One workload must infer absence from the model's catalog, not from the
  // test stage. Old catalog entries and ordinary user text cannot resurrect it.
  assert.equal((await fetch(`${origin}/admin/agent-workloads`, {
    method: "POST", headers: { "content-type": "application/json" },
    body: JSON.stringify({ workloads: [{ promptMarker: "Read current instructions", latestUserTurn: true,
      finalReply: "UNAVAILABLE", finalReplyFrom: "last-tool-text",
      steps: [{ tool: "skill", argumentsFrom: "skill-catalog", arguments: { skill: "release-briefing" } }],
    }] }),
  })).status, 200);
  const skillEntry = '<skill><id>release-current</id><name>release-briefing</name><description>Release reports</description></skill>';
  const initial = { role: "system", content: `You are OmniRush.ai.\n<available_skills>${skillEntry}</available_skills>` };
  const update = content => ({ role: "user", content: `<system-update>\n${content.replaceAll("<", "&lt;").replaceAll(">", "&gt;")}\n</system-update>` });
  const removed = update("The following skill IDs are no longer available and must not be used: release-current.");
  for (const [history, available] of [
    [[initial], true], [[initial, removed], false],
    [[initial, removed, update(`New skills are available in addition to those previously listed:\n${skillEntry}`)], true],
    [[initial, update("The available skills have changed. This list supersedes the previous available skills list.\nNo skills are currently available.")], false],
    [[initial, update("Skill guidance is no longer available. Do not use any previously listed skill.")], false],
    [[initial, removed, { role: "user", content: skillEntry }], false],
  ]) {
    const response = await fetch(`${origin}/v1/chat/completions`, {
      method: "POST", headers: { "content-type": "application/json" },
      body: JSON.stringify({ model: "skill-model", messages: [...history, { role: "user", content: "Read current instructions" }],
        tools: [{ type: "function", function: { name: "skill" } }],
      }),
    });
    assert.equal(response.status, 200);
    const frames = (await response.text()).split("\n").filter(line => line.startsWith("data: {")).map(line => JSON.parse(line.slice(6)));
    const calls = frames.flatMap(frame => frame.choices[0].delta.tool_calls ?? []);
    if (available) assert.deepEqual(JSON.parse(calls[0].function.arguments), { id: "release-current" });
    else {
      assert.equal(calls.length, 0);
      assert.equal(frames.map(frame => frame.choices[0].delta.content ?? "").join(""), "UNAVAILABLE");
    }
  }

  const contextWorkload = await fetch(`${origin}/admin/agent-workloads`, {
    method: "POST", headers: { "content-type": "application/json" },
    body: JSON.stringify({ workloads: [{ promptMarker: "Inspect context", finalReply: "unused fixture reply",
      finalReplyFrom: "system-text", latestUserTurn: true, steps: [],
    }] }),
  });
  assert.equal(contextWorkload.status, 200);
  const contextResponse = await fetch(`${origin}/v1/chat/completions`, {
    method: "POST", headers: { "content-type": "application/json" },
    body: JSON.stringify({ model: "context-model", messages: [
      { role: "system", content: "system witness" }, { role: "developer", content: "developer witness" },
      { role: "user", content: "Inspect context; user text must not be echoed" },
    ], tools: [{ type: "function", function: { name: "question" } }] }),
  });
  assert.equal(contextResponse.status, 200);
  const contextFrames = (await contextResponse.text()).split("\n").filter(line => line.startsWith("data: {")).map(line => JSON.parse(line.slice(6)));
  assert.equal(contextFrames.map(frame => frame.choices[0].delta.content ?? "").join(""), "system witness\ndeveloper witness");

  const failedResponse = await fetch(`${origin}/admin/agent-workloads`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: "{",
  });
  assert.equal(failedResponse.status, 500);
  assert.deepEqual(await failedResponse.json(), { error: "internal_server_error" });
  await waitFor(() => stderr.includes("[mock-oauth-mcp] request failed"));
});