fix(webui): separate file cards from media previews

This commit is contained in:
Xubin Ren committed 2026-10-24 23:55:10 +08:00
1 parent d6cc08f335
commit 0b22c1875a
21 files changed
+432 -44

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { AttachmentTile } from "@/components/AttachmentTile";
describe("attachment download cards", () => {
it("keeps named download actions distinct and shows the file extension", () => {
render(<>
<AttachmentTile attachment={{ kind: "file", name: "猫咪图鉴完整包.zip", url: "/api/media/signed/archive" }} />
<AttachmentTile attachment={{ kind: "file", name: "source.tar.gz", url: "/api/media/signed/source" }} />
</>);
const archive = screen.getByRole("link", { name: "Download 猫咪图鉴完整包.zip" });
expect(archive).toHaveAttribute("download", "猫咪图鉴完整包.zip");
expect(archive).toHaveAttribute("href", "/api/media/signed/archive");
expect(archive).toHaveAttribute("title", "猫咪图鉴完整包.zip");
expect(archive).toHaveTextContent("ZIP");
expect(archive.querySelector(".lucide-download")).toHaveAttribute("aria-hidden", "true");
expect(screen.getByRole("link", { name: "Download source.tar.gz" })).toHaveTextContent("TAR.GZ");
});
it("changes a pending file to a download only after its URL arrives", () => {
const attachment = { kind: "file" as const, name: "report.pdf" };
const { rerender } = render(<AttachmentTile attachment={attachment} pending />);
expect(screen.getByText("Sending…")).toBeVisible();
expect(screen.queryByRole("link")).not.toBeInTheDocument();
rerender(<AttachmentTile attachment={{ ...attachment, url: "/api/media/signed/report" }} />);
expect(screen.getByRole("link", { name: "Download report.pdf" })).toHaveTextContent("PDF");
rerender(<AttachmentTile attachment={attachment} />);
expect(screen.getByText("Attachment unavailable")).toBeVisible();
expect(screen.queryByRole("link")).not.toBeInTheDocument();
});
it("keeps unnamed and extensionless attachments identifiable", () => {
render(<>
<AttachmentTile attachment={{ kind: "file", url: "/api/media/signed/archive.zip?signature=example#payload.txt" }} />
<AttachmentTile attachment={{ kind: "file", name: "LICENSE", url: "/api/media/signed/license" }} />
</>);
const unnamed = screen.getByRole("link", { name: "Download File attachment" });
expect(unnamed).toHaveAttribute("download", "File attachment");
expect(unnamed).toHaveTextContent("File attachment");
expect(unnamed.querySelector(".lucide-file-archive")).toBeInTheDocument();
expect(screen.getByRole("link", { name: "Download LICENSE" })).toHaveTextContent("File attachment");
});
});
+16 -4
View File
@@ -14,6 +14,7 @@ describe("shared file icons", () => {
["styles.css", "file-code2"],
["app.js", "file-code2"],
["app.ts", "file-code2"],
["app#draft.ts", "file-code2"],
["app.tsx", "file-code2"],
["script.py", "file-code2"],
["analysis.ipynb", "file-code2"],
@@ -22,7 +23,18 @@ describe("shared file icons", () => {
["notes.txt", "file-text"],
["build.log", "file-text"],
["demo.mp4", "file-video2"],
["archive.zip", "file"],
["https://files.example.com/demo.mp4?signature=abc#download.zip", "file-video2"],
["archive.zip", "file-archive"],
["backup.tar.gz", "file-archive"],
["voice.mp3", "file-audio"],
["recording.flac", "file-audio"],
["REPORT#2.PDF", "file-text"],
["proposal.docx", "file-text"],
["budget.xlsx", "file-spreadsheet"],
["results.csv", "file-spreadsheet"],
["slides.pptx", "file-chart-column"],
["slides.odp", "file-chart-column"],
["payload.bin", "file"],
])("uses a consistent monochrome file-family icon for %s", (path, icon) => {
const { container } = render(<FileReferenceIcon kind={fileKindForPath(path)} />);
const svg = container.querySelector("svg")!;
@@ -36,7 +48,7 @@ describe("shared file icons", () => {
expect(svg.querySelector("text, [fill], [stroke]")).toBeNull();
});
it.each(["chart.png", "index.html", "data.json", "script.py", "notes.md"])(
it.each(["chart.png", "index.html", "data.json", "script.py", "notes.md", "archive.zip", "report.pdf", "budget.xlsx", "slides.pptx", "song.mp3"])(
"reuses the same glyph for %s in replies, tabs and attachments without changing actions", (path) => {
const open = vi.fn();
render(<>
@@ -48,12 +60,12 @@ describe("shared file icons", () => {
</>);
const reference = screen.getByRole("button", { name: path });
const tab = screen.getByRole("tab", { name: path });
const attachment = screen.getByRole("link", { name: "File attachment" });
const attachment = screen.getByRole("link", { name: `Download ${path}` });
const glyph = reference.querySelector("svg")!.innerHTML;
expect(tab.querySelector("svg")!.innerHTML).toBe(glyph);
expect(attachment.querySelector("svg")!.innerHTML).toBe(glyph);
expect(tab.querySelector("svg")).toHaveClass("size-3.5");
expect(attachment.querySelector("svg")).toHaveClass("size-4");
expect(attachment.querySelector("svg")).toHaveClass("size-5");
expect(tab.querySelector("svg")).not.toHaveClass("translate-y-[0.12em]");
expect(attachment.querySelector("svg")).not.toHaveClass("translate-y-[0.12em]");
expect(attachment).toHaveAttribute("href", `/download/${path}`);
@@ -291,7 +291,7 @@ describe("MarkdownTextRenderer", () => {
it("renders markdown links with file-looking names as file attachments", () => {
render(<MarkdownTextRenderer>![index.html](/api/media/sig/html)</MarkdownTextRenderer>);
expect(screen.getByLabelText("File attachment")).toHaveTextContent("index.html");
expect(screen.getByRole("link", { name: "Download index.html" })).toHaveTextContent("index.html");
expect(screen.queryByRole("img", { name: "index.html" })).not.toBeInTheDocument();
});
+2 -2
View File
@@ -1268,7 +1268,7 @@ describe("MessageBubble", () => {
const { container } = render(<MessageBubble message={message} />);
expect(screen.getByLabelText("File attachment")).toHaveTextContent("index.html");
expect(screen.getByRole("link", { name: "Download index.html" })).toHaveTextContent("index.html");
expect(container.querySelector("img")).not.toBeInTheDocument();
});
@@ -1291,7 +1291,7 @@ describe("MessageBubble", () => {
expect(screen.getByRole("button", { name: /view image: growth.svg/i })).toBeInTheDocument();
expect(container.querySelector('img[src="/api/media/sig/svg"]')).toBeInTheDocument();
expect(screen.queryByLabelText("File attachment")).not.toBeInTheDocument();
expect(screen.queryByRole("link", { name: "Download growth.svg" })).not.toBeInTheDocument();
});
});