Skip to content

An open-source, faster Devin alternative

TypeScript
18.7 KB
Raw Blame
import assert from "node:assert/strict";
import test from "node:test";

import { APIError, OpenComputerClient } from "./api.js";

test("project service connections use project-scoped attachment routes", async (context) => {
  const requests: Request[] = [];
  context.mock.method(
    globalThis,
    "fetch",
    async (input: string | URL | Request, init?: RequestInit) => {
      const request = new Request(input, init);
      requests.push(request);
      if (request.method === "GET") return Response.json({ attachments: [] });
      if (request.method === "DELETE") return new Response(null, { status: 204 });
      return Response.json({
        projectId: "prj_1",
        service: "linear",
        provider: "linear",
        label: "default",
        connectionId: "conn_1",
        connection: { id: "conn_1", provider: "linear", label: "default", status: "connected" },
        createdAt: "2026-10-07T00:00:00.000Z",
        updatedAt: "2026-10-07T00:00:00.000Z",
      });
    },
  );
  const client = new OpenComputerClient({
    apiUrl: "https://app.opencomputer.dev",
    apiKey: "test",
  }, "setup");

  await client.projectServiceConnections("prj_1");
  await client.attachProjectServiceConnection({
    projectId: "prj_1",
    connectionId: "conn_1",
  });
  await client.attachProjectServiceConnection({
    projectId: "prj_1",
    connectionId: "conn_2",
  });
  await client.detachProjectServiceConnection({
    projectId: "prj_1",
    connectionId: "conn_1",
  });
  await client.detachProjectServiceConnection({
    projectId: "prj_1",
    connectionId: "conn_2",
  });

  assert.deepEqual(
    requests.map((request) => [request.method, new URL(request.url).pathname]),
    [
      ["GET", "/api/managed-agents/projects/prj_1/service-connections"],
      ["POST", "/api/managed-agents/projects/prj_1/service-connections"],
      ["POST", "/api/managed-agents/projects/prj_1/service-connections"],
      ["DELETE", "/api/managed-agents/projects/prj_1/service-connections"],
      ["DELETE", "/api/managed-agents/projects/prj_1/service-connections"],
    ],
  );
  assert.deepEqual(await requests[1]!.json(), { connectionId: "conn_1" });
  assert.deepEqual(await requests[2]!.json(), { connectionId: "conn_2" });
  assert.deepEqual(await requests[3]!.json(), { connectionId: "conn_1" });
  assert.deepEqual(await requests[4]!.json(), { connectionId: "conn_2" });
  assert.notEqual(
    requests[1]!.headers.get("idempotency-key"),
    requests[2]!.headers.get("idempotency-key"),
  );
  assert.notEqual(
    requests[3]!.headers.get("idempotency-key"),
    requests[4]!.headers.get("idempotency-key"),
  );
});

test("reconnect asks the backend to preserve and reauthorize the logical connection", async (context) => {
  let request: Request | undefined;
  context.mock.method(
    globalThis,
    "fetch",
    async (input: string | URL | Request, init?: RequestInit) => {
      request = new Request(input, init);
      return Response.json({
        service: "linear",
        label: "engineering",
        status: "pending",
        connectionId: "conn_1",
        authorizationUrl: "https://connect.example/linear",
      });
    },
  );
  const client = new OpenComputerClient({
    apiUrl: "https://app.opencomputer.dev",
    apiKey: "test",
  });

  await client.linkServiceConnection({
    service: "linear",
    label: "engineering",
    reconnect: true,
  });

  assert.equal(
    new URL(request!.url).pathname,
    "/api/managed-agents/connections/linear/link",
  );
  assert.deepEqual(await request!.json(), {
    service: "linear",
    label: "engineering",
    reconnect: true,
  });
});

// Review reproduction (U6): the header names the operation the caller's key
// stands for, not the request it was sent with. The body is the backend's
// to compare; hashing it here turned a retry with different inputs into a
// second resource instead of the conflict the key promises.
test("mutations derive idempotency headers from the caller's key and the target only", async (context) => {
  const requests: Request[] = [];
  context.mock.method(
    globalThis,
    "fetch",
    async (input: string | URL | Request, init?: RequestInit) => {
      const request = new Request(input, init);
      requests.push(request);
      return Response.json({
        id: "project",
        agents: [],
        environments: [],
        session: { id: "ses" },
      });
    },
  );
  const client = new OpenComputerClient(
    { apiUrl: "https://app.opencomputer.dev", apiKey: "test" },
    "retry-42",
  );

  await client.createProject("Agent", "agent");
  await client.createProject("Agent", "agent");
  await client.createProject("Different", "different");
  await client.createSession("muse@development");
  await client.projects();

  const keys = requests.map((request) =>
    request.headers.get("idempotency-key"),
  );
  assert.ok(keys[0]);
  assert.equal(keys[0], keys[1]);
  assert.equal(keys[0], keys[2], "a different body is the same operation");
  assert.notEqual(
    keys[0],
    keys[3],
    "a different target is a different operation",
  );
  assert.equal(keys[4], null, "reads carry no key");
  assert.equal(keys[0]?.includes("retry-42"), false);
});

test("workspace exports derive one idempotency key per workspace path", async (context) => {
  const requests: Request[] = [];
  const artifact = {
    id: "wsart_1",
    sessionId: "ses",
    path: "a.txt",
    size: 1,
    sha256: "0".repeat(64),
    receipt: { etag: null, sourceEtag: null, sourceVersionId: null },
    exportedAt: "2026-09-10T12:00:00.000Z",
  };
  context.mock.method(
    globalThis,
    "fetch",
    async (input: string | URL | Request, init?: RequestInit) => {
      const request = new Request(input, init);
      requests.push(request);
      return Response.json({ export: null, artifact });
    },
  );
  const client = new OpenComputerClient(
    { apiUrl: "https://app.opencomputer.dev", apiKey: "test" },
    "retry-42",
  );
  await client.exportWorkspaceFile("ses", "a.txt");
  await client.exportWorkspaceFile("ses", "a.txt");
  await client.exportWorkspaceFile("ses", "b.txt");
  const keys = requests.map((request) =>
    request.headers.get("idempotency-key"),
  );
  assert.ok(keys[0]);
  assert.equal(keys[0], keys[1], "same path retries under the same key");
  assert.notEqual(keys[0], keys[2], "each path is its own export operation");
});

test("workspace artifact downloads use a signed URL without forwarding credentials", async (context) => {
  const requests: Request[] = [];
  context.mock.method(
    globalThis,
    "fetch",
    async (input: string | URL | Request, init?: RequestInit) => {
      const request = new Request(input, init);
      requests.push(request);
      if (request.url.includes("/workspace/exports/wsart_1/download")) {
        return Response.json({
          url: "https://objects.example.test/artifact?signature=test",
          expiresAt: "2026-09-25T20:00:00.000Z",
        });
      }
      return new Response(new Uint8Array([1, 2, 3]));
    },
  );
  const client = new OpenComputerClient({
    apiUrl: "https://app.opencomputer.dev",
    apiKey: "test",
  });
  const response = await client.workspaceArtifactContent({
    sessionId: "ses",
    id: "wsart_1",
  });
  assert.deepEqual(
    new Uint8Array(await response.arrayBuffer()),
    new Uint8Array([1, 2, 3]),
  );
  assert.equal(requests.length, 2);
  assert.equal(
    requests[0]?.url,
    "https://app.opencomputer.dev/api/managed-agents/sessions/ses/workspace/exports/wsart_1/download",
  );
  assert.equal(requests[0]?.redirect, "manual");
  assert.equal(requests[0]?.headers.get("x-api-key"), "test");
  assert.equal(
    requests[1]?.url,
    "https://objects.example.test/artifact?signature=test",
  );
  assert.equal(requests[1]?.redirect, "error");
  assert.equal(requests[1]?.headers.get("x-api-key"), null);
});

test("memory documents travel with their ETag and send it back as a precondition", async (context) => {
  const requests: Request[] = [];
  const document = {
    id: "workshop",
    title: "Workshop requirements",
    text: "Node.js 22.",
    summary: "",
    agentWrites: "enabled",
    revision: "rev-1",
    bytes: 11,
    maxBytes: 8192,
    updatedAt: "2026-09-10T12:00:00.000Z",
    writer: { kind: "owner" },
  };
  context.mock.method(
    globalThis,
    "fetch",
    async (input: string | URL | Request, init?: RequestInit) => {
      const request = new Request(input, init);
      requests.push(request);
      if (request.method === "DELETE")
        return new Response(null, { status: 204 });
      return Response.json(document, { headers: { etag: '"rev-1"' } });
    },
  );
  const client = new OpenComputerClient({
    apiUrl: "https://app.opencomputer.dev",
    apiKey: "test",
  });
  const target = {
    projectId: "prj_1",
    resource: "requirements",
    id: "workshop",
    environment: "development" as const,
  };

  const created = await client.createMemoryDocument({
    ...target,
    title: "Workshop requirements",
    text: "Node.js 22.",
  });
  assert.equal(created.etag, '"rev-1"');
  assert.equal(created.document.revision, "rev-1");
  await client.replaceMemoryDocument({
    ...target,
    etag: created.etag,
    text: "Node 22",
  });
  await client.patchMemoryDocument({
    ...target,
    etag: created.etag,
    agentWrites: "disabled",
  });
  await client.deleteMemoryDocument({ ...target, etag: created.etag });
  await client.memoryDocuments({ ...target, cursor: "c2" });

  const [create, replace, patch, remove, list] = requests;
  assert.ok(create && replace && patch && remove && list);
  assert.equal(
    create.url,
    "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory/requirements/documents/workshop?environment=development",
  );
  assert.equal(create.method, "PUT");
  assert.equal(create.headers.get("if-none-match"), "*");
  assert.equal(create.headers.get("if-match"), null);
  assert.deepEqual(await create.json(), {
    title: "Workshop requirements",
    text: "Node.js 22.",
  });
  assert.equal(replace.headers.get("if-match"), '"rev-1"');
  assert.deepEqual(await replace.json(), { text: "Node 22" });
  assert.equal(patch.method, "PATCH");
  assert.deepEqual(await patch.json(), { agentWrites: "disabled" });
  assert.equal(remove.method, "DELETE");
  assert.equal(remove.headers.get("if-match"), '"rev-1"');
  assert.equal(
    list.url,
    "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/memory/requirements/documents?environment=development&cursor=c2",
  );
  for (const request of requests) {
    assert.equal(request.headers.get("x-api-key"), "test");
  }
});

test("memory precondition failures surface the API's code", async (context) => {
  context.mock.method(globalThis, "fetch", async () =>
    Response.json(
      {
        error: {
          code: "precondition_failed",
          message: "The document changed.",
        },
      },
      { status: 412 },
    ),
  );
  const client = new OpenComputerClient({
    apiUrl: "https://app.opencomputer.dev",
    apiKey: "test",
  });

  await assert.rejects(
    client.replaceMemoryDocument({
      projectId: "prj_1",
      resource: "requirements",
      id: "workshop",
      environment: "development",
      etag: '"rev-0"',
      text: "stale",
    }),
    (error: unknown) =>
      error instanceof APIError &&
      error.status === 412 &&
      error.code === "precondition_failed" &&
      error.message === "The document changed.",
  );
});

test("a 402 carries the halt reason and prefers the action link", async (context) => {
  context.mock.method(globalThis, "fetch", async () =>
    Response.json(
      {
        error: {
          code: "insufficient_credits",
          message: "Your 3-day trial has ended.",
          haltReason: "trial_days",
          actionUrl: "https://app.opencomputer.dev/billing",
          upgradeUrl: "https://app.opencomputer.dev/billing?plan=max",
        },
      },
      { status: 402 },
    ),
  );
  const client = new OpenComputerClient({
    apiUrl: "https://app.opencomputer.dev",
    apiKey: "test",
  });
  await assert.rejects(client.credits(), (error: unknown) =>
    error instanceof APIError &&
    error.status === 402 &&
    error.haltReason === "trial_days" &&
    error.actionUrl === "https://app.opencomputer.dev/billing",
  );
});

test("credits signals display only when the caller shows the warning", async (context) => {
  const urls: string[] = [];
  context.mock.method(globalThis, "fetch", async (input: string | URL | Request) => {
    urls.push(String(input instanceof Request ? input.url : input));
    return Response.json({ usagePlan: "base" });
  });
  const client = new OpenComputerClient({ apiUrl: "https://app.opencomputer.dev", apiKey: "test" });
  await client.credits();
  await client.credits(true);
  assert.deepEqual(urls, [
    "https://app.opencomputer.dev/api/billing/credits",
    "https://app.opencomputer.dev/api/billing/credits?display=1",
  ]);
});

test("managed GitHub App requests use project-scoped status and connect routes", async (context) => {
  const requests: Request[] = [];
  context.mock.method(
    globalThis,
    "fetch",
    async (input: string | URL | Request, init?: RequestInit) => {
      const request = new Request(input, init);
      requests.push(request);
      if (request.method === "POST") {
        return Response.json({
          installUrl: "https://github.com/apps/opencomputer/installations/new",
          authorizeUrl: "https://github.com/login/oauth/authorize",
        });
      }
      return Response.json({ environments: [], connections: [], app: null });
    },
  );
  const client = new OpenComputerClient({
    apiUrl: "https://app.opencomputer.dev",
    apiKey: "test",
  });

  await client.githubStatus("project/one");
  await client.connectGitHub({
    projectId: "project/one",
    environments: ["development", "production"],
  });
  await client.attachGitHub({
    projectId: "project/one",
    environment: "production",
    connectionId: "ghi_one",
  });

  assert.equal(
    requests[0]?.url,
    "https://app.opencomputer.dev/api/managed-agents/projects/project%2Fone/github",
  );
  assert.equal(requests[0]?.method, "GET");
  assert.equal(
    requests[1]?.url,
    "https://app.opencomputer.dev/api/managed-agents/projects/project%2Fone/github/connect",
  );
  assert.equal(requests[1]?.method, "POST");
  assert.deepEqual(await requests[1]?.json(), {
    environments: ["development", "production"],
  });
  assert.equal(
    requests[2]?.url,
    "https://app.opencomputer.dev/api/managed-agents/projects/project%2Fone/github/attach",
  );
  assert.equal(requests[2]?.method, "POST");
  assert.deepEqual(await requests[2]?.json(), {
    environment: "production",
    connectionId: "ghi_one",
  });
});

test("model connections and routes use write-only connection input and project route endpoints", async (context) => {
  const requests: Request[] = [];
  context.mock.method(
    globalThis,
    "fetch",
    async (input: string | URL | Request, init?: RequestInit) => {
      const request = new Request(input, init);
      requests.push(request);
      if (request.method === "DELETE")
        return new Response(null, { status: 204 });
      if (request.url.endsWith("/model-access/connections")) {
        return Response.json({
          id: "mac_scx",
          organizationId: "org_test",
          connectedByUserId: "user_test",
          provider: "openai_compatible",
          kind: "openai_compatible_api",
          label: "SCX",
          baseUrl: "https://api.scx.ai/v1",
          status: "connected",
        });
      }
      return Response.json({
        id: "mr_test",
        projectId: "prj_test",
        environment: "development",
        connectionId: "mac_scx",
        model: "GLM-5.3",
        fallback: "fail",
        revision: 1,
      });
    },
  );
  const client = new OpenComputerClient({
    apiUrl: "https://app.opencomputer.dev",
    apiKey: "test",
  });

  await client.connectModelAccessApiKey({
    provider: "openai_compatible",
    api_key: "secret-key",
    base_url: "https://api.scx.ai/v1",
  });
  await client.putModelRoute({
    projectId: "prj_test",
    environment: "development",
    connectionId: "mac_scx",
    model: "GLM-5.3",
    fallback: "fail",
  });
  await client.deleteModelRoute({
    projectId: "prj_test",
    environment: "production",
  });

  assert.deepEqual(await requests[0]!.json(), {
    provider: "openai_compatible",
    api_key: "secret-key",
    base_url: "https://api.scx.ai/v1",
  });
  assert.equal(
    requests[1]!.url,
    "https://app.opencomputer.dev/api/managed-agents/projects/prj_test/model-routes/development",
  );
  assert.deepEqual(await requests[1]!.json(), {
    connection_id: "mac_scx",
    model: "GLM-5.3",
    fallback: "fail",
  });
  assert.equal(requests[2]!.method, "DELETE");
});

test("database queries use the project read endpoint with positional parameters", async (context) => {
  let request: Request | undefined;
  context.mock.method(
    globalThis,
    "fetch",
    async (input: string | URL | Request, init?: RequestInit) => {
      request = new Request(input, init);
      return Response.json({
        environment: "production",
        result: {
          columns: ["id"],
          rows: [{ id: 7 }],
          rowsAffected: 0,
          truncated: false,
        },
      });
    },
  );
  const client = new OpenComputerClient({
    apiUrl: "https://app.opencomputer.dev",
    apiKey: "test",
  });

  const result = await client.databaseQuery({
    projectId: "prj_1",
    environment: "production",
    sql: "SELECT id FROM records WHERE name = ? LIMIT ?",
    parameters: ["pricing", 20],
  });

  assert.deepEqual(result.rows, [{ id: 7 }]);
  assert.ok(request);
  assert.equal(
    request.url,
    "https://app.opencomputer.dev/api/managed-agents/projects/prj_1/database/query",
  );
  assert.equal(request.method, "POST");
  assert.deepEqual(await request.json(), {
    environment: "production",
    sql: "SELECT id FROM records WHERE name = ? LIMIT ?",
    parameters: ["pricing", 20],
  });
});

test("session list sends exact filters, the page size and the cursor as query parameters", async (context) => {
  const requests: Request[] = [];
  context.mock.method(globalThis, "fetch", async (input: string | URL | Request, init?: RequestInit) => {
    const request = new Request(input, init);
    requests.push(request);
    return Response.json({ sessions: [], nextCursor: null });
  });
  const client = new OpenComputerClient({ apiUrl: "https://app.opencomputer.dev", apiKey: "test" });

  await client.sessions();
  await client.sessions({
    status: "suspended",
    agent: "reviewer",
    externalReference: "order 42/α",
    limit: 2,
    cursor: "eyJjIjoxfQ",
  });

  assert.equal(new URL(requests[0]!.url).search, "");
  const query = new URL(requests[1]!.url).searchParams;
  assert.equal(query.get("status"), "suspended");
  assert.equal(query.get("agentId"), "reviewer");
  assert.equal(query.get("externalReference"), "order 42/α");
  assert.equal(query.get("limit"), "2");
  assert.equal(query.get("cursor"), "eyJjIjoxfQ");
});