fix(webui): skip confirmation for undoable topic deletion (#6723)
* fix(webui): skip confirmation for undoable topic deletion * refactor(webui): rely on gateway for linked automation confirmation
This commit is contained in:
1 parent
f9052884ba
commit
eb82ab84cc
19 files changed
+113
-233
No files matched your search
@@ -24,7 +24,6 @@ let clientOptions: NanobotClientOptions | undefined;
|
||||
const refreshSpy = vi.fn();
|
||||
const createChatSpy = vi.fn().mockResolvedValue("chat-1");
|
||||
const deleteChatSpy = vi.fn();
|
||||
const getSessionAutomationsSpy = vi.fn<(key: string) => Promise<SessionAutomationJob[]>>();
|
||||
const toggleThemeSpy = vi.fn();
|
||||
const updateUrlSpy = vi.fn();
|
||||
const attachSpy = vi.fn();
|
||||
@@ -50,10 +49,9 @@ const pairingChangeHandlers = new Set<() => void>();
|
||||
const readyHandlers = new Set<() => void>();
|
||||
let mockSessions: ChatSummary[] = [];
|
||||
|
||||
async function confirmTopicDeletion(button: HTMLElement) {
|
||||
fireEvent.click(button);
|
||||
// Happy DOM delivers the modal's aria-hidden removal on a timer. Start
|
||||
// measuring the Undo window after the observer makes the toast reachable.
|
||||
async function clickDeleteAction(action: HTMLElement) {
|
||||
fireEvent.click(action);
|
||||
// Flush menu dismissal and Happy DOM's aria-hidden observer before timing Undo.
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(0); });
|
||||
}
|
||||
|
||||
@@ -211,7 +209,6 @@ vi.mock("@/hooks/useSessions", async (importOriginal) => {
|
||||
return chatId;
|
||||
},
|
||||
forkChat: async () => "fork-chat",
|
||||
getSessionAutomations: getSessionAutomationsSpy,
|
||||
deleteChat: async (key: string, options?: { deleteAutomations?: boolean }) => {
|
||||
const result = options === undefined ? await deleteChatSpy(key) : await deleteChatSpy(key, options);
|
||||
if (result && !result.deleted) return result;
|
||||
@@ -349,7 +346,6 @@ describe("App layout", () => {
|
||||
refreshSpy.mockReset();
|
||||
createChatSpy.mockClear();
|
||||
deleteChatSpy.mockReset();
|
||||
getSessionAutomationsSpy.mockReset().mockResolvedValue([]);
|
||||
toggleThemeSpy.mockReset();
|
||||
attachSpy.mockReset();
|
||||
setSidebarStateSpy.mockReset().mockImplementation(
|
||||
@@ -2469,28 +2465,23 @@ describe("App layout", () => {
|
||||
preview: "Second chat",
|
||||
},
|
||||
];
|
||||
window.history.replaceState(null, "", "/#/chat/websocket%3Achat-a");
|
||||
|
||||
render(<App />);
|
||||
|
||||
await waitFor(() => expect(connectSpy).toHaveBeenCalled());
|
||||
const sidebar = screen.getByRole("navigation", { name: "Sidebar navigation" });
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
within(sidebar).getByRole("button", { name: /^First chat$/ }),
|
||||
).toBeInTheDocument(),
|
||||
);
|
||||
|
||||
expect(await within(sidebar).findByRole("button", { name: "First chat" }))
|
||||
.toHaveAttribute("aria-current", "page");
|
||||
fireEvent.pointerDown(screen.getByLabelText("Topic actions for First chat"), {
|
||||
button: 0,
|
||||
});
|
||||
fireEvent.click(await screen.findByRole("menuitem", { name: "Delete" }));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText("Delete this topic?")).toBeInTheDocument(),
|
||||
);
|
||||
const deleteFirst = await screen.findByRole("menuitem", { name: "Delete" });
|
||||
vi.useFakeTimers();
|
||||
await confirmTopicDeletion(screen.getByRole("button", { name: "Delete" }));
|
||||
await clickDeleteAction(deleteFirst);
|
||||
expect(screen.getByText("Topic will be deleted")).toBeVisible();
|
||||
expect(screen.getByRole("button", { name: "Undo" })).toBeVisible();
|
||||
expect(window.location.hash).toBe("#/chat/websocket%3Achat-b");
|
||||
expect(within(sidebar).queryByRole("button", { name: /^First chat$/ })).not.toBeInTheDocument();
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(TOAST_ACTION_MS - 1); });
|
||||
expect(deleteChatSpy).not.toHaveBeenCalled();
|
||||
@@ -2499,14 +2490,12 @@ describe("App layout", () => {
|
||||
expect(deleteChatSpy).toHaveBeenCalledTimes(1);
|
||||
expect(deleteChatSpy).toHaveBeenCalledWith("websocket:chat-a");
|
||||
expect(within(sidebar).getByRole("button", { name: /^Second chat$/ })).toBeInTheDocument();
|
||||
expect(screen.queryByText("Delete this topic?")).not.toBeInTheDocument();
|
||||
expect(document.body.style.pointerEvents).not.toBe("none");
|
||||
|
||||
fireEvent.pointerDown(screen.getByLabelText("Topic actions for Second chat"), { button: 0 });
|
||||
fireEvent.click(await screen.findByRole("menuitem", { name: "Delete" }));
|
||||
const lastDelete = await screen.findByRole("alertdialog", { name: "Delete this topic?" });
|
||||
const deleteLast = await screen.findByRole("menuitem", { name: "Delete" });
|
||||
vi.useFakeTimers();
|
||||
await confirmTopicDeletion(within(lastDelete).getByRole("button", { name: "Delete" }));
|
||||
await clickDeleteAction(deleteLast);
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(TOAST_ACTION_MS); });
|
||||
expect(screen.getByTestId("host-sidebar-flow")).toHaveStyle({ width: "56px" });
|
||||
}, 15_000);
|
||||
@@ -2609,7 +2598,7 @@ describe("App layout", () => {
|
||||
expect(setSidebarStateSpy).toHaveBeenLastCalledWith(expect.objectContaining({ pinned_keys: keys, archived_keys: [] }));
|
||||
});
|
||||
|
||||
it("deletes multiple selected topics through one confirmation", async () => {
|
||||
it("offers one Undo for a bulk topic deletion", async () => {
|
||||
mockSessions = [
|
||||
{
|
||||
key: "websocket:chat-a",
|
||||
@@ -2648,23 +2637,8 @@ describe("App layout", () => {
|
||||
fireEvent.click(within(sidebar).getByRole("button", { name: "Second chat" }));
|
||||
expect(within(sidebar).getByText("2 selected")).toBeInTheDocument();
|
||||
|
||||
const automationReads: Array<(jobs: SessionAutomationJob[]) => void> = [];
|
||||
getSessionAutomationsSpy.mockImplementation(() => new Promise((resolve) => automationReads.push(resolve)));
|
||||
fireEvent.click(within(sidebar).getByRole("button", { name: "Delete" }));
|
||||
fireEvent.click(document.body);
|
||||
expect(screen.getByTestId("delete-selection-bar")).toHaveTextContent("2 selected");
|
||||
expect(deleteChatSpy).not.toHaveBeenCalled();
|
||||
await act(async () => automationReads.forEach((resolve) => resolve([])));
|
||||
expect(await screen.findByText("Delete 2 conversations?")).toBeInTheDocument();
|
||||
fireEvent.keyDown(screen.getByRole("alertdialog"), { key: "Escape" });
|
||||
expect(screen.getByTestId("delete-selection-bar")).toHaveTextContent("2 selected");
|
||||
expect(deleteChatSpy).not.toHaveBeenCalled();
|
||||
|
||||
getSessionAutomationsSpy.mockResolvedValue([]);
|
||||
fireEvent.click(within(sidebar).getByRole("button", { name: "Delete" }));
|
||||
expect(await screen.findByText("Delete 2 conversations?")).toBeInTheDocument();
|
||||
vi.useFakeTimers();
|
||||
await confirmTopicDeletion(screen.getByRole("button", { name: "Delete" }));
|
||||
await clickDeleteAction(within(sidebar).getByRole("button", { name: "Delete" }));
|
||||
expect(screen.queryByTestId("delete-selection-bar")).not.toBeInTheDocument();
|
||||
expect(screen.getByText("2 topics will be deleted")).toBeVisible();
|
||||
expect(screen.getAllByRole("button", { name: "Undo" })).toHaveLength(1);
|
||||
@@ -2675,8 +2649,6 @@ describe("App layout", () => {
|
||||
"websocket:chat-a",
|
||||
"websocket:chat-b",
|
||||
]);
|
||||
expect(getSessionAutomationsSpy).toHaveBeenCalledWith("websocket:chat-a");
|
||||
expect(getSessionAutomationsSpy).toHaveBeenCalledWith("websocket:chat-b");
|
||||
expect(within(sidebar).getByRole("button", { name: "Third chat" }))
|
||||
.toBeInTheDocument();
|
||||
}, 15_000);
|
||||
@@ -2695,10 +2667,9 @@ describe("App layout", () => {
|
||||
const { unmount } = render(<App />);
|
||||
const sidebar = await screen.findByRole("navigation", { name: "Sidebar navigation" });
|
||||
fireEvent.pointerDown(await within(sidebar).findByLabelText("Topic actions for Review notes"), { button: 0 });
|
||||
fireEvent.click(await screen.findByRole("menuitem", { name: "Delete" }));
|
||||
expect(await screen.findByRole("alertdialog", { name: "Delete this topic?" })).toBeVisible();
|
||||
const deleteItem = await screen.findByRole("menuitem", { name: "Delete" });
|
||||
vi.useFakeTimers();
|
||||
await confirmTopicDeletion(screen.getByRole("button", { name: "Delete" }));
|
||||
await clickDeleteAction(deleteItem);
|
||||
expect(within(sidebar).queryByRole("button", { name: "Review notes" })).not.toBeInTheDocument();
|
||||
expect(screen.getByText("Topic will be deleted").closest('[role="status"]')).toHaveTextContent("Review notes");
|
||||
expect(deleteChatSpy).not.toHaveBeenCalled();
|
||||
@@ -2718,7 +2689,7 @@ describe("App layout", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("asks again only for the topic whose automations appeared during the delete window", async () => {
|
||||
it("confirms only the topic whose deletion was blocked by linked automations", async () => {
|
||||
mockSessions = ["a", "b"].map((id) => ({
|
||||
key: `websocket:chat-${id}`, channel: "websocket", chatId: `chat-${id}`, title: `Topic ${id}`, preview: "",
|
||||
createdAt: "2026-04-16T10:00:00Z", updatedAt: "2026-04-16T10:00:00Z",
|
||||
@@ -2734,21 +2705,22 @@ describe("App layout", () => {
|
||||
fireEvent.pointerDown(await within(sidebar).findByLabelText("Topic actions for Topic a"), { button: 0 });
|
||||
fireEvent.click(await screen.findByRole("menuitem", { name: "Select" }));
|
||||
fireEvent.click(within(sidebar).getByRole("button", { name: "Topic b" }));
|
||||
fireEvent.click(within(sidebar).getByRole("button", { name: "Delete" }));
|
||||
const initial = await screen.findByRole("alertdialog", { name: "Delete 2 conversations?" });
|
||||
vi.useFakeTimers();
|
||||
await confirmTopicDeletion(within(initial).getByRole("button", { name: "Delete" }));
|
||||
await clickDeleteAction(within(sidebar).getByRole("button", { name: "Delete" }));
|
||||
expect(screen.getByText("2 topics will be deleted")).toBeVisible();
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(TOAST_ACTION_MS); });
|
||||
expect(deleteChatSpy.mock.calls).toEqual([["websocket:chat-a"], ["websocket:chat-b"]]);
|
||||
const retry = screen.getByRole("alertdialog", { name: "Delete this topic?" });
|
||||
vi.useRealTimers();
|
||||
const retry = await screen.findByRole("alertdialog", { name: "Delete this topic?" });
|
||||
expect(within(retry).getByText("Added during undo")).toBeVisible();
|
||||
await confirmTopicDeletion(within(retry).getByRole("button", { name: "Delete" }));
|
||||
vi.useFakeTimers();
|
||||
await clickDeleteAction(within(retry).getByRole("button", { name: "Delete" }));
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(TOAST_ACTION_MS); });
|
||||
expect(deleteChatSpy).toHaveBeenCalledTimes(3);
|
||||
expect(deleteChatSpy).toHaveBeenLastCalledWith("websocket:chat-b", { deleteAutomations: true });
|
||||
}, 15_000);
|
||||
|
||||
it("shows localized bound automations in the first delete confirmation", async () => {
|
||||
it("shows a localized automation warning and keeps the topic when cancelled", async () => {
|
||||
mockSessions = [
|
||||
{
|
||||
key: "websocket:chat-a",
|
||||
@@ -2767,16 +2739,18 @@ describe("App layout", () => {
|
||||
preview: "Second chat",
|
||||
},
|
||||
];
|
||||
getSessionAutomationsSpy.mockResolvedValue([
|
||||
{
|
||||
deleteChatSpy.mockResolvedValueOnce({
|
||||
deleted: false,
|
||||
blocked_by_automations: true,
|
||||
automations: [{
|
||||
id: "job-1",
|
||||
name: "Daily repo check",
|
||||
enabled: true,
|
||||
schedule: { kind: "every", every_ms: 86_400_000 },
|
||||
payload: { message: "Check the repo" },
|
||||
state: { next_run_at_ms: Date.UTC(2026, 3, 17, 10, 0, 0) },
|
||||
},
|
||||
]);
|
||||
}],
|
||||
});
|
||||
await i18n.changeLanguage("zh-CN");
|
||||
|
||||
render(<App />);
|
||||
@@ -2792,12 +2766,14 @@ describe("App layout", () => {
|
||||
fireEvent.pointerDown(screen.getByLabelText(/First chat.*话题操作/), {
|
||||
button: 0,
|
||||
});
|
||||
fireEvent.click(await screen.findByRole("menuitem", { name: "删除" }));
|
||||
const deleteItem = await screen.findByRole("menuitem", { name: "删除" });
|
||||
vi.useFakeTimers();
|
||||
await clickDeleteAction(deleteItem);
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(TOAST_ACTION_MS); });
|
||||
vi.useRealTimers();
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText("Daily repo check")).toBeInTheDocument(),
|
||||
);
|
||||
expect(getSessionAutomationsSpy).toHaveBeenCalledWith("websocket:chat-a");
|
||||
const dialog = await screen.findByRole("alertdialog", { name: "删除这个话题?" });
|
||||
expect(within(dialog).getByText("Daily repo check")).toBeVisible();
|
||||
expect(
|
||||
screen.getByText("删除话题也会删除关联的自动任务"),
|
||||
).toBeInTheDocument();
|
||||
@@ -2806,12 +2782,10 @@ describe("App layout", () => {
|
||||
).not.toBeInTheDocument();
|
||||
|
||||
vi.useFakeTimers();
|
||||
await confirmTopicDeletion(screen.getByRole("button", { name: "删除" }));
|
||||
expect(deleteChatSpy).not.toHaveBeenCalled();
|
||||
await clickDeleteAction(within(dialog).getByRole("button", { name: "取消" }));
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(TOAST_ACTION_MS); });
|
||||
expect(deleteChatSpy).toHaveBeenCalledWith("websocket:chat-a", { deleteAutomations: true });
|
||||
expect(deleteChatSpy).toHaveBeenCalledTimes(1);
|
||||
expect(screen.queryByText("Daily repo check")).not.toBeInTheDocument();
|
||||
expect(deleteChatSpy.mock.calls).toEqual([["websocket:chat-a"]]);
|
||||
expect(within(sidebar).getByRole("button", { name: "First chat" })).toBeVisible();
|
||||
}, 15_000);
|
||||
|
||||
it("closes the mobile drawer when selecting a new or current topic without a search tooltip", async () => {
|
||||
@@ -2902,9 +2876,8 @@ describe("App layout", () => {
|
||||
expect(deleteItem).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(deleteItem);
|
||||
await waitFor(() =>
|
||||
expect(screen.getByText("Delete this topic?")).toBeInTheDocument(),
|
||||
);
|
||||
expect(await screen.findByRole("button", { name: "Undo" })).toBeVisible();
|
||||
expect(sheet).not.toBeInTheDocument();
|
||||
}, 15_000);
|
||||
|
||||
it("renames inline in the mobile drawer without dismissing it and reloads the saved title", async () => {
|
||||
@@ -4587,10 +4560,9 @@ describe("App layout", () => {
|
||||
fireEvent.pointerDown(within(sidebar).getByRole("button", {
|
||||
name: "New topic pane actions",
|
||||
}), { button: 0, ctrlKey: false });
|
||||
fireEvent.click(await screen.findByRole("menuitem", { name: "Delete" }));
|
||||
expect(await screen.findByText("Delete this topic?")).toBeInTheDocument();
|
||||
const deletePane = await screen.findByRole("menuitem", { name: "Delete" });
|
||||
vi.useFakeTimers();
|
||||
await confirmTopicDeletion(screen.getByRole("button", { name: "Delete" }));
|
||||
await clickDeleteAction(deletePane);
|
||||
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(TOAST_ACTION_MS); });
|
||||
expect(deleteChatSpy).toHaveBeenCalledWith("websocket:new-pane");
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { act, fireEvent, render, screen, within } from "@testing-library/react";
|
||||
import { fireEvent, render, screen, within } from "@testing-library/react";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { ChatList } from "@/components/ChatList";
|
||||
@@ -94,7 +94,6 @@ describe("ChatList", () => {
|
||||
{ archivedPaneKeys: [], projectLocked: undefined },
|
||||
])("archives only persisted panes in a mixed selection ($archivedPaneKeys, draft=$projectLocked)", async ({ archivedPaneKeys, projectLocked }) => {
|
||||
const onArchiveMany = vi.fn();
|
||||
const onRequestDeleteMany = vi.fn();
|
||||
render(<ChatList
|
||||
sessions={[session({ chatId: "draft", title: "Draft", projectLocked: false })]}
|
||||
paneGroups={{
|
||||
@@ -110,7 +109,6 @@ describe("ChatList", () => {
|
||||
activeKey="websocket:draft"
|
||||
onSelect={vi.fn()}
|
||||
onRequestDelete={vi.fn()}
|
||||
onRequestDeleteMany={onRequestDeleteMany}
|
||||
onTogglePin={vi.fn()}
|
||||
onRequestRename={vi.fn()}
|
||||
onToggleArchive={vi.fn()}
|
||||
@@ -122,11 +120,6 @@ describe("ChatList", () => {
|
||||
fireEvent.click(screen.getByRole("menuitem", { name: "Select" }));
|
||||
const bar = screen.getByTestId("delete-selection-bar");
|
||||
expect(within(bar).queryByRole("button", { name: "Archive" })).not.toBeInTheDocument();
|
||||
fireEvent.click(within(bar).getByRole("button", { name: "Delete" }));
|
||||
expect(onRequestDeleteMany).toHaveBeenCalledWith([
|
||||
{ key: "websocket:draft", label: "Draft" },
|
||||
], expect.any(Function));
|
||||
act(() => onRequestDeleteMany.mock.calls[0][1](false));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Empty fork" }));
|
||||
const action = archivedPaneKeys.length ? "Unarchive" : "Archive";
|
||||
fireEvent.click(within(bar).getByRole("button", { name: action }));
|
||||
@@ -875,9 +868,7 @@ describe("ChatList", () => {
|
||||
{ key: "websocket:root", label: "Root topic" },
|
||||
{ key: "websocket:child", label: "Research pane" },
|
||||
{ key: "websocket:target", label: "Target tab" },
|
||||
], expect.any(Function));
|
||||
expect(screen.getByText("3 selected")).toBeInTheDocument();
|
||||
act(() => onRequestDeleteMany.mock.calls[0][1](true));
|
||||
]);
|
||||
expect(screen.queryByTestId("delete-selection-bar")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user