diff --git a/scripts/check-browser-manifest.mjs b/scripts/check-browser-manifest.mjs index 90c714160..db982cbd9 100644 --- a/scripts/check-browser-manifest.mjs +++ b/scripts/check-browser-manifest.mjs @@ -48,7 +48,7 @@ for (const entry of entries) { const KNOWN_LANES = new Set(["gate", "extended", "extendedFixture", "production", "perf", "manual"]); // Static harness pages do not open the workspace, so they do not see the welcome dialog. -const welcomeExempt = new Set(["assistant_activity.mjs"]); +const welcomeExempt = new Set(["assistant_activity.mjs", "assistant_reference_overflow.mjs"]); for (const entry of entries) { const text = entry.file === undefined ? "" : fs.readFileSync(path.join(browserDir, entry.file), "utf8"); const importsSrc = text.includes('"/src/'); diff --git a/scripts/run-browser-suite.mjs b/scripts/run-browser-suite.mjs index d129f2345..4f5a053a3 100644 --- a/scripts/run-browser-suite.mjs +++ b/scripts/run-browser-suite.mjs @@ -17,6 +17,7 @@ const workflows = [ { name: "locale", script: "tests/browser/locale.mjs" }, { name: "creation closure", script: "scripts/verify_creation_browser.mjs" }, { name: "assistant activity", script: "tests/browser/assistant_activity.mjs" }, + { name: "assistant reference overflow", script: "tests/browser/assistant_reference_overflow.mjs" }, { name: "generation reference", script: "tests/browser/canvas_generation_reference.mjs", diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index 1da3e8e12..2dc923102 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -14,6 +14,8 @@ import { AgentPicker } from "./AgentPicker"; import { ModelPicker } from "./AssistantPickers"; import { SkillPicker } from "./SkillPicker"; import { CanvasIcon } from "./CanvasIcon"; +import { assetDisplayName } from "./assetNames.ts"; +import { ChatReferenceChip } from "./ChatReferenceChip"; import { currentReferences, fingerprintMatches, @@ -42,15 +44,6 @@ import { useReferenceUploads } from "./useReferenceUploads.tsx"; import { useT } from "../i18n/I18nProvider.tsx"; import type { MessageKey } from "../i18n/types.ts"; -function kindOf(asset: ChatAsset) { - return asset.type.startsWith("image/") - ? "image" - : asset.type.startsWith("video/") - ? "video" - : asset.type.startsWith("audio/") - ? "audio" - : "text"; -} /** OpenDirector conversation tools in an OpenDirector floating canvas window. */ export function AssistantPanel({ @@ -449,25 +442,18 @@ export function AssistantPanel({ ) : part.type === "skill" ? ( /{part.name} ) : ( - - {assetPreviews[part.assetId] ? ( - - ) : ( - asset.id === part.assetId, - ) ?? ({ type: "text/plain" } as ChatAsset), - )} - size={18} - /> + name={part.name} + assetId={part.assetId} + asset={message.assets?.find( + (asset) => asset.id === part.assetId, )} - {part.name} - + previewSrc={assetPreviews[part.assetId]} + ariaLabel={t("assistant.reference.chip", { + name: assetDisplayName(part.name), + })} + /> ), ) ) : ( diff --git a/src/canvas/ChatReferenceChip.tsx b/src/canvas/ChatReferenceChip.tsx new file mode 100644 index 000000000..db4d58faf --- /dev/null +++ b/src/canvas/ChatReferenceChip.tsx @@ -0,0 +1,50 @@ +import type { ChatAsset } from "../backend/chat"; +import { assetDisplayName } from "./assetNames.ts"; +import { CanvasIcon } from "./CanvasIcon"; + +function kindOf(asset: ChatAsset) { + return asset.type.startsWith("image/") + ? "image" + : asset.type.startsWith("video/") + ? "video" + : asset.type.startsWith("audio/") + ? "audio" + : "text"; +} + +/** Reference chip in a sent user bubble. Matches the composer chip's truncation. */ +export function ChatReferenceChip({ + name, + assetId, + asset, + previewSrc, + ariaLabel, +}: { + name: string; + assetId: string; + asset?: ChatAsset; + previewSrc?: string; + ariaLabel?: string; +}) { + const displayName = assetDisplayName(name); + return ( + + {previewSrc ? ( + + ) : ( + + )} + {displayName ? ( + {displayName} + ) : null} + + ); +} diff --git a/src/canvas/reference-mentions.css b/src/canvas/reference-mentions.css index 11dc2ee4a..fc884e292 100644 --- a/src/canvas/reference-mentions.css +++ b/src/canvas/reference-mentions.css @@ -170,6 +170,7 @@ height: 1.5em; margin: 0 0.15em; padding: 0 0.2em; + overflow: hidden; border: 1px solid var(--chat-border, #ffffff20); border-radius: 4px; background: var(--chat-surface, #303034); diff --git a/tests/browser/assistant_reference_overflow.html b/tests/browser/assistant_reference_overflow.html new file mode 100644 index 000000000..165731d5c --- /dev/null +++ b/tests/browser/assistant_reference_overflow.html @@ -0,0 +1,14 @@ + + + + + Assistant reference overflow test + + +
+ + + diff --git a/tests/browser/assistant_reference_overflow.mjs b/tests/browser/assistant_reference_overflow.mjs new file mode 100644 index 000000000..47fa2dccf --- /dev/null +++ b/tests/browser/assistant_reference_overflow.mjs @@ -0,0 +1,273 @@ +/** Sent-bubble reference chips must truncate long names without covering adjacent text. + * Mounts /tests/browser/assistant_reference_overflow_fixture.tsx through Vite. + * No project, provider, or model calls. + */ +import assert from "node:assert/strict"; +import { launchBrowser } from "./engine.mjs"; + +const LONG_ZH = + "角色三视图设定稿:在同一张画面中并排绘制同一人物的正面、侧面(90度侧脸)和背面三个视角,三个人物高度尽量一致、脚部处于同一水平线,穿着与发型在三视图中完全一致.png"; +const LONG_EN = + "Character three-view turnaround sheet: front, 90-degree side and back of the same person on one page, aligned feet and identical costume.png"; +const SHORT_ZH = "导演台 1 · 动作预演"; + +const base = process.env.CREATION_WEB_URL ?? "http://127.0.0.1:5179"; +const browser = await launchBrowser(); +const page = await browser.newPage({ + // Chromium language only. The fixture switches zh-CN / en-US itself via + // setActiveLocale; this script does not assert Playwright-selected UI copy. + locale: "en-US", + viewport: { width: 1100, height: 900 }, +}); +page.setDefaultTimeout(10000); +const errors = []; +page.on("pageerror", (error) => errors.push(error.message)); + +function measure(chip, followingText) { + return chip.evaluate((element, expectedFollowing) => { + const chipRect = element.getBoundingClientRect(); + const label = + element.querySelector(".cw-chat-reference-label") ?? + [...element.querySelectorAll("span")].find( + (node) => node.textContent && !node.querySelector("svg"), + ) ?? + null; + const range = document.createRange(); + const textRects = []; + if (label) { + range.selectNodeContents(label); + textRects.push(...range.getClientRects()); + } + const style = label ? getComputedStyle(label) : null; + const chipStyle = getComputedStyle(element); + const clipsHorizontally = + ["hidden", "clip", "scroll", "auto"].includes(chipStyle.overflowX) || + ["hidden", "clip", "scroll", "auto"].includes(style?.overflowX ?? ""); + // nowrap+ellipsis Range rects may extend past the clipped box horizontally; + // that is not visible. The defect is wrapped text leaking below the 1.5em chip. + const overflowRects = textRects.filter((rect) => { + const belowOrAbove = + rect.bottom > chipRect.bottom + 1 || rect.top < chipRect.top - 1; + const sideways = + rect.right > chipRect.right + 1 || rect.left < chipRect.left - 1; + return belowOrAbove || (sideways && !clipsHorizontally); + }); + const article = element.closest("article"); + const toggle = article?.querySelector(".cw-chat-attachments-toggle"); + const toggleRect = toggle?.getBoundingClientRect(); + const overlap = (a, b, slack = 0.5) => { + if (!a || !b) return 0; + const ix = Math.min(a.right, b.right) - Math.max(a.left, b.left); + const iy = Math.min(a.bottom, b.bottom) - Math.max(a.top, b.top); + return ix > slack && iy > slack ? ix * iy : 0; + }; + let followingRects = []; + if (expectedFollowing && element.parentElement) { + const parent = element.parentElement; + const index = [...parent.childNodes].indexOf(element); + for (let i = index + 1; i < parent.childNodes.length; i += 1) { + const node = parent.childNodes[i]; + if ( + node.nodeType === Node.TEXT_NODE && + node.textContent.includes(expectedFollowing) + ) { + range.selectNodeContents(node); + followingRects = [...range.getClientRects()]; + break; + } + } + } + const coverage = (targets) => + overflowRects.reduce( + (sum, rect) => + sum + + targets.reduce((inner, target) => inner + overlap(rect, target), 0), + 0, + ); + return { + chipHeight: chipRect.height, + chipWidth: chipRect.width, + labelText: label?.textContent ?? "", + whiteSpace: style?.whiteSpace ?? "", + textOverflow: style?.textOverflow ?? "", + overflow: style?.overflow ?? "", + labelScrollWidth: label?.scrollWidth ?? 0, + labelClientWidth: label?.clientWidth ?? 0, + title: + element.getAttribute("title") || label?.getAttribute("title") || "", + ariaLabel: element.getAttribute("aria-label") || "", + overflow: overflowRects.length > 0, + overflowRectCount: overflowRects.length, + overlapFollowing: coverage(followingRects), + overlapToggle: toggleRect ? coverage([toggleRect]) : 0, + followingTop: followingRects[0]?.top ?? null, + toggleTop: toggleRect?.top ?? null, + chipBottom: chipRect.bottom, + }; + }, followingText); +} + +try { + await page.goto(base + "/tests/browser/assistant_reference_overflow.html"); + await page.evaluate(async () => { + const { mountReferenceOverflow } = + await import("/tests/browser/assistant_reference_overflow_fixture.tsx"); + window.referenceOverflowFixture = mountReferenceOverflow( + document.getElementById("reference-overflow-test"), + ); + }); + const render = (locale, width, theme) => + page.evaluate( + ({ locale, width, theme }) => + window.referenceOverflowFixture.render(locale, width, theme), + { locale, width, theme }, + ); + + const cases = [ + { locale: "zh-CN", width: 420, theme: "dark" }, + { locale: "zh-CN", width: 280, theme: "dark" }, + { locale: "en-US", width: 420, theme: "light" }, + { locale: "en-US", width: 280, theme: "light" }, + ]; + + for (const view of cases) { + await render(view.locale, view.width, view.theme); + const suffix = `${view.locale} ${view.theme} ${view.width}px`; + + const longZh = page.locator( + 'article[data-case="long-zh"] .cw-chat-reference[data-preview-asset="long-image"]', + ); + const longEn = page.locator( + 'article[data-case="long-en"] .cw-chat-reference[data-preview-asset="long-en"]', + ); + const short = page.locator( + 'article[data-case="short-only"] .cw-chat-reference', + ); + const icon = page.locator( + 'article[data-case="icon-only"] .cw-chat-reference', + ); + const composer = page.locator("[data-composer] .cw-chat-reference"); + + await longZh.waitFor(); + const zh = await measure(longZh, ",场景为 nvdia 大楼"); + assert.equal( + zh.overflow, + false, + `long zh name overflows the chip (${suffix}): ${JSON.stringify(zh)}`, + ); + assert.equal( + zh.overlapFollowing, + 0, + `long zh name covers following text (${suffix}): ${JSON.stringify(zh)}`, + ); + assert.equal( + zh.overlapToggle, + 0, + `long zh name covers the attachment toggle (${suffix}): ${JSON.stringify(zh)}`, + ); + assert.equal( + zh.whiteSpace, + "nowrap", + `long zh name must stay on one line (${suffix})`, + ); + assert.equal( + zh.textOverflow, + "ellipsis", + `long zh name must ellipsize (${suffix})`, + ); + assert.ok( + zh.labelScrollWidth > zh.labelClientWidth + 1, + `long zh name was not truncated (${suffix}): ${JSON.stringify(zh)}`, + ); + assert.ok( + zh.title.includes("角色三视图设定稿") && zh.title.endsWith(".png"), + `missing hover title (${suffix}): ${zh.title}`, + ); + assert.ok( + zh.ariaLabel.includes("角色三视图设定稿"), + `missing accessible name (${suffix}): ${zh.ariaLabel}`, + ); + assert.ok( + zh.chipHeight > 10 && zh.chipHeight < 28, + `chip height should stay about 1.5em (${suffix}): ${zh.chipHeight}`, + ); + + const en = await measure(longEn, " in this shot."); + assert.equal( + en.overflow, + false, + `long en name overflows the chip (${suffix}): ${JSON.stringify(en)}`, + ); + assert.equal( + en.overlapFollowing, + 0, + `long en name covers following text (${suffix})`, + ); + assert.equal( + en.overlapToggle, + 0, + `long en name covers the attachment toggle (${suffix})`, + ); + assert.ok( + en.labelScrollWidth > en.labelClientWidth + 1, + `long en name was not truncated (${suffix})`, + ); + assert.ok( + en.title.includes("Character three-view"), + `missing English hover title (${suffix}): ${en.title}`, + ); + + const shortMeasure = await measure(short, "即可"); + assert.equal( + shortMeasure.labelText, + SHORT_ZH, + `short name changed (${suffix})`, + ); + assert.equal( + shortMeasure.overflow, + false, + `short name overflows (${suffix})`, + ); + assert.ok( + shortMeasure.labelScrollWidth <= shortMeasure.labelClientWidth + 1, + `short name should not ellipsize (${suffix}): ${JSON.stringify(shortMeasure)}`, + ); + + const iconMeasure = await measure(icon, "引用"); + assert.equal( + iconMeasure.overflow, + false, + `icon-only chip overflows (${suffix})`, + ); + assert.equal( + iconMeasure.overlapFollowing, + 0, + `icon-only chip covers following text (${suffix})`, + ); + + const composerMeasure = await measure(composer, "继续"); + assert.equal( + composerMeasure.overflow, + false, + `composer chip overflows (${suffix})`, + ); + assert.equal( + composerMeasure.whiteSpace, + "nowrap", + `composer truncation regressed (${suffix})`, + ); + assert.equal( + composerMeasure.textOverflow, + "ellipsis", + `composer ellipsis regressed (${suffix})`, + ); + assert.ok( + composerMeasure.labelScrollWidth > composerMeasure.labelClientWidth + 1, + `composer long name was not truncated (${suffix})`, + ); + } + + assert.equal(errors.length, 0, errors.join("\n")); +} finally { + await browser.close(); +} diff --git a/tests/browser/assistant_reference_overflow_fixture.tsx b/tests/browser/assistant_reference_overflow_fixture.tsx new file mode 100644 index 000000000..352430403 --- /dev/null +++ b/tests/browser/assistant_reference_overflow_fixture.tsx @@ -0,0 +1,188 @@ +import { createElement, type ReactNode } from "react"; +import { createRoot } from "react-dom/client"; +import type { ChatAsset } from "../../src/backend/chat"; +import { assetDisplayName } from "../../src/canvas/assetNames.ts"; +import { ChatReferenceChip } from "../../src/canvas/ChatReferenceChip"; +import { setActiveLocale, translate } from "../../src/i18n/messages"; +import type { Locale } from "../../src/i18n/types"; +import "../../src/canvas/assistant-panel.css"; +import "../../src/canvas/assistant-experience.css"; +import "../../src/canvas/reference-mentions.css"; + +const LONG_ZH = + "角色三视图设定稿:在同一张画面中并排绘制同一人物的正面、侧面(90度侧脸)和背面三个视角,三个人物高度尽量一致、脚部处于同一水平线,穿着与发型在三视图中完全一致.png"; +const LONG_EN = + "Character three-view turnaround sheet: front, 90-degree side and back of the same person on one page, aligned feet and identical costume.png"; +const SHORT_ZH = "导演台 1 · 动作预演"; + +const imageAsset = (id: string, name: string): ChatAsset => ({ + id, + name, + type: "image/png", + size: 1024, + fingerprint: `fp-${id}`, +}); + +const videoAsset = (id: string, name: string): ChatAsset => ({ + id, + name, + type: "video/mp4", + size: 2048, + fingerprint: `fp-${id}`, +}); + +function bubble(key: string, children: ReactNode, toggleLabel: string) { + return createElement( + "article", + { key, className: "cw-assistant-message is-user", "data-case": key }, + createElement("div", { className: "cw-assistant-message-text" }, children), + createElement( + "div", + { className: "cw-chat-attachments" }, + createElement( + "button", + { + type: "button", + className: "cw-chat-attachments-toggle", + "aria-expanded": "true", + }, + toggleLabel, + ), + ), + ); +} + +function composerChip(name: string) { + return createElement( + "span", + { className: "cw-chat-reference", "data-composer-chip": name }, + createElement("span", { + "data-reference-icon": "1", + "aria-hidden": "true", + }), + createElement( + "span", + { className: "cw-chat-reference-label" }, + assetDisplayName(name), + ), + ); +} + +export function mountReferenceOverflow(element: HTMLElement) { + const root = createRoot(element); + return { + render( + locale: Locale = "zh-CN", + panelWidth = 420, + theme: "dark" | "light" = "dark", + ) { + setActiveLocale(locale); + document.documentElement.dataset.theme = theme; + const chipLabel = (name: string) => + translate(locale, "assistant.reference.chip", { + name: assetDisplayName(name), + }); + const toggle = translate(locale, "assistant.media.collapse"); + const longImage = imageAsset("long-image", LONG_ZH); + const shortVideo = videoAsset("short-video", SHORT_ZH); + const longEnglish = imageAsset("long-en", LONG_EN); + root.render( + createElement( + "aside", + { + className: "cw-assistant-panel", + style: { + position: "relative", + width: panelWidth, + height: "auto", + inset: "auto", + }, + }, + bubble( + "long-zh", + [ + "参考视频", + createElement(ChatReferenceChip, { + key: "short", + name: shortVideo.name, + assetId: shortVideo.id, + asset: shortVideo, + ariaLabel: chipLabel(shortVideo.name), + }), + "替换成", + createElement(ChatReferenceChip, { + key: "long", + name: longImage.name, + assetId: longImage.id, + asset: longImage, + ariaLabel: chipLabel(longImage.name), + }), + ",场景为 nvdia 大楼", + ], + toggle, + ), + bubble( + "long-en", + [ + "Replace ", + createElement(ChatReferenceChip, { + key: "en", + name: longEnglish.name, + assetId: longEnglish.id, + asset: longEnglish, + ariaLabel: chipLabel(longEnglish.name), + }), + " in this shot.", + ], + toggle, + ), + bubble( + "short-only", + [ + "只用", + createElement(ChatReferenceChip, { + key: "short-only", + name: shortVideo.name, + assetId: shortVideo.id, + asset: shortVideo, + ariaLabel: chipLabel(shortVideo.name), + }), + "即可", + ], + toggle, + ), + bubble( + "icon-only", + [ + "图标", + createElement(ChatReferenceChip, { + key: "icon", + name: "", + assetId: "icon-only", + asset: imageAsset("icon-only", ""), + ariaLabel: chipLabel(""), + }), + "引用", + ], + toggle, + ), + createElement( + "div", + { + className: "cw-chat-reference-editor", + "data-composer": "true", + contentEditable: true, + suppressContentEditableWarning: true, + }, + "草稿", + composerChip(LONG_ZH), + "继续", + ), + ), + ); + }, + unmount() { + root.unmount(); + }, + }; +} diff --git a/tests/browser/manifest.json b/tests/browser/manifest.json index ea78287e6..e34e04f33 100644 --- a/tests/browser/manifest.json +++ b/tests/browser/manifest.json @@ -28,6 +28,11 @@ ], "note": "Mounts /tests/browser/assistant_activity_fixture.tsx through Vite. vite preview cannot serve that module, so this script is not in the production lane." }, + { + "file": "assistant_reference_overflow.mjs", + "lanes": ["gate"], + "note": "Mounts /tests/browser/assistant_reference_overflow_fixture.tsx through Vite. vite preview cannot serve that module, so this script is not in the production lane." + }, { "file": "assistant_auxiliary_panels.mjs", "fixture": true,