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.

11.9 KB
Raw Blame
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, expect, test, vi } from "vitest";
import { AgentRoutineDetail, routineRuns } from "./AgentRoutineDetail";
import type { LiveRoutine } from "../cardData";
import type { TaskDetail, TaskStep } from "@/lib/tasksApi";
import en from "@/i18n/locales/society/en.json";
import { RoutineChatHost } from "../chat/RoutineChatHost";
import { useRoutineNavigation } from "../chat/routineNavigation";

// Transcript rendering is covered separately; this suite exercises lazy lifecycle.
vi.mock("@/components/agentchat/AgentTimeline", () => ({ AgentTimeline: () => null }));

vi.mock("@/i18n", () => ({
  useLocaleChunk: () => {},
  useUiLanguage: () => "en",
  useI18nStore: { getState: () => ({ ui: "en" }) },
  useT: () => (key: string) => key.split(".").reduce<unknown>((value, part) => (value as Record<string, unknown>)?.[part], en) ?? key,
}));

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

function setup(multiple = false, steps: TaskStep[] = [], host = false) {
  const trigger = { type: "calendar", local_time: "08:00", timezone: "Europe/Berlin", weekdays: [0, 2] };
  const member: LiveRoutine = { id: "r1", title: "[agent:Mail] Inbox", prompt: "Read mail.", trigger, schedule: "", state: "scheduled", dueMs: null, lastRunMs: null };
  const members = multiple ? [member, { ...member, id: "r2" }] : [member];
  const tasks = new Map(members.map((m) => [m.id, {
    ...m, trigger_type: "calendar", spec: { action: { kind: "agent", prompt: "Identity\nRoutine:\nRead mail." } }, steps,
  } as unknown as TaskDetail]));
  const fetcher = vi.fn(async (url: string, init?: RequestInit) => {
    const method = init?.method ?? "GET";
    const id = url.split("/").at(-1)!;
    if (method === "PATCH" && url.startsWith("/api/tasks/")) tasks.get(id)!.state = JSON.parse(String(init?.body)).enabled ? "scheduled" : "paused";
    return { ok: true, status: 200, json: async () => tasks.get(id) ?? {} } as Response;
  });
  vi.stubGlobal("fetch", fetcher);
  const onClose = vi.fn();
  render(<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
    {host && <RoutineChatHost agentId="mail" onOpen={() => {}}><p data-testid="main-agent-chat">Main conversation</p></RoutineChatHost>}
    <AgentRoutineDetail routine={{ ...member, members }} agentId="mail" onClose={onClose} />
  </QueryClientProvider>);
  return { fetcher, onClose };
}

test("loads the instruction and saves all timings under the original ids", async () => {
  const { fetcher } = setup(true);
  await waitFor(() => expect((screen.getByLabelText("Name") as HTMLButtonElement).disabled).toBe(false));
  expect((screen.getByLabelText("Instruction") as HTMLTextAreaElement).value).toBe("Read mail.");
  fireEvent.change(screen.getByLabelText("Name"), { target: { value: "Updated inbox" } });
  fireEvent.click(screen.getByText("Save"));
  await screen.findByText("Saved.");
  const writes = fetcher.mock.calls.filter(([url, init]) => url.includes("/routines/") && init?.method === "PATCH");
  expect(writes.map(([url]) => url)).toEqual(["/api/society/agents/mail/routines/r1", "/api/society/agents/mail/routines/r2"]);
  expect(JSON.parse(String(writes[0][1]?.body))).toMatchObject({ title: "Updated inbox", prompt: "Read mail.", schedule: { weekdays: [0, 2] } });
});

test("opens only the selected execution socket and releases it on return", async () => {
  const sockets: { url: string; close: ReturnType<typeof vi.fn> }[] = [];
  class Socket {
    close = vi.fn();
    constructor(public url: string) { sockets.push(this); }
  }
  vi.stubGlobal("WebSocket", Socket);
  const sid = "society:mail:routine:r1:proof";
  const events = [{ event: "run_started" }, { event: "routine_chat", session_id: sid }, { event: "run_completed" }];
  const { fetcher } = setup(false, events.map((payload, seq) => ({ seq, kind: "log", timestamp_ns: (seq + 1) * 1e9, payload })), true);
  await screen.findByText("Open latest chat");
  expect(sockets).toHaveLength(0);
  expect(fetcher.mock.calls.some(([url]) => url.includes("agent-chat"))).toBe(false);
  fireEvent.click(screen.getByTestId("routine-run-r1:0"));
  // Cold module transformation includes the real dialog and socket store.
  await screen.findByTestId("routine-chat", {}, { timeout: 10000 });
  expect(screen.queryByRole("dialog")).toBeNull();
  expect(screen.queryByTestId("main-agent-chat")).toBeNull();
  expect(screen.getByTestId("agent-routine-detail")).toBeTruthy();
  expect(sockets).toHaveLength(1);
  expect(decodeURIComponent(sockets[0].url)).toContain(sid);
  fireEvent.click(screen.getByRole("button", { name: "Back to agent chat" }));
  await screen.findByTestId("main-agent-chat");
  expect(sockets[0].close).toHaveBeenCalledOnce();
  expect(fetcher.mock.calls.some(([url]) => url.endsWith("/cancel"))).toBe(false);
}, 15000);

test("latest means the newest execution even when only an older run has a separate chat", async () => {
  const events = [{ event: "run_started" }, { event: "routine_chat", session_id: "society:mail:routine:r1:older" }, { event: "run_completed" }, { event: "run_started" }, { event: "error" }];
  const { fetcher } = setup(false, events.map((payload, seq) => ({ seq, kind: "log", timestamp_ns: (seq + 1) * 1e12, payload })));
  await screen.findByText("Open latest chat");
  expect(useRoutineNavigation.getState().target).toBeNull();
  fireEvent.click(screen.getByText("Open latest chat"));
  expect(useRoutineNavigation.getState().target).toMatchObject({ sessionId: "society:mail", timestamp: 4000000, legacy: { taskId: "r1", startedMs: 4000000, finishedMs: 5000000 } });
  expect(fetcher.mock.calls.some(([url]) => url.includes("agent-chat"))).toBe(false);
  fireEvent.click(screen.getByTestId("routine-run-r1:0"));
  expect(useRoutineNavigation.getState().target?.sessionId).toBe("society:mail:routine:r1:older");
});

test("pauses every timing, tests once, and requires a delete confirmation", async () => {
  const { fetcher, onClose } = setup(true);
  await waitFor(() => expect((screen.getByRole("switch") as HTMLButtonElement).disabled).toBe(false));
  expect(screen.getByTestId("routine-switch-state").textContent).toBe("Active");
  fireEvent.click(screen.getByRole("switch"));
  await waitFor(() => expect(screen.getByRole("switch").getAttribute("aria-checked")).toBe("false"));
  // The switch's words follow its state: a paused routine never reads "Active".
  await waitFor(() => expect(screen.getByTestId("routine-switch-state").textContent).toBe("Paused"));
  expect(fetcher.mock.calls.filter(([, init]) => init?.method === "PATCH")).toHaveLength(2);
  fireEvent.click(screen.getByText("Test run"));
  await waitFor(() => expect(fetcher.mock.calls.filter(([url]) => url.endsWith("/run"))).toHaveLength(1));
  await waitFor(() => expect((screen.getByText("Delete") as HTMLButtonElement).disabled).toBe(false));
  fireEvent.click(screen.getByText("Delete"));
  expect(fetcher.mock.calls.some(([, init]) => init?.method === "DELETE")).toBe(false);
  fireEvent.click(screen.getAllByText("Delete")[1]);
  await waitFor(() => expect(onClose).toHaveBeenCalledOnce());
  expect(fetcher.mock.calls.filter(([, init]) => init?.method === "DELETE").map(([url]) => url)).toEqual(["/api/tasks/r2", "/api/tasks/r1"]);
});

test("schedule editing keeps calendar restrictions and adding uses the parent", async () => {
  const { fetcher } = setup();
  await waitFor(() => expect((screen.getByText("Add another schedule") as HTMLButtonElement).disabled).toBe(false));
  fireEvent.click(screen.getByText("Add another schedule"));
  fireEvent.click(screen.getByText("Save"));
  await screen.findByText("Saved.");
  const post = fetcher.mock.calls.find(([, init]) => init?.method === "POST");
  expect(JSON.parse(String(post?.[1]?.body))).toMatchObject({ parent_task_id: "r1", prompt: "Read mail.", schedule: { type: "every" } });
});

test("failed saves remain editable and expose the error", async () => {
  const { fetcher } = setup();
  await waitFor(() => expect((screen.getByLabelText("Name") as HTMLButtonElement).disabled).toBe(false));
  const original = fetcher.getMockImplementation()!;
  fetcher.mockImplementation(async (url, init) => init?.method === "PATCH" ? { ok: false, status: 409, json: async () => ({ detail: "Routine is running" }) } as Response : original(url, init));
  fireEvent.change(screen.getByLabelText("Name"), { target: { value: "Unsaved name" } });
  fireEvent.click(screen.getByText("Save"));
  expect((await screen.findByRole("alert")).textContent).toContain("Routine is running");
  expect((screen.getByLabelText("Name") as HTMLInputElement).value).toBe("Unsaved name");
});

test("history distinguishes success, failure, active and legacy executions", () => {
  const events = ["agent_result", "run_started", "agent_result", "run_completed", "run_started", "error", "run_started"];
  const steps = events.map((event, seq) => ({ seq, kind: "log", timestamp_ns: seq * 1e9, payload: { event, text: "Result" } } as TaskStep));
  expect(routineRuns("r1", steps).map((run) => run.status)).toEqual(["legacy", "completed", "failed", "running"]);
});


test("a skipped run is its own entry with its reason, also in an older two-step history", () => {
  const step = (seq: number, payload: Record<string, unknown>) => ({ seq, kind: "log", timestamp_ns: seq * 1e9, payload } as TaskStep);
  const steps = [
    step(1, { event: "run_started" }),
    step(2, { event: "skipped", reason: "waiting_capacity", code: "api_key_seat", provider: "gemini", message: "Held back." }),
    step(3, { event: "run_started" }),
    step(4, { event: "skipped", reason: "waiting_capacity", message: "Older skip." }),
    step(5, { event: "error", message: "Older skip." }),
    step(6, { event: "run_started" }),
    step(7, { event: "error", message: "Provider failed." }),
  ];
  const runs = routineRuns("r1", steps);
  expect(runs.map((run) => run.status)).toEqual(["skipped", "skipped", "failed"]);
  expect(runs[0]).toMatchObject({ skipCode: "api_key_seat", skipProvider: "gemini", text: "Held back." });
  expect(runs[1]).toMatchObject({ skipCode: "", text: "Older skip." });
});

test("the history shows a skipped run with its reason in plain words", async () => {
  setup(false, [
    { seq: 1, kind: "log", timestamp_ns: 1e12, payload: { event: "run_started" } },
    { seq: 2, kind: "log", timestamp_ns: 1e12 + 1, payload: { event: "skipped", code: "api_key_seat", provider: "gemini", message: "Held back." } },
  ] as TaskStep[]);
  const reason = await screen.findByTestId("routine-run-skip-reason");
  expect(reason.textContent).toBe("society.chat.skip_api_key_seat");
  expect(screen.getByTestId("routine-run-r1:1").getAttribute("aria-label")).toContain("Skipped");
});

test("editing the time saves the new value without dropping weekdays", async () => {
  const { fetcher } = setup();
  await waitFor(() => expect((screen.getByLabelText("Name") as HTMLInputElement).disabled).toBe(false));
  const timing = screen.getAllByRole("button").find((el) => el.textContent?.includes("08:00"))!;
  fireEvent.click(timing);
  fireEvent.change(screen.getByLabelText("Time"), { target: { value: "09:30" } });
  fireEvent.click(screen.getByText("Save"));
  await screen.findByText("Saved.");
  const patch = fetcher.mock.calls.find(([url, init]) => url.includes("/routines/") && init?.method === "PATCH");
  expect(JSON.parse(String(patch?.[1]?.body)).schedule).toMatchObject({ local_time: "09:30", weekdays: [0, 2] });
});


test("removing a timing cancels it instead of deleting its history", async () => {
  const { fetcher } = setup(true);
  await waitFor(() => expect((screen.getByText("Remove schedule") as HTMLButtonElement).disabled).toBe(false));
  fireEvent.click(screen.getByText("Remove schedule"));
  await waitFor(() => expect(fetcher.mock.calls.some(([url, init]) => url === "/api/tasks/r2/cancel" && init?.method === "POST")).toBe(true));
  expect(fetcher.mock.calls.some(([, init]) => init?.method === "DELETE")).toBe(false);
});