Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

18.4 KB
Raw Blame
/**
 * The provider page: a Live calls tab on OpenAI GPT-Live, and an Agents tab
 * with one entry per company — any number on at once, each reached by its
 * subscription or its key — beside the selected company's settings.
 */
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

const voiceMode = vi.hoisted(() => ({
  mode: "realtime",
  realtimeAvailable: true,
  setMode: (() => undefined) as (mode: string) => void,
}));
vi.mock("@/hooks/useVoiceMode", () => ({
  useVoiceMode: () => ({
    mode: voiceMode.mode,
    realtimeAvailable: voiceMode.realtimeAvailable,
    statusKnown: true,
    setMode: voiceMode.setMode,
    saveError: null,
    isLoading: false,
    isSaving: false,
    sessionActive: false,
  }),
}));

// The sections below the providers own their data sources and have their own
// tests; here they only need to stay out of the way.
vi.mock("@/components/AgentAccountsPanel", () => ({ AgentAccountsPanel: () => null }));
// GPT-Live's own form has its own tests; here it only has to place the key row.
vi.mock("@/components/providers/LiveProfile", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@/components/providers/LiveProfile")>()),
  LiveProfile: ({ keyField }: { keyField?: React.ReactNode }) => <div data-testid="live-profile">{keyField}</div>,
}));

import { _resetProvidersCacheForTests } from "@/hooks/useProviders";
import { ApiKeysView } from "@/views/ApiKeysView";

const PROVIDERS = [
  { id: "claude-api", label: "Claude (API-Key)", tier: "brain", auth_mode: "api_key", secret_keys: ["anthropic_api_key"], secrets_set: {}, configured: false, active: false, billing: "api" },
  { id: "claude-cli", label: "Claude (Anthropic subscription)", tier: "brain", auth_mode: "claude_cli", secret_keys: [], secrets_set: {}, configured: true, active: false, billing: "subscription" },
  { id: "gemini", label: "Google Gemini", tier: "brain", auth_mode: "api_key", secret_keys: ["gemini_api_key"], secrets_set: { gemini_api_key: true }, configured: true, active: false, billing: "api" },
  { id: "gemini-live", label: "Gemini Live", tier: "realtime", auth_mode: "api_key", secret_keys: ["realtime_gemini_api_key"], secrets_set: {}, configured: true, active: true, billing: "api" },
].map((p) => ({ dashboard_url: null, login_cli: null, install_hint: null, credential_path_hint: null, cli_installed: null, credential_help: null, signup_url: null, alt_credential: null, ...p }));

const FAMILIES = [
  {
    id: "claude-api", label: "Anthropic", logo_id: "claude-api", local: false,
    key_slot: "anthropic_api_key", key_present: false, separate_keys: [],
    dashboard_url: "https://console.anthropic.com/settings/keys", signup_url: null,
    subscription: { kind: "claude_cli", provider_id: "claude-cli", label: "Claude (Anthropic subscription)" },
    provider_ids: ["claude-api", "claude-cli"], hidden_ids: [], agent_ids: ["claude-api", "claude-cli"],
  },
  {
    id: "gemini", label: "Google Gemini", logo_id: "gemini", local: false,
    key_slot: "gemini_api_key", key_present: true,
    separate_keys: [{ slot: "realtime_gemini_api_key", surface: "live_voice" }],
    dashboard_url: null, signup_url: null, subscription: null,
    provider_ids: ["gemini", "gemini-live"], hidden_ids: [], agent_ids: ["gemini", "gemini-live"],
  },
];

const AGENTS = {
  configured: true, enabled: true, binary_path: "", binary_detected: null, version_pin: null,
  time_cap_min: null, concurrency: null, state_dir_root: null, brain_primary: "gemini",
  provider_slug: null, model_override: null, sub_model_override: "", model_resolved: null,
  mapping: [
    { jarvis: "claude-api", label: "Claude", key_set: true, is_active_brain: false, billing: "subscription_or_api" },
    { jarvis: "openai", label: "OpenAI", key_set: false, is_active_brain: false, billing: "api" },
    { jarvis: "gemini", label: "Google Gemini", key_set: true, is_active_brain: true, billing: "api" },
  ],
};

const CLAUDE_STATUS = {
  installed: true, connected: true, mode: "subscription", message: "Connected",
  user_email: "[email protected]", account_label: "Claude Max", subscription_type: "max",
  version: "2.1.0", binary_path: "/usr/bin/claude",
};

const CATALOG = {
  providers: [
    {
      id: "claude-api", label: "Claude", family: "claude", runner: "claude-cli", models_source: "curated",
      curated_models: [{ id: "opus", label: "Opus" }, { id: "sonnet", label: "Sonnet" }],
      default_model: "", keyless: false, native_resume: true, effort_levels: [], default_effort: "",
      permission_modes: [], default_permission_mode: "", cli_installed: true, enabled: true, hidden_models: [],
    },
  ],
  default_cwd: "",
  shell: "",
};

const LIVE = {
  profile: {
    auth_mode: "api_key", model: "gpt-live-1", voice: "gleam", backend_model: "", reasoning_effort: "medium",
    web_search: true, instructions: "", backend_instructions: "", configured: false,
  },
  key_ready: false, active: false, agent_configured: false,
};

let calls: { url: string; method: string; body: unknown }[] = [];
let prefs: { disabled: string[]; api_only: string[]; hidden_models: Record<string, string[]> };
let routeOverrides: { providers?: unknown; families?: unknown; agents?: unknown };

beforeEach(() => {
  voiceMode.mode = "realtime";
  voiceMode.realtimeAvailable = true;
  voiceMode.setMode = vi.fn();
  calls = [];
  prefs = { disabled: [], api_only: [], hidden_models: {} };
  routeOverrides = {};
  _resetProvidersCacheForTests();
  const routes: Record<string, unknown | (() => unknown)> = {
    "/api/providers/families": () => routeOverrides.families ?? { families: FAMILIES },
    "/api/providers/section-health": { sections: {}, checked_at: 0, cached: false },
    "/api/providers/local-openai/base-url": { base_url: "http://localhost:8000" },
    "/api/providers": () => routeOverrides.providers ?? { providers: PROVIDERS },
    "/api/jarvis-agent/status": () => routeOverrides.agents ?? AGENTS,
    "/api/jarvis-agent/switch": { ok: true },
    "/api/brain/switch": { ok: true },
    "/api/claude/status": CLAUDE_STATUS,
    "/api/codex/status": null,
    "/api/secrets/": { ok: true, key: "x", written: ["anthropic_api_key"] },
    "/api/society/provider-prefs": prefs,
    "/api/agent-chat/catalog": CATALOG,
    "/api/live/profile": LIVE,
  };
  vi.stubGlobal(
    "fetch",
    vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
      const url = String(input);
      const method = (init?.method ?? "GET").toUpperCase();
      calls.push({ url, method, body: init?.body ? JSON.parse(String(init.body)) : null });
      const match = Object.keys(routes)
        .sort((a, b) => b.length - a.length)
        .find((prefix) => url.startsWith(prefix));
      const route = match ? routes[match] : {};
      const body = typeof route === "function" ? route() : route;
      const ok = body !== null;
      return { ok, status: ok ? 200 : 404, json: async () => body ?? {}, text: async () => "" } as Response;
    }),
  );
});

afterEach(() => {
  cleanup();
  vi.unstubAllGlobals();
});

function renderPage() {
  const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
  return render(
    <QueryClientProvider client={client}>
      <ApiKeysView />
    </QueryClientProvider>,
  );
}

function openAgents() {
  fireEvent.click(screen.getByRole("tab", { name: "Agents" }));
}

beforeEach(() => {
  try {
    // Most tests start on Live calls and switch over; the default is tested below.
    localStorage.setItem("jarvis.apikeys.tab", "voice");
  } catch {
    /* no storage in this environment */
  }
});

describe("ApiKeysView — Live calls", () => {
  it("opens on live calls with the GPT-Live pane and one OpenAI key row", async () => {
    renderPage();
    const voice = await screen.findByTestId("apikeys-voice");
    expect(within(voice).getByTestId("live-profile")).toBeTruthy();
    expect(await within(voice).findByTestId("voice-key")).toBeTruthy();
  });

  it("has only the two tabs, Agents first", async () => {
    renderPage();
    await screen.findByTestId("apikeys-voice");
    expect(screen.getAllByRole("tab").map((tab) => tab.textContent)).toEqual(["Agents", "Live calls"]);
  });

  it("offers the Pipeline as the other voice engine", async () => {
    renderPage();
    const engine = await screen.findByTestId("voice-engine-switch");
    expect(within(engine).getByRole("radio", { name: "GPT-Live" }).getAttribute("aria-checked")).toBe("true");
    fireEvent.click(within(engine).getByRole("radio", { name: "Pipeline" }));
    expect(voiceMode.setMode).toHaveBeenCalledWith("pipeline");
  });

  it("shows voice input, the brain and voice output while the Pipeline is the engine", async () => {
    voiceMode.mode = "pipeline";
    renderPage();
    const pipeline = await screen.findByTestId("voice-pipeline");
    expect(screen.queryByTestId("live-profile")).toBeNull();
    expect(within(pipeline).getByText("Voice Input (STT)")).toBeTruthy();
    expect(within(pipeline).getByText("Voice Output (TTS)")).toBeTruthy();
    // The brain is picked right here (#1028): the Agents tab's default only
    // decides agent tasks, so the old "Change in Agents" link led nowhere.
    const brain = within(pipeline).getByTestId("voice-pipeline-brain");
    expect(within(brain).queryByRole("button", { name: "Change in Agents" })).toBeNull();
    const picker = within(pipeline).getByTestId("voice-pipeline-brain-picker");
    fireEvent.click(await within(picker).findByTestId("provider-row-gemini"));
    await waitFor(() => {
      const sw = calls.find((call) => call.method === "POST" && call.url === "/api/brain/switch");
      expect(sw?.body).toEqual({ provider: "gemini", persist: true });
    });
    // The Agents default is a different setting and is never written from here.
    expect(calls.some((call) => call.url.startsWith("/api/jarvis-agent/switch"))).toBe(false);
  });

  it("shows GPT-Live's settings without switching while no live provider is ready", async () => {
    voiceMode.mode = "pipeline";
    voiceMode.realtimeAvailable = false;
    renderPage();
    const engine = await screen.findByTestId("voice-engine-switch");
    fireEvent.click(within(engine).getByRole("radio", { name: "GPT-Live" }));
    expect(await screen.findByTestId("live-profile")).toBeTruthy();
    expect(voiceMode.setMode).not.toHaveBeenCalled();
    expect(screen.getByTestId("voice-engine").textContent).toContain("Set up GPT-Live below");
  });

  it("opens on Agents the first time", async () => {
    localStorage.removeItem("jarvis.apikeys.tab");
    renderPage();
    expect(await screen.findByTestId("apikeys-agents")).toBeTruthy();
  });
});

describe("ApiKeysView — Agents", () => {
  it("shows and saves the local OpenAI-compatible server URL and optional key", async () => {
    routeOverrides.providers = {
      providers: [
        ...PROVIDERS,
        {
          id: "remote-compatible", label: "Remote compatibility endpoint", tier: "brain", auth_mode: "api_key",
          secret_keys: ["remote_compatible_api_key"], secrets_set: { remote_compatible_api_key: false }, configured: false,
          active: false, billing: "api", supports_base_url: true, default_base_url: null, base_url: null,
        },
        {
          id: "local-openai", label: "Local server (OpenAI-compatible)", tier: "brain", auth_mode: "none",
          secret_keys: ["local_openai_api_key"], secrets_set: { local_openai_api_key: false }, configured: false,
          active: false, billing: "local", supports_base_url: true, default_base_url: null, base_url: null,
        },
      ],
    };
    routeOverrides.families = {
      families: [
        ...FAMILIES,
        {
          id: "local", label: "On this computer", logo_id: "local-openai", local: true,
          key_slot: null, key_present: false, separate_keys: [], dashboard_url: null, signup_url: null,
          subscription: null, provider_ids: ["local-openai"], hidden_ids: [], agent_ids: ["local-openai"],
        },
      ],
    };
    routeOverrides.agents = {
      ...AGENTS,
      mapping: [
        ...AGENTS.mapping,
        { jarvis: "local-openai", label: "Local server (OpenAI-compatible)", key_set: false, keyless: true, is_active_brain: false, billing: "local" },
      ],
    };

    renderPage();
    await screen.findByTestId("apikeys-voice");
    openAgents();
    fireEvent.click(within(await screen.findByTestId("agent-provider-local")).getByRole("button", { name: "On this computer" }));

    const detail = await screen.findByTestId("agent-provider-detail-local");
    const url = within(detail).getByRole("textbox", { name: "Server URL" });
    fireEvent.change(url, { target: { value: "http://localhost:8000/v1" } });
    fireEvent.click(within(detail).getByTestId("provider-base-url-save"));
    await waitFor(() => {
      const save = calls.find((call) => call.method === "PUT" && call.url === "/api/providers/local-openai/base-url");
      expect(save?.body).toEqual({ base_url: "http://localhost:8000/v1" });
    });

    expect(within(detail).getByText("Local provider — runs without credentials. The key below is optional, only for servers that enforce one (e.g. vLLM --api-key).")).toBeTruthy();
    fireEvent.change(within(detail).getByTestId("local-openai-key-input"), { target: { value: "local-test-key" } });
    fireEvent.click(within(detail).getByTestId("local-openai-key-save"));
    await waitFor(() => {
      const save = calls.find((call) => call.method === "POST" && call.url === "/api/secrets/local_openai_api_key");
      expect(save?.body).toEqual({ value: "local-test-key", scope: "everywhere" });
    });
  });

  it("lists each company once with its subscription account", async () => {
    renderPage();
    await screen.findByTestId("apikeys-voice");
    openAgents();
    const claude = await screen.findByTestId("agent-provider-claude-api");
    expect(claude.textContent).toContain("Anthropic");
    expect(claude.textContent).toContain("[email protected] · Claude Max");
    expect(claude.textContent).toContain("v2.1.0");
    expect(await screen.findByTestId("agent-provider-gemini")).toBeTruthy();
  });

  it("turns several companies on at once instead of switching between them", async () => {
    Object.assign(prefs, { disabled: ["claude-api"], api_only: [] });
    renderPage();
    await screen.findByTestId("apikeys-voice");
    openAgents();
    const claude = await screen.findByTestId("agent-provider-claude-api");
    const gemini = await screen.findByTestId("agent-provider-gemini");
    await waitFor(() => expect(within(gemini).getByRole("switch").getAttribute("aria-checked")).toBe("true"));
    await waitFor(() => expect((within(claude).getByRole("switch") as HTMLButtonElement).disabled).toBe(false));
    fireEvent.click(within(claude).getByRole("switch"));
    await waitFor(() => {
      const put = calls.find((c) => c.method === "PUT" && c.url === "/api/society/provider-prefs");
      expect(put?.body).toEqual({ disabled: [] });
    });
    // Gemini stays the default worker; turning Claude on never turns it off.
    expect(calls.some((c) => c.url === "/api/jarvis-agent/switch")).toBe(false);
  });

  it("switches a company between its subscription and its key in one place", async () => {
    renderPage();
    await screen.findByTestId("apikeys-voice");
    openAgents();
    fireEvent.click(await screen.findByRole("button", { name: "Anthropic" }));
    const access = await screen.findByTestId("agent-provider-access");
    await waitFor(() =>
      expect(within(access).getByRole("radio", { name: "Subscription" }).getAttribute("aria-checked")).toBe("true"),
    );
    expect(screen.queryByTestId("provider-key-input")).toBeNull();
    fireEvent.click(within(access).getByRole("radio", { name: "API key" }));
    await waitFor(() => {
      const put = calls.find((c) => c.method === "PUT" && c.url === "/api/society/provider-prefs");
      expect(put?.body).toEqual({ api_only: ["claude-api"], disabled: [] });
    });
  });

  it("saves one key for the whole company", async () => {
    Object.assign(prefs, { disabled: [], api_only: ["claude-api"] });
    renderPage();
    await screen.findByTestId("apikeys-voice");
    openAgents();
    fireEvent.click(await screen.findByRole("button", { name: "Anthropic" }));
    const input = await screen.findByTestId("provider-key-input");
    fireEvent.change(input, { target: { value: "sk-ant-api03-test" } });
    fireEvent.click(screen.getByTestId("provider-key-save"));
    await waitFor(() => {
      const save = calls.find((c) => c.method === "POST" && c.url === "/api/secrets/anthropic_api_key");
      expect(save?.body).toEqual({ value: "sk-ant-api03-test", scope: "everywhere" });
    });
  });

  it("moves a feature with its own key back onto the main key", async () => {
    renderPage();
    await screen.findByTestId("apikeys-voice");
    openAgents();
    fireEvent.click(await screen.findByRole("button", { name: "Google Gemini" }));
    const row = await screen.findByTestId("provider-separate-key-realtime_gemini_api_key");
    fireEvent.click(within(row).getByRole("button"));
    await waitFor(() =>
      expect(calls.some((c) => c.method === "DELETE" && c.url === "/api/secrets/realtime_gemini_api_key")).toBe(true),
    );
  });

  it("hides a model from the agents with its switch", async () => {
    renderPage();
    await screen.findByTestId("apikeys-voice");
    openAgents();
    fireEvent.click(await screen.findByRole("button", { name: "Anthropic" }));
    const models = await screen.findByTestId("agent-models-claude-api");
    await waitFor(() =>
      expect((within(models).getByRole("switch", { name: "Sonnet" }) as HTMLButtonElement).disabled).toBe(false),
    );
    fireEvent.click(within(models).getByRole("switch", { name: "Sonnet" }));
    await waitFor(() => {
      const put = calls.find((c) => c.method === "PUT" && c.url === "/api/society/provider-prefs");
      expect(put?.body).toEqual({ hidden_models: { "claude-api": ["sonnet"] } });
    });
  });

  it("shows the sign-in with the account and the binary behind it", async () => {
    renderPage();
    await screen.findByTestId("apikeys-voice");
    openAgents();
    fireEvent.click(await screen.findByRole("button", { name: "Anthropic" }));
    const detail = await screen.findByTestId("agent-provider-detail-claude-api");
    await waitFor(() => expect(detail.textContent).toContain("[email protected]"));
    expect(within(detail).getByTestId("subscription-disconnect")).toBeTruthy();
    expect((within(detail).getByLabelText("Binary path") as HTMLInputElement).value).toBe("/usr/bin/claude");
  });
});