AkurAI Build
Menu

popagent

public

Latest change 2fb6f198c4c71ef37dffc8ac5dca8482068a7bfc - Add governed AkurAI Build maintenance by AkurAI Build

import { describe, expect, test } from "bun:test";
import { AkuraiBuildClient } from "./akurai-build";

function rpc(id: number, result: unknown): Response {
  return Response.json({ jsonrpc: "2.0", id, result });
}

describe("AkurAI Build MCP client", () => {
  test("fails closed and reports disabled without a bearer key", async () => {
    const client = new AkuraiBuildClient({ url: "https://build.example", apiKey: "" });
    expect(client.getStatus()).toMatchObject({ enabled: false, configured: false, state: "disabled" });
    await expect(client.call("akurai_repo_list")).rejects.toThrow("AKURAI_BUILD_API_KEY is not configured");
  });

  test("initializes once, parses MCP JSON text content, and never exposes credentials in errors", async () => {
    const requests: Array<{ body: string; authorization: string }> = [];
    let calls = 0;
    const client = new AkuraiBuildClient({
      url: "https://build.example/",
      apiKey: "secret-build-key",
      fetchImpl: async (_input, init) => {
        calls += 1;
        requests.push({
          body: String(init?.body),
          authorization: String(new Headers(init?.headers).get("authorization")),
        });
        const request = JSON.parse(String(init?.body)) as { id: number; method: string };
        if (request.method === "initialize") {
          return rpc(request.id, { protocolVersion: "2025-06-18" });
        }
        return rpc(request.id, {
          content: [{ type: "text", text: JSON.stringify({ repositories: ["public-demo"] }) }],
          isError: false,
        });
      },
    });

    await expect(client.call("akurai_repo_list", { visibility: "public" })).resolves.toEqual({
      repositories: ["public-demo"],
    });
    await expect(client.call("akurai_repo_list", {})).resolves.toEqual({ repositories: ["public-demo"] });
    expect(calls).toBe(3);
    expect(requests.every(({ authorization }) => authorization === "Bearer secret-build-key")).toBe(true);
    expect(client.getStatus()).toMatchObject({ state: "healthy", enabled: true });
  });

  test("rejects malformed JSON-RPC and MCP tool errors", async () => {
    let malformed = true;
    const client = new AkuraiBuildClient({
      apiKey: "key",
      fetchImpl: async (_input, init) => {
        const request = JSON.parse(String(init?.body)) as { id: number; method: string };
        if (request.method === "initialize") return rpc(request.id, { protocolVersion: "2025-06-18" });
        if (malformed) return Response.json({ jsonrpc: "2.0", id: request.id, result: {} });
        return rpc(request.id, {
          content: [{ type: "text", text: "repository is private" }],
          isError: true,
        });
      },
    });

    await expect(client.call("akurai_repo_list")).rejects.toThrow("tool result is malformed");
    malformed = false;
    await expect(client.call("akurai_repo_list")).rejects.toThrow("repository is private");
    expect(client.getStatus().state).toBe("unhealthy");
  });

  test("honors caller abort signals", async () => {
    const controller = new AbortController();
    const client = new AkuraiBuildClient({
      apiKey: "key",
      fetchImpl: async (_input, init) => new Promise<Response>((_resolve, reject) => {
        init?.signal?.addEventListener("abort", () => reject(new Error("aborted")), { once: true });
      }),
    });
    const pending = client.call("akurai_repo_list", {}, controller.signal);
    controller.abort();
    await expect(pending).rejects.toThrow("request aborted");
  });
});