Files
nanobot/webui/src/tests/automation-chat-binding.test.tsx
T

125 lines
7.7 KiB
TypeScript
Raw Normal View History

import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { AutomationDetailDialog } from "@/components/settings/system/AutomationsSettings";
import { fetchAutomationChats } from "@/lib/api";
import type { AutomationChatsPayload, SessionAutomationJob } from "@/lib/types";
vi.mock("@/lib/api", () => ({ fetchAutomationChats: vi.fn() }));
const source = { id: "source", title: "My planning", channel: "websocket" };
const target = { id: "target", title: "Product team", channel: "telegram" };
const choices: AutomationChatsPayload = { revision: "rev-1", current: source, chats: [source, target] };
const job: SessionAutomationJob = {
id: "daily", name: "Daily report", enabled: true, chat_binding_revision: "rev-1",
schedule: { kind: "every", every_ms: 86400000 }, state: {},
payload: { message: "Summarize the work." }, origin: { ...source, session_key: "websocket:source" },
};
const props = { open: true, locale: "en", actionKey: null, error: null,
onOpenChange: vi.fn(), onAction: vi.fn(), onRequestEdit: vi.fn(), onRequestDelete: vi.fn() };
beforeEach(() => { vi.mocked(fetchAutomationChats).mockResolvedValue(choices); });
afterEach(() => { cleanup(); vi.clearAllMocks(); });
async function choose() {
const control = await screen.findByRole("combobox", { name: "Run and reply in" });
await waitFor(() => expect(control).toBeEnabled());
fireEvent.keyDown(control, { key: "ArrowDown" });
fireEvent.click(await screen.findByRole("option", { name: /Product team/ }));
await screen.findByRole("heading", { name: "Change chat" });
}
it("keeps the original route until acknowledgement and saves the reviewed prompt", async () => {
let finish!: () => void;
const save = vi.fn(() => new Promise<void>(resolve => { finish = resolve; }));
const user = userEvent.setup();
const { rerender } = render(<AutomationDetailDialog {...props} job={job} onChangeChat={save} />);
await choose();
expect(save).not.toHaveBeenCalled();
expect(screen.getByRole("group", { name: "Now" })).toHaveTextContent("My planning");
await user.clear(screen.getByRole("textbox", { name: "Task instructions" }));
await user.type(screen.getByRole("textbox", { name: "Task instructions" }), "New instructions");
await user.click(screen.getByRole("button", { name: "Confirm change" }));
expect(save).toHaveBeenCalledWith(job, { target_id: "target", revision: "rev-1", message: "New instructions" });
expect(screen.getByRole("button", { name: "Changing…" })).toBeDisabled();
expect(screen.queryByRole("status")).not.toBeInTheDocument();
let refresh!: (value: AutomationChatsPayload) => void;
vi.mocked(fetchAutomationChats).mockImplementation(() => new Promise(resolve => { refresh = resolve; }));
await act(async () => {
rerender(<AutomationDetailDialog {...props} job={{ ...job, chat_binding_revision: "rev-2" }} onChangeChat={save} />);
finish();
});
expect(await screen.findByRole("status")).toHaveTextContent("Applies from the next run");
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toHaveTextContent("Product team");
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toBeDisabled();
await act(async () => refresh({ ...choices, current: target, revision: "rev-2" }));
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toHaveFocus();
expect(props.onAction).not.toHaveBeenCalled();
});
it("cancels a chat change without saving the edited instructions", async () => {
const user = userEvent.setup();
const save = vi.fn();
render(<AutomationDetailDialog {...props} job={job} onChangeChat={save} />);
await choose();
await user.clear(screen.getByRole("textbox", { name: "Task instructions" }));
await user.type(screen.getByRole("textbox", { name: "Task instructions" }), "Discard this draft");
await user.click(screen.getByRole("button", { name: "Cancel" }));
expect(save).not.toHaveBeenCalled();
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toHaveTextContent("My planning");
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toHaveFocus();
await choose();
expect(screen.getByRole("textbox", { name: "Task instructions" })).toHaveValue("Summarize the work.");
});
it("keeps a rejected draft and its original revision across polling", async () => {
const user = userEvent.setup();
const save = vi.fn().mockRejectedValue(new Error("automation_chat_conflict"));
const { rerender } = render(<AutomationDetailDialog {...props} job={job} onChangeChat={save} />);
await choose();
await user.type(screen.getByRole("textbox", { name: "Task instructions" }), " Reviewed");
rerender(<AutomationDetailDialog {...props} job={{ ...job, chat_binding_revision: "rev-2" }} onChangeChat={save} />);
await user.click(screen.getByRole("button", { name: "Confirm change" }));
expect(save.mock.calls[0][1].revision).toBe("rev-1");
expect(await screen.findByRole("alert")).toHaveTextContent("draft is kept");
expect(screen.getByRole("textbox", { name: "Task instructions" })).toHaveValue("Summarize the work. Reviewed");
await user.click(screen.getByRole("button", { name: "Cancel" }));
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toHaveTextContent("My planning");
});
it("does not send the new request to an old host and locks a pending task", async () => {
const save = vi.fn();
const { rerender } = render(<AutomationDetailDialog {...props} job={{ ...job, chat_binding_revision: undefined }} onChangeChat={save} />);
expect(fetchAutomationChats).not.toHaveBeenCalled();
expect(screen.queryByRole("combobox")).not.toBeInTheDocument();
rerender(<AutomationDetailDialog {...props} job={{ ...job, state: { pending: true } }} onChangeChat={save} />);
expect(await screen.findByRole("combobox", { name: "Run and reply in" })).toBeDisabled();
expect(save).not.toHaveBeenCalled();
});
it("keeps the reviewed chat identity when refreshed choices no longer contain it", async () => {
const user = userEvent.setup();
const save = vi.fn();
const { rerender } = render(<AutomationDetailDialog {...props} job={job} onChangeChat={save} />);
await choose();
await user.type(screen.getByRole("textbox", { name: "Task instructions" }), " Reviewed");
vi.mocked(fetchAutomationChats).mockResolvedValue({
revision: "rev-2", current: { ...source, title: "Renamed planning" }, chats: [source],
});
rerender(<AutomationDetailDialog {...props} job={{ ...job, chat_binding_revision: "rev-2" }} onChangeChat={save} />);
expect(await screen.findByRole("alert")).toHaveTextContent("no longer available");
expect(screen.getByRole("combobox", { name: "Change to" })).toHaveTextContent("Product team");
expect(screen.getByRole("group", { name: "Now" })).toHaveTextContent("My planning");
expect(screen.getByRole("textbox", { name: "Task instructions" })).toHaveValue("Summarize the work. Reviewed");
expect(screen.getByRole("button", { name: "Confirm change" })).toBeDisabled();
expect(save).not.toHaveBeenCalled();
await user.click(screen.getByRole("button", { name: "Cancel" }));
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toHaveTextContent("Renamed planning");
});
it("keeps the current chat visible when discovery fails and permits a retry", async () => {
const user = userEvent.setup();
vi.mocked(fetchAutomationChats).mockRejectedValueOnce(new Error("offline"));
render(<AutomationDetailDialog {...props} job={job} onChangeChat={vi.fn()} />);
await user.click(await screen.findByRole("button", { name: "Retry loading chats" }));
await waitFor(() => expect(screen.getByRole("combobox", { name: "Run and reply in" })).toBeEnabled());
expect(fetchAutomationChats).toHaveBeenCalledTimes(2);
});