Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): preserve mobile share image assets and brand font (#873)

Gustavo Ye4a21f14a63a8
diff --git a/src/web/src/lib/community/share-image-session.dom.test.ts b/src/web/src/lib/community/share-image-session.dom.test.ts
index f8d629f5e..9d4cbcbfb 100644
--- a/src/web/src/lib/community/share-image-session.dom.test.ts
+++ b/src/web/src/lib/community/share-image-session.dom.test.ts
@@ -526,6 +526,35 @@ describe("prepareShareImageSession", () => {
expect(document.querySelector("[data-share-detached-tree]")).toBeNull()
})
+ it("resolves inline font variables before the embedder collects font families", async () => {
+ const source = sourceCard('<span style="font-family:var(--font-body)">Message</span>')
+ const brand = source.querySelector<HTMLElement>("[data-share-brand]")!
+ brand.style.fontFamily = "var(--font-brand)"
+ const nativeComputedStyle = window.getComputedStyle.bind(window)
+ vi.spyOn(window, "getComputedStyle").mockImplementation((element, pseudo) => {
+ const computed = nativeComputedStyle(element, pseudo)
+ if (element instanceof HTMLElement && element.style.fontFamily.startsWith("var(")) {
+ return new Proxy(computed, {
+ get(target, key) {
+ if (key === "fontFamily") return element.hasAttribute("data-share-brand") ? "Brand" : "Body"
+ const value = Reflect.get(target, key)
+ return typeof value === "function" ? value.bind(target) : value
+ },
+ })
+ }
+ return computed
+ })
+ const getFontCSS = vi.fn(async (card: HTMLElement) => {
+ expect(card.querySelector<HTMLElement>("[data-share-brand]")!.style.fontFamily).toBe("Brand")
+ expect(card.querySelector<HTMLElement>("span")!.style.fontFamily).toBe("Body")
+ return FONT_CSS
+ })
+ await expect(prepareShareImageSession(source, { getFontCSS, waitForPaint: async () => undefined }))
+ .resolves.toMatchObject({ fontEmbedCSS: FONT_CSS })
+ expect(brand.style.fontFamily).toBe("var(--font-brand)")
+ expect(getFontCSS).toHaveBeenCalledTimes(1)
+ })
+
it("treats an empty font embed as a hard preparation failure", async () => {
const source = sourceCard("<span>hello</span>")
@@ -950,6 +979,62 @@ describe("prepareShareImageSession", () => {
})
describe("capturePreparedShareImage", () => {
+ const webkitUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148"
+
+ beforeEach(() => {
+ vi.spyOn(navigator, "userAgent", "get").mockReturnValue("AppleWebKit/537.36 Chrome/146.0 Safari/537.36")
+ })
+
+ it("returns the second WebKit rasterization after warming the same frozen resources", async () => {
+ vi.spyOn(navigator, "userAgent", "get").mockReturnValue(webkitUserAgent)
+ const preview = sourceCard('<img src="data:image/png;base64,AQID">')
+ const warmup = new Blob(["missing image"], { type: "image/png" })
+ const final = new Blob(["complete image"], { type: "image/png" })
+ const rasterize = vi.fn().mockResolvedValueOnce(warmup).mockResolvedValueOnce(final)
+
+ await expect(capturePreparedShareImage(preview, FONT_CSS, rasterize)).resolves.toBe(final)
+ expect(rasterize).toHaveBeenCalledTimes(2)
+ expect(rasterize.mock.calls[0]).toEqual(rasterize.mock.calls[1])
+ expect(document.querySelector("[data-share-detached-tree]")).toBeNull()
+ })
+
+ it("rejects an empty WebKit warmup without publishing or retrying it", async () => {
+ vi.spyOn(navigator, "userAgent", "get").mockReturnValue(webkitUserAgent)
+ const rasterize = vi.fn().mockResolvedValue(null)
+ await expect(capturePreparedShareImage(sourceCard("ready"), FONT_CSS, rasterize))
+ .rejects.toMatchObject({ stage: "rasterize", cause: { message: "Rasterizer returned no warmup image" } })
+ expect(rasterize).toHaveBeenCalledTimes(1)
+ expect(document.querySelector("[data-share-detached-tree]")).toBeNull()
+ })
+
+ it("does not start the final rasterization after cancelling WebKit warmup", async () => {
+ vi.spyOn(navigator, "userAgent", "get").mockReturnValue(webkitUserAgent)
+ const controller = new AbortController()
+ const warmup = deferred<Blob>()
+ const rasterize = vi.fn(() => warmup.promise)
+ const pending = capturePreparedShareImage(sourceCard("ready"), FONT_CSS, rasterize, { signal: controller.signal })
+ const assertion = expect(pending).rejects.toMatchObject({ name: "AbortError" })
+ await vi.waitFor(() => expect(rasterize).toHaveBeenCalledTimes(1))
+ controller.abort()
+ warmup.resolve(new Blob(["warmup"], { type: "image/png" }))
+ await assertion
+ await Promise.resolve()
+ expect(rasterize).toHaveBeenCalledTimes(1)
+ expect(document.querySelector("[data-share-detached-tree]")).toBeNull()
+ })
+
+ it("keeps WebKit warmup within the capture deadline", async () => {
+ vi.useFakeTimers()
+ vi.spyOn(navigator, "userAgent", "get").mockReturnValue(webkitUserAgent)
+ const rasterize = vi.fn(() => new Promise<Blob>(() => {}))
+ const pending = capturePreparedShareImage(sourceCard("ready"), FONT_CSS, rasterize, { timeoutMs: 25 })
+ const assertion = expect(pending).rejects.toMatchObject({ stage: "rasterize", timedOut: true })
+ await act(async () => vi.advanceTimersByTimeAsync(25))
+ await assertion
+ expect(rasterize).toHaveBeenCalledTimes(1)
+ expect(document.querySelector("[data-share-detached-tree]")).toBeNull()
+ })
+
it("rasterizes a detached byte-backed clone with the prepared font CSS", async () => {
const preview = sourceCard('<img src="data:image/png;base64,AQID">')
preview.removeAttribute("data-share-card-source")
diff --git a/src/web/src/lib/community/share-image-session.ts b/src/web/src/lib/community/share-image-session.ts
index b537b5f35..67304f7e2 100644
--- a/src/web/src/lib/community/share-image-session.ts
+++ b/src/web/src/lib/community/share-image-session.ts
@@ -680,6 +680,9 @@ async function loadAndEmbedFonts(
if (loadedFaces.length === 0) throw new Error("Brand font is unavailable")
await document.fonts.ready
throwIfAborted(signal)
+ for (const element of [card, ...card.querySelectorAll<HTMLElement>("*")]) {
+ element.style.fontFamily = getComputedStyle(element).fontFamily
+ }
const css = await getFontCSS(card)
if (!css.trim()) throw new Error("Share-card fonts could not be embedded")
return css
@@ -799,13 +802,19 @@ export async function capturePreparedShareImage(
element.style.setProperty("transition", "none", "important")
}
throwIfAborted(signal)
- const blob = await rasterize(card, {
+ const rasterizeOptions = {
pixelRatio: SHARE_IMAGE_PIXEL_RATIO,
backgroundColor: getComputedStyle(card).getPropertyValue("--card").trim() || undefined,
fontEmbedCSS,
includeQueryParams: true,
cacheBust: false,
- })
+ }
+ if (/AppleWebKit/.test(navigator.userAgent) && !/Chrome|Chromium|Android|Edg\/|OPR\//.test(navigator.userAgent)) {
+ const warmup = await rasterize(card, rasterizeOptions)
+ throwIfAborted(signal)
+ if (!warmup) throw new Error("Rasterizer returned no warmup image")
+ }
+ const blob = await rasterize(card, rasterizeOptions)
throwIfAborted(signal)
if (!blob) throw new Error("Rasterizer returned no image")
return blob
diff --git a/src/web/src/test/e2e-ui/51-share-image-assets.spec.ts b/src/web/src/test/e2e-ui/51-share-image-assets.spec.ts
index 7df9af400..12199f073 100644
--- a/src/web/src/test/e2e-ui/51-share-image-assets.spec.ts
+++ b/src/web/src/test/e2e-ui/51-share-image-assets.spec.ts
@@ -283,9 +283,20 @@ async function comparePreviewToCapture(
page: Page,
card: Locator,
captureIndex: number,
+ regionSelector?: string,
) {
+ const region = await card.evaluate((node, selector) => {
+ const cardRect = node.getBoundingClientRect()
+ const rect = selector ? node.querySelector(selector)!.getBoundingClientRect() : cardRect
+ return {
+ left: (rect.left - cardRect.left) / cardRect.width,
+ top: (rect.top - cardRect.top) / cardRect.height,
+ right: (rect.right - cardRect.left) / cardRect.width,
+ bottom: (rect.bottom - cardRect.top) / cardRect.height,
+ }
+ }, regionSelector)
const screenshot = await card.screenshot({ animations: "disabled" })
- return page.evaluate(async ({ previewBase64, index }) => {
+ return page.evaluate(async ({ previewBase64, index, region }) => {
const captured = (window as typeof window & {
__shareCaptures?: { clipboard: Blob[] }
}).__shareCaptures?.clipboard[index]
@@ -311,8 +322,8 @@ async function comparePreviewToCapture(
const exportPixels = exportContext.getImageData(0, 0, exported.width, exported.height).data
let difference = 0
let samples = 0
- for (let y = 0; y < exported.height; y += 4) {
- for (let x = 0; x < exported.width; x += 4) {
+ for (let y = Math.ceil(region.top * exported.height); y < region.bottom * exported.height; y += 1) {
+ for (let x = Math.ceil(region.left * exported.width); x < region.right * exported.width; x += 1) {
const offset = (y * exported.width + x) * 4
difference += Math.abs(previewPixels[offset]! - exportPixels[offset]!)
difference += Math.abs(previewPixels[offset + 1]! - exportPixels[offset + 1]!)
@@ -330,7 +341,7 @@ async function comparePreviewToCapture(
preview.close()
exported.close()
return result
- }, { previewBase64: screenshot.toString("base64"), index: captureIndex })
+ }, { previewBase64: screenshot.toString("base64"), index: captureIndex, region })
}
function sampleCenter(card: Locator, selector: string): Promise<SamplePoint> {
@@ -687,7 +698,8 @@ test("light and dark desktop and narrow previews match their frozen full PNG", a
const { page } = await asUser("alice")
const route = `/c/channels/${serverId}/${channelId}`
await gotoAfterUserWsAuth(page, route)
- const seeded = await seedMessage(page, channelId, "Frozen theme, font, logo, geometry, and full-card pixels")
+ const attachmentId = await uploadAttachment(page, channelId)
+ const seeded = await seedMessage(page, channelId, "Frozen theme, font, logo, geometry, and full-card pixels", [attachmentId])
await installShareCapture(page)
const observations: Array<{
@@ -733,6 +745,15 @@ test("light and dark desktop and narrow previews match their frozen full PNG", a
expect(Math.abs(comparison.exportWidth - comparison.previewWidth * 2)).toBeLessThanOrEqual(2)
expect(Math.abs(comparison.exportHeight - comparison.previewHeight * 2)).toBeLessThanOrEqual(2)
expect(comparison.meanChannelDifference).toBeLessThan(28)
+ const brandComparison = await comparePreviewToCapture(page, card, captureIndex, "[data-share-brand]")
+ expect(brandComparison.meanChannelDifference).toBeLessThan(20)
+ const point = await sampleCenter(card, `[data-testid="${tid.messageShareImage(seeded.id, 0)}"]`)
+ expect((await capturePixel(page, "clipboard", captureIndex, point)).pixel[2]).toBeGreaterThan(180)
+ const downloadStarted = page.waitForEvent("download")
+ await dialog.getByRole("button", { name: "Download" }).click()
+ await downloadStarted
+ expect((await capturePixel(page, "download", captureIndex, point)).pixel[2]).toBeGreaterThan(180)
+ expect(await captureDigest(page, "clipboard", captureIndex)).toBe(await captureDigest(page, "download", captureIndex))
observations.push({
scheme,
viewport,