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.1 KB
Raw Blame
import { act, cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { AgentChatCatalog, AgentChatSession } from "@/lib/agentChatApi";
import type { IdeProject } from "@/lib/agenticIdeApi";
import { useIdeProjectsStore } from "@/store/ideProjects";
import { useIdeThreadsStore } from "@/store/ideThreads";
import { useThreadSubagentsStore } from "@/store/threadSubagents";

const api = vi.hoisted(() => ({
  fetchAgentChatCatalog: vi.fn(),
  fetchAgentConnections: vi.fn(),
  fetchProviderHealth: vi.fn(),
  fetchAgentChatSessions: vi.fn(),
  createAgentChatSession: vi.fn(),
  sendAgentChatMessage: vi.fn(),
  patchAgentChatSession: vi.fn(),
}));
vi.mock("@/lib/agentChatApi", async (importOriginal) => ({ ...(await importOriginal<typeof import("@/lib/agentChatApi")>()), ...api }));
const teams = vi.hoisted(() => ({ rows: [] as unknown[] }));
vi.mock("@/lib/teamThreadsApi", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@/lib/teamThreadsApi")>()),
  useTeamThreads: () => ({ data: teams.rows }),
}));
vi.mock("@/lib/gitApi", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@/lib/gitApi")>()),
  inspectGit: vi.fn().mockResolvedValue({ is_repo: false, branches: [], changes: [], remotes: [] }),
  prepareGit: vi.fn(),
}));
vi.mock("@/lib/chatLibraryApi", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@/lib/chatLibraryApi")>()),
  fetchProjectLaunchers: vi.fn().mockResolvedValue({ file_manager: true, editors: [{ id: "code", label: "VS Code" }], remote_url: null, remote_label: null }),
}));

import { ThreadView } from "./ThreadView";
import { ThreadTree } from "./ThreadTree";
import { useThreadChatStore } from "./threadModel";

class FakeSocket {
  static instances: FakeSocket[] = [];
  onopen: (() => void) | null = null;
  onmessage: ((msg: { data: string }) => void) | null = null;
  onclose: (() => void) | null = null;
  onerror: (() => void) | null = null;
  constructor(readonly url: string) { FakeSocket.instances.push(this); }
  close() { /* the test drives frames by hand */ }
}

const CATALOG: AgentChatCatalog = {
  default_cwd: "/home",
  shell: "bash",
  selection: null,
  providers: [{
    id: "claude-api", label: "Anthropic Claude", family: "anthropic", runner: "claude-cli", agent: "claude",
    models_source: "curated", curated_models: [{ id: "claude-opus-5-5", label: "Opus 5.5" }], default_model: "",
    keyless: false, native_resume: true, effort_levels: ["low", "high"], default_effort: "high",
    permission_modes: [{ id: "default", label: "Ask first", description: "" }], default_permission_mode: "default",
    cli_installed: true, typeahead: [],
  }],
} as AgentChatCatalog;

const project = { id: "p1", path: "/repo", name: "App", color: null, pinned: false, archived: false, scratch: false,
  created_at: 0, last_opened_at: 0, exists: true, chats: 0, workspaces: [] } as IdeProject;

function sessionRow(extra: Partial<AgentChatSession> = {}): AgentChatSession {
  return { session_id: "s1", title: "fix the login test", provider: "claude-api", model: "", effort: "high", cwd: "/repo",
    permission_mode: "default", surface: "agent", vendor_session: null, created_ms: 1, updated_ms: 2, message_count: 1, preview: "", ...extra };
}

beforeEach(() => {
  localStorage.clear();
  vi.stubGlobal("WebSocket", FakeSocket);
  FakeSocket.instances = [];
  api.fetchAgentChatCatalog.mockReset().mockResolvedValue(CATALOG);
  api.fetchAgentConnections.mockReset().mockResolvedValue([]);
  api.fetchProviderHealth.mockReset().mockResolvedValue([]);
  api.fetchAgentChatSessions.mockReset().mockResolvedValue([]);
  api.createAgentChatSession.mockReset().mockResolvedValue(sessionRow());
  api.sendAgentChatMessage.mockReset().mockResolvedValue({ turn_id: "t1" });
  api.patchAgentChatSession.mockReset().mockResolvedValue(sessionRow());
  useIdeProjectsStore.setState({ projects: [project], activeWorkspaceId: null });
  useIdeThreadsStore.setState({ layout: "threads", selection: { projectId: "p1", sessionId: null }, projectOf: {}, seen: {}, archived: {}, order: {}, focusNonce: 0 });
  useThreadChatStore.getState().newChat();
  useThreadSubagentsStore.setState({ sessionId: null, rows: [], open: null });
});

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

describe("ThreadView", () => {
  it("starts a new thread in the project's folder with its first message", async () => {
    render(<ThreadView onScreen />);
    expect(screen.getByText(/What should we build in/)).toBeTruthy();
    expect(screen.getByTestId("thread-project-picker").textContent).toContain("App");
    await waitFor(() => expect(useThreadChatStore.getState().draft.provider).toBe("claude-api"));
    await waitFor(() => expect(useThreadChatStore.getState().draft.cwd).toBe("/repo"));

    const box = screen.getByTestId("thread-composer-input");
    fireEvent.change(box, { target: { value: "fix the login test" } });
    await act(async () => { fireEvent.keyDown(box, { key: "Enter" }); });

    await waitFor(() => expect(api.sendAgentChatMessage).toHaveBeenCalled());
    expect(api.createAgentChatSession).toHaveBeenCalledWith(expect.objectContaining({ provider: "claude-api", cwd: "/repo", surface: "agent" }));
    expect(api.sendAgentChatMessage.mock.calls[0][1]).toBe("fix the login test");
    await waitFor(() => expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: "s1" }));
    expect(useIdeThreadsStore.getState().projectOf).toEqual({ s1: "p1" });
  });

  it("starts a new thread on the agent and model the person picked last", async () => {
    localStorage.setItem("jarvis.ide.threadSeat.v1", JSON.stringify({ provider: "claude-api", model: "claude-opus-5-5", effort: "low", permissionMode: "default" }));
    render(<ThreadView onScreen />);
    await waitFor(() => expect(useThreadChatStore.getState().draft).toEqual(expect.objectContaining({ provider: "claude-api", model: "claude-opus-5-5", effort: "low" })));
    await waitFor(() => expect(screen.getByTestId("thread-model-picker").textContent).toContain("Opus 5.5"));
  });

  it("files a thread started in a draft under its project when the person moved on meanwhile", async () => {
    let finishCreate: (row: AgentChatSession) => void = () => {};
    api.createAgentChatSession.mockReturnValue(new Promise<AgentChatSession>((resolve) => { finishCreate = resolve; }));
    render(<ThreadView onScreen />);
    await waitFor(() => expect(useThreadChatStore.getState().draft.cwd).toBe("/repo"));
    const box = screen.getByTestId("thread-composer-input");
    fireEvent.change(box, { target: { value: "start something" } });
    await act(async () => { fireEvent.keyDown(box, { key: "Enter" }); });
    await waitFor(() => expect(api.createAgentChatSession).toHaveBeenCalled());

    // The person opens another thread before the first one exists.
    act(() => useIdeThreadsStore.getState().openThread("other", "p1"));
    await act(async () => { finishCreate(sessionRow({ session_id: "s-new" })); });

    await waitFor(() => expect(useIdeThreadsStore.getState().projectOf).toEqual({ "s-new": "p1" }));
    expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: "other" });
    await waitFor(() => expect(useThreadChatStore.getState().activeSessionId).toBe("other"));
  });

  it("draws the turn: the message, the work as one line, the answer and how long it took", async () => {
    useIdeThreadsStore.setState({ selection: { projectId: "p1", sessionId: "s1" } });
    api.fetchAgentChatSessions.mockResolvedValue([sessionRow()]);
    render(<ThreadView onScreen />);
    const store = useThreadChatStore.getState();
    act(() => {
      store.ingest({ seq: 1, ts_ms: 1000, kind: "user_message", payload: { text: "fix the login test" } });
      store.ingest({ seq: 2, ts_ms: 1000, kind: "turn_started", payload: { turn_id: "t1", provider: "claude-api", model: "", effort: "high", runner: "claude-cli" } });
      store.ingest({ seq: 3, ts_ms: 1100, kind: "tool_call", payload: { turn_id: "t1", call_id: "c1", name: "Bash", input: { command: "npm test" } } });
      store.ingest({ seq: 4, ts_ms: 1200, kind: "tool_result", payload: { turn_id: "t1", call_id: "c1", output: "ok", is_error: false, duration_ms: 100 } });
      store.ingest({ seq: 5, ts_ms: 1300, kind: "tool_call", payload: { turn_id: "t1", call_id: "c2", name: "Read", input: { file_path: "/repo/src/login.ts" } } });
      store.ingest({ seq: 6, ts_ms: 1400, kind: "tool_result", payload: { turn_id: "t1", call_id: "c2", output: "code", is_error: false, duration_ms: 50 } });
      store.ingest({ seq: 7, ts_ms: 1500, kind: "assistant_text", payload: { turn_id: "t1", message_id: "m1", text: "The test passes now." } });
      store.ingest({ seq: 8, ts_ms: 4000, kind: "turn_finished", payload: { turn_id: "t1", status: "done", duration_ms: 3000, usage: null, error: null } });
    });
    expect(screen.getByTestId("thread-user-message").textContent).toContain("fix the login test");
    expect(screen.getByText("The test passes now.")).toBeTruthy();
    expect(screen.getByTestId("thread-turn").getAttribute("data-status")).toBe("done");
    expect(screen.getByText(/Worked for/)).toBeTruthy();
    // A finished turn folds every step behind one line; the answer stays in view.
    expect(screen.getByTestId("thread-worked-for").textContent).toContain("Worked for 3.0s");
    expect(screen.queryByTestId("thread-worked-steps")).toBeNull();
    expect(screen.queryByText("npm test")).toBeNull();
    fireEvent.click(screen.getByTestId("thread-worked-for"));
    expect(screen.getByTestId("thread-worked-steps")).toBeTruthy();
  });
});

describe("ThreadTree", () => {
  it("lists a project's threads by the CLI's own title with the agent's mark", async () => {
    api.fetchAgentChatSessions.mockResolvedValue([sessionRow({ cli_title: "Fix login test" }), sessionRow({ session_id: "s2", cwd: "/elsewhere", title: "stray" })]);
    render(<ThreadTree />);
    await waitFor(() => expect(screen.getByText("Fix login test")).toBeTruthy());
    expect(screen.queryByText("stray")).toBeNull();
    fireEvent.click(screen.getByText("Fix login test"));
    expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: "s1" });
    fireEvent.click(screen.getByTestId("thread-new-p1"));
    expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: null });
  });

  it("archives a thread from its menu and brings it back from the project's archive", async () => {
    api.fetchAgentChatSessions.mockResolvedValue([sessionRow({ cli_title: "Fix login test" })]);
    useIdeThreadsStore.setState({ selection: { projectId: "p1", sessionId: "s1" } });
    render(<ThreadTree />);
    await waitFor(() => expect(screen.getByText("Fix login test")).toBeTruthy());
    fireEvent.contextMenu(screen.getByTestId("thread-row-s1"));
    fireEvent.click(screen.getByRole("menuitem", { name: /Archive/ }));
    expect(screen.queryByTestId("thread-row-s1")).toBeNull();
    expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: null });
    expect(JSON.parse(localStorage.getItem("jarvis.agenticIde.threadArchived.v1") ?? "{}")).toHaveProperty("s1");

    fireEvent.click(screen.getByTestId("thread-archived-p1"));
    fireEvent.click(screen.getByRole("button", { name: "Restore Fix login test" }));
    expect(screen.getByTestId("thread-row-s1")).toBeTruthy();
    expect(screen.queryByTestId("thread-archived-p1")).toBeNull();
  });

  it("lists a team thread once and keeps its members out of the thread list", async () => {
    teams.rows = [{ team_id: "t1", title: "Add a retry", cwd: "/repo", mode: "peers", state: "needs_input", phase: "review", reason: "", created_ms: 1, updated_ms: 2, members: [] }];
    api.fetchAgentChatSessions.mockResolvedValue([
      sessionRow({ cli_title: "Fix login test" }),
      sessionRow({ session_id: "s2", cli_title: "Add a retry · Claude", team_id: "t1" }),
    ]);
    try {
      render(<ThreadTree />);
      await waitFor(() => expect(screen.getByText("Fix login test")).toBeTruthy());
      expect(screen.queryByText("Add a retry · Claude")).toBeNull();
      fireEvent.click(screen.getByTestId("team-thread-row-t1"));
      expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: null, team: "t1" });
      expect(screen.getByTestId("team-thread-row-t1").getAttribute("aria-current")).toBe("page");
      // The ordinary thread is no longer the one on screen.
      expect(screen.getByText("Fix login test").closest("button")?.getAttribute("aria-current")).toBeNull();
      fireEvent.click(screen.getByTestId("team-thread-new-p1"));
      expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: null, team: "new" });
    } finally {
      teams.rows = [];
    }
  });

  it("leads from a member's own thread back to its team, and keeps the team marked meanwhile", async () => {
    teams.rows = [{ team_id: "t1", title: "Add a retry", cwd: "/repo", mode: "peers", state: "running", phase: "review", reason: "", created_ms: 1, updated_ms: 2, members: [] }];
    const member = sessionRow({ session_id: "s2", cli_title: "Add a retry · Claude", team_id: "t1" });
    api.fetchAgentChatSessions.mockResolvedValue([member]);
    try {
      useIdeThreadsStore.getState().openMemberThread("s2", "p1", "t1");
      const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
      render(<QueryClientProvider client={client}><ThreadTree /><ThreadView onScreen /></QueryClientProvider>);
      const back = await screen.findByTestId("thread-back-to-team");
      expect(back.textContent).toContain("Add a retry");
      expect(screen.getByTestId("team-thread-row-t1").getAttribute("aria-current")).toBe("page");
      fireEvent.click(within(back).getByRole("button"));
      expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: null, team: "t1" });
    } finally {
      teams.rows = [];
    }
  });

  it("lists the open thread's sub-agents under it as threads that open, nest and fold", async () => {
    api.fetchAgentChatSessions.mockResolvedValue([sessionRow({ cli_title: "Fix login test" })]);
    useIdeThreadsStore.setState({ selection: { projectId: "p1", sessionId: "s1" } });
    useThreadSubagentsStore.setState({ open: null });
    useThreadSubagentsStore.getState().publish("s1", [
      { id: "a1", parentId: null, depth: 0, title: "Explore the auth code", agentType: "Explore", status: "running", waiting: false },
      { id: "a2", parentId: "a1", depth: 1, title: "Read the tests", agentType: "", status: "done", waiting: false },
    ]);
    render(<ThreadTree />);
    await waitFor(() => expect(screen.getByText("Fix login test")).toBeTruthy());
    const rows = screen.getAllByTestId("thread-tree-subagent");
    expect(rows.map((row) => row.textContent)).toEqual(["Explore the auth code", "Read the tests"]);
    expect(rows.map((row) => row.getAttribute("data-status"))).toEqual(["running", "done"]);
    expect(rows[1].style.paddingLeft).toBe("46px");

    fireEvent.click(rows[1]);
    expect(useThreadSubagentsStore.getState().open).toEqual({ sessionId: "s1", id: "a2" });
    expect(screen.getAllByTestId("thread-tree-subagent")[1].getAttribute("aria-current")).toBe("page");
    expect(screen.getByText("Fix login test").closest("button")?.getAttribute("aria-current")).toBe("true");

    // The thread's own row leads back to the main conversation.
    fireEvent.click(screen.getByText("Fix login test"));
    expect(useThreadSubagentsStore.getState().open).toBeNull();

    fireEvent.click(screen.getByTestId("thread-subagents-fold"));
    expect(screen.queryAllByTestId("thread-tree-subagent")).toHaveLength(0);
    fireEvent.click(screen.getByTestId("thread-subagents-fold"));
    expect(screen.getAllByTestId("thread-tree-subagent")).toHaveLength(2);
  });

  it("shows no sub-agent rows for a thread that is not the one on screen", async () => {
    api.fetchAgentChatSessions.mockResolvedValue([sessionRow({ cli_title: "Fix login test" })]);
    useIdeThreadsStore.setState({ selection: { projectId: "p1", sessionId: null } });
    useThreadSubagentsStore.getState().publish("s1", [
      { id: "a1", parentId: null, depth: 0, title: "Explore", agentType: "", status: "running", waiting: false },
    ]);
    render(<ThreadTree />);
    await waitFor(() => expect(screen.getByText("Fix login test")).toBeTruthy());
    expect(screen.queryByTestId("thread-tree-subagents")).toBeNull();
    expect(screen.queryByTestId("thread-subagents-fold")).toBeNull();
  });

  it("starts a thread or a team from the rows on top, in the project in view, and leads every thread with its agent's logo", async () => {
    api.fetchAgentChatSessions.mockResolvedValue([sessionRow({ cli_title: "Fix login test" })]);
    render(<ThreadTree />);
    await waitFor(() => expect(screen.getByText("Fix login test")).toBeTruthy());
    const row = screen.getByTestId("thread-row-s1");
    const mark = await waitFor(() => within(row).getByTestId("agent-mark-claude"));
    const title = within(row).getByText("Fix login test");
    expect(mark.compareDocumentPosition(title) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
    fireEvent.click(screen.getByTestId("thread-tree-new-thread"));
    expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: null });
    fireEvent.click(screen.getByTestId("thread-tree-new-team"));
    expect(useIdeThreadsStore.getState().selection).toEqual({ projectId: "p1", sessionId: null, team: "new" });
  });

  it("moves a thread with Alt and the arrow keys and keeps that order", async () => {
    api.fetchAgentChatSessions.mockResolvedValue([
      sessionRow({ session_id: "s1", cli_title: "Older", updated_ms: 1 }),
      sessionRow({ session_id: "s2", cli_title: "Newer", updated_ms: 5 }),
    ]);
    render(<ThreadTree />);
    await waitFor(() => expect(screen.getByText("Older")).toBeTruthy());
    const titles = () => screen.getAllByTestId(/^thread-row-/).map((row) => row.textContent);
    expect(titles()[0]).toContain("Newer");
    fireEvent.keyDown(screen.getByText("Older"), { key: "ArrowUp", altKey: true });
    expect(titles()[0]).toContain("Older");
    expect(useIdeThreadsStore.getState().order).toEqual({ p1: ["s1", "s2"] });
  });
});