2026-10-04 23:53:37 +08:00
|
|
|
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() {
|
2026-10-06 00:53:06 +08:00
|
|
|
const control = await screen.findByRole("combobox", { name: "Run and reply in" });
|
2026-10-04 23:53:37 +08:00
|
|
|
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();
|
2026-10-05 01:51:59 +08:00
|
|
|
const { rerender } = render(<AutomationDetailDialog {...props} job={job} onChangeChat={save} />);
|
2026-10-04 23:53:37 +08:00
|
|
|
await choose();
|
|
|
|
|
expect(save).not.toHaveBeenCalled();
|
2026-10-06 00:53:06 +08:00
|
|
|
expect(screen.getByRole("group", { name: "Now" })).toHaveTextContent("My planning");
|
2026-10-04 23:53:37 +08:00
|
|
|
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();
|
2026-10-05 01:51:59 +08:00
|
|
|
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();
|
|
|
|
|
});
|
2026-10-06 00:53:06 +08:00
|
|
|
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();
|
2026-10-05 01:51:59 +08:00
|
|
|
await act(async () => refresh({ ...choices, current: target, revision: "rev-2" }));
|
2026-10-06 00:53:06 +08:00
|
|
|
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toHaveFocus();
|
2026-10-04 23:53:37 +08:00
|
|
|
expect(props.onAction).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-06 00:53:06 +08:00
|
|
|
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.");
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-04 23:53:37 +08:00
|
|
|
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" }));
|
2026-10-06 00:53:06 +08:00
|
|
|
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toHaveTextContent("My planning");
|
2026-10-04 23:53:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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} />);
|
2026-10-06 00:53:06 +08:00
|
|
|
expect(await screen.findByRole("combobox", { name: "Run and reply in" })).toBeDisabled();
|
2026-10-04 23:53:37 +08:00
|
|
|
expect(save).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-05 01:51:59 +08:00
|
|
|
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");
|
2026-10-06 00:53:06 +08:00
|
|
|
expect(screen.getByRole("combobox", { name: "Change to" })).toHaveTextContent("Product team");
|
|
|
|
|
expect(screen.getByRole("group", { name: "Now" })).toHaveTextContent("My planning");
|
2026-10-05 01:51:59 +08:00
|
|
|
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" }));
|
2026-10-06 00:53:06 +08:00
|
|
|
expect(screen.getByRole("combobox", { name: "Run and reply in" })).toHaveTextContent("Renamed planning");
|
2026-10-05 01:51:59 +08:00
|
|
|
});
|
|
|
|
|
|
2026-10-04 23:53:37 +08:00
|
|
|
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" }));
|
2026-10-06 00:53:06 +08:00
|
|
|
await waitFor(() => expect(screen.getByRole("combobox", { name: "Run and reply in" })).toBeEnabled());
|
2026-10-04 23:53:37 +08:00
|
|
|
expect(fetchAutomationChats).toHaveBeenCalledTimes(2);
|
|
|
|
|
});
|