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.

3.7 KB
Raw Blame
/**
 * Sidebar API-Keys alert dot.
 *
 * The API-Keys tab dots already say "this section is broken" once you are on the
 * page. The sidebar drills that one level up: a red dot on the "API Keys" nav row
 * makes a set-up-but-failing provider visible from anywhere in the app. Only a
 * hard "error" lights it — the amber "needs_setup" state stays off the bar so a
 * fresh install isn't permanently flagged.
 */
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { cleanup, render as rtlRender, screen } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";

import type { SectionHealth } from "@/hooks/useProviders";

let mockHealth: Record<string, SectionHealth> = {};

vi.mock("@/hooks/useVoiceReadiness", () => ({
  useVoiceReadiness: () => ({
    connected: true,
    voiceWarming: false,
    bootWarming: false,
    warming: false,
  }),
}));

vi.mock("@/hooks/useVoiceMode", () => ({
  useVoiceMode: () => ({
    mode: "pipeline",
    realtimeAvailable: false,
    setMode: vi.fn(),
    isLoading: false,
    isSaving: false,
  }),
}));

vi.mock("@/hooks/useProviders", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@/hooks/useProviders")>();
  return {
    ...actual,
    useSectionHealth: () => ({ health: mockHealth, reload: vi.fn() }),
  };
});

import { Sidebar } from "@/components/layout/Sidebar";
import { useEventStore } from "@/store/events";

// The sidebar carries the recent-runs block (a query) since 2026-08-23, so it
// mounts under a query client like the app does.
function render(ui: React.ReactElement) {
  const client = new QueryClient({
    defaultOptions: { queries: { retry: false, staleTime: Infinity } },
  });
  return rtlRender(<QueryClientProvider client={client}>{ui}</QueryClientProvider>);
}

beforeEach(() => {
  mockHealth = {};
  useEventStore.setState({ activeSection: "chats" });
});

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

describe("Sidebar — API-Keys alert dot", () => {
  it("shows a red attention dot on the profile button when a section reports error", () => {
    mockHealth = {
      realtime: { status: "error", reason: "no_credits", detail: "OpenAI GPT-Live: out of credit", subject_id: "openai-live" },
      subagents: { status: "ok", reason: "ok", detail: "", subject_id: null },
    };
    render(<Sidebar />);
    // The profile button IS the Settings hub's entry point now, so its dot is
    // the app-wide signal; the per-row dot lives on the hub's own nav (see
    // SettingsHubView.test).
    const dot = screen.getByTestId("sidebar-profile-attention");
    expect(dot).toBeTruthy();
    expect(dot.className).toMatch(/bg-destructive/);
  });

  it("ignores a failing section the API Keys page does not show", () => {
    mockHealth = {
      brain: { status: "error", reason: "rate_limited", detail: "OpenRouter: rate limited", subject_id: "openrouter" },
      stt: { status: "error", reason: "bad_key", detail: "Groq STT: key invalid", subject_id: "groq-api" },
    };
    render(<Sidebar />);
    expect(screen.queryByTestId("sidebar-profile-attention")).toBeNull();
  });

  it("stays calm (no dot) when nothing is broken", () => {
    mockHealth = {
      brain: { status: "needs_setup", reason: "not_configured", detail: "", subject_id: "openrouter" },
      tts: { status: "ok", reason: "ok", detail: "", subject_id: "gemini-flash-tts" },
      stt: { status: "unknown", reason: "unknown", detail: "", subject_id: "groq-api" },
    };
    render(<Sidebar />);
    expect(screen.queryByTestId("nav-alert-apikeys")).toBeNull();
  });

  it("stays calm when health hasn't loaded yet (empty map)", () => {
    mockHealth = {};
    render(<Sidebar />);
    expect(screen.queryByTestId("nav-alert-apikeys")).toBeNull();
  });
});