Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): isolate composer file input and prepare images in workers (#1160)

Gener9b8310401b26
diff --git a/src/web/src/app/sw.ts b/src/web/src/app/sw.ts
index 8f6ec47e1..45c6d14d4 100644
--- a/src/web/src/app/sw.ts
+++ b/src/web/src/app/sw.ts
@@ -4,6 +4,7 @@ import { NavigationRoute, Serwist } from "serwist"
import { isPublicAssetResponse, publicFaviconQueryParameter, restrictPublicPrecacheRoutes } from "../lib/service-worker/public-cache"
import { publicWorkerCacheNames } from "../lib/service-worker/cache-identity"
import { createPublicRuntimeCaching } from "../lib/service-worker/public-cache-runtime"
+import { createPublicWorkerScriptResponsePlugin } from "../lib/service-worker/worker-script-response"
import { clearOutdatedPublicWorkerCaches } from "../lib/service-worker/public-cache-storage"
import { communityShellHash, isCommunityShellAssetRequest, isCommunityShellNavigation, isCommunityShellResponse } from "../lib/service-worker/community-shell"
@@ -38,7 +39,7 @@ const serwist = new Serwist({
cleanURLs: false,
directoryIndex: null,
ignoreURLParametersMatching: [publicFaviconQueryParameter],
- plugins: [{
+ plugins: [createPublicWorkerScriptResponsePlugin(self.location.origin), {
requestWillFetch: async ({ request }) => new Request(request, { credentials: "omit", redirect: "error" }),
cacheWillUpdate: async ({ request, response }) => (
new URL(request.url).pathname === shellUrl
diff --git a/src/web/src/components/community/community-dnd.tsx b/src/web/src/components/community/community-dnd.tsx
index 0bab57fa2..afda0da8b 100644
--- a/src/web/src/components/community/community-dnd.tsx
+++ b/src/web/src/components/community/community-dnd.tsx
@@ -3,7 +3,7 @@
import { useContext, useEffect, useMemo, type ReactNode, type RefObject } from "react"
import { DndContext, DndProvider, useDragDropManager } from "react-dnd"
import { createVerticalStrength, useDndScrolling } from "react-dnd-scrolling"
-import { HTML5Backend } from "react-dnd-html5-backend"
+import { CommunityHTML5Backend } from "./community-html5-backend"
import { TouchBackend } from "react-dnd-touch-backend"
import { MouseTransition, MultiBackend, TouchTransition } from "dnd-multi-backend"
@@ -11,7 +11,7 @@ export const communityTouchHoldDelay = 500
export const communityDndOptions = {
backends: [
- { id: "html5", backend: HTML5Backend, transition: MouseTransition },
+ { id: "html5", backend: CommunityHTML5Backend, transition: MouseTransition },
{
id: "touch", backend: TouchBackend, transition: TouchTransition,
options: { delayTouchStart: communityTouchHoldDelay, touchSlop: 8, enableMouseEvents: false, enableKeyboardEvents: true, ignoreContextMenu: true },
diff --git a/src/web/src/components/community/community-html5-backend.dom.test.tsx b/src/web/src/components/community/community-html5-backend.dom.test.tsx
new file mode 100644
index 000000000..eb99bb29c
--- /dev/null
+++ b/src/web/src/components/community/community-html5-backend.dom.test.tsx
@@ -0,0 +1,68 @@
+import { useDrag, useDragDropManager, useDrop } from "react-dnd"
+import { describe, expect, it, vi } from "vitest"
+import { act, fireEvent, render } from "@/test/react-dom-harness"
+import { CommunityDndProvider } from "./community-dnd"
+import { useDndContextMenu } from "./use-dnd-context-menu"
+
+describe("Community HTML5 file isolation", () => {
+ it("leaves external files to Compose, prevents outside navigation, and keeps the next real backend sort working", async () => {
+ let manager!: ReturnType<typeof useDragDropManager>
+ let menu!: ReturnType<typeof useDndContextMenu>
+ const sorted = vi.fn(), attached = vi.fn(), closeMenu = vi.fn(), unmountMenu = vi.fn()
+ function Sort() {
+ manager = useDragDropManager()
+ menu = useDndContextMenu({ canOpen: true, canDrag: () => true, suppressOpen: () => false })
+ useLayoutEffect(() => { menu.actionsRef.current = { close: closeMenu, unmount: unmountMenu } })
+ const [, drag] = useDrag(() => ({ type: "community/test-sort", item: { id: "source" } }), [])
+ const [, drop] = useDrop(() => ({ accept: "community/test-sort", drop: sorted }), [])
+ return <><div ref={node => { drag(node) }} data-testid="source">source</div><div ref={node => { drop(node) }} data-testid="target">target</div></>
+ }
+ const view = render(<>
+ <CommunityDndProvider><Sort /></CommunityDndProvider>
+ <div data-testid="compose" onDragOver={event => { event.preventDefault(); event.stopPropagation(); event.dataTransfer.dropEffect = "copy" }} onDrop={event => { event.preventDefault(); event.stopPropagation(); attached(...event.dataTransfer.files) }}>compose</div>
+ </>)
+ const files = [new File(["document"], "notes.txt", { type: "text/plain" })]
+ const transfer = { types: ["Files"], files, items: [], dropEffect: "none", getData: () => "", setData: vi.fn(), setDragImage: vi.fn() }
+ for (const name of ["source", "target", "compose"]) {
+ fireEvent.dragEnter(view.getByTestId(name), { dataTransfer: transfer })
+ fireEvent.dragOver(view.getByTestId(name), { dataTransfer: transfer })
+ expect(manager.getMonitor().isDragging()).toBe(false)
+ const contextEvent = { preventDefault: vi.fn(), preventBaseUIHandler: vi.fn() }
+ menu.triggerProps.onContextMenu?.(contextEvent as never)
+ expect(contextEvent.preventDefault).not.toHaveBeenCalled()
+ expect(contextEvent.preventBaseUIHandler).not.toHaveBeenCalled()
+ expect(transfer.dropEffect).toBe(name === "compose" ? "copy" : "none")
+ fireEvent.dragLeave(view.getByTestId(name), { dataTransfer: transfer })
+ }
+ expect(fireEvent.drop(view.getByTestId("target"), { dataTransfer: transfer })).toBe(false)
+ expect(fireEvent.drop(document, { dataTransfer: transfer })).toBe(false)
+ expect(attached).not.toHaveBeenCalled()
+ fireEvent.dragEnter(view.getByTestId("compose"), { dataTransfer: transfer })
+ fireEvent.drop(view.getByTestId("compose"), { dataTransfer: transfer })
+ expect(attached).toHaveBeenCalledExactlyOnceWith(files[0])
+ expect(manager.getMonitor().isDragging()).toBe(false)
+ expect(sorted).not.toHaveBeenCalled()
+ expect(closeMenu).not.toHaveBeenCalled()
+ expect(unmountMenu).not.toHaveBeenCalled()
+
+ const internal = { ...transfer, types: ["text/plain"], files: [] }
+ fireEvent.dragStart(view.getByTestId("source"), { dataTransfer: internal })
+ expect(manager.getMonitor().isDragging()).toBe(true)
+ expect(closeMenu).toHaveBeenCalledOnce()
+ expect(unmountMenu).toHaveBeenCalledOnce()
+ fireEvent.dragEnter(view.getByTestId("target"), { dataTransfer: internal })
+ fireEvent.dragOver(view.getByTestId("target"), { dataTransfer: internal })
+ await act(async () => { await new Promise(resolve => requestAnimationFrame(resolve)) })
+ fireEvent.drop(view.getByTestId("target"), { dataTransfer: internal })
+ expect(sorted).toHaveBeenCalledOnce()
+ fireEvent.dragEnd(view.getByTestId("source"), { dataTransfer: internal })
+ expect(manager.getMonitor().isDragging()).toBe(false)
+ view.unmount()
+
+ const reopened = render(<CommunityDndProvider><Sort /></CommunityDndProvider>)
+ fireEvent.dragEnter(reopened.getByTestId("target"), { dataTransfer: transfer })
+ expect(manager.getMonitor().isDragging()).toBe(false)
+ reopened.unmount()
+ })
+})
+import { useLayoutEffect } from "react"
diff --git a/src/web/src/components/community/community-html5-backend.ts b/src/web/src/components/community/community-html5-backend.ts
new file mode 100644
index 000000000..c87cf5c21
--- /dev/null
+++ b/src/web/src/components/community/community-html5-backend.ts
@@ -0,0 +1,41 @@
+import { HTML5Backend } from "react-dnd-html5-backend"
+
+function sortingEventRoot(root: Node): Node {
+ const listeners = new WeakMap<EventListenerOrEventListenerObject, EventListener>()
+ const wrap = (listener: EventListenerOrEventListenerObject) => {
+ let wrapped = listeners.get(listener)
+ if (!wrapped) {
+ wrapped = (event) => {
+ const transfer = (event as DragEvent).dataTransfer
+ if (transfer && Array.from(transfer.types).includes("Files")) {
+ if (event.type === "dragover" || event.type === "drop") {
+ event.preventDefault()
+ transfer.dropEffect = "none"
+ }
+ return
+ }
+ if (typeof listener === "function") listener.call(root, event)
+ else listener.handleEvent(event)
+ }
+ listeners.set(listener, wrapped)
+ }
+ return wrapped
+ }
+ return new Proxy(root, {
+ get(target, property) {
+ if (property === "addEventListener") {
+ return (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => target.addEventListener(type, wrap(listener), options)
+ }
+ if (property === "removeEventListener") {
+ return (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions) => target.removeEventListener(type, listeners.get(listener) ?? listener, options)
+ }
+ const value = Reflect.get(target, property, target)
+ return typeof value === "function" ? value.bind(target) : value
+ },
+ })
+}
+
+export const CommunityHTML5Backend: typeof HTML5Backend = (manager, context, options) => {
+ const root = options?.rootElement ?? context?.document ?? globalThis.document
+ return HTML5Backend(manager, context, root ? { ...options, rootElement: sortingEventRoot(root) } : options)
+}
diff --git a/src/web/src/components/community/messages/composer-file-utils.ts b/src/web/src/components/community/messages/composer-file-utils.ts
index 032ab32c5..30beb22ea 100644
--- a/src/web/src/components/community/messages/composer-file-utils.ts
+++ b/src/web/src/components/community/messages/composer-file-utils.ts
@@ -79,8 +79,9 @@ export function createComposerAttachmentInputHandlers(
onDragLeave: attachments.handleDragLeave,
onDragOver: guardDrag(attachments.handleDragOver),
onDrop: guardDrag((event) => {
+ if (!event.dataTransfer.files.length && !event.dataTransfer.types.includes("Files")) return
attachments.handleDrop(event)
- focusAfterDrop()
+ if (event.dataTransfer.files.length) focusAfterDrop()
}),
onFileSelect: (event) => {
if (!isActive()) {
diff --git a/src/web/src/components/community/messages/composer-paste.dom.test.tsx b/src/web/src/components/community/messages/composer-paste.dom.test.tsx
new file mode 100644
index 000000000..067c8cd1e
--- /dev/null
+++ b/src/web/src/components/community/messages/composer-paste.dom.test.tsx
@@ -0,0 +1,38 @@
+import { EditorContent } from "@tiptap/react"
+import { describe, expect, it, vi } from "vitest"
+import { act, fireEvent, waitFor } from "@/test/react-dom-harness"
+import { renderCommunity } from "@/test/community-owner-harness"
+import { useComposerController } from "./use-composer-controller"
+
+describe("Compose native editor paste admission", () => {
+ it("consumes file clipboard data before generic parsing and still parses ordinary HTML", async () => {
+ let current!: ReturnType<typeof useComposerController>
+ function Capture() {
+ current = useComposerController({ channel: "general", context: "channel", members: [], hideEmoji: true, sendContract: "accepted", onAcceptSend: () => false }, null)
+ return <EditorContent editor={current.editor} />
+ }
+ const view = renderCommunity(<Capture />)
+ await waitFor(() => expect(current.editor?.view.dom).toBeDefined())
+ const editor = current.editor!
+ const parseHtml = vi.fn((html: string) => html)
+ editor.setOptions({ editorProps: { ...editor.options.editorProps, transformPastedHTML: parseHtml } })
+ const file = new File(["document"], "clipboard.txt", { type: "text/plain" })
+ const readClipboard = vi.fn(() => { throw new Error("File paste must not read/parse text or HTML") })
+ await act(async () => {
+ fireEvent.paste(editor.view.dom, { clipboardData: { items: [{ kind: "file", getAsFile: () => file }], getData: readClipboard } })
+ })
+ await waitFor(() => expect(current.pendingFiles.map(item => item.file)).toEqual([file]))
+ expect(readClipboard).not.toHaveBeenCalled()
+ expect(parseHtml).not.toHaveBeenCalled()
+ expect(editor.isEmpty).toBe(true)
+
+ editor.setOptions({ editorProps: { ...editor.options.editorProps, transformPastedHTML: parseHtml } })
+ await act(async () => {
+ fireEvent.paste(editor.view.dom, { clipboardData: { items: [], getData: (type: string) => type === "text/html" ? "<p><strong>Ordinary HTML</strong></p>" : "Ordinary HTML" } })
+ })
+ expect(parseHtml).toHaveBeenCalledOnce()
+ expect(editor.getText()).toBe("Ordinary HTML")
+ expect(editor.getHTML()).toBe("<p>Ordinary HTML</p>")
+ view.unmount()
+ })
+})
diff --git a/src/web/src/components/community/messages/composer-view.dom.test.ts b/src/web/src/components/community/messages/composer-view.dom.test.ts
index aeb7777b3..45f6bdb0c 100644
--- a/src/web/src/components/community/messages/composer-view.dom.test.ts
+++ b/src/web/src/components/community/messages/composer-view.dom.test.ts
@@ -86,6 +86,21 @@ function baseProps(
}
describe("ComposerView", () => {
+ it("shows preparation feedback without replacing the editor and keeps removal available", () => {
+ const file = new File(["image"], "preparing.png", { type: "image/png" })
+ const removePendingFile = vi.fn()
+ const pending = { draftId: "draft", file, preparing: true, thumbnailUrl: null, thumbnailBlob: null }
+ const view = render(createElement(ComposerView, baseProps({ pendingFiles: [pending], removePendingFile })))
+ const editor = view.getByTestId(tid.composerInput)
+ expect(view.getByRole("status").textContent).toBe("Preparing…")
+ expect(view.getByText(file.name).parentElement).toHaveAttribute("aria-busy", "true")
+ fireEvent.click(view.getByRole("button", { name: "Remove file" }))
+ expect(removePendingFile).toHaveBeenCalledExactlyOnceWith(0)
+ view.rerender(createElement(ComposerView, baseProps({ pendingFiles: [{ ...pending, preparing: false }] })))
+ expect(view.queryByRole("status")).toBeNull()
+ expect(view.getByTestId(tid.composerInput)).toBe(editor)
+ })
+
it.each([false, true])("keeps the composer free of native keyboard controls and reservations (forum=%s)", async (isForumThreadBody) => {
const descriptor = Object.getOwnPropertyDescriptor(window, "webkit")
const tauriDescriptor = Object.getOwnPropertyDescriptor(window, "__TAURI__")
diff --git a/src/web/src/components/community/messages/composer-view.tsx b/src/web/src/components/community/messages/composer-view.tsx
index 2d808e4bc..264a27ac3 100644
--- a/src/web/src/components/community/messages/composer-view.tsx
+++ b/src/web/src/components/community/messages/composer-view.tsx
@@ -156,7 +156,8 @@ export function ComposerView({
const isImage = pendingFile.file.type.startsWith("image/")
return (
<div
- key={index}
+ key={pendingFile.draftId ?? index}
+ aria-busy={pendingFile.preparing || undefined}
className="group relative flex items-center gap-2 rounded-md border border-border bg-background px-3 py-2 text-xs"
>
{isImage ? (
@@ -167,6 +168,7 @@ export function ComposerView({
<span className="max-w-30 truncate text-foreground">
{pendingFile.file.name}
</span>
+ {pendingFile.preparing && <span role="status" className="text-muted-foreground">Preparing…</span>}
<button
onClick={() => removePendingFile(index)}
className="grid size-4 shrink-0 place-items-center rounded-full hover:bg-destructive/10 hover:text-destructive"
diff --git a/src/web/src/components/community/messages/use-composer-controller.dom.test.ts b/src/web/src/components/community/messages/use-composer-controller.dom.test.ts
index 7dc12f17d..3caa39beb 100644
--- a/src/web/src/components/community/messages/use-composer-controller.dom.test.ts
+++ b/src/web/src/components/community/messages/use-composer-controller.dom.test.ts
@@ -112,6 +112,7 @@ type EditorOptions = {
editorProps: {
attributes: Record<string, string>
handleKeyDown: (_view: unknown, event: KeyboardEvent) => boolean
+ handleDOMEvents: { paste: (_view: unknown, event: ClipboardEvent) => boolean }
handlePaste: (_view: unknown, event: ClipboardEvent, slice?: unknown) => boolean
clipboardTextParser: (text: string, context: never) => unknown
}
@@ -467,7 +468,7 @@ describe("useComposerController", () => {
} as unknown as DataTransferItemList
const preventDefault = vi.fn()
expect(
- editorOptions.editorProps.handlePaste({} as never, {
+ editorOptions.editorProps.handleDOMEvents.paste({} as never, {
clipboardData: { items },
preventDefault,
} as unknown as ClipboardEvent),
@@ -494,7 +495,7 @@ describe("useComposerController", () => {
const preventLongPaste = vi.fn()
expect(
- editorOptions.editorProps.handlePaste({} as never, {
+ editorOptions.editorProps.handleDOMEvents.paste({} as never, {
clipboardData: {
items: { length: 0 },
getData: () => "x".repeat(1_001),
@@ -509,7 +510,7 @@ describe("useComposerController", () => {
expect(await firstLongPasteFile.text()).toBe("x".repeat(1_001))
expect(
- editorOptions.editorProps.handlePaste({} as never, {
+ editorOptions.editorProps.handleDOMEvents.paste({} as never, {
clipboardData: {
items: { length: 0 },
getData: () => "y".repeat(1_001),
@@ -552,7 +553,7 @@ describe("useComposerController", () => {
expect(transferPendingFiles).toHaveBeenCalledTimes(transfersBeforeReset)
expect(
- editorOptions.editorProps.handlePaste({} as never, {
+ editorOptions.editorProps.handleDOMEvents.paste({} as never, {
clipboardData: {
items: { length: 0 },
getData: () => "z".repeat(1_001),
@@ -595,7 +596,7 @@ describe("useComposerController", () => {
renderer.rerender(createElement(Harness, props))
expect(
- editorOptions.editorProps.handlePaste({} as never, {
+ editorOptions.editorProps.handleDOMEvents.paste({} as never, {
clipboardData: {
items: { length: 0 },
getData: () => "x".repeat(1_001),
@@ -1096,7 +1097,7 @@ describe("useComposerController", () => {
const view = controllerView
focus.mockClear()
- await act(async () => view.onDrop({ type: "drop" }))
+ await act(async () => view.onDrop({ type: "drop", dataTransfer: { files: [new File(["x"], "drop.txt")] } } as unknown as Parameters<typeof view.onDrop>[0]))
expect(handleDropRaw).toHaveBeenCalledOnce()
expect(focus).toHaveBeenCalledWith()
expect(handleDropRaw.mock.invocationCallOrder[0]).toBeLessThan(
@@ -1383,7 +1384,7 @@ describe("useComposerController", () => {
if (kind === "upload") retained.onUploadFile()
if (kind === "file") retained.onFileSelect(event as unknown as Parameters<typeof retained.onFileSelect>[0])
if (kind === "drop") retained.onDrop(event as unknown as Parameters<typeof retained.onDrop>[0])
- if (kind === "paste") editorOptions.editorProps.handlePaste({}, event as unknown as ClipboardEvent)
+ if (kind === "paste") editorOptions.editorProps.handleDOMEvents.paste({}, event as unknown as ClipboardEvent)
if (kind === "remove") retained.removePendingFile(0)
if (kind === "cancel") retained.onCancelReply?.()
if (kind === "send") retained.onSend()
@@ -1420,7 +1421,7 @@ describe("useComposerController", () => {
for (const active of [true, false, true]) {
view.rerender(node(active))
vi.clearAllMocks()
- const event = { preventDefault: vi.fn(), stopPropagation: vi.fn(), currentTarget: { value: "picked" } }
+ const event = { preventDefault: vi.fn(), stopPropagation: vi.fn(), currentTarget: { value: "picked" }, dataTransfer: { files: [new File(["x"], "drop.txt")], types: ["Files"] } }
await act(async () => {
if (kind === "upload") retained.onUploadFile()
if (kind === "file") retained.onFileSelect(event as unknown as Parameters<typeof retained.onFileSelect>[0])
diff --git a/src/web/src/components/community/messages/use-composer-controller.ts b/src/web/src/components/community/messages/use-composer-controller.ts
index d99986607..152957aae 100644
--- a/src/web/src/components/community/messages/use-composer-controller.ts
+++ b/src/web/src/components/community/messages/use-composer-controller.ts
@@ -161,7 +161,7 @@ export function useComposerController(
splitListItem: () => editorRef.current?.commands.splitListItem("listItem") ?? false,
undoInputRule: () => editorRef.current?.commands.undoInputRule() ?? false,
}),
- handlePaste: (view, event, slice) => {
+ handleDOMEvents: { paste: (_view, event) => {
if (!protocol.get().active) { event.preventDefault(); return true }
const files = clipboardFiles(event.clipboardData?.items)
if (files.length > 0) {
@@ -183,9 +183,13 @@ export function useComposerController(
return true
}
+ return false
+ } },
+ handlePaste: (view, event, slice) => {
+ if (!protocol.get().active) { event.preventDefault(); return true }
return preserveComposerPlainTextPaste(
view,
- clipboardText,
+ event.clipboardData?.getData("text/plain"),
event.clipboardData?.getData("text/html"),
slice,
)
diff --git a/src/web/src/hooks/use-file-attachments.dom.test.ts b/src/web/src/hooks/use-file-attachments.dom.test.ts
index 3754e8984..0250f775b 100644
--- a/src/web/src/hooks/use-file-attachments.dom.test.ts
+++ b/src/web/src/hooks/use-file-attachments.dom.test.ts
@@ -87,6 +87,27 @@ async function dropFiles(
}
describe("useFileAttachments — DOM drop ownership", () => {
+ it("ignores non-file drags and clears nested file feedback after leaving or an empty file drop", async () => {
+ const hook = await renderCapture()
+ const event = { preventDefault: vi.fn(), stopPropagation: vi.fn(), dataTransfer: { types: ["text/plain"], files: [], dropEffect: "none" } }
+ act(() => { hook.current.handleDragEnter(event as never); hook.current.handleDragOver(event as never); hook.current.handleDrop(event as never) })
+ expect(event.preventDefault).not.toHaveBeenCalled()
+ expect(event.stopPropagation).not.toHaveBeenCalled()
+ expect(hook.current.dragging).toBe(false)
+ event.dataTransfer.types = ["Files"]
+ act(() => { hook.current.handleDragEnter(event as never); hook.current.handleDragEnter(event as never); hook.current.handleDragOver(event as never) })
+ expect(hook.current.dragging).toBe(true)
+ expect(event.dataTransfer.dropEffect).toBe("copy")
+ act(() => hook.current.handleDragLeave(event as never))
+ expect(hook.current.dragging).toBe(true)
+ act(() => hook.current.handleDragLeave(event as never))
+ expect(hook.current.dragging).toBe(false)
+ act(() => { hook.current.handleDragEnter(event as never); hook.current.handleDrop(event as never) })
+ expect(hook.current.dragging).toBe(false)
+ expect(hook.current.pendingFiles).toEqual([])
+ expect(generateThumbnailMock).not.toHaveBeenCalled()
+ })
+
it("routes picker selection and DOM drop through the same pending-file lifecycle", async () => {
generateThumbnailMock.mockResolvedValue(null)
const hook = await renderCapture()
@@ -155,6 +176,49 @@ describe("useFileAttachments — DOM drop ownership", () => {
})
describe("useFileAttachments — PendingFile width/height", () => {
+ it("publishes ordered preparation cards immediately and settles each file before the rest of its batch", async () => {
+ let finishFirst!: (value: null) => void
+ let finishSecond!: (value: null) => void
+ prepareCommunityImageMock
+ .mockReturnValueOnce(new Promise(resolve => { finishFirst = resolve }))
+ .mockReturnValueOnce(new Promise(resolve => { finishSecond = resolve }))
+ const hook = await renderCapture({ thumbnailPolicy: "community", maxFiles: 2 })
+ const files = [new File(["one"], "one.png", { type: "image/png" }), new File(["two"], "two.png", { type: "image/png" })]
+ let adding!: Promise<void>
+ act(() => { adding = hook.current.addPendingFiles(files) })
+ expect(hook.current.pendingFiles.map(item => [item.file.name, item.preparing])).toEqual([["one.png", true], ["two.png", true]])
+ await waitFor(() => expect(prepareCommunityImageMock).toHaveBeenCalledOnce())
+ await act(async () => { finishFirst(null) })
+ await waitFor(() => expect(prepareCommunityImageMock).toHaveBeenCalledTimes(2))
+ expect(hook.current.pendingFiles[0].preparing).toBeFalsy()
+ expect(hook.current.pendingFiles[1].preparing).toBe(true)
+ await act(async () => { finishSecond(null); await adding })
+ expect(hook.current.pendingFiles.map(item => item.file)).toEqual(files)
+ })
+
+ it("cancels a removed preparation, releases its draft/capacity, and never republishes its late result", async () => {
+ let finishRemoved!: (value: { blob: Blob; width: number; height: number }) => void
+ prepareCommunityImageMock
+ .mockReturnValueOnce(new Promise(resolve => { finishRemoved = resolve }))
+ .mockResolvedValue(null)
+ const hook = await renderCapture({ thumbnailPolicy: "community", draftSessionScope: "scope", maxFiles: 1 })
+ const removed = new File(["one"], "removed.png", { type: "image/png" })
+ const kept = new File(["two"], "kept.txt", { type: "text/plain" })
+ let adding!: Promise<void>
+ act(() => { adding = hook.current.addPendingFiles([removed]) })
+ await waitFor(() => expect(prepareCommunityImageMock).toHaveBeenCalledOnce())
+ const signal = prepareCommunityImageMock.mock.calls[0][1] as AbortSignal
+ act(() => hook.current.removePendingFile(0))
+ expect(signal.aborted).toBe(true)
+ expect(readComposerAttachmentSession("scope")).toEqual([])
+ await act(async () => { await hook.current.addPendingFiles([kept]); await adding })
+ await act(async () => finishRemoved({ blob: new Blob(["old"]), width: 100, height: 100 }))
+ expect(hook.current.pendingFiles.map(item => item.file)).toEqual([kept])
+ expect(URL.createObjectURL).not.toHaveBeenCalled()
+ expect(toastErrorMock).not.toHaveBeenCalled()
+ expect(readComposerAttachmentSession("scope").map(item => item.file)).toEqual([kept])
+ })
+
it("carries the image's natural width/height from generateThumbnail onto the PendingFile", async () => {
generateThumbnailMock.mockResolvedValue({ blob: { size: 1 } as Blob, width: 1920, height: 1080 })
@@ -294,7 +358,9 @@ describe("useFileAttachments — PendingFile width/height", () => {
expect(readComposerAttachmentSession("server/channel")).toEqual([
{ draftId: expect.stringMatching(/.+/), file },
])
- expect(hook.current.pendingFiles).toEqual([])
+ expect(hook.current.pendingFiles).toEqual([
+ expect.objectContaining({ file, preparing: true }),
+ ])
await act(async () => {
release(null)
diff --git a/src/web/src/hooks/use-file-attachments.ts b/src/web/src/hooks/use-file-attachments.ts
index 536b6e143..021cbc7b4 100644
--- a/src/web/src/hooks/use-file-attachments.ts
+++ b/src/web/src/hooks/use-file-attachments.ts
@@ -19,6 +19,7 @@ export type PendingFile = {
file: File;
thumbnailUrl: string | null;
thumbnailBlob: Blob | null;
+ preparing?: boolean;
width?: number;
height?: number;
};
@@ -45,12 +46,15 @@ function createDraftId() {
return `attachment-draft-${draftIdentity.get()}`;
}
function revokeThumbnailUrls(files: readonly PendingFile[]) { for (const file of files) if (file.thumbnailUrl) URL.revokeObjectURL(file.thumbnailUrl); }
+type PreparationLease = { generation: number; controller: AbortController };
+function cancelPreparations(queued: ReadonlyMap<string, PreparationLease>) { for (const lease of queued.values()) lease.controller.abort(); }
+function pendingDraft(draft: AttachmentDraftFile): PendingFile { return { ...draft, thumbnailUrl: null, thumbnailBlob: null, preparing: true }; }
export function useFileAttachments(opts: UseFileAttachmentsOptions = {}) {
const client = useQueryClient();
const id = useMemo(() => createDraftId(), []);
const key = useMemo(() => ["application", "attachment-preparation", id], [id]);
- const protocol = useCreateStore({ files: [] as PendingFile[], queued: new Map<string, number>() as ReadonlyMap<string, number>, generation: 0, active: true, controller: new AbortController(), dragging: false, dragDepth: 0, options: { maxFileSize: opts.maxFileSize ?? DEFAULT_MAX_FILE_SIZE, maxFiles: opts.maxFiles, policy: opts.thumbnailPolicy ?? "legacy", scope: opts.draftSessionScope } });
+ const protocol = useCreateStore({ files: [] as PendingFile[], queued: new Map<string, PreparationLease>() as ReadonlyMap<string, PreparationLease>, generation: 0, active: true, controller: new AbortController(), dragging: false, dragDepth: 0, options: { maxFileSize: opts.maxFileSize ?? DEFAULT_MAX_FILE_SIZE, maxFiles: opts.maxFiles, policy: opts.thumbnailPolicy ?? "legacy", scope: opts.draftSessionScope } });
const pendingFiles = useSelector(protocol, (state) => state.files);
const dragging = useSelector(protocol, (state) => state.dragging);
const fileInputRef = useRef<HTMLInputElement>(null);
@@ -59,11 +63,11 @@ export function useFileAttachments(opts: UseFileAttachmentsOptions = {}) {
}, [opts.maxFileSize, opts.maxFiles, opts.thumbnailPolicy, opts.draftSessionScope, protocol]);
useLayoutEffect(() => {
protocol.setState((state) => ({ ...state, active: true, controller: state.controller.signal.aborted ? new AbortController() : state.controller, files: state.files.map((file) => file.thumbnailBlob && !file.thumbnailUrl ? { ...file, thumbnailUrl: URL.createObjectURL(file.thumbnailBlob) } : file) }));
- const original = protocol.get().controller;
return () => {
- original.abort();
+ protocol.get().controller.abort();
+ cancelPreparations(protocol.get().queued);
revokeThumbnailUrls(protocol.get().files);
- protocol.setState((state) => ({ ...state, active: false, generation: state.generation + 1, queued: new Map(), files: state.files.map((file) => ({ ...file, thumbnailUrl: null })) }));
+ protocol.setState((state) => ({ ...state, active: false, generation: state.generation + 1, queued: new Map(), files: state.files.filter((file) => !file.preparing).map((file) => ({ ...file, thumbnailUrl: null })), dragging: false, dragDepth: 0 }));
};
}, [protocol]);
type Preparation = { drafts: readonly AttachmentDraftFile[]; generation: number; policy: "legacy" | "community"; scope?: string; signal: AbortSignal };
@@ -71,36 +75,36 @@ export function useFileAttachments(opts: UseFileAttachmentsOptions = {}) {
mutationFn: async ({ drafts, generation, policy, scope, signal }: Preparation) => {
const eligible = () => protocol.get().active && protocol.get().generation === generation && !signal.aborted;
if (!eligible()) return;
- const prepared = await Promise.all(drafts.map(async ({ draftId, file }): Promise<PendingFile | null> => {
+ for (const { draftId, file } of drafts) {
+ const lease = protocol.get().queued.get(draftId);
+ const owns = () => eligible() && protocol.get().queued.get(draftId) === lease && !lease?.controller.signal.aborted;
+ if (!lease || lease.generation !== generation || !owns()) continue;
+ const fileSignal = lease.controller.signal;
+ let prepared: PendingFile | null = null;
try {
const thumbnail = await new Promise<Awaited<ReturnType<typeof prepareCommunityImage>>>((resolve, reject) => {
const decoding = new AbortController();
const abort = () => { decoding.abort(); cleanup(); reject(new DOMException("Retired attachment preparation", "AbortError")); };
const timer = setTimeout(() => { decoding.abort(); cleanup(); reject(new Error("Image preparation timed out")); }, 30_000);
- const cleanup = () => { clearTimeout(timer); signal.removeEventListener("abort", abort); };
- signal.addEventListener("abort", abort, { once: true });
- if (signal.aborted) { abort(); return; }
+ const cleanup = () => { clearTimeout(timer); fileSignal.removeEventListener("abort", abort); };
+ fileSignal.addEventListener("abort", abort, { once: true });
+ if (fileSignal.aborted) { abort(); return; }
void (policy === "community" ? prepareCommunityImage(file, decoding.signal) : generateThumbnail(file, decoding.signal)).then((value) => { cleanup(); resolve(value); }, (error) => { cleanup(); reject(error); });
});
- if (!eligible()) return null;
+ if (!owns()) continue;
const blob = thumbnail?.blob ?? null;
const preview = blob ?? (policy === "community" && thumbnail ? file : null);
- return { draftId, file, thumbnailUrl: preview ? URL.createObjectURL(preview) : null, thumbnailBlob: blob, width: thumbnail?.width, height: thumbnail?.height };
+ prepared = { draftId, file, thumbnailUrl: preview ? URL.createObjectURL(preview) : null, thumbnailBlob: blob, width: thumbnail?.width, height: thumbnail?.height };
} catch {
- if (eligible()) { toast.error(`Could not prepare "${file.name}" for upload`); if (scope) removeComposerAttachmentSessionFiles(scope, [draftId]); }
- return null;
+ if (owns()) { toast.error(`Could not prepare "${file.name}" for upload`); if (scope) removeComposerAttachmentSessionFiles(scope, [draftId]); }
}
- }));
- const pending = prepared.filter((file): file is PendingFile => file !== null);
- if (!eligible()) { revokeThumbnailUrls(pending); return; }
- protocol.setState((state) => {
- const queued = new Map(state.queued);
- for (const { draftId } of drafts) if (queued.get(draftId) === generation) queued.delete(draftId);
- const ids = new Set(state.files.map((file) => file.draftId));
- const unique = pending.filter((file) => !ids.has(file.draftId));
- revokeThumbnailUrls(pending.filter((file) => ids.has(file.draftId)));
- return { ...state, queued, files: [...state.files, ...unique] };
- });
+ if (!owns()) { if (prepared) revokeThumbnailUrls([prepared]); continue; }
+ protocol.setState((state) => {
+ const queued = new Map(state.queued);
+ queued.delete(draftId);
+ return { ...state, queued, files: state.files.flatMap((pending) => pending.draftId !== draftId ? [pending] : prepared ? [prepared] : []) };
+ });
+ }
},
});
const mutatePreparation = preparation.mutateAsync;
@@ -108,12 +112,16 @@ export function useFileAttachments(opts: UseFileAttachmentsOptions = {}) {
const setPendingFiles = useCallback((next: PendingFile[] | ((previous: PendingFile[]) => PendingFile[])) => {
const state = protocol.get();
const files = typeof next === "function" ? next(state.files) : next;
+ const retained = new Set(files.map((file) => file.draftId));
+ const queued = new Map(state.queued);
+ for (const [draftId, lease] of queued) if (!retained.has(draftId)) { lease.controller.abort(); queued.delete(draftId); }
if (!files.length) { state.controller.abort(); if (state.options.scope) clearComposerAttachmentSession(state.options.scope); revokeThumbnailUrls(state.files); }
- protocol.setState((current) => ({ ...current, files, ...(!files.length ? { generation: current.generation + 1, queued: new Map(), controller: new AbortController() } : {}) }));
+ protocol.setState((current) => ({ ...current, files, queued, ...(!files.length ? { generation: current.generation + 1, controller: new AbortController() } : {}) }));
}, [protocol]);
const transferPendingFiles = useCallback(() => {
const state = protocol.get();
state.controller.abort();
+ cancelPreparations(state.queued);
if (state.options.scope) transferComposerAttachmentSession(state.options.scope);
protocol.setState((current) => ({ ...current, files: [], generation: current.generation + 1, queued: new Map(), controller: new AbortController() }));
return state.files;
@@ -127,24 +135,26 @@ export function useFileAttachments(opts: UseFileAttachmentsOptions = {}) {
toast.error(`"${file.name}" exceeds ${Math.floor(maxFileSize / 1024 / 1024)} MB limit`); return false;
});
if (!valid.length) return;
- if (maxFiles !== undefined && state.files.length + state.queued.size + valid.length > maxFiles) { toast.error(`You can attach up to ${maxFiles} files`); return; }
+ if (maxFiles !== undefined && state.files.length + valid.length > maxFiles) { toast.error(`You can attach up to ${maxFiles} files`); return; }
const drafts = valid.map((file) => ({ draftId: createDraftId(), file }));
if (scope) {
const result = appendComposerAttachmentSession(scope, drafts);
if (result.evictedScopes > 0) toast.info("Older attachment drafts were cleared to free memory");
if (!result.accepted) { toast.error("These files exceed the attachment draft memory limit"); return; }
}
- protocol.setState((current) => ({ ...current, queued: new Map([...current.queued, ...drafts.map((draft) => [draft.draftId, state.generation] as const)]) }));
+ protocol.setState((current) => ({ ...current, files: [...current.files, ...drafts.map(pendingDraft)], queued: new Map([...current.queued, ...drafts.map((draft) => [draft.draftId, { generation: state.generation, controller: new AbortController() }] as const)]) }));
await queuePreparation(drafts, state.generation, policy, scope);
}, [protocol, queuePreparation]);
const restorePendingFiles = useCallback(async (drafts: readonly AttachmentDraftFile[]) => {
const previous = protocol.get();
previous.controller.abort();
+ cancelPreparations(previous.queued);
revokeThumbnailUrls(previous.files);
protocol.setState((state) => ({ ...state, files: [], generation: state.generation + 1, queued: new Map(), controller: new AbortController() }));
const state = protocol.get(), { maxFiles, policy, scope } = state.options;
- const accepted = maxFiles === undefined ? drafts : drafts.slice(0, maxFiles);
- protocol.setState((current) => ({ ...current, queued: new Map(accepted.map((draft) => [draft.draftId, state.generation])) }));
+ const unique = [...new Map(drafts.map((draft) => [draft.draftId, draft])).values()];
+ const accepted = maxFiles === undefined ? unique : unique.slice(0, maxFiles);
+ protocol.setState((current) => ({ ...current, files: accepted.map(pendingDraft), queued: new Map(accepted.map((draft) => [draft.draftId, { generation: state.generation, controller: new AbortController() }])) }));
if (accepted.length) await queuePreparation(accepted, state.generation, policy, scope);
}, [protocol, queuePreparation]);
useLayoutEffect(() => {
@@ -172,13 +182,15 @@ export function useFileAttachments(opts: UseFileAttachmentsOptions = {}) {
const handleFileSelect = useCallback((event: React.ChangeEvent<HTMLInputElement>) => { if (event.target.files) void addPendingFiles(Array.from(event.target.files)).catch(() => undefined); event.target.value = ""; }, [addPendingFiles]);
const removePendingFile = useCallback((index: number) => {
const state = protocol.get(), removed = state.files[index];
+ const queued = new Map(state.queued);
+ if (removed?.draftId) { queued.get(removed.draftId)?.controller.abort(); queued.delete(removed.draftId); }
if (removed?.draftId && state.options.scope) removeComposerAttachmentSessionFiles(state.options.scope, [removed.draftId]);
if (removed?.thumbnailUrl) URL.revokeObjectURL(removed.thumbnailUrl);
- protocol.setState((current) => ({ ...current, files: current.files.filter((_, item) => item !== index) }));
+ protocol.setState((current) => ({ ...current, queued, files: current.files.filter((_, item) => item !== index) }));
}, [protocol]);
- const handleDragEnter = useCallback((event: React.DragEvent) => { event.preventDefault(); event.stopPropagation(); protocol.setState((state) => ({ ...state, dragDepth: state.dragDepth + 1, dragging: state.dragging || event.dataTransfer.types.includes("Files") })); }, [protocol]);
- const handleDragLeave = useCallback((event: React.DragEvent) => { event.preventDefault(); event.stopPropagation(); protocol.setState((state) => ({ ...state, dragDepth: Math.max(0, state.dragDepth - 1), dragging: state.dragDepth > 1 && state.dragging })); }, [protocol]);
- const handleDragOver = useCallback((event: React.DragEvent) => { event.preventDefault(); event.stopPropagation(); }, []);
- const handleDrop = useCallback((event: React.DragEvent) => { event.preventDefault(); event.stopPropagation(); protocol.setState((state) => ({ ...state, dragging: false, dragDepth: 0 })); void addPendingFiles(Array.from(event.dataTransfer.files)).catch(() => undefined); }, [addPendingFiles, protocol]);
+ const handleDragEnter = useCallback((event: React.DragEvent) => { if (!event.dataTransfer.types.includes("Files")) return; event.preventDefault(); event.stopPropagation(); protocol.setState((state) => ({ ...state, dragDepth: state.dragDepth + 1, dragging: true })); }, [protocol]);
+ const handleDragLeave = useCallback((event: React.DragEvent) => { if (!protocol.get().dragging) return; event.preventDefault(); event.stopPropagation(); protocol.setState((state) => ({ ...state, dragDepth: Math.max(0, state.dragDepth - 1), dragging: state.dragDepth > 1 })); }, [protocol]);
+ const handleDragOver = useCallback((event: React.DragEvent) => { if (!event.dataTransfer.types.includes("Files")) return; event.preventDefault(); event.stopPropagation(); event.dataTransfer.dropEffect = "copy"; }, []);
+ const handleDrop = useCallback((event: React.DragEvent) => { if (!event.dataTransfer.files.length && !event.dataTransfer.types?.includes("Files")) return; event.preventDefault(); event.stopPropagation(); protocol.setState((state) => ({ ...state, dragging: false, dragDepth: 0 })); void addPendingFiles(Array.from(event.dataTransfer.files)).catch(() => undefined); }, [addPendingFiles, protocol]);
return { pendingFiles, readPendingFiles: () => protocol.get().files, setPendingFiles, transferPendingFiles, restorePendingFiles, awaitPendingFiles, fileInputRef, addPendingFiles, handleFileSelect, removePendingFile, dragging, handleDragEnter, handleDragLeave, handleDragOver, handleDrop };
}
diff --git a/src/web/src/lib/community-image-thumbnail.ts b/src/web/src/lib/community-image-thumbnail.ts
new file mode 100644
index 000000000..5355b2315
--- /dev/null
+++ b/src/web/src/lib/community-image-thumbnail.ts
@@ -0,0 +1,33 @@
+import { MAX_ATTACHMENT_THUMBNAIL_EDGE_PX, MAX_ATTACHMENT_THUMBNAIL_SIZE_BYTES } from "@alook/shared"
+
+const QUALITIES = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]
+
+export type CommunityImagePreparation = { blob: Blob | null; width: number; height: number }
+export type CommunityImageWorkerResult = { result: CommunityImagePreparation | null } | { error: string } | { unsupported: true }
+
+export function requiresCommunityThumbnail(size: number, width: number, height: number) {
+ return size > MAX_ATTACHMENT_THUMBNAIL_SIZE_BYTES || Math.max(width, height) > MAX_ATTACHMENT_THUMBNAIL_EDGE_PX
+}
+
+export async function renderCommunityThumbnail(
+ width: number,
+ height: number,
+ frame: (width: number, height: number) => ((quality: number) => Promise<Blob | null>) | null,
+ signal?: AbortSignal,
+): Promise<Blob | null> {
+ const fitted = Math.min(1, MAX_ATTACHMENT_THUMBNAIL_EDGE_PX / Math.max(width, height))
+ const fittedWidth = Math.round(width * fitted), fittedHeight = Math.round(height * fitted)
+ for (let attempt = 0; attempt < 10; attempt++) {
+ signal?.throwIfAborted()
+ const scale = 0.85 ** attempt
+ const encode = frame(Math.max(1, Math.round(fittedWidth * scale)), Math.max(1, Math.round(fittedHeight * scale)))
+ if (!encode) return null
+ for (const quality of QUALITIES) {
+ signal?.throwIfAborted()
+ const blob = await encode(quality)
+ if (!blob) return null
+ if (blob.size <= MAX_ATTACHMENT_THUMBNAIL_SIZE_BYTES) return blob
+ }
+ }
+ return null
+}
diff --git a/src/web/src/lib/community-image.worker.ts b/src/web/src/lib/community-image.worker.ts
new file mode 100644
index 000000000..43cd21b42
--- /dev/null
+++ b/src/web/src/lib/community-image.worker.ts
@@ -0,0 +1,32 @@
+import { MAX_ATTACHMENT_THUMBNAIL_SIZE_BYTES } from "@alook/shared"
+import { renderCommunityThumbnail, requiresCommunityThumbnail, type CommunityImageWorkerResult } from "./community-image-thumbnail"
+
+self.addEventListener("message", async (event: MessageEvent<File>) => {
+ const respond = (message: CommunityImageWorkerResult) => self.postMessage(message)
+ if (typeof createImageBitmap !== "function" || typeof OffscreenCanvas !== "function" || typeof OffscreenCanvas.prototype.convertToBlob !== "function") {
+ respond({ unsupported: true })
+ return
+ }
+ const file = event.data
+ let bitmap: ImageBitmap | undefined
+ let required = file.size > MAX_ATTACHMENT_THUMBNAIL_SIZE_BYTES
+ try {
+ bitmap = await createImageBitmap(file)
+ const { width, height } = bitmap
+ required = requiresCommunityThumbnail(file.size, width, height)
+ const source = bitmap
+ const blob = required ? await renderCommunityThumbnail(width, height, (w, h) => {
+ const canvas = new OffscreenCanvas(w, h)
+ const context = canvas.getContext("2d")
+ if (!context) return null
+ context.drawImage(source, 0, 0, w, h)
+ return quality => canvas.convertToBlob({ type: "image/jpeg", quality })
+ }) : null
+ if (required && !blob) throw new Error("could not generate a required image preview")
+ respond({ result: { blob, width, height } })
+ } catch {
+ respond(required ? { error: "could not generate a required image preview" } : { result: null })
+ } finally {
+ bitmap?.close()
+ }
+})
diff --git a/src/web/src/lib/image-thumbnail.test.ts b/src/web/src/lib/image-thumbnail.test.ts
index fd9b5b245..d8eabe693 100644
--- a/src/web/src/lib/image-thumbnail.test.ts
+++ b/src/web/src/lib/image-thumbnail.test.ts
@@ -1,6 +1,8 @@
import { afterEach, describe, expect, it, vi } from "vitest"
import { generateThumbnail, prepareCommunityImage } from "./image-thumbnail"
+const NativeURL = URL
+
// `generateThumbnail` runs entirely against browser APIs (Image decode,
// canvas draw, URL.createObjectURL) that don't exist under this repo's
// `environment: "node"` vitest config. Stub the minimal surface it touches —
@@ -36,9 +38,9 @@ function stubBrowserImageApis(blobSizes = [1]) {
const canvases: Array<{ width: number; height: number }> = []
let blobIndex = 0
vi.stubGlobal("Image", FakeImage)
- vi.stubGlobal("URL", {
- createObjectURL: vi.fn(() => "blob:fake"),
- revokeObjectURL: vi.fn(),
+ vi.stubGlobal("URL", class extends NativeURL {
+ static createObjectURL = vi.fn(() => "blob:fake")
+ static revokeObjectURL = vi.fn()
})
vi.stubGlobal("document", {
createElement: (tag: string) => {
@@ -65,6 +67,58 @@ describe("generateThumbnail", () => {
FakeImage.nextShouldError = false
})
+ it("returns a worker-prepared image without decoding it again on the page, and terminates its worker", async () => {
+ stubBrowserImageApis()
+ const blob = new Blob(["jpeg"], { type: "image/jpeg" })
+ const terminate = vi.fn()
+ vi.stubGlobal("OffscreenCanvas", class {})
+ vi.stubGlobal("createImageBitmap", vi.fn())
+ const postMessage = vi.fn()
+ vi.stubGlobal("Worker", class {
+ onmessage: ((event: { data: unknown }) => void) | null = null
+ onerror = null
+ terminate = terminate
+ postMessage(file: File) { postMessage(file); queueMicrotask(() => this.onmessage?.({ data: { result: { blob, width: 6000, height: 4000 } } })) }
+ })
+ const file = new File(["original"], "large.jpg", { type: "image/jpeg" })
+ await expect(prepareCommunityImage(file)).resolves.toEqual({ blob, width: 6000, height: 4000 })
+ expect(postMessage).toHaveBeenCalledExactlyOnceWith(file)
+ expect(terminate).toHaveBeenCalledOnce()
+ expect(URL.createObjectURL).not.toHaveBeenCalled()
+ })
+
+ it("terminates a worker immediately when its file preparation is cancelled", async () => {
+ const terminate = vi.fn()
+ vi.stubGlobal("OffscreenCanvas", class {})
+ vi.stubGlobal("createImageBitmap", vi.fn())
+ vi.stubGlobal("Worker", class { onmessage = null; onerror = null; terminate = terminate; postMessage() {} })
+ const controller = new AbortController()
+ const pending = prepareCommunityImage(new File(["image"], "large.jpg", { type: "image/jpeg" }), controller.signal)
+ const rejected = expect(pending).rejects.toMatchObject({ name: "AbortError" })
+ controller.abort()
+ await rejected
+ expect(terminate).toHaveBeenCalledOnce()
+ })
+
+ it("falls back to the existing decoder when the worker cannot support preparation", async () => {
+ const canvases = stubBrowserImageApis()
+ FakeImage.nextWidth = 721
+ FakeImage.nextHeight = 512
+ vi.stubGlobal("OffscreenCanvas", class {})
+ vi.stubGlobal("createImageBitmap", vi.fn())
+ const terminate = vi.fn()
+ vi.stubGlobal("Worker", class {
+ onmessage: ((event: { data: unknown }) => void) | null = null
+ onerror = null
+ terminate = terminate
+ postMessage() { queueMicrotask(() => this.onmessage?.({ data: { unsupported: true } })) }
+ })
+ await expect(prepareCommunityImage(new File(["image"], "photo.png", { type: "image/png" }))).resolves.toMatchObject({ width: 721, height: 512 })
+ expect(canvases).toHaveLength(1)
+ expect(terminate).toHaveBeenCalledOnce()
+ expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:fake")
+ })
+
it.each([generateThumbnail, prepareCommunityImage])("cancels an unfinished decode and releases its source URL", async (prepare) => {
const canvases = stubBrowserImageApis()
const images: Array<{ src: string; onload: (() => void) | null; onerror: (() => void) | null }> = []
diff --git a/src/web/src/lib/image-thumbnail.ts b/src/web/src/lib/image-thumbnail.ts
index f2a1276f0..672ee02a5 100644
--- a/src/web/src/lib/image-thumbnail.ts
+++ b/src/web/src/lib/image-thumbnail.ts
@@ -1,12 +1,11 @@
import {
- MAX_ATTACHMENT_THUMBNAIL_EDGE_PX,
MAX_ATTACHMENT_THUMBNAIL_SIZE_BYTES,
} from "@alook/shared"
+import { renderCommunityThumbnail, requiresCommunityThumbnail, type CommunityImagePreparation, type CommunityImageWorkerResult } from "./community-image-thumbnail"
+
+export type { CommunityImagePreparation } from "./community-image-thumbnail"
const LEGACY_MAX_SIZE = 200
-const COMMUNITY_QUALITIES = [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]
-const COMMUNITY_DIMENSION_ATTEMPTS = 10
-const COMMUNITY_DIMENSION_SCALE = 0.85
const COMMUNITY_RASTER_MIME_TYPES = new Set([
"image/png",
"image/jpeg",
@@ -17,7 +16,6 @@ const COMMUNITY_RASTER_MIME_TYPES = new Set([
class RequiredThumbnailError extends Error {}
export type ThumbnailResult = { blob: Blob; width: number; height: number }
-export type CommunityImagePreparation = { blob: Blob | null; width: number; height: number }
export async function generateThumbnail(file: File, signal?: AbortSignal): Promise<ThumbnailResult | null> {
if (!isRasterImage(file)) return null
@@ -45,6 +43,12 @@ export async function prepareCommunityImage(
): Promise<CommunityImagePreparation | null> {
if (!COMMUNITY_RASTER_MIME_TYPES.has(file.type.toLowerCase())) return null
+ if (typeof Worker === "function" && typeof OffscreenCanvas === "function" && typeof createImageBitmap === "function") {
+ const response = await prepareInWorker(file, signal)
+ if ("error" in response) throw new Error(response.error)
+ if ("result" in response) return response.result
+ }
+
let objectUrl: string | undefined
let requiredThumbnail = file.size > MAX_ATTACHMENT_THUMBNAIL_SIZE_BYTES
try {
@@ -52,11 +56,18 @@ export async function prepareCommunityImage(
const img = await loadImage(objectUrl, signal)
const width = img.naturalWidth
const height = img.naturalHeight
- requiredThumbnail = Math.max(width, height) > MAX_ATTACHMENT_THUMBNAIL_EDGE_PX
- || requiredThumbnail
+ requiredThumbnail = requiresCommunityThumbnail(file.size, width, height)
if (!requiredThumbnail) return { blob: null, width, height }
- const blob = await renderCommunityJpeg(img, signal)
+ const blob = await renderCommunityThumbnail(width, height, (w, h) => {
+ const canvas = document.createElement("canvas")
+ canvas.width = w
+ canvas.height = h
+ const context = canvas.getContext("2d")
+ if (!context) return null
+ context.drawImage(img, 0, 0, w, h)
+ return quality => canvasToBlob(canvas, "image/jpeg", quality, signal)
+ }, signal)
if (!blob) throw new RequiredThumbnailError()
return { blob, width, height }
} catch (error) {
@@ -73,6 +84,21 @@ export async function prepareCommunityImage(
}
}
+function prepareInWorker(file: File, signal?: AbortSignal): Promise<CommunityImageWorkerResult> {
+ return new Promise((resolve, reject) => {
+ signal?.throwIfAborted()
+ let worker: Worker
+ try { worker = new Worker(new URL("./community-image.worker.ts", import.meta.url), { type: "module" }) }
+ catch { resolve({ unsupported: true }); return }
+ const cleanup = () => { signal?.removeEventListener("abort", abort); worker.terminate() }
+ const abort = () => { cleanup(); reject(signal?.reason ?? new DOMException("Image preparation cancelled", "AbortError")) }
+ signal?.addEventListener("abort", abort, { once: true })
+ worker.onmessage = (event: MessageEvent<CommunityImageWorkerResult>) => { cleanup(); resolve(event.data) }
+ worker.onerror = (event) => { event.preventDefault(); cleanup(); resolve({ unsupported: true }) }
+ try { worker.postMessage(file) } catch { cleanup(); resolve({ unsupported: true }) }
+ })
+}
+
function loadImage(src: string, signal?: AbortSignal): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const img = new Image()
@@ -121,28 +147,3 @@ async function renderJpeg(
ctx.drawImage(img, 0, 0, width, height)
return canvasToBlob(canvas, "image/jpeg", quality, signal)
}
-
-async function renderCommunityJpeg(img: HTMLImageElement, signal?: AbortSignal): Promise<Blob | null> {
- const fitted = fitWithin(
- img.naturalWidth,
- img.naturalHeight,
- MAX_ATTACHMENT_THUMBNAIL_EDGE_PX,
- )
- for (let attempt = 0; attempt < COMMUNITY_DIMENSION_ATTEMPTS; attempt++) {
- const scale = COMMUNITY_DIMENSION_SCALE ** attempt
- const width = Math.max(1, Math.round(fitted.w * scale))
- const height = Math.max(1, Math.round(fitted.h * scale))
- const canvas = document.createElement("canvas")
- canvas.width = width
- canvas.height = height
- const ctx = canvas.getContext("2d")
- if (!ctx) return null
- ctx.drawImage(img, 0, 0, width, height)
- for (const quality of COMMUNITY_QUALITIES) {
- const blob = await canvasToBlob(canvas, "image/jpeg", quality, signal)
- if (!blob) return null
- if (blob.size <= MAX_ATTACHMENT_THUMBNAIL_SIZE_BYTES) return blob
- }
- }
- return null
-}
diff --git a/src/web/src/lib/service-worker/public-cache-runtime.ts b/src/web/src/lib/service-worker/public-cache-runtime.ts
index de67dd658..5572ac7f3 100644
--- a/src/web/src/lib/service-worker/public-cache-runtime.ts
+++ b/src/web/src/lib/service-worker/public-cache-runtime.ts
@@ -1,6 +1,7 @@
import { CacheFirst, ExpirationPlugin, NetworkFirst, type RuntimeCaching, type SerwistPlugin } from "serwist"
import { publicWorkerRuntimeCacheNames } from "./cache-identity"
import { isPublicAssetRequest, isPublicAssetResponse, publicRuntimeAssetKind } from "./public-cache"
+import { createPublicWorkerScriptResponsePlugin } from "./worker-script-response"
export type PublicRuntimeLimits = Record<keyof ReturnType<typeof publicWorkerRuntimeCacheNames>, { maxEntries: number; maxAgeSeconds: number }>
@@ -38,6 +39,7 @@ export function createPublicRuntimeCaching(origin: string, limits: PublicRuntime
cacheName: names[kind],
fetchOptions: { credentials: "omit", redirect: "error", ...(kind === "images" ? { cache: "no-cache" as const } : {}) },
plugins: [
+ createPublicWorkerScriptResponsePlugin(origin),
{ cacheWillUpdate: async ({ request, response }) => isPublicAssetResponse(request, response) ? response : null },
waitForStaleExpiration(new ExpirationPlugin({ ...limits[kind] })),
],
diff --git a/src/web/src/lib/service-worker/worker-script-response.test.ts b/src/web/src/lib/service-worker/worker-script-response.test.ts
new file mode 100644
index 000000000..f8f7dbcec
--- /dev/null
+++ b/src/web/src/lib/service-worker/worker-script-response.test.ts
@@ -0,0 +1,97 @@
+import { afterEach, describe, expect, it, vi } from "vitest"
+import { CacheFirst, PrecacheStrategy } from "serwist"
+import { createPublicWorkerScriptResponsePlugin } from "./worker-script-response"
+
+const origin = "https://alook.test"
+const path = "/_next/static/chunks/worker.js"
+const scriptUrl = `${origin}${path}`
+const body = "postMessage(self.location.href)"
+const headers = { "Content-Type": "text/javascript; charset=utf-8", "Cache-Control": "public, max-age=31536000, immutable", "X-Content-Type-Options": "nosniff" }
+
+function request(destination: RequestDestination = "worker", init?: RequestInit, url = `${scriptUrl}#fixture=config`) {
+ const value = new Request(url, init)
+ Object.defineProperty(value, "destination", { value: destination })
+ return value
+}
+
+function response(url = scriptUrl, init?: ResponseInit) {
+ const value = new Response(body, { headers, ...init })
+ Object.defineProperty(value, "url", { value: url })
+ return value
+}
+
+const respond = (req: Request, res: Response) => createPublicWorkerScriptResponsePlugin(origin).handlerWillRespond!({ request: req, response: res, event: new Event("fetch") as FetchEvent })
+
+afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); vi.unstubAllEnvs() })
+
+describe("public Worker script response URL boundary", () => {
+ it.each(["worker", "sharedworker"] as const)("keeps original script bytes and headers while leaving %s location to the request", async destination => {
+ const source = response()
+ const result = await respond(request(destination), source)
+ expect(result).not.toBe(source)
+ expect(result.url).toBe("")
+ expect(result.status).toBe(source.status)
+ expect(result.statusText).toBe(source.statusText)
+ expect([...result.headers]).toEqual([...source.headers])
+ expect(await result.text()).toBe(body)
+ })
+
+ it("leaves other destinations, sensitive requests and unqualified responses untouched", async () => {
+ for (const destination of ["", "script", "style", "document", "serviceworker"] as const) {
+ const source = response()
+ expect(await respond(request(destination), source)).toBe(source)
+ }
+ for (const init of [{ headers: { Authorization: "fixture" } }, { headers: { Cookie: "fixture" } }, { headers: { RSC: "1" } }, { method: "POST" }]) {
+ const source = response()
+ expect(await respond(request("worker", init), source)).toBe(source)
+ }
+ for (const url of [`${scriptUrl}?version=fixture`, `https://other.test${path}`, `${origin}/api/private.js`]) {
+ const source = response()
+ expect(await respond(request("worker", undefined, url), source)).toBe(source)
+ }
+ for (const source of [response(""), response(`https://other.test${path}`), response(`${origin}/_next/static/chunks/other.js`), response(scriptUrl, { status: 404 }), response(scriptUrl, { headers: { ...headers, "Cache-Control": "private, no-store" } }), response(scriptUrl, { headers: { ...headers, "Content-Type": "text/html" } }), response(scriptUrl, { headers: { ...headers, Vary: "Cookie" } }), response(scriptUrl, { headers: { ...headers, "Set-Cookie": "fixture=private" } }), Response.error()]) {
+ expect(await respond(request(), source)).toBe(source)
+ }
+ const redirected = response()
+ Object.defineProperty(redirected, "redirected", { value: true })
+ expect(await respond(request(), redirected)).toBe(redirected)
+ })
+
+ it.each(["precache-hit", "runtime-hit", "runtime-miss"] as const)("uses the official final-response hook for %s without changing cache identity", async scenario => {
+ class WorkerEvent extends Event {
+ pending: Promise<unknown>[] = []
+ readonly request: Request
+ constructor(request: Request) { super("fetch"); this.request = request }
+ waitUntil(promise: Promise<unknown>) { this.pending.push(promise) }
+ }
+ vi.stubEnv("NODE_ENV", "production")
+ vi.stubGlobal("ExtendableEvent", WorkerEvent)
+ vi.stubGlobal("FetchEvent", WorkerEvent)
+ const location = new URL(`${origin}/sw.js`)
+ vi.stubGlobal("location", location)
+ const source = response()
+ const match = vi.fn<(key: RequestInfo | URL, options?: CacheQueryOptions) => Promise<Response | undefined>>(async () => scenario === "runtime-miss" ? undefined : source)
+ const put = vi.fn<(key: RequestInfo | URL, value: Response) => Promise<void>>(async () => undefined)
+ const storage = { match, open: async () => ({ match, put }) }
+ vi.stubGlobal("caches", storage)
+ vi.stubGlobal("self", { location, caches: storage, registration: { scope: `${origin}/` } })
+ const network = vi.fn<typeof fetch>(async () => source)
+ vi.stubGlobal("fetch", network)
+ const plugins = [createPublicWorkerScriptResponsePlugin(origin)]
+ const strategy = scenario === "precache-hit" ? new PrecacheStrategy({ cacheName: "fixture-public", plugins }) : new CacheFirst({ cacheName: "fixture-public", plugins })
+ const req = request()
+ const event = new WorkerEvent(req)
+ const [result, done] = strategy.handleAll({ request: req, event: event as unknown as FetchEvent })
+ const output = await result
+ await done
+ await Promise.all(event.pending)
+ expect(output.url).toBe("")
+ expect([...output.headers]).toEqual([...source.headers])
+ expect(await output.text()).toBe(body)
+ expect(source.url).toBe(scriptUrl)
+ expect(network).toHaveBeenCalledTimes(scenario === "runtime-miss" ? 1 : 0)
+ expect(put).toHaveBeenCalledTimes(scenario === "runtime-miss" ? 1 : 0)
+ expect(match.mock.calls[0][0]).toMatchObject({ url: req.url })
+ if (scenario === "runtime-miss") expect(await (put.mock.calls[0][1] as Response).text()).toBe(body)
+ })
+})
diff --git a/src/web/src/lib/service-worker/worker-script-response.ts b/src/web/src/lib/service-worker/worker-script-response.ts
new file mode 100644
index 000000000..1a997fbc1
--- /dev/null
+++ b/src/web/src/lib/service-worker/worker-script-response.ts
@@ -0,0 +1,15 @@
+import type { SerwistPlugin } from "serwist"
+import { isPublicAssetRequest, isPublicAssetResponse } from "./public-cache"
+
+export function createPublicWorkerScriptResponsePlugin(origin: string): SerwistPlugin {
+ return {
+ handlerWillRespond: async ({ request, response }) => {
+ if (request.destination !== "worker" && request.destination !== "sharedworker") return response
+ if (!response.url || !isPublicAssetRequest(request, origin) || !isPublicAssetResponse(request, response)) return response
+ const requestUrl = new URL(request.url)
+ const responseUrl = new URL(response.url)
+ if (!/\.m?js$/.test(requestUrl.pathname) || responseUrl.origin !== origin || responseUrl.pathname !== requestUrl.pathname) return response
+ return new Response(response.body, { status: response.status, statusText: response.statusText, headers: response.headers })
+ },
+ }
+}
diff --git a/src/web/src/test/e2e-ui/02-server-channel-message.spec.ts b/src/web/src/test/e2e-ui/02-server-channel-message.spec.ts
index 19966a68a..0d7edfffd 100644
--- a/src/web/src/test/e2e-ui/02-server-channel-message.spec.ts
+++ b/src/web/src/test/e2e-ui/02-server-channel-message.spec.ts
@@ -4,6 +4,8 @@ import { test, expect } from "./_fixtures/case-fixture"
import { tid } from "./_fixtures/testids"
import { composerEditable, createServer, sendMessage, expectMessageVisible } from "./_fixtures/actions"
+type AttachmentDraftUrls = { created: string[]; revoked: string[]; files: { url: string; name: string; size: number; type: string }[] }
+
async function tabTo(page: Page, target: Locator) {
for (let step = 0; step < 40; step++) {
await page.keyboard.press("Tab")
@@ -136,7 +138,7 @@ test("mobile category actions stay hidden while title menus and desktop keyboard
mutations }), contentType: "application/json" })
})
-test("server → channel → message", async ({ caseActors, caseResources }) => {
+test("server → channel → message", async ({ caseActors, caseResources }, testInfo) => {
test.setTimeout(120_000)
const actors = await caseActors(["alice"])
@@ -384,57 +386,90 @@ test("server → channel → message", async ({ caseActors, caseResources }) =>
await restored.pressSequentially(rejectedDraft)
await page.evaluate(() => {
const created: string[] = [], revoked: string[] = []
+ const files: { url: string; name: string; size: number; type: string }[] = []
const create = URL.createObjectURL.bind(URL), revoke = URL.revokeObjectURL.bind(URL)
- Object.defineProperty(window, "__attachmentDraftUrls", { configurable: true, value: { created, revoked } })
- URL.createObjectURL = (object) => { const url = create(object); created.push(url); return url }
+ Object.defineProperty(window, "__attachmentDraftUrls", { configurable: true, value: { created, revoked, files } })
+ URL.createObjectURL = (object) => {
+ const url = create(object)
+ created.push(url)
+ if (object instanceof File) files.push({ url, name: object.name, size: object.size, type: object.type })
+ return url
+ }
URL.revokeObjectURL = (url) => { revoked.push(url); revoke(url) }
})
const previewBytes = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=", "base64")
await page.getByTestId(tid.composerFileInput).setInputFiles({ name: "retry-preview.png", mimeType: "image/png", buffer: previewBytes })
- await expect(page.getByText("retry-preview.png", { exact: true })).toBeVisible()
- await expect.poll(() => page.evaluate(() => (window as unknown as { __attachmentDraftUrls: { created: string[] } }).__attachmentDraftUrls.created.length)).toBeGreaterThanOrEqual(2)
- const currentPreview = await page.evaluate(() => (window as unknown as { __attachmentDraftUrls: { created: string[] } }).__attachmentDraftUrls.created.at(-1)!)
- await expect.poll(() => pendingPostCount).toBe(1)
- await expect(restored).toContainText(rejectedDraft)
+ try {
+ const previewCard = page.getByTestId(tid.composerInput).locator("../..").getByText("retry-preview.png", { exact: true }).locator("..")
+ await expect(previewCard).toBeVisible()
+ await expect(previewCard).not.toHaveAttribute("aria-busy", "true")
+ await expect.poll(() => page.evaluate(() => {
+ const { files, revoked } = (window as unknown as { __attachmentDraftUrls: AttachmentDraftUrls }).__attachmentDraftUrls
+ return files.filter(file => file.name === "retry-preview.png" && !revoked.includes(file.url))
+ })).toEqual([{ url: expect.stringMatching(/^blob:/), name: "retry-preview.png", type: "image/png", size: previewBytes.length }])
+ const currentPreview = await page.evaluate(() => {
+ const { files, revoked } = (window as unknown as { __attachmentDraftUrls: AttachmentDraftUrls }).__attachmentDraftUrls
+ return files.find(file => file.name === "retry-preview.png" && !revoked.includes(file.url))!.url
+ })
+ await expect.poll(() => pendingPostCount).toBe(1)
+ await expect(restored).toContainText(rejectedDraft)
- releasePendingSend()
- await expect.poll(() => pendingSendCompleted).toBe(true)
- const retry = page.getByRole("button", { name: "Message failed to send. Click to retry." })
- await expect(retry).toHaveCount(1)
- await expectMessageVisible(page, pendingBody)
- await page.unroute("**/api/community/channels/*/messages")
- const retryResponsePromise = page.waitForResponse((response) => response.request().method() === "POST"
- && new URL(response.url()).pathname === `/api/community/channels/${channelId}/messages`)
- await retry.click()
- const retryResponse = await retryResponsePromise
- expect(retryResponse.status()).toBe(201)
- const retryBody = retryResponse.request().postDataJSON() as { content: string; nonce: string }
- expect(retryBody).toMatchObject({ content: pendingBody, nonce: sendBodies[0]!.nonce })
- expect(sends).toBe(3)
- const retryPayload = await retryResponse.json() as { message: { id: string } }
- await expect(page.getByTestId(tid.message(retryPayload.message.id))).toHaveCount(1)
- await expectMessageVisible(page, pendingBody)
- await expect(page.getByRole("button", { name: "Message failed to send. Click to retry." })).toHaveCount(0)
- await expect(restored).toContainText(rejectedDraft)
- await expect(page.getByText("retry-preview.png", { exact: true })).toBeVisible()
- expect(await page.evaluate((url) => (window as unknown as { __attachmentDraftUrls: { revoked: string[] } }).__attachmentDraftUrls.revoked.includes(url), currentPreview)).toBe(false)
- expect(await page.evaluate(async (url) => Array.from(new Uint8Array(await (await fetch(url)).arrayBuffer())), currentPreview)).toEqual(Array.from(previewBytes))
- const previewUploadResponsePromise = page.waitForResponse((response) => response.request().method() === "POST" && new URL(response.url()).pathname === `/api/community/channels/${channelId}/attachments`)
- const previewMessageResponsePromise = page.waitForResponse((response) => response.request().method() === "POST" && new URL(response.url()).pathname === `/api/community/channels/${channelId}/messages`)
- await restored.click()
- await expect.poll(() => restored.evaluate((element) => document.activeElement === element)).toBe(true)
- await page.keyboard.press("Enter")
- const previewUpload = await previewUploadResponsePromise
- expect(previewUpload.ok()).toBe(true)
- const previewAttachment = await previewUpload.json() as { id: string; filename: string; contentType: string; size: number }
- expect(previewAttachment).toMatchObject({ filename: "retry-preview.png", contentType: "image/png", size: previewBytes.length })
- const previewMessage = await previewMessageResponsePromise
- expect(previewMessage.status()).toBe(201)
- expect(previewMessage.request().postDataJSON()).toMatchObject({ content: rejectedDraft, attachments: [previewAttachment.id] })
- const downloadedPreview = await page.request.get(`/api/community/channels/${channelId}/attachments/${previewAttachment.id}`)
- expect(downloadedPreview.status()).toBe(200)
- expect(await downloadedPreview.body()).toEqual(previewBytes)
- await expect(restored).toHaveText("")
+ releasePendingSend()
+ await expect.poll(() => pendingSendCompleted).toBe(true)
+ const retry = page.getByRole("button", { name: "Message failed to send. Click to retry." })
+ await expect(retry).toHaveCount(1)
+ await expectMessageVisible(page, pendingBody)
+ await page.unroute("**/api/community/channels/*/messages")
+ const retryResponsePromise = page.waitForResponse((response) => response.request().method() === "POST"
+ && new URL(response.url()).pathname === `/api/community/channels/${channelId}/messages`)
+ await retry.click()
+ const retryResponse = await retryResponsePromise
+ expect(retryResponse.status()).toBe(201)
+ const retryBody = retryResponse.request().postDataJSON() as { content: string; nonce: string }
+ expect(retryBody).toMatchObject({ content: pendingBody, nonce: sendBodies[0]!.nonce })
+ expect(sends).toBe(3)
+ const retryPayload = await retryResponse.json() as { message: { id: string } }
+ await expect(page.getByTestId(tid.message(retryPayload.message.id))).toHaveCount(1)
+ await expectMessageVisible(page, pendingBody)
+ await expect(page.getByRole("button", { name: "Message failed to send. Click to retry." })).toHaveCount(0)
+ await expect(restored).toContainText(rejectedDraft)
+ await expect(page.getByText("retry-preview.png", { exact: true })).toBeVisible()
+ expect(await page.evaluate(() => {
+ const { files, revoked } = (window as unknown as { __attachmentDraftUrls: AttachmentDraftUrls }).__attachmentDraftUrls
+ return files.filter(file => file.name === "retry-preview.png" && !revoked.includes(file.url)).map(file => file.url)
+ })).toEqual([currentPreview])
+ expect(await page.evaluate((url) => (window as unknown as { __attachmentDraftUrls: { revoked: string[] } }).__attachmentDraftUrls.revoked.includes(url), currentPreview)).toBe(false)
+ expect(await page.evaluate(async (url) => Array.from(new Uint8Array(await (await fetch(url)).arrayBuffer())), currentPreview)).toEqual(Array.from(previewBytes))
+ const previewUploadResponsePromise = page.waitForResponse((response) => response.request().method() === "POST" && new URL(response.url()).pathname === `/api/community/channels/${channelId}/attachments`)
+ const previewMessageResponsePromise = page.waitForResponse((response) => response.request().method() === "POST" && new URL(response.url()).pathname === `/api/community/channels/${channelId}/messages`)
+ await restored.click()
+ await expect.poll(() => restored.evaluate((element) => document.activeElement === element)).toBe(true)
+ await page.keyboard.press("Enter")
+ const previewUpload = await previewUploadResponsePromise
+ expect(previewUpload.ok()).toBe(true)
+ const previewAttachment = await previewUpload.json() as { id: string; filename: string; contentType: string; size: number }
+ expect(previewAttachment).toMatchObject({ filename: "retry-preview.png", contentType: "image/png", size: previewBytes.length })
+ const previewMessage = await previewMessageResponsePromise
+ expect(previewMessage.status()).toBe(201)
+ expect(previewMessage.request().postDataJSON()).toMatchObject({ content: rejectedDraft, attachments: [previewAttachment.id] })
+ const downloadedPreview = await page.request.get(`/api/community/channels/${channelId}/attachments/${previewAttachment.id}`)
+ expect(downloadedPreview.status()).toBe(200)
+ expect(await downloadedPreview.body()).toEqual(previewBytes)
+ await expect(restored).toHaveText("")
+ await expect(previewCard).toHaveCount(0)
+ await expect.poll(() => page.evaluate(() => {
+ const { files, revoked } = (window as unknown as { __attachmentDraftUrls: AttachmentDraftUrls }).__attachmentDraftUrls
+ const owned = files.filter(file => file.name === "retry-preview.png")
+ return owned.length > 0 && owned.every(file => revoked.filter(url => url === file.url).length === 1)
+ })).toBe(true)
+ } finally {
+ const lifecycle = await page.evaluate(() => ({
+ ...(window as unknown as { __attachmentDraftUrls: AttachmentDraftUrls }).__attachmentDraftUrls,
+ at: performance.now(),
+ clock: "performance.now in the current page",
+ })).catch(error => ({ captureError: String(error) }))
+ await testInfo.attach("attachment-draft-urls.json", { body: JSON.stringify(lifecycle), contentType: "application/json" })
+ }
const attachmentDraft = `${rejectedDraft} next attachment`
await restored.pressSequentially(attachmentDraft)
await page.goto("/c/me", { waitUntil: "commit" })
diff --git a/src/web/src/test/e2e-ui/43-composer-attachment-drafts.spec.ts b/src/web/src/test/e2e-ui/43-composer-attachment-drafts.spec.ts
index 4f3641b2e..e79195db5 100644
--- a/src/web/src/test/e2e-ui/43-composer-attachment-drafts.spec.ts
+++ b/src/web/src/test/e2e-ui/43-composer-attachment-drafts.spec.ts
@@ -1,7 +1,8 @@
import { currentCommunityFixtureError } from "./_fixtures/business-contract"
-import type { Page } from "@playwright/test"
+import type { Locator, Page } from "@playwright/test"
import { test, expect } from "./_fixtures/case-fixture"
import { composerEditable, installInputCapability } from "./_fixtures/actions"
+import { rootWorker } from "./_fixtures/public-static-qa"
import { tid } from "./_fixtures/testids"
import type { CaseActors } from "./_fixtures/case-fixture"
@@ -11,25 +12,73 @@ const PNG_1X1 = Buffer.from(
"base64",
)
+async function menuTargetState(target: Locator) {
+ return target.evaluate(element => {
+ const doc = element.ownerDocument
+ const identify = (node: Element | null) => node ? { tag: node.tagName, id: node.id, testId: node.getAttribute("data-testid"), slot: node.getAttribute("data-slot") } : null
+ const rectangle = (node: Element) => { const { x, y, width, height } = node.getBoundingClientRect(); return { x, y, width, height } }
+ const bounds = element.getBoundingClientRect()
+ const hit = doc.elementFromPoint(bounds.left + bounds.width / 2, bounds.top + bounds.height / 2)
+ return {
+ at: performance.now(),
+ clock: "performance.now in the current page",
+ target: identify(element),
+ bounds: rectangle(element),
+ focused: identify(doc.activeElement),
+ centerHit: identify(hit),
+ targetOwnsCenterHit: !!hit && element.contains(hit),
+ tooltips: Array.from(doc.querySelectorAll('[data-slot="tooltip-content"]'), popup => {
+ const style = doc.defaultView!.getComputedStyle(popup)
+ const parent = popup.parentElement
+ return {
+ identity: identify(popup),
+ open: popup.hasAttribute("data-open"),
+ closed: popup.hasAttribute("data-closed"),
+ ending: popup.hasAttribute("data-ending-style"),
+ state: popup.getAttribute("data-state"),
+ hidden: popup.hasAttribute("hidden"),
+ inert: popup.hasAttribute("inert"),
+ hiddenAncestor: !!popup.closest("[hidden]"),
+ inertAncestor: !!popup.closest("[inert]"),
+ display: style.display,
+ visibility: style.visibility,
+ opacity: style.opacity,
+ pointerEvents: style.pointerEvents,
+ bounds: rectangle(popup),
+ parent: parent ? { identity: identify(parent), hidden: parent.hidden, inert: parent.inert, ownsCenterHit: !!hit && parent.contains(hit) } : null,
+ }
+ }),
+ }
+ })
+}
+
+async function revealSidebar(page: Page) {
+ const sidebar = page.locator('[data-slot="community-sidebar-panel-content"]')
+ if (await sidebar.getAttribute("inert") !== null) {
+ const back = page.getByRole("banner").getByRole("button", { name: "Back", exact: true })
+ await expect(back).toBeVisible()
+ await back.click()
+ await expect(sidebar).not.toHaveAttribute("inert")
+ }
+}
+
async function navigateChannel(page: Page, serverId: string, channelId: string) {
const route = `/c/channels/${serverId}/${channelId}`
const row = page.getByTestId(tid.channelRow(channelId))
- if (!await row.isVisible()) {
- const back = page.getByRole("banner").getByRole("button", { name: "Back" })
- if (await back.isVisible()) await back.evaluate((element) => (element as HTMLElement).click())
- else await page.getByTestId(tid.serverIcon(serverId)).evaluate((element) => (element as HTMLElement).click())
- }
+ await revealSidebar(page)
+ if (!await row.isVisible()) await page.getByTestId(tid.serverIcon(serverId)).click()
await expect(row).toBeVisible()
- await row.evaluate((element) => (element as HTMLElement).click())
+ await row.click()
await page.waitForURL((url) => url.pathname === route)
await expect(composerEditable(page)).toBeVisible({ timeout: 20_000 })
}
async function navigateDm(page: Page, dmId: string) {
- await page.getByTestId(tid.homeButton).evaluate((element) => (element as HTMLElement).click())
+ await revealSidebar(page)
+ await page.getByTestId(tid.homeButton).click()
const row = page.getByTestId(tid.dmRow(dmId))
await expect(row).toBeVisible()
- await row.evaluate((element) => (element as HTMLElement).click())
+ await row.click()
await page.waitForURL((url) => url.pathname === `/c/me/${dmId}`)
await expect(composerEditable(page)).toBeVisible({ timeout: 20_000 })
}
@@ -51,6 +100,244 @@ test.describe("same-tab Community attachment drafts", () => {
return { serverId, channelAId, channelBId, openerId, threadId, dmId }
}
+ test("desktop isolates external file events and accepts input while real image workers prepare attachments", async ({ caseActors }, testInfo) => {
+ test.setTimeout(120_000)
+ const actors = await caseActors(["alice", "bob"])
+ const { serverId, channelAId, channelBId } = await prepareCase1(actors)
+ const otherServerId = await actors.seed.seedServer("alice", `file-event-sort-${Date.now()}`)
+ const { page } = await actors.asUser("alice")
+ await installInputCapability(page, true)
+ await page.goto(`/c/channels/${serverId}/${channelAId}`, { waitUntil: "commit" })
+ await expect(composerEditable(page)).toBeVisible()
+ await rootWorker(page)
+ const composer = composerAttachments(page)
+ const transfer = await page.evaluateHandle(() => {
+ const data = new DataTransfer()
+ data.items.add(new File(["dropped document"], "dropped.txt", { type: "text/plain" }))
+ return data
+ })
+ const menuStates: Array<{ stage: string; state: Awaited<ReturnType<typeof menuTargetState>> }> = []
+ let activeMenuTarget: Locator | null = null
+ const recordMenuState = async (stage: string, target: Locator) => { menuStates.push({ stage, state: await menuTargetState(target) }) }
+ try {
+ for (const target of [page.getByTestId(tid.serverIcon(serverId)), page.getByTestId(tid.channelRow(channelAId))]) {
+ activeMenuTarget = target
+ await recordMenuState("before-editor-focus", target)
+ await composerEditable(page).click()
+ await expect(composerEditable(page)).toBeFocused()
+ await recordMenuState("editor-focused", target)
+ await page.keyboard.press("Escape")
+ await recordMenuState("after-escape", target)
+ await target.click({ button: "right", trial: true, timeout: 10_000 })
+ await recordMenuState("native-ready-before-files", target)
+ await target.dispatchEvent("dragenter", { dataTransfer: transfer })
+ await target.dispatchEvent("dragover", { dataTransfer: transfer })
+ await expect(composer.getByText("Drop files here", { exact: true })).toHaveCount(0)
+ await target.click({ button: "right" })
+ if (await target.getAttribute("data-testid") === tid.serverIcon(serverId)) await expect(page.getByTestId(tid.serverSettingsOpen)).toBeVisible()
+ else await expect(page.getByRole("menuitem", { name: "Edit channel", exact: true })).toBeVisible()
+ await recordMenuState("menu-open-after-files", target)
+ await page.keyboard.press("Escape")
+ await target.dispatchEvent("dragleave", { dataTransfer: transfer })
+ }
+ await page.getByTestId(tid.channelRow(channelAId)).dispatchEvent("drop", { dataTransfer: transfer })
+ await expect(composer.getByText("dropped.txt", { exact: true })).toHaveCount(0)
+ await composer.dispatchEvent("dragenter", { dataTransfer: transfer })
+ await expect(composer.getByText("Drop files here", { exact: true })).toBeVisible()
+ await composer.dispatchEvent("drop", { dataTransfer: transfer })
+ await expect(composer.getByText("Drop files here", { exact: true })).toHaveCount(0)
+ await expect(composer.getByText("dropped.txt", { exact: true })).toBeVisible()
+ } finally {
+ const finalTargetState = activeMenuTarget ? await menuTargetState(activeMenuTarget).catch(error => ({ captureError: String(error) })) : { captureError: "No menu target selected" }
+ await testInfo.attach("external-file-menu-input.json", { body: JSON.stringify({ observations: menuStates, finalTargetState, boundary: "Read-only DOM focus/style/center-hit evidence; native trial and actual secondary-click/menu assertions are the gates" }), contentType: "application/json" })
+ await transfer.dispose()
+ }
+
+ await composerEditable(page).focus()
+ await composerEditable(page).evaluate(element => {
+ const data = new DataTransfer()
+ data.items.add(new File(["pasted document"], "pasted.txt", { type: "text/plain" }))
+ data.setData("text/html", `<p>${"discarded HTML ".repeat(30_000)}</p>`)
+ data.setData("text/plain", "discarded clipboard text")
+ element.dispatchEvent(new ClipboardEvent("paste", { clipboardData: data, bubbles: true, cancelable: true }))
+ })
+ await expect(composer.getByText("pasted.txt", { exact: true })).toBeVisible()
+ await expect(composerEditable(page)).toHaveText("")
+ const workers: string[] = []
+ page.on("worker", worker => workers.push(worker.url()))
+ const workerScriptResponses: { path: string; status: number; contentType: string | null; fromServiceWorker: boolean }[] = []
+ const recordWorkerScriptResponse = (response: import("@playwright/test").Response) => {
+ const path = new URL(response.url()).pathname
+ if (path.startsWith("/_next/static/chunks/") && path.endsWith(".js")) workerScriptResponses.push({ path, status: response.status(), contentType: response.headers()["content-type"] ?? null, fromServiceWorker: response.fromServiceWorker() })
+ }
+ page.on("response", recordWorkerScriptResponse)
+ const serviceWorkerController = await page.evaluate(() => ({ script: navigator.serviceWorker.controller!.scriptURL, state: navigator.serviceWorker.controller!.state }))
+ const imageData = await page.evaluateHandle(async () => {
+ const canvas = document.createElement("canvas")
+ canvas.width = 6000
+ canvas.height = 4000
+ const context = canvas.getContext("2d")!
+ const pixels = context.createImageData(canvas.width, canvas.height)
+ let seed = 98251
+ for (let index = 0; index < pixels.data.length; index += 4) {
+ seed = (seed * 1664525 + 1013904223) >>> 0
+ pixels.data[index] = seed & 255
+ pixels.data[index + 1] = seed >>> 8 & 255
+ pixels.data[index + 2] = seed >>> 16 & 255
+ pixels.data[index + 3] = 255
+ }
+ context.putImageData(pixels, 0, 0)
+ const blob = await new Promise<Blob>(resolve => canvas.toBlob(blob => resolve(blob!), "image/jpeg", 0.94))
+ canvas.width = 0
+ canvas.height = 0
+ const data = new DataTransfer()
+ for (let index = 0; index < 10; index++) data.items.add(new File([blob], `large-${index}.jpg`, { type: "image/jpeg" }))
+ return data
+ })
+ for (const name of ["dropped.txt", "pasted.txt"]) await composer.getByText(name, { exact: true }).locator("..").getByRole("button", { name: "Remove file" }).click()
+ await composerEditable(page).focus()
+ const workerResults = await page.evaluateHandle(() => {
+ const results: { width: number; height: number; size: number; type: string }[] = []
+ const messages: { worker: number; kind: string; keys: string[]; unsupported: boolean; error: string | null; blob: { constructor: string; tag: string; mainRealmBlob: boolean; bytes: number | null; type: string | null } | null }[] = []
+ const failures: { worker: number; event: string; message: string; filename: string; line: number; column: number }[] = []
+ const instances: { worker: number; url: string; type: string }[] = []
+ const capabilities = { Worker: typeof Worker, createImageBitmap: typeof createImageBitmap, OffscreenCanvas: typeof OffscreenCanvas }
+ let nextWorker = 0
+ const NativeWorker = window.Worker
+ window.Worker = class extends NativeWorker {
+ constructor(url: string | URL, options?: WorkerOptions) {
+ super(url, options)
+ const worker = ++nextWorker
+ instances.push({ worker, url: String(url), type: options?.type ?? "classic" })
+ this.addEventListener("error", event => {
+ failures.push({ worker, event: event.type, message: event.message, filename: event.filename, line: event.lineno, column: event.colno })
+ })
+ this.addEventListener("messageerror", event => {
+ failures.push({ worker, event: event.type, message: "Worker message deserialization failed", filename: "", line: 0, column: 0 })
+ })
+ this.addEventListener("message", ({ data }) => {
+ const object = data && typeof data === "object" ? data : null
+ const blob = object?.result?.blob
+ messages.push({
+ worker,
+ kind: object && "result" in object ? "result" : object && "error" in object ? "error" : object?.unsupported === true ? "unsupported" : "unknown",
+ keys: object ? Object.keys(object) : [],
+ unsupported: object?.unsupported === true,
+ error: typeof object?.error === "string" ? object.error : null,
+ blob: blob == null ? null : { constructor: blob.constructor?.name ?? "unknown", tag: Object.prototype.toString.call(blob), mainRealmBlob: blob instanceof Blob, bytes: typeof blob.size === "number" ? blob.size : null, type: typeof blob.type === "string" ? blob.type : null },
+ })
+ if (data?.result?.blob instanceof Blob) results.push({ width: data.result.width, height: data.result.height, size: data.result.blob.size, type: data.result.blob.type })
+ })
+ }
+ }
+ return { results, messages, failures, instances, capabilities }
+ })
+ const inputProbe = await composerEditable(page).evaluateHandle((element, inputId) => {
+ const host = element.closest(`[data-testid="${inputId}"]`)?.parentElement?.parentElement
+ const busy = () => !!host?.querySelector('[aria-busy="true"]')
+ const started = performance.now()
+ const keys: { at: number; eventToHandlerMs: number; trusted: boolean; preparing: boolean }[] = []
+ const inputs: { at: number; keyToInputMs: number | null; eventToHandlerMs: number; nextFrameMs: number | null; trusted: boolean; preparing: boolean }[] = []
+ const timerGaps: number[] = []
+ const longTasks: { at: number; durationMs: number }[] = []
+ let lastKey: number | null = null
+ let lastTimer = started
+ const keydown = (event: Event) => {
+ const at = performance.now()
+ lastKey = at
+ keys.push({ at: at - started, eventToHandlerMs: Math.max(0, at - event.timeStamp), trusted: event.isTrusted, preparing: busy() })
+ }
+ const input = (event: Event) => {
+ const at = performance.now()
+ const sample = { at: at - started, keyToInputMs: lastKey === null ? null : at - lastKey, eventToHandlerMs: Math.max(0, at - event.timeStamp), nextFrameMs: null as number | null, trusted: event.isTrusted, preparing: busy() }
+ inputs.push(sample)
+ requestAnimationFrame(() => { sample.nextFrameMs = performance.now() - at })
+ }
+ element.addEventListener("keydown", keydown)
+ element.addEventListener("input", input)
+ const timer = setInterval(() => { const now = performance.now(); timerGaps.push(now - lastTimer); lastTimer = now }, 10)
+ const longTasksSupported = PerformanceObserver.supportedEntryTypes.includes("longtask")
+ const observer = longTasksSupported ? new PerformanceObserver(list => {
+ for (const entry of list.getEntries()) longTasks.push({ at: entry.startTime - started, durationMs: entry.duration })
+ }) : null
+ observer?.observe({ type: "longtask" })
+ return {
+ snapshot: () => ({ startedAt: started, clock: "performance.now in the current page", elapsedMs: performance.now() - started, keys, inputs, timerGaps, longTasksSupported, longTasks }),
+ stop: () => { element.removeEventListener("keydown", keydown); element.removeEventListener("input", input); clearInterval(timer); observer?.disconnect() },
+ }
+ }, tid.composerInput)
+ let admission: { constructor: string; files: { name: string; bytes: number }[]; consumed: boolean; focused: boolean } | undefined
+ let acceptedFiles: string[] = []
+ let survivingFiles: string[] = []
+ try {
+ admission = await composerEditable(page).evaluate((element, clipboardData) => {
+ const event = new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true })
+ const files = Array.from(event.clipboardData?.files ?? [], file => ({ name: file.name, bytes: file.size }))
+ element.dispatchEvent(event)
+ return { constructor: event.constructor.name, files, consumed: event.defaultPrevented, focused: document.activeElement === element }
+ }, imageData)
+ expect(admission.constructor).toBe("ClipboardEvent")
+ expect(admission.files).toHaveLength(10)
+ expect(admission.consumed).toBe(true)
+ expect(admission.focused).toBe(true)
+ await expect(composer.getByRole("status").first()).toBeVisible()
+ await expect(composer.getByRole("button", { name: "Remove file" })).toHaveCount(10)
+ acceptedFiles = await composer.locator('[aria-label="Remove file"]').evaluateAll(buttons => buttons.map(button => button.parentElement!.querySelector("span")!.textContent!))
+ expect(acceptedFiles).toEqual(admission.files.map(file => file.name))
+ await composer.getByText("large-9.jpg", { exact: true }).locator("..").getByRole("button", { name: "Remove file" }).click()
+ await expect(composer.getByRole("button", { name: "Remove file" })).toHaveCount(9)
+ await composerEditable(page).pressSequentially("typing while image files prepare", { delay: 5 })
+ await expect(composerEditable(page)).toHaveText("typing while image files prepare")
+ await expect.poll(() => inputProbe.evaluate(probe => probe.snapshot().inputs.some(input => input.trusted && input.preparing))).toBe(true)
+ await expect(composer.getByRole("status")).toHaveCount(0, { timeout: 30_000 })
+ await expect(composer.getByText("large-9.jpg", { exact: true })).toHaveCount(0)
+ await expect(composer.getByRole("button", { name: "Remove file" })).toHaveCount(9)
+ survivingFiles = await composer.locator('[aria-label="Remove file"]').evaluateAll(buttons => buttons.map(button => button.parentElement!.querySelector("span")!.textContent!))
+ expect(survivingFiles).toEqual(acceptedFiles.slice(0, 9))
+ expect(workers.length).toBeGreaterThan(0)
+ const prepared = await workerResults.evaluate(probe => probe.results)
+ expect(prepared.length).toBeGreaterThan(0)
+ expect(prepared).toHaveLength(survivingFiles.length)
+ for (const result of prepared) {
+ expect(result).toMatchObject({ width: 6000, height: 4000, type: "image/jpeg" })
+ expect(result.size).toBeLessThanOrEqual(256 * 1024)
+ }
+ } finally {
+ page.off("response", recordWorkerScriptResponse)
+ const input = await inputProbe.evaluate(probe => { probe.stop(); return probe.snapshot() })
+ const workerObservation = await workerResults.jsonValue()
+ await testInfo.attach("image-preparation-input.json", { body: JSON.stringify({ admission, acceptedFiles, survivingFiles, workers, prepared: workerObservation.results, workerObservation, serviceWorkerController, workerScriptResponses, input, fixture: { width: 6000, height: 4000, files: 10 }, removedBeforePreparation: "large-9.jpg", timingBoundary: "Trusted page event delivery and next-frame opportunities; not compositor timestamps or a statistical performance gate" }), contentType: "application/json" })
+ await imageData.dispose()
+ await workerResults.dispose()
+ await inputProbe.dispose()
+ }
+ while (await composer.getByRole("button", { name: "Remove file" }).count()) await composer.getByRole("button", { name: "Remove file" }).first().click()
+
+ const nativeSource = (id: string, server: boolean) => page.getByTestId(server ? tid.serverIcon(id) : tid.channelRow(id)).locator("xpath=ancestor-or-self::*[@draggable='true'][1]")
+ const orderPair = async (first: ReturnType<typeof nativeSource>, second: ReturnType<typeof nativeSource>) => {
+ const firstBox = await first.boundingBox(), secondBox = await second.boundingBox()
+ if (!firstBox || !secondBox) throw new Error("Native sorting source or target is not visible")
+ return firstBox.y > secondBox.y ? [first, second] as const : [second, first] as const
+ }
+ const [railSource, railTarget] = await orderPair(nativeSource(serverId, true), nativeSource(otherServerId, true))
+ const railAccepted = page.waitForResponse(response => response.request().method() === "PATCH" && new URL(response.url()).pathname === "/api/community/users/me/server-rail")
+ await railSource.dragTo(railTarget, { targetPosition: { x: 22, y: 4 } })
+ const railResponse = await railAccepted
+ expect(railResponse.status()).toBe(200)
+ expect(railResponse.request().postDataJSON()).toMatchObject({ commands: [{ kind: "reorder-top-level" }] })
+ const [channelSource, channelTarget] = await orderPair(nativeSource(channelAId, false), nativeSource(channelBId, false))
+ const channelAccepted = page.waitForResponse(response => response.request().method() === "PATCH" && new URL(response.url()).pathname === `/api/community/servers/${serverId}/channels/reorder`)
+ await channelSource.dragTo(channelTarget, { targetPosition: { x: 22, y: 4 } })
+ const channelResponse = await channelAccepted
+ expect(channelResponse.status()).toBe(200)
+ expect(channelResponse.request().postDataJSON()).toMatchObject({ channelIds: expect.arrayContaining([channelAId, channelBId]) })
+ await expect(page).toHaveURL(url => url.pathname === `/c/channels/${serverId}/${channelAId}`)
+ await navigateChannel(page, serverId, channelBId)
+ await navigateChannel(page, serverId, channelAId)
+ await expect(composer.getByRole("button", { name: "Remove file" })).toHaveCount(0)
+ await expect(composerEditable(page)).toHaveText("typing while image files prepare")
+ })
+
test("desktop restores scoped real image/file drafts, rebuilds URLs, and removal never revives", async ({ caseActors }) => {
test.setTimeout(120_000)
@@ -271,7 +558,7 @@ test.describe("same-tab Community attachment drafts", () => {
response.request().method() === "POST"
&& new URL(response.url()).pathname === `/api/community/channels/${channelAId}/messages`,
)
- await send.evaluate((button) => (button as HTMLButtonElement).click())
+ await send.click()
expect((await committed).status()).toBe(201)
await expect.poll(() => uploads).toBe(1)
await expect.poll(() => messages).toBe(1)