feat(webui): refine attachment galleries and file cards (#6457)

* feat(webui): refine attachment galleries and file cards

* fix(webui): align attachment tests and Escape handling
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-25 22:39:44 +08:00
1 parent 44a7ef53ce
commit eed7b47753
19 files changed
+660 -255

No files matched your search

@@ -4,7 +4,9 @@ import {
render,
screen,
waitFor,
within,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ThreadComposer } from "@/components/thread/ThreadComposer";
@@ -87,6 +89,7 @@ beforeEach(() => {
describe("ThreadComposer — attachments", () => {
it("previews a picked image with size details on hover and includes its data url on send", async () => {
const user = userEvent.setup();
const file = pngFile("a.png", 100);
encodeImage.mockResolvedValueOnce({ ...resolveReady(file), normalized: true, bytes: 50 });
const onSend = vi.fn();
@@ -103,7 +106,10 @@ describe("ThreadComposer — attachments", () => {
});
expect(screen.getByRole("img", { name: "a.png" })).toHaveAttribute("src", expect.stringContaining("blob:"));
expect(screen.getByTestId("composer-chip")).toHaveAttribute("title", "a.png\n100 B → 50 B (auto)");
await user.hover(screen.getByRole("button", { name: "a.png" }));
const details = await screen.findByRole("tooltip");
expect(details).toHaveTextContent("a.png");
expect(details).toHaveTextContent("100 B → 50 B (auto)");
expect(screen.getByRole("button", { name: "Remove attachment" })).toHaveAccessibleDescription("a.png");
const textarea = screen.getByLabelText(/message input/i);
@@ -118,6 +124,32 @@ describe("ThreadComposer — attachments", () => {
expect(images[0].media.name).toBe("a.png");
});
it("identifies same-type files through focus/tap details and removes only the chosen attachment", async () => {
const user = userEvent.setup();
const first = new File(['{"theme":"light"}'], "render-config.json", { type: "application/json" });
const second = new File(['{"theme":"dark"}'], "another-config.json", { type: "application/json" });
const onSend = vi.fn();
render(<ThreadComposer onSend={onSend} />);
const textarea = screen.getByLabelText(/message input/i);
const input = textarea.closest("form")!.querySelector('input[type="file"]')!;
await act(async () => fireEvent.change(input, { target: { files: [first, second] } }));
const cards = screen.getAllByTestId("composer-chip");
expect(cards.map((card) => card.textContent)).toEqual(["JSON", "JSON"]);
const firstInfo = within(cards[0]).getByRole("button", { name: first.name });
act(() => firstInfo.focus());
expect(await screen.findByRole("tooltip")).toHaveTextContent(first.name);
expect(screen.getByRole("tooltip")).toHaveTextContent(`${first.size} B`);
await user.keyboard("{Escape}");
await user.click(within(cards[1]).getByRole("button", { name: second.name }));
expect(await screen.findByRole("dialog", { name: second.name })).toHaveTextContent(second.name);
await user.keyboard("{Escape}");
await user.click(within(cards[1]).getByRole("button", { name: "Remove attachment" }));
expect(screen.getAllByTestId("composer-chip")).toHaveLength(1);
expect(screen.getByRole("button", { name: first.name })).toBeVisible();
fireEvent.keyDown(textarea, { key: "Enter" });
expect(onSend.mock.calls[0][1].map((attachment: { media: { name: string } }) => attachment.media.name)).toEqual([first.name]);
});
it("attaches a picked PDF and includes its data url on send", async () => {
const file = pdfFile();
const onSend = vi.fn();
@@ -134,7 +166,7 @@ describe("ThreadComposer — attachments", () => {
});
await waitFor(() =>
expect(screen.getByTestId("composer-chip")).toHaveTextContent("report.pdf"),
expect(screen.getByTestId("composer-chip")).toHaveTextContent("PDF"),
);
const textarea = screen.getByLabelText(/message input/i);
@@ -168,7 +200,7 @@ describe("ThreadComposer — attachments", () => {
});
await waitFor(() =>
expect(screen.getByTestId("composer-chip")).toHaveTextContent("report.csv"),
expect(screen.getByRole("button", { name: "report.csv" })).toHaveAccessibleDescription(expect.stringContaining("B")),
);
const textarea = screen.getByLabelText(/message input/i);
@@ -242,8 +274,8 @@ describe("ThreadComposer — attachments", () => {
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
expect(screen.getAllByTestId("composer-chip")).toHaveLength(2);
expect(screen.getByText("first.pdf")).toBeInTheDocument();
expect(screen.getByText("second.pdf")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "first.pdf" })).toBeInTheDocument();
expect(screen.getByRole("button", { name: "second.pdf" })).toBeInTheDocument();
});
it("enforces the decoded attachment-total policy independently", async () => {
@@ -317,7 +349,7 @@ describe("ThreadComposer — attachments", () => {
});
await waitFor(() =>
expect(screen.getByText("pasted.pdf")).toBeInTheDocument(),
expect(screen.getByRole("button", { name: "pasted.pdf" })).toBeInTheDocument(),
);
await act(async () => {
@@ -332,7 +364,7 @@ describe("ThreadComposer — attachments", () => {
});
await waitFor(() =>
expect(screen.getByText("dropped.pdf")).toBeInTheDocument(),
expect(screen.getByRole("button", { name: "dropped.pdf" })).toBeInTheDocument(),
);
expect(screen.getAllByTestId("composer-chip")).toHaveLength(2);
expect(encodeImage).not.toHaveBeenCalled();
@@ -421,7 +453,7 @@ describe("local attachment paths and video ingress", () => {
fireEvent.change(input, { target: { value: "keep this " } });
(input as HTMLTextAreaElement).setSelectionRange(10, 10);
fireEvent.paste(input, { clipboardData: { items: [], getData: () => '"C:\\project\\my clip.mp4"' } });
await waitFor(() => expect(screen.getByTestId("composer-chip")).toHaveTextContent("my clip.mp4"));
await waitFor(() => expect(screen.getByRole("button", { name: "my clip.mp4" })).toBeInTheDocument());
expect(input).toHaveValue("keep this ");
expect(inspect).toHaveBeenCalledWith(local.path);
fireEvent.keyDown(input, { key: "Enter" });
@@ -568,7 +600,7 @@ describe("local attachment paths and video ingress", () => {
render(<ThreadComposer onSend={onSend} onInspectAttachment={vi.fn().mockResolvedValue(document)} ingressLimits={ingressLimits({ maxFileBytes: 8, maxTotalBytes: 8 })} />);
const input = screen.getByLabelText(/message input/i);
fireEvent.paste(input, { clipboardData: { items: [], getData: () => document.path } });
await screen.findByText(document.name);
await screen.findByRole("button", { name: document.name });
const picker = input.closest("form")!.querySelector('input[type="file"]')!;
fireEvent.change(picker, { target: { files: [pdfFile("upload.pdf", 8)] } });
await waitFor(() => expect(screen.getByRole("button", { name: /send message/i })).toBeEnabled());