feat(webui): persist reviewed automation chat changes

This commit is contained in:
Xubin Ren committed 2026-10-07 01:12:23 +08:00
1 parent 707f7eb079
commit 845afe9215
41 files changed
+1210 -626

No files matched your search

+16
View File
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
changeAutomationChat,
cancelMcpOAuth,
configureChannel,
completeMcpOAuth,
@@ -13,6 +14,7 @@ import {
fetchFilePreview,
fetchFilePreviewAvailability,
fetchAutomations,
fetchAutomationChats,
fetchAutomationRunResult,
fetchApiService,
fetchCliApps,
@@ -377,6 +379,20 @@ describe("webui API helpers", () => {
);
});
it("reads chat choices over HTTP and commits the opaque choice over WebSocket", async () => {
const controller = new AbortController();
await fetchAutomationChats("tok", "task/id", controller.signal);
expect(fetch).toHaveBeenCalledWith(
"/api/webui/automations/chats?id=task%2Fid",
expect.objectContaining({ headers: { Authorization: "Bearer tok" }, signal: expect.any(AbortSignal) }),
);
vi.mocked(fetch).mockClear();
const values = { target_id: "opaque-id", revision: "reviewed-version", message: "Reviewed prompt" };
await changeAutomationChat(mutationTransport, "task/id", values);
expect(requestMutation).toHaveBeenCalledWith("automation.change_chat", { id: "task/id", values }, 20_000);
expect(fetch).not.toHaveBeenCalled();
});
it("validates channel settings with form values", async () => {
await validateChannel(
mutationTransport,
@@ -0,0 +1,81 @@
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: "Task chat" });
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();
render(<AutomationDetailDialog {...props} job={job} onChangeChat={save} />);
await choose();
expect(save).not.toHaveBeenCalled();
expect(screen.getByText(/Previous chat:/)).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();
await act(async () => finish());
expect(await screen.findByRole("status")).toHaveTextContent("Applies to future runs");
expect(props.onAction).not.toHaveBeenCalled();
});
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: "Task chat" })).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: "Task chat" })).toBeDisabled();
expect(save).not.toHaveBeenCalled();
});
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: "Task chat" })).toBeEnabled());
expect(fetchAutomationChats).toHaveBeenCalledTimes(2);
});
+32 -29
View File
@@ -54,6 +54,13 @@ function openFilters() {
return screen.getByRole("menu", { name: /^Filter/ });
}
function taskMenuItem(name: string) {
if (!screen.queryByRole("menu")) {
fireEvent.pointerDown(screen.getByRole("button", { name: i18n.t("settings.automations.moreActions") }), { button: 0, ctrlKey: false });
}
return screen.getByRole("menuitem", { name, exact: true });
}
function selectFilter(name: string) {
const menu = openFilters();
fireEvent.click(within(menu).getByRole("menuitemradio", { name }));
@@ -255,7 +262,7 @@ describe("Automation task list and detail sheet", () => {
const dialog = screen.getByRole("dialog", { name: "PR watch" });
expect(dialog).not.toHaveAttribute("aria-describedby");
expect(within(dialog).queryByText("Instructions")).not.toBeInTheDocument();
expect(within(dialog).getByRole("link", { name: "Open a chat" })).toHaveAttribute(
expect(taskMenuItem("Open a chat")).toHaveAttribute(
"href", "#/chat/websocket%3Ademo",
);
expect(within(dialog).getByRole("button", { name: "Task information" })).toHaveAttribute("aria-expanded", "false");
@@ -424,7 +431,8 @@ describe("Automation task list and detail sheet", () => {
await user.click(within(screen.getByRole("dialog", { name: dayLabel })).getByRole("button", { name: /Crowded 4.*Planned/ }));
const detail = await screen.findByRole("dialog", { name: "Crowded 4" });
expect(screen.queryByRole("dialog", { name: dayLabel })).not.toBeInTheDocument();
expect(within(detail).getByRole("link", { name: "Open a chat" })).toBeVisible();
expect(taskMenuItem("Open a chat")).toBeVisible();
await user.keyboard("{Escape}");
await user.click(within(detail).getByRole("button", { name: "Close" }));
await waitFor(() => expect(more).toHaveFocus());
});
@@ -626,14 +634,13 @@ describe("Automation task list and detail sheet", () => {
expect(details).toHaveAttribute("aria-expanded", "false");
if (!job.protected) {
expect(within(dialog).queryByRole("switch")).not.toBeInTheDocument();
const disable = within(dialog).getByRole("button", { name: "Disable" });
const taskActions = within(dialog).getByRole("button", { name: "Run now" }).parentElement;
expect(disable.parentElement).toBe(taskActions);
expect(within(dialog).getByRole("button", { name: "Delete" }).parentElement).toBe(taskActions);
const edit = within(dialog).getByRole("button", { name: "Edit" });
expect(edit.parentElement).not.toBe(taskActions);
expect(edit.parentElement).toHaveClass("ms-auto", "flex-wrap", "justify-end");
expect(within(dialog).getByRole("link", { name: "Open a chat" }).parentElement).toBe(edit.parentElement);
expect(edit.parentElement).toBe(taskActions);
expect(edit.parentElement).toHaveClass("ml-auto");
expect(taskMenuItem("Disable")).toBeVisible();
expect(taskMenuItem("Delete")).toBeVisible();
expect(taskMenuItem("Open a chat")).toHaveAttribute("href", "#/chat/websocket%3Ademo");
}
});
@@ -782,7 +789,7 @@ describe("Automation task list and detail sheet", () => {
rerender(<Harness payload={payload} onAction={action}
titleOverrides={{ "websocket:demo": "新会话名称" }} />);
expect(within(dialog).queryByText("新会话名称")).not.toBeInTheDocument();
await user.click(within(dialog).getByRole("button", { name: "Disable" }));
await user.click(taskMenuItem("Disable"));
expect(action).toHaveBeenCalledWith("disable", expect.objectContaining({
id: task.id, payload: task.payload,
origin: expect.objectContaining({ session_key: "websocket:demo" }),
@@ -792,19 +799,19 @@ describe("Automation task list and detail sheet", () => {
rerender(<Harness payload={payload} titleOverrides={{}} />);
await user.click(screen.getByRole("button", { name: /PR watch/ }));
expect(screen.queryByText("nanobot-development")).not.toBeInTheDocument();
expect(screen.getByRole("link", { name: "Open a chat" })).toHaveAttribute("href", "#/chat/websocket%3Ademo");
expect(taskMenuItem("Open a chat")).toHaveAttribute("href", "#/chat/websocket%3Ademo");
});
it("retains the inspected task across refreshes and closes if it is removed", () => {
const action = vi.fn();
const { rerender } = render(<Harness onAction={action} />);
fireEvent.click(screen.getByRole("button", { name: /PR watch/ }));
fireEvent.click(screen.getByRole("button", { name: "Disable" }));
fireEvent.click(taskMenuItem("Disable"));
expect(action).toHaveBeenCalledWith("disable", task);
rerender(<Harness payload={{ jobs: [{ ...task, enabled: false }] }} onAction={action} filter="active" />);
expect(screen.getByRole("dialog", { name: "PR watch" })).toBeVisible();
expect(screen.getByRole("button", { name: "Enable" })).toBeEnabled();
fireEvent.click(screen.getByRole("button", { name: "Enable" }));
expect(taskMenuItem("Enable")).not.toHaveAttribute("aria-disabled");
fireEvent.click(taskMenuItem("Enable"));
expect(action).toHaveBeenLastCalledWith("enable", { ...task, enabled: false });
rerender(<Harness payload={{ jobs: [] }} />);
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
@@ -819,12 +826,12 @@ describe("Automation task list and detail sheet", () => {
expect(screen.getByRole("button", { name: "筛选", exact: true })).toBeVisible();
fireEvent.click(screen.getByRole("button", { name: /PR watch/ }));
const dialog = screen.getByRole("dialog", { name: "PR watch" });
fireEvent.click(within(dialog).getByRole("button", { name: "停用", exact: true }));
fireEvent.click(taskMenuItem("停用"));
expect(action).toHaveBeenLastCalledWith("disable", task);
const disabledTask = { ...task, enabled: false };
rerender(<Harness payload={{ jobs: [disabledTask] }} onAction={action} />);
expect(within(dialog).getByRole("button", { name: "启用", exact: true })).toBeEnabled();
fireEvent.click(within(dialog).getByRole("button", { name: "启用", exact: true }));
expect(taskMenuItem("启用")).not.toHaveAttribute("aria-disabled");
fireEvent.click(taskMenuItem("启用"));
expect(action).toHaveBeenLastCalledWith("enable", disabledTask);
expect(dialog).not.toHaveTextContent("暂停");
expect(dialog).not.toHaveTextContent("恢复");
@@ -882,7 +889,7 @@ describe("Automation task list and detail sheet", () => {
await waitFor(() => expect(edit).toHaveBeenCalledWith(task));
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
await user.click(screen.getByRole("button", { name: /PR watch/ }));
await user.click(screen.getByRole("button", { name: "Delete" }));
await user.click(taskMenuItem("Delete"));
await waitFor(() => expect(remove).toHaveBeenCalledWith(task));
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
});
@@ -932,7 +939,7 @@ describe("Automation task list and detail sheet", () => {
await user.click(screen.getByRole("button", { name: /PR watch/ }));
const detail = screen.getByRole("dialog", { name: "PR watch" });
expect(document.body.style.pointerEvents).toBe("none");
await user.click(screen.getByRole("button", { name: "Delete" }));
await user.click(taskMenuItem("Delete"));
expect(detail).toHaveAttribute("data-state", "closed");
finishExit(detail);
const confirmation = await screen.findByRole("dialog", { name: "Delete automation" });
@@ -1005,10 +1012,9 @@ describe("Automation task list and detail sheet", () => {
await user.click(screen.getByRole("button", { name: "Run now" }));
expect(action).toHaveBeenCalledWith("run", task);
rerender(<Harness actionKey="run:private-job-id" />);
for (const name of ["Edit", "Run now", "Delete"]) {
for (const name of ["Edit", "Run now", "More actions"]) {
expect(screen.getByRole("button", { name })).toBeDisabled();
}
expect(screen.getByRole("button", { name: "Disable" })).toBeDisabled();
rerender(<Harness error="Unable to run task" />);
expect(within(screen.getByRole("dialog")).getByRole("alert")).toHaveTextContent("Unable to run task");
});
@@ -1018,18 +1024,15 @@ describe("Automation task list and detail sheet", () => {
const action = vi.fn();
const { rerender } = render(<Harness onAction={action} />);
await user.click(screen.getByRole("button", { name: /PR watch/ }));
const control = screen.getByRole("button", { name: "Disable" });
control.focus();
const control = taskMenuItem("Disable");
act(() => control.focus());
await user.keyboard(" ");
expect(action).toHaveBeenCalledWith("disable", task);
expect(control).toHaveTextContent("Disable");
rerender(<Harness onAction={action} actionKey={`disable:${task.id}`} />);
expect(control).toBeDisabled();
await user.click(control);
expect(screen.getByRole("button", { name: "More actions" })).toBeDisabled();
expect(action).toHaveBeenCalledTimes(1);
rerender(<Harness onAction={action} payload={{ jobs: [{ ...task, enabled: false }] }} />);
expect(control).toHaveTextContent("Enable");
expect(control).toBeEnabled();
expect(taskMenuItem("Enable")).not.toHaveAttribute("aria-disabled");
});
it("does not allow running a pending task or resuming an unlinked task", async () => {
@@ -1040,7 +1043,7 @@ describe("Automation task list and detail sheet", () => {
await user.click(screen.getByRole("button", { name: /PR watch/ }));
expect(screen.getByRole("button", { name: "Run now" })).toBeDisabled();
rerender(<Harness payload={{ jobs: [{ ...task, enabled: false, origin: null }] }} />);
expect(screen.getByRole("button", { name: "Enable" })).toBeDisabled();
expect(taskMenuItem("Enable")).toHaveAttribute("aria-disabled", "true");
expect(screen.queryByRole("link", { name: "Open a chat" })).not.toBeInTheDocument();
});
@@ -1055,7 +1058,7 @@ describe("Automation task list and detail sheet", () => {
expect(within(screen.getByRole("dialog")).getByText("nanobot trigger run trigger-1")).toBeVisible();
expect(screen.getByRole("button", { name: "Copy" })).toBeEnabled();
expect(screen.queryByRole("button", { name: "Run now" })).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Delete" })).toBeVisible();
expect(taskMenuItem("Delete")).toBeVisible();
});
it("shows paused failures honestly and does not fabricate successful runs", () => {