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.

13.9 KB
Raw Blame
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeAll, expect, it } from "vitest";
import { loadLocaleChunk } from "@/i18n";
import { AgentMessageActivity, RoutineActivity, TeamEventLine, routineTask } from "./ChatActivity";
import { ReasoningTrace, WorkTrace } from "@/components/agentchat/WorkTrace";
import type { InternalMessageItem, ReasoningBlock, ToolBlock } from "@/components/agentchat/reduce";

beforeAll(() => loadLocaleChunk("society"));
afterEach(cleanup);

/** A finished, answered turn folds its work behind "Thought for …"; open every fold. */
const openWork = () => {
  for (const toggle of Array.from(document.querySelectorAll<HTMLElement>("[data-testid='conversation-work-fold'][data-open='false'] > button"))) fireEvent.click(toggle);
};

const envelope = "Scheduled routine task-123. Follow your CURRENT standing instructions and permissions.\nUse your memory and conversation archive for prior results. For information watches, check sources and dates, remember last-seen items, and report only meaningful new findings.\n\n";

it("folds only a full scheduler envelope, preserving ordinary user messages", () => {
  expect(routineTask(envelope + "Check the inbox.\nSummarize changes.")).toBe("Check the inbox.\nSummarize changes.");
  expect(routineTask("Scheduled routine task-123. Please explain this.")).toBeNull();
  expect(routineTask("Please quote:\n" + envelope)).toBeNull();
  const background = envelope.replace(" and permissions.\n", ".\nThis execution has its own background chat with bypass permissions.\n");
  expect(routineTask(background + "Check the inbox.")).toBe("Check the inbox.");
  render(<RoutineActivity task={"Check the inbox.\nSummarize changes."} original={envelope + "Check the inbox.\nSummarize changes."} />);
  expect(screen.queryByText("Summarize changes.")).toBeNull();
  fireEvent.click(screen.getByRole("button", { name: "Routine check · Check the inbox." }));
  expect(screen.getAllByText(/Summarize changes\./, { selector: "p" }).length).toBeGreaterThan(0);
});

it("shows live reasoning, hides it as soon as thinking finishes, and permits reopening", () => {
  const block: ReasoningBlock = { kind: "reasoning", id: "r", text: "Inspect the inbox first.", live: true, startedMs: Date.now(), durationMs: null };
  const { rerender } = render(<ReasoningTrace block={block} turnLive compact />);
  expect(screen.getByText(block.text)).toBeTruthy();
  rerender(<ReasoningTrace block={{ ...block, live: false, durationMs: 3000 }} turnLive compact />);
  expect(screen.queryByText(block.text)).toBeNull();
  fireEvent.click(screen.getByRole("button", { name: "Thought for 3.0s" }));
  expect(screen.getByText(block.text)).toBeTruthy();
  rerender(<ReasoningTrace block={{ ...block, live: false, durationMs: 3000 }} turnLive={false} compact />);
  expect(screen.queryByText(block.text)).toBeNull();
});

it("folds a live manual choice on completion even when the provider never ends reasoning", () => {
  const block: ReasoningBlock = { kind: "reasoning", id: "r", text: "Inspect the inbox first.", live: true, startedMs: Date.now(), durationMs: null };
  const { rerender } = render(<ReasoningTrace block={block} turnLive compact />);
  rerender(<ReasoningTrace block={block} turnLive={false} compact />);
  expect(screen.queryByText(block.text)).toBeNull();
  fireEvent.click(screen.getByRole("button", { name: "Thought" }));
  expect(screen.getByText(block.text)).toBeTruthy();
});

it("keeps live and interrupted conversation tools in the left lane", () => {
  const live: ToolBlock = {
    kind: "tool", callId: "live", name: "search_files", input: { pattern: "archive" },
    output: null, isError: false, durationMs: null, approval: null, startedMs: 0,
  };
  const thought: ReasoningBlock = { kind: "reasoning", id: "r", text: "Inspect archive.", live: false, durationMs: 2000, startedMs: 0 };
  const { container, rerender } = render(<WorkTrace conversation status="running" startedMs={0} durationMs={null} blocks={[thought, live]} />);
  const lane = screen.getByTestId("work-trace");
  expect(lane.hasAttribute("data-conversation")).toBe(true);
  expect(lane.className).toMatch(/max-w-\[44rem\]/);
  expect(lane.className).toMatch(/self-start/);
  expect(container.querySelectorAll(".mx-auto")).toHaveLength(0);
  expect(container.querySelector("[data-trace-entry='call']")?.closest("[data-testid='work-trace']")).toBe(lane);
  rerender(<WorkTrace conversation status="cancelled" startedMs={0} durationMs={4100} blocks={[
    thought,
    { kind: "text", id: "reply", text: "I will send the mail next." },
    live,
  ]} />);
  // The stopped turn folds its work; opened, the model's words, the call
  // that never returned and the reply read in order — the reply last.
  openWork();
  const text = screen.getByTestId("work-trace").textContent!;
  expect(text.indexOf("Inspect archive.")).toBeLessThan(text.indexOf("Searched for archive"));
  expect(text.indexOf("Searched for archive")).toBeLessThan(text.indexOf("I will send the mail next."));
  expect(text).toContain("Stopped");
  expect(container.querySelectorAll(".mx-auto")).toHaveLength(0);
  expect(screen.getByTestId("work-trace").className).toMatch(/self-start/);
});

it("keeps a failed tool visible while folding the surrounding work", () => {
  const tool: ToolBlock = { kind: "tool", callId: "a", name: "read_file", input: { path: "report.csv" }, output: "Report contents", isError: false, durationMs: 100, approval: null, startedMs: 0 };
  render(<WorkTrace conversation status="done" startedMs={0} durationMs={2000} blocks={[
    tool,
    { ...tool, callId: "error", isError: true, output: "Upload failed" },
    { kind: "text", id: "reply", text: "I could not finish." },
  ]} />);
  expect(screen.getByText("Upload failed")).toBeTruthy();
  expect(screen.getByText("I could not finish.")).toBeTruthy();
  expect(screen.queryByText(/: Upload failed/)).toBeNull();
  openWork();
  // One quiet line for the stretch; it opens to the calls, and the failed
  // one says why under its own line.
  fireEvent.click(screen.getByRole("button", { name: /^Read files$/ }));
  expect(screen.getByText(/: Upload failed/)).toBeTruthy();
  // The raw output stays one tap further, under the call.
  expect(screen.queryByText("Report contents")).toBeNull();
  fireEvent.click(screen.getAllByRole("button", { name: /^Read report\.csv/ })[0]);
  expect(screen.getByText("Report contents")).toBeTruthy();
});

it("keeps failures and pending approvals visible in the conversation style", () => {
  const base: ToolBlock = { kind: "tool", callId: "failure", name: "send_message", input: {}, output: "Delivery failed", isError: true, durationMs: 100, approval: null, startedMs: 0 };
  render(<WorkTrace conversation status="done" startedMs={0} durationMs={1000} blocks={[
    base,
    { ...base, callId: "approval", isError: false, output: null, approval: { approvalId: "ap", summary: "Send this message?", decision: null } },
  ]} onDecide={() => undefined} />);
  expect(screen.getAllByText(/Delivery failed/).length).toBeGreaterThan(0);
  expect(screen.getByText("Send this message?")).toBeTruthy();
  expect(screen.getByRole("button", { name: "Approve" })).toBeTruthy();
  // With no other work to fold, both items stay in place and need no toggle.
  expect(screen.queryByTestId("conversation-work-fold")).toBeNull();
});

it("shows agent message direction and truthful delivery state without preview clutter", () => {
  const item: InternalMessageItem = { type: "internal", id: "m", tsMs: 0, outgoing: { recipientId: "scout", recipientName: "Scout" }, message: { message_id: "m", sender_id: "lead", sender_name: "Lead", sender_kind: "agent", text: "Please check the report.", prompt: "", trace_id: "trace", turn_id: "turn", status: "queued", error: "" } };
  const { rerender } = render(<AgentMessageActivity item={item} roster={[]} />);
  expect(screen.getByRole("button", { name: /Message to Scout.*Queued/ })).toBeTruthy();
  expect(screen.queryByText(item.message.text)).toBeNull();
  fireEvent.click(screen.getByRole("button"));
  expect(screen.getByText(item.message.text)).toBeTruthy();
  rerender(<AgentMessageActivity item={{ ...item, message: { ...item.message, status: "failed", error: "Connection closed" } }} roster={[]} />);
  expect(screen.getByRole("button", { name: /Message failed Scout/ })).toBeTruthy();
  expect(screen.getByRole("alert").textContent).toBe("Connection closed");
});

it("keeps the reply last, even when work followed it", () => {
  const tool: ToolBlock = { kind: "tool", callId: "a", name: "read_file", input: { path: "report.csv" }, output: "Report contents", isError: false, durationMs: 100, approval: null, startedMs: 0 };
  const blocks = [tool, { ...tool, callId: "b", name: "write_file" }, { kind: "text" as const, id: "reply", text: "Your report is ready." }, { ...tool, callId: "error", isError: true, output: "Upload failed" }];
  const { rerender } = render(<WorkTrace conversation status="running" startedMs={0} durationMs={null} blocks={blocks} />);
  // While it runs, finished calls already read as their line.
  expect(screen.getByRole("button", { name: /^Read a file, created a file/ })).toBeTruthy();
  rerender(<WorkTrace conversation status="done" startedMs={0} durationMs={1000} blocks={blocks} />);
  // Finished: only the reply shows until the work is opened.
  expect(screen.queryByRole("button", { name: /^Read a file, created a file/ })).toBeNull();
  openWork();
  const text = screen.getByTestId("work-trace").textContent ?? "";
  expect(text.indexOf("Read a file, created a file")).toBeLessThan(text.indexOf("Your report is ready."));
  // The call after the reply is drawn above it: nobody scrolls back up for the answer.
  expect(text.indexOf("Read report.csv")).toBeLessThan(text.indexOf("Your report is ready."));
  expect(screen.getAllByText("Your report is ready.")).toHaveLength(1);
  // The call after the reply failed and says why without opening anything.
  expect(screen.getByText(/: Upload failed/)).toBeTruthy();
  // Each stretch opens to its calls, and closes again.
  const stretch = screen.getByRole("button", { name: /^Read a file, created a file/ });
  fireEvent.click(stretch);
  expect(screen.getByRole("button", { name: /^Created report\.csv/ })).toBeTruthy();
  fireEvent.click(stretch);
  expect(screen.queryByRole("button", { name: /^Created report\.csv/ })).toBeNull();
});

it("does not offer a thought toggle when the turn is only a reply", () => {
  render(<WorkTrace conversation status="done" startedMs={0} durationMs={1000} blocks={[{ kind: "text", id: "a", text: "Hello." }]} />);
  expect(screen.queryByTestId("conversation-work-fold")).toBeNull();
  expect(screen.getByText("Hello.")).toBeTruthy();
});

it("tells intermediate replies as narration once the turn completes", () => {
  const tool: ToolBlock = { kind: "tool", callId: "a", name: "read_file", input: { path: "report.csv" }, output: "Report contents", isError: false, durationMs: 100, approval: null, startedMs: 0 };
  const blocks = [
    tool,
    { kind: "text" as const, id: "think", text: "I will inspect the files first." },
    { ...tool, callId: "b", name: "list_dir" },
    { kind: "text" as const, id: "final", text: "The report is ready." },
  ];
  const { container, rerender } = render(<WorkTrace conversation status="running" startedMs={0} durationMs={null} blocks={blocks} />);
  expect(screen.getByText("I will inspect the files first.")).toBeTruthy();
  rerender(<WorkTrace conversation status="done" startedMs={0} durationMs={4000} blocks={blocks} />);
  openWork();
  // The words between calls read as narration, not as a second answer.
  const narration = container.querySelector("[data-trace-entry='narration']");
  expect(narration?.textContent).toContain("I will inspect the files first.");
  expect(screen.getByText("The report is ready.")).toBeTruthy();
});

it("folds a finished turn to the reply and opens the whole trace again", () => {
  const tool: ToolBlock = { kind: "tool", callId: "a", name: "read_file", input: { path: "report.csv" }, output: "Report contents", isError: false, durationMs: 100, approval: null, startedMs: 0 };
  const thought: ReasoningBlock = { kind: "reasoning", id: "r", text: "Check the report first.", live: false, durationMs: 2000, startedMs: 0 };
  const blocks = [thought, tool, { kind: "text" as const, id: "final", text: "The report is ready." }];
  const { rerender } = render(<WorkTrace conversation status="running" startedMs={0} durationMs={null} blocks={blocks} />);
  // While the turn works, its work stays in view.
  expect(screen.queryByTestId("conversation-work-fold")).toBeNull();
  rerender(<WorkTrace conversation status="done" startedMs={0} durationMs={65_000} blocks={blocks} />);
  const toggle = screen.getByRole("button", { name: /^Thought for/ });
  expect(toggle.getAttribute("aria-expanded")).toBe("false");
  expect(screen.queryByText(/Check the report first/)).toBeNull();
  expect(screen.getByText("The report is ready.")).toBeTruthy();
  fireEvent.click(toggle);
  expect(screen.getByText(/Check the report first/)).toBeTruthy();
  const text = screen.getByTestId("work-trace").textContent!;
  expect(text.indexOf("Check the report first")).toBeLessThan(text.indexOf("The report is ready."));
  fireEvent.click(toggle);
  expect(screen.queryByText(/Check the report first/)).toBeNull();
});

it("shows a turn the team engine gave a lead as a quiet line, never as a message", () => {
  const { rerender } = render(<TeamEventLine event="update" text={"Launch the page: 2 results"} />);
  const line = screen.getByTestId("team-event-line");
  expect(line.getAttribute("data-event")).toBe("update");
  expect(line.textContent).toContain("Results from the team (2)");
  expect(line.textContent).toContain("· Launch the page");
  expect(line.textContent).not.toContain("2 results");
  rerender(<TeamEventLine event="check" text="Launch the page" />);
  expect(screen.getByTestId("team-event-line").textContent).toBe("Asked for the report· Launch the page");
  rerender(<TeamEventLine event="resumed" text="" />);
  expect(screen.getByTestId("team-event-line").textContent).toBe("Goal continued");
});