fix(webui): merge consecutive waiting messages (#6309)
* fix(webui): merge consecutive waiting messages * fix(webui): retain legacy draft session targets on refresh
This commit is contained in:
1 parent
8778c69774
commit
125d9221d0
17 files changed
+680
-606
No files matched your search
@@ -0,0 +1,293 @@
|
||||
import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { ThreadComposer } from "@/components/thread/ThreadComposer";
|
||||
import type { SessionMention, SlashCommand, WebUIIngressLimits } from "@/lib/types";
|
||||
import { ComposerDraftStore } from "@/lib/composer-draft";
|
||||
import { formatQuotedUserMessage } from "@/lib/user-message-quote";
|
||||
|
||||
vi.mock("@/lib/imageEncode", () => ({
|
||||
encodeImage: vi.fn(async () => ({
|
||||
ok: true,
|
||||
dataUrl: "data:image/png;base64,aW1hZ2U=",
|
||||
bytes: 5,
|
||||
normalized: false,
|
||||
})),
|
||||
}));
|
||||
|
||||
const STORAGE_KEY = "nanobot.webui.composerQueuedGuidance.v1:chat-a";
|
||||
const PNG = { dataUrl: "data:image/png;base64,aW1hZ2U=", kind: "image", name: "first.png" };
|
||||
const DOCUMENT = { dataUrl: "data:text/plain;base64,aGVsbG8=", kind: "file", name: "second.txt" };
|
||||
|
||||
function limits(attachments: Partial<WebUIIngressLimits["attachments"]> = {}, maxTextBytes = 65536): WebUIIngressLimits {
|
||||
return {
|
||||
transport: { max_frame_bytes: 1024 * 1024, envelope_reserve_bytes: 1024 },
|
||||
message: { max_text_bytes: maxTextBytes },
|
||||
attachments: { max_count: 4, max_file_bytes: 6 * 1024 * 1024, max_total_bytes: 24 * 1024 * 1024, ...attachments },
|
||||
};
|
||||
}
|
||||
|
||||
function enter(text: string): void {
|
||||
const input = screen.getByLabelText("Message input");
|
||||
fireEvent.change(input, { target: { value: text } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
}
|
||||
|
||||
function mention(name: string): SessionMention {
|
||||
return { name, session_key: `websocket:${name}`, title: name };
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
window.localStorage.clear();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe("merged waiting messages", () => {
|
||||
it("restores legacy entries as one message with all attachments, quotes and stable session targets", () => {
|
||||
const alpha = mention("alpha");
|
||||
const beta = mention("beta");
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify([
|
||||
{ id: "first", text: "1 @alpha", images: [PNG], quotedContext: "first quote", sessionMentions: [alpha] },
|
||||
{ id: "second", text: "2 @alpha @beta", images: [DOCUMENT], quotedContext: "second quote", sessionMentions: [alpha, beta] },
|
||||
]));
|
||||
const onSend = vi.fn();
|
||||
render(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" isStreaming />);
|
||||
const pending = screen.getByRole("group", { name: "Waiting to send" });
|
||||
expect(within(pending).getAllByRole("button", { name: "Send now" })).toHaveLength(1);
|
||||
expect(pending).toHaveTextContent("first.png, second.txt");
|
||||
fireEvent.click(within(pending).getByRole("button", { name: "Send now" }));
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
expect(onSend).toHaveBeenCalledWith("1 @alpha\n2 @alpha @beta", [
|
||||
expect.objectContaining({ media: { data_url: PNG.dataUrl, name: PNG.name }, preview: expect.objectContaining({ kind: "image" }) }),
|
||||
expect.objectContaining({ media: { data_url: DOCUMENT.dataUrl, name: DOCUMENT.name }, preview: expect.objectContaining({ kind: "file" }) }),
|
||||
], {
|
||||
continueActiveTurn: true,
|
||||
quotedContext: "first quote\n\nsecond quote",
|
||||
sessionMentions: [alpha, beta],
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves merged text longer than the former per-entry storage truncation across remounts", () => {
|
||||
const onSend = vi.fn();
|
||||
const first = render(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" isStreaming />);
|
||||
const texts = ["a".repeat(3000), "b".repeat(3000)];
|
||||
texts.forEach(enter);
|
||||
first.unmount();
|
||||
render(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" isStreaming />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send now" }));
|
||||
expect(onSend).toHaveBeenCalledWith(texts.join("\n"), undefined, { continueActiveTurn: true });
|
||||
});
|
||||
|
||||
it("leaves an over-limit UTF-8 append in the input without changing the pending content", () => {
|
||||
const onSend = vi.fn();
|
||||
render(<ThreadComposer onSend={onSend} isStreaming ingressLimits={limits({}, 6)} />);
|
||||
enter("一");
|
||||
enter("二");
|
||||
expect(screen.getByLabelText("Message input")).toHaveValue("二");
|
||||
expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("一");
|
||||
expect(screen.getByText(/Message text is too large/)).toBeInTheDocument();
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("counts the combined formatted quotes and separators against the text limit", () => {
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify([
|
||||
{ id: "first", text: "1", quotedContext: "first quote" },
|
||||
]));
|
||||
const bytes = new TextEncoder().encode(formatQuotedUserMessage("1\n2", "first quote\n\nsecond quote")).length;
|
||||
const onSend = vi.fn();
|
||||
render(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" isStreaming quotedContext="second quote" ingressLimits={limits({}, bytes - 1)} />);
|
||||
enter("2");
|
||||
expect(screen.getByLabelText("Message input")).toHaveValue("2");
|
||||
expect(JSON.parse(localStorage.getItem(STORAGE_KEY)!)[0]).toMatchObject({ text: "1", quotedContext: "first quote" });
|
||||
expect(screen.getByText(/Message text is too large/)).toBeInTheDocument();
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ policy: { max_count: 1 }, error: "Max 1 attachments per message" },
|
||||
{ policy: { max_total_bytes: 9 }, error: "Attachments are too large together" },
|
||||
])("retains both pending and current attachments when merging exceeds $policy", async ({ policy, error }) => {
|
||||
Object.defineProperty(URL, "createObjectURL", { configurable: true, value: vi.fn(() => "blob:queued-test") });
|
||||
Object.defineProperty(URL, "revokeObjectURL", { configurable: true, value: vi.fn() });
|
||||
const onSend = vi.fn();
|
||||
const view = render(<ThreadComposer onSend={onSend} isStreaming ingressLimits={limits(policy)} />);
|
||||
const picker = view.container.querySelector<HTMLInputElement>('input[type="file"]')!;
|
||||
for (const [index, name] of ["first.png", "second.png"].entries()) {
|
||||
fireEvent.change(picker, { target: { files: [new File(["image"], name, { type: "image/png" })] } });
|
||||
await waitFor(() => expect(screen.queryByLabelText("Encoding…")).not.toBeInTheDocument());
|
||||
enter(String(index + 1));
|
||||
}
|
||||
expect(screen.getByLabelText("Message input")).toHaveValue("2");
|
||||
expect(screen.getByTestId("composer-chip")).toHaveTextContent("second.png");
|
||||
expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("first.png");
|
||||
expect(screen.getByText(error, { exact: false })).toBeInTheDocument();
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("lets users edit an oversized legacy aggregate without dropping excess attachments", () => {
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify([
|
||||
{ id: "first", text: "1", images: [PNG] },
|
||||
{ id: "second", text: "2", images: [DOCUMENT] },
|
||||
]));
|
||||
const onSend = vi.fn();
|
||||
render(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" ingressLimits={limits({ max_count: 1 })} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send now" }));
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
expect(screen.getByText("Max 1 attachments per message")).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Edit message" }));
|
||||
expect(screen.getByLabelText("Message input")).toHaveValue("1\n2");
|
||||
expect(screen.getAllByTestId("composer-chip")).toHaveLength(2);
|
||||
fireEvent.click(within(screen.getAllByTestId("composer-chip")[0]).getByRole("button", { name: "Remove attachment" }));
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
|
||||
expect(onSend).toHaveBeenCalledWith("1\n2", [expect.objectContaining({ media: expect.objectContaining({ name: "second.txt" }) })], undefined);
|
||||
});
|
||||
|
||||
it("keeps all edited legacy attachments through a topic round trip", async () => {
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify([
|
||||
{ id: "first", text: "1", images: [PNG] },
|
||||
{ id: "second", text: "2", images: [DOCUMENT] },
|
||||
]));
|
||||
const draftStore = new ComposerDraftStore();
|
||||
const props = { onSend: vi.fn(), pendingQueueKey: "chat-a", draftKey: "chat-a", draftStore, ingressLimits: limits({ max_count: 1 }) };
|
||||
const view = render(<ThreadComposer {...props} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Edit message" }));
|
||||
view.unmount();
|
||||
render(<ThreadComposer {...props} />);
|
||||
await waitFor(() => expect(screen.queryByLabelText("Encoding…")).not.toBeInTheDocument());
|
||||
expect(screen.getAllByTestId("composer-chip")).toHaveLength(2);
|
||||
expect(draftStore.get("chat-a")?.files.map((file) => file.name)).toEqual(["first.png", "second.txt"]);
|
||||
});
|
||||
|
||||
it("retains a merged rejected send after leaving the topic during upload", async () => {
|
||||
let reject!: (reason: Error) => void;
|
||||
const onSend = vi.fn(() => new Promise<boolean>((_, no) => { reject = no; }));
|
||||
const props = { onSend, pendingQueueKey: "chat-a", isStreaming: true };
|
||||
const view = render(<ThreadComposer {...props} />);
|
||||
enter("1");
|
||||
fireEvent.change(screen.getByLabelText("Message input"), { target: { value: "2" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
|
||||
view.unmount();
|
||||
await act(async () => { reject(new Error("Upload failed")); });
|
||||
render(<ThreadComposer {...props} />);
|
||||
expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("1 2");
|
||||
expect(JSON.parse(localStorage.getItem(STORAGE_KEY)!)[0]).toMatchObject({ text: "1\n2", autoPaused: true });
|
||||
});
|
||||
|
||||
it("shares pending acceptance across remounts and clears only the accepted message", async () => {
|
||||
let accept!: (accepted: boolean) => void;
|
||||
const onSend = vi.fn(() => new Promise<boolean>((yes) => { accept = yes; }));
|
||||
const props = { onSend, pendingQueueKey: "chat-a", isStreaming: true };
|
||||
const view = render(<ThreadComposer {...props} />);
|
||||
enter("1"); enter("2");
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send now" }));
|
||||
view.unmount();
|
||||
render(<ThreadComposer {...props} />);
|
||||
expect(screen.getByRole("button", { name: "Send now" })).toBeDisabled();
|
||||
await act(async () => { accept(true); });
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
expect(localStorage.getItem(STORAGE_KEY)).toBeNull();
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("preserves the paused pending message when /stop is submitted with the send button", () => {
|
||||
const onStop = vi.fn();
|
||||
const onSend = vi.fn();
|
||||
const slashCommands: SlashCommand[] = [{ command: "/stop", title: "Stop", description: "Stop", icon: "square", lifecycle: "stop_active_turn", acceptsArgs: false }];
|
||||
const props = { onSend, onStop, pendingQueueKey: "chat-a", isStreaming: true, activeTurnId: "turn-a", slashCommands };
|
||||
const view = render(<ThreadComposer {...props} />);
|
||||
enter("1"); enter("2");
|
||||
fireEvent.change(screen.getByLabelText("Message input"), { target: { value: "/stop" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
|
||||
expect(onStop).toHaveBeenCalledTimes(1);
|
||||
expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("1 2");
|
||||
view.rerender(<ThreadComposer {...props} isStreaming={false} completedTurnIds={["turn-a"]} />);
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps a new quote-only selection when editing the complete pending message", () => {
|
||||
const onQuotedContextChange = vi.fn();
|
||||
const props = { onSend: vi.fn(), isStreaming: true, onQuotedContextChange };
|
||||
const view = render(<ThreadComposer {...props} />);
|
||||
enter("1");
|
||||
view.rerender(<ThreadComposer {...props} quotedContext="new selection" />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Edit message" }));
|
||||
expect(screen.getByLabelText("Message input")).toHaveValue("1");
|
||||
expect(onQuotedContextChange).toHaveBeenLastCalledWith("new selection");
|
||||
});
|
||||
|
||||
it("auto-sends attachment-only guidance after completion", async () => {
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify([
|
||||
{ id: "first", text: "", images: [PNG], autoAfterTurnId: "turn-a" },
|
||||
{ id: "second", text: "", images: [DOCUMENT], autoAfterTurnId: "turn-a" },
|
||||
]));
|
||||
const onSend = vi.fn();
|
||||
render(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["turn-a"]} />);
|
||||
await waitFor(() => expect(onSend).toHaveBeenCalledTimes(1));
|
||||
expect(onSend).toHaveBeenCalledWith("", [
|
||||
expect.objectContaining({ media: expect.objectContaining({ name: "first.png" }) }),
|
||||
expect.objectContaining({ media: expect.objectContaining({ name: "second.txt" }) }),
|
||||
]);
|
||||
});
|
||||
|
||||
it("retains a ninth session target in the input instead of silently dropping its reference", () => {
|
||||
const mentions = Array.from({ length: 8 }, (_, index) => mention(`target${index}`));
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify([
|
||||
{ id: "first", text: mentions.map((item) => `@${item.name}`).join(" "), sessionMentions: mentions },
|
||||
]));
|
||||
const onSend = vi.fn();
|
||||
render(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" isStreaming sessions={[{
|
||||
key: "websocket:target8", channel: "websocket", chatId: "target8", title: "Ninth chat", preview: "",
|
||||
createdAt: null, updatedAt: null, handle: { id: `handle_${"a".repeat(32)}`, name: "target8" },
|
||||
}]} />);
|
||||
fireEvent.change(screen.getByLabelText("Message input"), { target: { value: "@target8" } });
|
||||
fireEvent.mouseDown(screen.getByRole("option", { name: /target8/i }));
|
||||
fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" });
|
||||
expect(screen.getByLabelText("Message input")).toHaveValue("@target8 ");
|
||||
expect(screen.getByText("A message can mention up to 8 chats")).toBeInTheDocument();
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps all legacy session targets after editing and refreshing an over-limit draft", () => {
|
||||
const targets = Array.from({ length: 9 }, (_, index) => mention(`target${index}`));
|
||||
const text = targets.map((item) => `@${item.name}`).join(" ");
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify([
|
||||
{ id: "first", text: text.slice(0, text.lastIndexOf(" ")), sessionMentions: targets.slice(0, 8) },
|
||||
{ id: "second", text: "@target8", sessionMentions: targets.slice(8) },
|
||||
]));
|
||||
const onSend = vi.fn();
|
||||
const props = { onSend, pendingQueueKey: "chat-a", draftKey: "chat-a", persistDraft: true };
|
||||
const view = render(<ThreadComposer {...props} draftStore={new ComposerDraftStore()} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Edit message" }));
|
||||
view.unmount();
|
||||
const refreshed = new ComposerDraftStore();
|
||||
expect(refreshed.get("chat-a", true)?.sessionMentions).toEqual(targets);
|
||||
render(<ThreadComposer {...props} draftStore={refreshed} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
|
||||
expect(screen.getByText("A message can mention up to 8 chats")).toBeInTheDocument();
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("retains the whole message after a rejected immediate send", () => {
|
||||
const onSend = vi.fn(() => false);
|
||||
const view = render(<ThreadComposer onSend={onSend} isStreaming activeTurnId="turn-a" pendingQueueKey="chat-a" />);
|
||||
enter("1"); enter("2");
|
||||
fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" });
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
expect(screen.getByRole("group", { name: "Waiting to send" })).toHaveTextContent("1 2");
|
||||
view.rerender(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["turn-a"]} />);
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not automatically re-arm stopped content when another input is appended", () => {
|
||||
window.localStorage.setItem(STORAGE_KEY, JSON.stringify([
|
||||
{ id: "first", text: "stopped", autoPaused: true },
|
||||
]));
|
||||
const onSend = vi.fn();
|
||||
const view = render(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" isStreaming activeTurnId="turn-a" />);
|
||||
enter("new input");
|
||||
view.rerender(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["turn-a"]} />);
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send now" }));
|
||||
expect(onSend).toHaveBeenCalledWith("stopped\nnew input");
|
||||
});
|
||||
});
|
||||
@@ -3163,7 +3163,7 @@ describe("ThreadComposer", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves waiting guidance when the send button submits a newer draft", () => {
|
||||
it("includes waiting guidance when the send button submits a newer draft", () => {
|
||||
const onSend = vi.fn();
|
||||
render(<ThreadComposer onSend={onSend} onStop={vi.fn()} isStreaming />);
|
||||
const input = screen.getByLabelText("Message input");
|
||||
@@ -3172,10 +3172,9 @@ describe("ThreadComposer", () => {
|
||||
fireEvent.change(input, { target: { value: "send this now" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
|
||||
|
||||
expect(onSend).toHaveBeenCalledWith("send this now", undefined, { continueActiveTurn: true });
|
||||
expect(screen.getByText("waiting guidance")).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send now" }));
|
||||
expect(onSend).toHaveBeenLastCalledWith("waiting guidance", undefined, { continueActiveTurn: true });
|
||||
expect(onSend).toHaveBeenCalledWith("waiting guidance\nsend this now", undefined, { continueActiveTurn: true });
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("does not mark side-channel commands as guidance during an active turn", () => {
|
||||
@@ -3402,7 +3401,7 @@ describe("ThreadComposer", () => {
|
||||
expect(screen.getByText("leave this queued")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("guides the newly queued prompt when older guidance is still waiting", () => {
|
||||
it("merges consecutive queued inputs and interjects the whole message on a second Enter", () => {
|
||||
const onSend = vi.fn();
|
||||
render(
|
||||
<ThreadComposer
|
||||
@@ -3421,16 +3420,15 @@ describe("ThreadComposer", () => {
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
|
||||
expect(onSend).toHaveBeenCalledWith(
|
||||
"guide this one now",
|
||||
"older guidance\nguide this one now",
|
||||
undefined,
|
||||
{ continueActiveTurn: true },
|
||||
);
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
expect(screen.getByText("older guidance")).toBeInTheDocument();
|
||||
expect(screen.queryByText("guide this one now")).not.toBeInTheDocument();
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("keeps queued guidance attached to the composer and sends it one item at a time", async () => {
|
||||
it("shows one pending message and sends all consecutive inputs once after completion", async () => {
|
||||
const onSend = vi.fn();
|
||||
const { rerender } = render(
|
||||
<ThreadComposer
|
||||
@@ -3451,10 +3449,9 @@ describe("ThreadComposer", () => {
|
||||
expect(queue).toHaveClass("composer-status-strip");
|
||||
expect(queue).toHaveClass("mx-3");
|
||||
expect(queue.parentElement?.className).toContain("group/composer");
|
||||
expect(within(queue).getByText("first follow-up")).toBeInTheDocument();
|
||||
expect(within(queue).getByText("second follow-up")).toBeInTheDocument();
|
||||
expect(within(queue).getAllByRole("button", { name: "Edit message" })).toHaveLength(2);
|
||||
expect(within(queue).getAllByRole("button", { name: "Send now" })).toHaveLength(2);
|
||||
expect(within(queue).getByText("first follow-up second follow-up")).toHaveTextContent("first follow-up second follow-up");
|
||||
expect(within(queue).getAllByRole("button", { name: "Edit message" })).toHaveLength(1);
|
||||
expect(within(queue).getAllByRole("button", { name: "Send now" })).toHaveLength(1);
|
||||
|
||||
rerender(
|
||||
<ThreadComposer
|
||||
@@ -3466,11 +3463,10 @@ describe("ThreadComposer", () => {
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSend).toHaveBeenCalledWith("first follow-up");
|
||||
expect(onSend).toHaveBeenCalledWith("first follow-up\nsecond follow-up");
|
||||
});
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
expect(screen.queryByText("first follow-up")).not.toBeInTheDocument();
|
||||
expect(screen.getByText("second follow-up")).toBeInTheDocument();
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
|
||||
rerender(
|
||||
<ThreadComposer
|
||||
@@ -3489,10 +3485,7 @@ describe("ThreadComposer", () => {
|
||||
/>,
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSend).toHaveBeenLastCalledWith("second follow-up");
|
||||
});
|
||||
expect(onSend).toHaveBeenCalledTimes(2);
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
@@ -3544,65 +3537,23 @@ describe("ThreadComposer", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("requeues edited guidance at the end of the pending list", async () => {
|
||||
it("edits the complete pending message without overwriting a newer draft", () => {
|
||||
const onSend = vi.fn();
|
||||
const { rerender } = render(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
onStop={vi.fn()}
|
||||
isStreaming
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
const { rerender } = render(<ThreadComposer onSend={onSend} isStreaming />);
|
||||
const input = screen.getByLabelText("Message input");
|
||||
fireEvent.change(input, { target: { value: "first follow-up" } });
|
||||
for (const text of ["first follow-up", "second follow-up"]) {
|
||||
fireEvent.change(input, { target: { value: text } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
}
|
||||
fireEvent.change(input, { target: { value: "third follow-up" } });
|
||||
fireEvent.click(screen.getByRole("button", { name: "Edit message" }));
|
||||
expect(input).toHaveValue("first follow-up\nsecond follow-up\nthird follow-up");
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
fireEvent.change(input, { target: { value: "edited complete message" } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
fireEvent.change(input, { target: { value: "second follow-up" } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: "Edit message" })[0]);
|
||||
await waitFor(() => {
|
||||
expect(input).toHaveValue("first follow-up");
|
||||
});
|
||||
fireEvent.change(input, { target: { value: "first follow-up edited" } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
|
||||
rerender(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
onStop={vi.fn()}
|
||||
isStreaming={false}
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSend).toHaveBeenCalledWith("second follow-up");
|
||||
});
|
||||
rerender(<ThreadComposer onSend={onSend} />);
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
|
||||
rerender(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
onStop={vi.fn()}
|
||||
isStreaming
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
rerender(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
onStop={vi.fn()}
|
||||
isStreaming={false}
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSend).toHaveBeenLastCalledWith("first follow-up edited");
|
||||
});
|
||||
expect(onSend).toHaveBeenCalledTimes(2);
|
||||
expect(onSend).toHaveBeenCalledWith("edited complete message");
|
||||
});
|
||||
|
||||
it("queues image guidance while running and restores it for editing", async () => {
|
||||
@@ -3660,98 +3611,19 @@ describe("ThreadComposer", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("reorders queued guidance while dragging over another row", async () => {
|
||||
it("deletes the complete pending message across remounts", () => {
|
||||
const onSend = vi.fn();
|
||||
const { rerender } = render(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
onStop={vi.fn()}
|
||||
isStreaming
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
const first = render(<ThreadComposer onSend={onSend} isStreaming pendingQueueKey="chat-a" />);
|
||||
const input = screen.getByLabelText("Message input");
|
||||
fireEvent.change(input, { target: { value: "first follow-up" } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
fireEvent.change(input, { target: { value: "second follow-up" } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
|
||||
const handles = screen.getAllByLabelText("Drag to reorder");
|
||||
const secondRow = screen
|
||||
.getByText("second follow-up")
|
||||
.closest("[data-queued-prompt-row='true']");
|
||||
expect(secondRow).toBeTruthy();
|
||||
|
||||
const dataTransfer = {
|
||||
effectAllowed: "",
|
||||
dropEffect: "",
|
||||
setData: vi.fn(),
|
||||
getData: vi.fn(),
|
||||
};
|
||||
fireEvent.dragStart(handles[0], { dataTransfer });
|
||||
fireEvent.dragEnter(secondRow!, { dataTransfer });
|
||||
fireEvent.dragEnd(handles[0], { dataTransfer });
|
||||
|
||||
rerender(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
onStop={vi.fn()}
|
||||
isStreaming={false}
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSend).toHaveBeenCalledWith("second follow-up");
|
||||
});
|
||||
});
|
||||
|
||||
it("moves later queued guidance before an earlier item while dragging", async () => {
|
||||
const onSend = vi.fn();
|
||||
const { rerender } = render(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
onStop={vi.fn()}
|
||||
isStreaming
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
const input = screen.getByLabelText("Message input");
|
||||
fireEvent.change(input, { target: { value: "first follow-up" } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
fireEvent.change(input, { target: { value: "second follow-up" } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
|
||||
const handles = screen.getAllByLabelText("Drag to reorder");
|
||||
const firstRow = screen
|
||||
.getByText("first follow-up")
|
||||
.closest("[data-queued-prompt-row='true']");
|
||||
expect(firstRow).toBeTruthy();
|
||||
|
||||
const dataTransfer = {
|
||||
effectAllowed: "",
|
||||
dropEffect: "",
|
||||
setData: vi.fn(),
|
||||
getData: vi.fn(),
|
||||
};
|
||||
fireEvent.dragStart(handles[1], { dataTransfer });
|
||||
fireEvent.dragEnter(firstRow!, { dataTransfer });
|
||||
fireEvent.dragEnd(handles[1], { dataTransfer });
|
||||
|
||||
rerender(
|
||||
<ThreadComposer
|
||||
onSend={onSend}
|
||||
onStop={vi.fn()}
|
||||
isStreaming={false}
|
||||
placeholder="Type your message..."
|
||||
/>,
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(onSend).toHaveBeenCalledWith("second follow-up");
|
||||
});
|
||||
for (const text of ["1", "2"]) {
|
||||
fireEvent.change(input, { target: { value: text } });
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
}
|
||||
fireEvent.click(screen.getByRole("button", { name: "Delete message" }));
|
||||
first.unmount();
|
||||
render(<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" />);
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps queued guidance in its session when switching from running to idle", () => {
|
||||
@@ -3887,13 +3759,13 @@ describe("ThreadComposer", () => {
|
||||
<ThreadComposer onSend={otherSend} pendingQueueKey="chat-b" completedTurnIds={["turn-a"]} />,
|
||||
);
|
||||
expect(otherSend).not.toHaveBeenCalled();
|
||||
expect(screen.queryByText("first follow-up")).not.toBeInTheDocument();
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
other.unmount();
|
||||
|
||||
const returned = render(
|
||||
<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={[]} />,
|
||||
);
|
||||
expect(screen.getByText("first follow-up")).toBeInTheDocument();
|
||||
expect(screen.getByText("first follow-up second follow-up")).toBeInTheDocument();
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
returned.rerender(
|
||||
<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["turn-other"]} />,
|
||||
@@ -3902,15 +3774,16 @@ describe("ThreadComposer", () => {
|
||||
returned.rerender(
|
||||
<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["turn-a"]} />,
|
||||
);
|
||||
await waitFor(() => expect(onSend).toHaveBeenCalledWith("first follow-up"));
|
||||
await waitFor(() => expect(onSend).toHaveBeenCalledWith("first follow-up\nsecond follow-up"));
|
||||
for (let update = 0; update < 3; update += 1) {
|
||||
returned.rerender(
|
||||
<ThreadComposer onSend={(...args) => onSend(...args)} pendingQueueKey="chat-a" completedTurnIds={["turn-a"]} />,
|
||||
);
|
||||
}
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
expect(screen.getByText("second follow-up")).toBeInTheDocument();
|
||||
expect(screen.getByRole("button", { name: "Send now" })).toBeDisabled();
|
||||
await act(async () => { acceptDelivery(true); await delivery; });
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
returned.rerender(
|
||||
<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" isStreaming activeTurnId="turn-next" completedTurnIds={["turn-a"]} />,
|
||||
);
|
||||
@@ -3922,8 +3795,7 @@ describe("ThreadComposer", () => {
|
||||
render(
|
||||
<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["turn-a", "turn-next"]} />,
|
||||
);
|
||||
await waitFor(() => expect(onSend).toHaveBeenLastCalledWith("second follow-up"));
|
||||
expect(onSend).toHaveBeenCalledTimes(2);
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
@@ -3958,7 +3830,7 @@ describe("ThreadComposer", () => {
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send now" }));
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
expect(onSend).toHaveBeenCalledWith("keep for later", undefined, undefined);
|
||||
expect(onSend).toHaveBeenCalledWith("keep for later");
|
||||
});
|
||||
|
||||
it("retains a rejected queued attachment for manual retry after remounting", async () => {
|
||||
@@ -3973,25 +3845,28 @@ describe("ThreadComposer", () => {
|
||||
target: { files: [new File(["image"], "queued.png", { type: "image/png" })] },
|
||||
});
|
||||
await screen.findByText("queued.png");
|
||||
fireEvent.change(screen.getByLabelText("Message input"), { target: { value: "queued attachment" } });
|
||||
fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" });
|
||||
for (const text of ["queued attachment", "more guidance"]) {
|
||||
fireEvent.change(screen.getByLabelText("Message input"), { target: { value: text } });
|
||||
fireEvent.keyDown(screen.getByLabelText("Message input"), { key: "Enter" });
|
||||
}
|
||||
first.unmount();
|
||||
const returned = render(
|
||||
<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["turn-a"]} />,
|
||||
);
|
||||
await waitFor(() => expect(onSend).toHaveBeenCalledTimes(1));
|
||||
await act(async () => { rejectDelivery(new Error("Attachment upload interrupted")); });
|
||||
expect(screen.getByText("queued attachment")).toBeInTheDocument();
|
||||
expect(screen.getByText("queued attachment more guidance")).toBeInTheDocument();
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
returned.unmount();
|
||||
render(
|
||||
<ThreadComposer onSend={onSend} pendingQueueKey="chat-a" completedTurnIds={["turn-a"]} />,
|
||||
);
|
||||
expect(screen.getByText("queued attachment")).toBeInTheDocument();
|
||||
expect(screen.getByText("queued attachment more guidance")).toBeInTheDocument();
|
||||
expect(onSend).toHaveBeenCalledTimes(1);
|
||||
onSend.mockReturnValue(true);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send now" }));
|
||||
expect(onSend).toHaveBeenCalledTimes(2);
|
||||
expect(onSend.mock.calls[1][0]).toBe("queued attachment\nmore guidance");
|
||||
expect(onSend.mock.calls[1][1]).toEqual([expect.objectContaining({ media: expect.objectContaining({ name: "queued.png" }) })]);
|
||||
});
|
||||
|
||||
@@ -4004,7 +3879,7 @@ describe("ThreadComposer", () => {
|
||||
expect(screen.getByText("legacy follow-up")).toBeInTheDocument();
|
||||
expect(onSend).not.toHaveBeenCalled();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Send now" }));
|
||||
expect(onSend).toHaveBeenCalledWith("legacy follow-up", undefined, undefined);
|
||||
expect(onSend).toHaveBeenCalledWith("legacy follow-up");
|
||||
});
|
||||
|
||||
it("keeps temporary chat guidance in memory only", async () => {
|
||||
|
||||
@@ -1727,7 +1727,7 @@ describe("ThreadShell", () => {
|
||||
expect(screen.getByText("persist me across tabs")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it.each([false, true])("drains persisted follow-ups before history refresh resolves (pending tools=%s)", async (pendingTools) => {
|
||||
it.each([false, true])("sends the merged pending message before history refresh resolves (pending tools=%s)", async (pendingTools) => {
|
||||
const client = makeClient();
|
||||
const chatId = "queued-live-completion";
|
||||
const owner = "queued-owner";
|
||||
@@ -1766,7 +1766,8 @@ describe("ThreadShell", () => {
|
||||
await waitFor(() => expect(refreshStarted).toBe(true));
|
||||
try {
|
||||
act(() => client._emitChat(chatId, { event: "turn_end", chat_id: chatId, turn_id: owner }));
|
||||
await waitFor(() => expectSendMessageWithTurn(client, chatId, "first persisted follow-up"));
|
||||
await waitFor(() => expectSendMessageWithTurn(client, chatId, "first persisted follow-up\nsecond persisted follow-up"));
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
expect(client.sendMessage).toHaveBeenCalledTimes(1);
|
||||
const nextTurnId = client.sendMessage.mock.calls[0][3]!.turnId!;
|
||||
act(() => client._emitChat(chatId, {
|
||||
@@ -1775,14 +1776,13 @@ describe("ThreadShell", () => {
|
||||
}));
|
||||
expect(client.sendMessage).toHaveBeenCalledTimes(1);
|
||||
act(() => client._emitChat(chatId, { event: "turn_end", chat_id: chatId, turn_id: nextTurnId }));
|
||||
await waitFor(() => expectSendMessageWithTurn(client, chatId, "second persisted follow-up"));
|
||||
expect(client.sendMessage).toHaveBeenCalledTimes(2);
|
||||
expect(client.sendMessage).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
await act(async () => { finishRefresh(initialHistory as Response); });
|
||||
}
|
||||
});
|
||||
|
||||
it("drains temporary follow-ups once per live completed turn without persisted history", async () => {
|
||||
it("sends the complete temporary pending message once without persisted history", async () => {
|
||||
const client = makeClient();
|
||||
const chatId = "temporary-queued-follow-ups";
|
||||
render(wrap(client, <ThreadShell
|
||||
@@ -1803,9 +1803,9 @@ describe("ThreadShell", () => {
|
||||
act(() => client._emitChat(chatId, {
|
||||
event: "turn_end", chat_id: chatId, turn_id: "temporary-owner",
|
||||
}));
|
||||
await waitFor(() => expectSendMessageWithTurn(client, chatId, "first temporary follow-up"));
|
||||
await waitFor(() => expectSendMessageWithTurn(client, chatId, "first temporary follow-up\nsecond temporary follow-up"));
|
||||
expect(client.sendMessage).toHaveBeenCalledTimes(1);
|
||||
expect(screen.getByText("second temporary follow-up")).toBeInTheDocument();
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
const nextTurnId = client.sendMessage.mock.calls[0][3]!.turnId!;
|
||||
act(() => client._emitChat(chatId, {
|
||||
event: "goal_status", chat_id: chatId, turn_id: nextTurnId,
|
||||
@@ -1816,8 +1816,7 @@ describe("ThreadShell", () => {
|
||||
act(() => client._emitChat(chatId, {
|
||||
event: "turn_end", chat_id: chatId, turn_id: nextTurnId,
|
||||
}));
|
||||
await waitFor(() => expectSendMessageWithTurn(client, chatId, "second temporary follow-up"));
|
||||
expect(client.sendMessage).toHaveBeenCalledTimes(2);
|
||||
expect(client.sendMessage).toHaveBeenCalledTimes(1);
|
||||
expect(screen.queryByRole("group", { name: "Waiting to send" })).not.toBeInTheDocument();
|
||||
expect(vi.mocked(fetch).mock.calls.some(([url]) => String(url).includes("/webui-thread"))).toBe(false);
|
||||
expect(window.localStorage.getItem(`nanobot.webui.composerQueuedGuidance.v1:${chatId}`)).toBeNull();
|
||||
|
||||
Reference in new issue
Block a user