Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

4.3 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from "vitest";
import {
  ManagementApiError,
  createManagedAgent,
  enrollCommand,
  fetchProviders,
  managementError,
  providerLabel,
  shellQuote,
} from "./management";

function respond(status: number, body: unknown) {
  const fetch = vi.fn(async () => new Response(JSON.stringify(body), { status }));
  vi.stubGlobal("fetch", fetch);
  return fetch;
}

describe("managementError", () => {
  it("reads the code and message from the error envelope", async () => {
    const error = await managementError(
      new Response(
        JSON.stringify({
          error: {
            code: "provider_login_expired",
            message: "Codex is not logged in on laptop.",
            retryable: false,
          },
        }),
        { status: 409 },
      ),
    );
    expect(error).toBeInstanceOf(ManagementApiError);
    expect(error.status).toBe(409);
    expect(error.code).toBe("provider_login_expired");
    expect(error.message).toBe("Codex is not logged in on laptop.");
  });

  it("falls back to the status line for a body that is not the envelope", async () => {
    const error = await managementError(
      new Response(JSON.stringify({ detail: "Not Found" }), {
        status: 404,
        statusText: "Not Found",
      }),
    );
    expect(error.code).toBe("http_error");
    expect(error.message).toBe("404 Not Found");
  });
});

describe("createManagedAgent", () => {
  afterEach(() => vi.unstubAllGlobals());

  it("posts the definition and surfaces a placement refusal", async () => {
    const fetch = respond(409, {
      error: {
        code: "controller_offline",
        message: "The machine laptop has not reported recently.",
        retryable: true,
      },
    });
    await expect(
      createManagedAgent({
        name: "pm-agent",
        description: "Writes PRDs",
        display_name: null,
        controller_id: "c1",
        desired_state: "running",
        definition: {
          provider: "claude",
          model: null,
          instructions: "",
          auto_mode: true,
          auto_approve: false,
          directory: null,
          isolation: "shared",
          advanced_config: {},
        },
      }),
    ).rejects.toMatchObject({ code: "controller_offline" });
    const [url, init] = fetch.mock.calls[0] as unknown as [string, RequestInit];
    expect(url).toBe("/gateway/management/agents");
    expect(init.method).toBe("POST");
    expect(JSON.parse(init.body as string)).toMatchObject({
      name: "pm-agent",
      controller_id: "c1",
      definition: { provider: "claude", auto_mode: true, auto_approve: false },
    });
  });
});

describe("enrollCommand", () => {
  it("names the server and the code", () => {
    expect(
      enrollCommand("https://switch.example.com", "swce_example", { name: "", description: " " }),
    ).toBe("switch-agent-controller enroll --server https://switch.example.com --code swce_example");
  });

  it("adds the name and description, quoted for the shell", () => {
    expect(
      enrollCommand("https://switch.example.com", "swce_example", {
        name: " build-box ",
        description: "Bob's box: $HOME `x` \"y\"",
      }),
    ).toBe(
      "switch-agent-controller enroll --server https://switch.example.com --code swce_example" +
        " --name build-box --description 'Bob'\\''s box: $HOME `x` \"y\"'",
    );
  });
});

describe("shellQuote", () => {
  it("leaves plain words alone and single-quotes the rest", () => {
    expect(shellQuote("swce_a-b.c")).toBe("swce_a-b.c");
    expect(shellQuote("two words")).toBe("'two words'");
    expect(shellQuote("it's")).toBe("'it'\\''s'");
    expect(shellQuote("")).toBe("''");
  });
});

describe("fetchProviders", () => {
  afterEach(() => vi.unstubAllGlobals());

  it("reads the provider list the server serves", async () => {
    const providers = [{ id: "codex", label: "Codex", advanced_fields: [] }];
    const fetch = respond(200, { providers });
    await expect(fetchProviders()).resolves.toEqual(providers);
    expect(fetch).toHaveBeenCalledWith("/gateway/management/providers", expect.anything());
  });
});

describe("providerLabel", () => {
  it("is the listed label, else the id", () => {
    const providers = [{ id: "codex", label: "Codex", advanced_fields: [] }];
    expect(providerLabel(providers, "codex")).toBe("Codex");
    expect(providerLabel(providers, "newcli")).toBe("newcli");
  });
});