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

+50 -3
View File
@@ -70,13 +70,60 @@ describe("image presentation", () => {
expect(within(screen.getByRole("dialog")).getByRole("img", { name: "0.png" })).toBeVisible();
});
it("replaces a broken thumbnail with a quiet named placeholder", () => {
it("shows an explicit failure without painting a filename across the thumbnail", () => {
render(<ImageGallery images={[images[0]]} />);
fireEvent.error(screen.getByRole("img"));
expect(screen.queryByRole("img")).not.toBeInTheDocument();
expect(screen.getByText("0.png")).toBeVisible();
expect(screen.getByText("Attachment unavailable")).toBeVisible();
expect(screen.getByLabelText("Attachment unavailable: 0.png")).toHaveAttribute("title", "0.png");
expect(screen.queryByText("0.png")).not.toBeInTheDocument();
expect(screen.queryByRole("button")).not.toBeInTheDocument();
});
it("uses the loaded picture's shape for a sent user image and retains its accessible viewer action", async () => {
const user = userEvent.setup();
render(<MessageBubble message={{ id: "portrait", role: "user", content: "", createdAt: 1,
images: [{ url: "/portrait.png", name: "portrait.png" }] }} />);
const image = screen.getByRole("img", { name: "portrait.png" });
Object.defineProperties(image, { naturalWidth: { value: 600 }, naturalHeight: { value: 900 } });
fireEvent.load(image);
const open = screen.getByRole("button", { name: "View image: portrait.png" });
expect(Number.parseFloat(open.style.width)).toBeCloseTo(320 * 2 / 3);
expect(open.style.height).toBe("320px");
expect(screen.queryByText("portrait.png")).not.toBeInTheDocument();
await user.click(open);
expect(within(screen.getByRole("dialog")).getByRole("img", { name: "portrait.png" })).toBeVisible();
});
it("settles a mixed gallery together after loading and preserves click order through regrouping", async () => {
const user = userEvent.setup();
const { container } = render(<ImageGallery images={images.slice(0, 4)} size="compact" />);
for (const image of screen.getAllByRole("img")) {
Object.defineProperties(image, { naturalWidth: { value: 800 }, naturalHeight: { value: 800 } });
fireEvent.load(image);
}
const tiles = Array.from(container.querySelectorAll<HTMLElement>("[data-image-gallery] > button"));
expect(tiles.map((tile) => Number.parseFloat(tile.style.top))).toEqual([0, 0, 184, 184]);
expect(screen.getAllByRole("button").map((button) => button.getAttribute("aria-label"))).toEqual(
[0, 1, 2, 3].map((index) => `View image: ${index}.png`));
await user.click(screen.getByRole("button", { name: "View image: 2.png" }));
expect(screen.getByText("3 / 4")).toBeVisible();
expect(within(screen.getByRole("dialog")).getByRole("img", { name: "2.png" })).toBeVisible();
});
it("returns focus to the same thumbnail when late image loads change the row grouping", async () => {
const user = userEvent.setup();
const { container } = render(<ImageGallery images={images.slice(0, 4)} size="compact" />);
const thumbnails = container.querySelectorAll("[data-image-gallery] img");
const opener = screen.getByRole("button", { name: "View image: 2.png" });
await user.click(opener);
for (const image of thumbnails) {
Object.defineProperties(image, { naturalWidth: { value: 600 }, naturalHeight: { value: 900 } });
fireEvent.load(image);
}
await user.keyboard("{Escape}");
expect(screen.getByRole("button", { name: "View image: 2.png" })).toBe(opener);
expect(opener).toHaveFocus();
});
});
describe("file quick look", () => {