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:
1 parent
44a7ef53ce
commit
eed7b47753
19 files changed
+660
-255
No files matched your search
@@ -26,6 +26,26 @@ async function bounds(locator: Locator) {
|
||||
return box!;
|
||||
}
|
||||
|
||||
async function expectFileDetails(page: Page, card: Locator, filename: string, width: number, description?: string) {
|
||||
const info = card.getByRole("button", { name: filename, exact: true });
|
||||
await info.hover();
|
||||
await expect(page.getByRole("tooltip")).toContainText(filename);
|
||||
await info.click();
|
||||
const details = page.getByRole("dialog", { name: filename, exact: true });
|
||||
const name = details.getByText(filename, { exact: true });
|
||||
await expect(name).toBeVisible();
|
||||
if (description) await expect(details.getByText(description, { exact: true })).toBeVisible();
|
||||
const box = await bounds(details);
|
||||
expect(box.x).toBeGreaterThanOrEqual(0);
|
||||
expect(box.x + box.width).toBeLessThanOrEqual(width);
|
||||
expect(await name.evaluate((element) => element.scrollWidth <= element.clientWidth)).toBe(true);
|
||||
expect(await details.evaluate((element) => element.scrollHeight <= element.clientHeight)).toBe(true);
|
||||
await page.keyboard.press("Escape");
|
||||
await expect(details).toHaveCount(0);
|
||||
await expect(info).toBeFocused();
|
||||
await page.mouse.move(0, 0);
|
||||
}
|
||||
|
||||
for (const width of [1024, 360]) {
|
||||
for (const theme of ["light", "dark"]) {
|
||||
test(`mixed attachments stay compact, aligned and readable at ${width}px in ${theme}`, async ({ page }, testInfo) => {
|
||||
@@ -44,16 +64,19 @@ for (const width of [1024, 360]) {
|
||||
|
||||
for (const role of ["Assistant", "User"]) {
|
||||
const region = page.getByRole("region", { name: `${role} mixed attachments`, exact: true });
|
||||
const card = region.locator("a[download]");
|
||||
const card = region.locator("[data-file-attachment]");
|
||||
await expect(card).toHaveCount(1);
|
||||
const file = await bounds(card);
|
||||
const regionBox = await bounds(region);
|
||||
const imageBoxes = await region.locator("img").evaluateAll((images) => images.map((image) => {
|
||||
const rect = image.getBoundingClientRect();
|
||||
return { bottom: rect.bottom, left: rect.left, right: rect.right };
|
||||
const { naturalWidth, naturalHeight } = image as HTMLImageElement;
|
||||
return { bottom: rect.bottom, left: rect.left, right: rect.right, naturalWidth, naturalHeight };
|
||||
}));
|
||||
await testInfo.attach(`${role} geometry`, { body: JSON.stringify({ file, region: regionBox, images: imageBoxes }, null, 2), contentType: "application/json" });
|
||||
expect(file.height, `${role} file must not stretch to the image row height`).toBeLessThanOrEqual(80);
|
||||
expect(file.width, `${role} file card keeps its compact width`).toBe(96);
|
||||
expect(file.height, `${role} file card must not stretch to the image row height`).toBe(96);
|
||||
for (const image of imageBoxes) expect(image).toMatchObject({ naturalWidth: 320, naturalHeight: 200 });
|
||||
expect(file.y, `${role} file belongs below all images`).toBeGreaterThanOrEqual(Math.max(...imageBoxes.map((image) => image.bottom)));
|
||||
if (role === "Assistant") {
|
||||
expect(Math.abs(file.x - regionBox.x)).toBeLessThanOrEqual(1);
|
||||
@@ -63,15 +86,15 @@ for (const width of [1024, 360]) {
|
||||
expect(Math.abs(file.x + file.width - right)).toBeLessThanOrEqual(1);
|
||||
expect(Math.abs(Math.max(...imageBoxes.map((image) => image.right)) - right)).toBeLessThanOrEqual(2);
|
||||
}
|
||||
await expect(card).toHaveAccessibleName(`${chinese ? "下载" : "Download"} archive.zip`);
|
||||
await expect(card.getByRole("link")).toHaveAccessibleName(`${chinese ? "下载" : "Download"} archive.zip`);
|
||||
await expect(card.getByText("ZIP", { exact: true })).toBeVisible();
|
||||
}
|
||||
|
||||
const files = page.getByRole("region", { name: "Multiple files and long names" }).locator("a[download]");
|
||||
const files = page.getByRole("region", { name: "Multiple files and long names" }).locator("[data-file-attachment]");
|
||||
await expect(files).toHaveCount(3);
|
||||
const userFiles = page.getByRole("region", { name: "User multiple files", exact: true });
|
||||
const userBubble = await bounds(userFiles.locator(":scope > div"));
|
||||
const userCards = userFiles.locator("a[download]");
|
||||
const userCards = userFiles.locator("[data-file-attachment]");
|
||||
await expect(userCards).toHaveCount(3);
|
||||
const userCardBoxes = await Promise.all((await userCards.all()).map(bounds));
|
||||
await testInfo.attach("User multiple files geometry", { body: JSON.stringify({ bubble: userBubble, files: userCardBoxes }, null, 2), contentType: "application/json" });
|
||||
@@ -79,36 +102,47 @@ for (const width of [1024, 360]) {
|
||||
expect(box.x, "user file stays inside its message bubble").toBeGreaterThanOrEqual(userBubble.x);
|
||||
expect(box.x + box.width, "user file stays inside its message bubble").toBeLessThanOrEqual(userBubble.x + userBubble.width + 1);
|
||||
}
|
||||
for (const card of await page.locator("a[download]").all()) {
|
||||
const availableCards = page.locator("[data-file-attachment]").filter({ has: page.locator("a[download]") });
|
||||
for (const card of await availableCards.all()) {
|
||||
const box = await bounds(card);
|
||||
expect(box.height).toBeLessThanOrEqual(80);
|
||||
expect(box.width).toBe(96);
|
||||
expect(box.height).toBe(96);
|
||||
expect(box.x).toBeGreaterThanOrEqual(0);
|
||||
expect(box.x + box.width).toBeLessThanOrEqual(width);
|
||||
const icon = card.locator("svg.lucide-download");
|
||||
const download = card.locator("a[download]");
|
||||
const target = await bounds(download);
|
||||
expect(target.width).toBe(44);
|
||||
expect(target.height).toBe(44);
|
||||
expect(target.x).toBeGreaterThanOrEqual(box.x);
|
||||
expect(target.x + target.width).toBeLessThanOrEqual(box.x + box.width);
|
||||
expect(target.y).toBeGreaterThanOrEqual(box.y);
|
||||
expect(target.y + target.height).toBeLessThanOrEqual(box.y + box.height);
|
||||
const icon = download.locator("svg.lucide-download");
|
||||
await expect(icon).toBeVisible();
|
||||
const iconBox = await bounds(icon);
|
||||
expect(iconBox.width).toBeGreaterThanOrEqual(12);
|
||||
expect(iconBox.x).toBeGreaterThanOrEqual(box.x);
|
||||
expect(iconBox.x + iconBox.width).toBeLessThanOrEqual(box.x + box.width);
|
||||
expect(iconBox.y).toBeGreaterThanOrEqual(box.y);
|
||||
expect(iconBox.y + iconBox.height).toBeLessThanOrEqual(box.y + box.height);
|
||||
expect(iconBox.x).toBeGreaterThanOrEqual(target.x);
|
||||
expect(iconBox.x + iconBox.width).toBeLessThanOrEqual(target.x + target.width);
|
||||
expect(iconBox.y).toBeGreaterThanOrEqual(target.y);
|
||||
expect(iconBox.y + iconBox.height).toBeLessThanOrEqual(target.y + target.height);
|
||||
}
|
||||
for (const card of [files.nth(0), files.nth(1)]) {
|
||||
const name = card.getByText(/release-deliverables|项目交付资料/);
|
||||
await expect(name).toHaveCSS("text-overflow", "ellipsis");
|
||||
expect(await name.evaluate((element) => element.scrollWidth > element.clientWidth)).toBe(true);
|
||||
const nameBox = await bounds(name);
|
||||
expect(nameBox.x + nameBox.width).toBeLessThanOrEqual((await bounds(card.locator("svg.lucide-download"))).x);
|
||||
for (const [index, filename, type] of [
|
||||
[0, "release-deliverables-with-a-very-long-English-filename-for-layout-regression.zip", "ZIP"],
|
||||
[1, "项目交付资料与详细验收记录包含中英文的超长文件名称-final-review.pdf", "PDF"],
|
||||
] as const) {
|
||||
const card = files.nth(index);
|
||||
await expect(card.getByRole("button", { name: filename, exact: true })).toHaveText(type);
|
||||
await expectFileDetails(page, card, filename, width);
|
||||
}
|
||||
await expect(files.nth(1).getByText("PDF", { exact: true })).toBeVisible();
|
||||
await expect(files.nth(2)).toHaveAccessibleName(chinese ? "下载 文件附件" : "Download File attachment");
|
||||
await expect(files.nth(2).getByRole("link")).toHaveAccessibleName(chinese ? "下载 文件附件" : "Download File attachment");
|
||||
for (const [label, filename, status] of [
|
||||
["Pending attachment", "pending.zip", chinese ? "发送中…" : "Sending…"],
|
||||
["Unavailable attachment", "unavailable.pdf", chinese ? "附件不可用" : "Attachment unavailable"],
|
||||
]) {
|
||||
const region = page.getByRole("region", { name: label, exact: true });
|
||||
await expect(region.getByText(filename, { exact: true })).toBeVisible();
|
||||
await expect(region.getByText(status, { exact: true }).first()).toBeVisible();
|
||||
const card = region.locator("[data-file-attachment]");
|
||||
await expect(card.getByText(status, { exact: true })).toBeVisible();
|
||||
await expectFileDetails(page, card, filename, width, status);
|
||||
// A replay placeholder has no download capability until it has a URL.
|
||||
await expect(region.getByRole("link")).toHaveCount(0);
|
||||
}
|
||||
@@ -135,7 +169,22 @@ test("image lightbox and file download remain keyboard accessible", async ({ pag
|
||||
await page.keyboard.press("Escape");
|
||||
await expect(page.getByRole("dialog")).toHaveCount(0);
|
||||
await expect(firstImage).toBeFocused();
|
||||
for (let step = 0; step < 3; step++) await page.keyboard.press("Tab");
|
||||
for (const name of ["Blue study.svg", "Green study.svg"]) {
|
||||
await page.keyboard.press("Tab");
|
||||
await expect(region.getByRole("button", { name: `View image: ${name}`, exact: true })).toBeFocused();
|
||||
}
|
||||
await page.keyboard.press("Tab");
|
||||
const fileInfo = region.getByRole("button", { name: "archive.zip", exact: true });
|
||||
await expect(fileInfo).toBeFocused();
|
||||
expect(await fileInfo.evaluate((element) => element.matches(":focus-visible"))).toBe(true);
|
||||
await expect(page.getByRole("tooltip")).toContainText("archive.zip");
|
||||
await page.keyboard.press("Enter");
|
||||
const details = page.getByRole("dialog", { name: "archive.zip", exact: true });
|
||||
await expect(details.getByText("archive.zip", { exact: true })).toBeVisible();
|
||||
await page.keyboard.press("Escape");
|
||||
await expect(details).toHaveCount(0);
|
||||
await expect(fileInfo).toBeFocused();
|
||||
await page.keyboard.press("Tab");
|
||||
const downloadLink = region.getByRole("link", { name: "Download archive.zip", exact: true });
|
||||
await expect(downloadLink).toBeFocused();
|
||||
expect(await downloadLink.evaluate((element) => element.matches(":focus-visible"))).toBe(true);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { expect, test, type Page } from "@playwright/test";
|
||||
import { expect, test, type Locator, type Page } from "@playwright/test";
|
||||
|
||||
async function imageFile(page: Page, name: string, background: string) {
|
||||
const data = await page.evaluate((fill) => {
|
||||
@@ -17,6 +17,26 @@ async function imageFile(page: Page, name: string, background: string) {
|
||||
return { name, mimeType: "image/png", buffer: Buffer.from(data, "base64") };
|
||||
}
|
||||
|
||||
async function expectAttachmentDetails(page: Page, chip: Locator, name: string, touch = false) {
|
||||
const info = chip.getByRole("button", { name, exact: true });
|
||||
const size = await info.getAttribute("aria-description");
|
||||
expect(size).toMatch(/^\d+(?:\.\d+)? (?:B|KiB|MiB)$/);
|
||||
if (touch) await info.tap();
|
||||
else {
|
||||
await info.hover();
|
||||
await expect(page.getByRole("tooltip")).toContainText(name);
|
||||
await expect(page.getByRole("tooltip")).toContainText(size!);
|
||||
await info.click();
|
||||
}
|
||||
const details = page.getByRole("dialog", { name, exact: true });
|
||||
await expect(details.getByText(name, { exact: true })).toBeVisible();
|
||||
await expect(details.getByText(size!, { exact: true })).toBeVisible();
|
||||
await page.keyboard.press("Escape");
|
||||
await expect(details).toHaveCount(0);
|
||||
await expect(info).toBeFocused();
|
||||
await page.mouse.move(0, 0);
|
||||
}
|
||||
|
||||
test.beforeEach(async ({ page }) => {
|
||||
await page.addInitScript(() => localStorage.setItem("nanobot.locale", "en"));
|
||||
await page.route("**/api/**", route => route.fulfill({ json: {} }));
|
||||
@@ -46,9 +66,8 @@ for (const { width, dark, variant } of [
|
||||
for (const name of ["white.png", "dark.png", "notes.png"]) {
|
||||
const image = page.getByRole("img", { name, exact: true });
|
||||
await expect(image).toBeVisible();
|
||||
expect(await image.evaluate(node => (node as HTMLImageElement).naturalWidth)).toBe(480);
|
||||
expect(await image.evaluate(node => ({ width: (node as HTMLImageElement).naturalWidth, height: (node as HTMLImageElement).naturalHeight }))).toEqual({ width: 480, height: 320 });
|
||||
const chip = chips.filter({ has: image });
|
||||
await expect(chip).toHaveAttribute("title", new RegExp(`^${name}\\n`));
|
||||
const box = (await chip.boundingBox())!;
|
||||
expect(box.width).toBe(96);
|
||||
expect(box.height).toBe(96);
|
||||
@@ -61,28 +80,33 @@ for (const { width, dark, variant } of [
|
||||
expect(target.height).toBeGreaterThanOrEqual(44);
|
||||
expect(target.x + target.width).toBeCloseTo(box.x + box.width - 1);
|
||||
expect(target.y).toBeCloseTo(box.y + 1);
|
||||
await expectAttachmentDetails(page, chip, name, width < 640);
|
||||
}
|
||||
const document = chips.filter({ hasText: "report.pdf" });
|
||||
await expect(document).toContainText("B");
|
||||
expect((await document.boundingBox())!.height).toBeLessThan(96);
|
||||
const document = chips.filter({ has: page.getByRole("button", { name: "report.pdf", exact: true }) });
|
||||
await expect(document).toHaveText("PDF");
|
||||
await expect(document.getByRole("button", { name: "report.pdf", exact: true })).toHaveAccessibleDescription(`${files[3].buffer.byteLength} B`);
|
||||
const documentBox = (await document.boundingBox())!;
|
||||
expect(documentBox.width).toBe(96);
|
||||
expect(documentBox.height).toBe(96);
|
||||
await expectAttachmentDetails(page, document, "report.pdf", width < 640);
|
||||
expect(await page.evaluate(() => window.document.documentElement.scrollWidth)).toBe(width);
|
||||
await page.getByRole("textbox").fill("Compare these images with the report.");
|
||||
const screenshot = testInfo.outputPath("composer-attachments.png");
|
||||
await page.locator("main").screenshot({ path: screenshot });
|
||||
await testInfo.attach("composer-attachments", { path: screenshot, contentType: "image/png" });
|
||||
|
||||
const whiteRemove = chips.filter({ has: page.getByRole("img", { name: "white.png", exact: true }) }).getByRole("button");
|
||||
const whiteRemove = chips.filter({ has: page.getByRole("img", { name: "white.png", exact: true }) }).getByRole("button", { name: "Remove attachment", exact: true });
|
||||
// Tap outside the visible circle, but inside the larger touch target.
|
||||
if (width < 640) await whiteRemove.tap({ position: { x: 6, y: 22 } });
|
||||
else await whiteRemove.click();
|
||||
await expect(chips).toHaveCount(3);
|
||||
const darkRemove = chips.filter({ has: page.getByRole("img", { name: "dark.png", exact: true }) }).getByRole("button");
|
||||
const darkRemove = chips.filter({ has: page.getByRole("img", { name: "dark.png", exact: true }) }).getByRole("button", { name: "Remove attachment", exact: true });
|
||||
await darkRemove.focus();
|
||||
await expect(darkRemove).toBeFocused();
|
||||
await page.keyboard.press("Delete");
|
||||
await expect(chips).toHaveCount(2);
|
||||
await chips.filter({ has: page.getByRole("img", { name: "notes.png", exact: true }) }).getByRole("button").click();
|
||||
await document.getByRole("button").click();
|
||||
await chips.filter({ has: page.getByRole("img", { name: "notes.png", exact: true }) }).getByRole("button", { name: "Remove attachment", exact: true }).click();
|
||||
await document.getByRole("button", { name: "Remove attachment", exact: true }).click();
|
||||
await expect(page.getByRole("textbox")).toBeFocused();
|
||||
await expect(page.getByRole("textbox")).toHaveValue("Compare these images with the report.");
|
||||
});
|
||||
@@ -91,13 +115,25 @@ for (const { width, dark, variant } of [
|
||||
|
||||
test("shows a readable error card and allows removal of a corrupt image", async ({ page }) => {
|
||||
await page.goto("/browser/fixture.html?case=composer-action");
|
||||
const input = page.getByRole("textbox");
|
||||
const draft = "Keep this draft while checking the attachment.";
|
||||
await input.fill(draft);
|
||||
const broken = Buffer.from("not a PNG image");
|
||||
await page.locator('input[type="file"]').setInputFiles({ name: "broken.png", mimeType: "image/png", buffer: broken });
|
||||
const chip = page.getByTestId("composer-chip");
|
||||
await expect(chip).toContainText("broken.png");
|
||||
await expect(chip).toContainText("File doesn't look like a real image");
|
||||
await expect(chip.getByRole("button", { name: "broken.png", exact: true })).toHaveText("PNG");
|
||||
await expect(chip.getByText("File doesn't look like a real image", { exact: true })).toBeVisible();
|
||||
await expectAttachmentDetails(page, chip, "broken.png");
|
||||
await expect(page.getByRole("button", { name: "Send message", exact: true })).toBeDisabled();
|
||||
await chip.getByRole("button", { name: "Remove attachment" }).click();
|
||||
await input.press("Enter");
|
||||
await expect(page.getByRole("status", { name: "Execution status", exact: true })).toHaveText("Idle");
|
||||
await expect(input).toHaveValue(draft);
|
||||
await expect(chip).toHaveCount(1);
|
||||
await chip.getByRole("button", { name: "Remove attachment", exact: true }).click();
|
||||
await expect(chip).toHaveCount(0);
|
||||
await expect(input).toBeFocused();
|
||||
await expect(input).toHaveValue(draft);
|
||||
await expect(page.getByRole("button", { name: "Send message", exact: true })).toBeEnabled();
|
||||
await input.fill("");
|
||||
await expect(page.getByRole("button", { name: "Voice input", exact: true })).toBeEnabled();
|
||||
await expect(page.getByRole("textbox")).toBeFocused();
|
||||
});
|
||||
Reference in new issue
Block a user