|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 3 | +import puppeteer, { type Browser } from "puppeteer-core"; |
| 4 | +import { resolveHeadlessShellPath } from "./browserManager.js"; |
| 5 | +import { applyVideoMetadataHints } from "./frameCapture.js"; |
| 6 | + |
| 7 | +const chromePath = resolveHeadlessShellPath(); |
| 8 | +const hasBrowser = chromePath !== undefined && spawnSync(chromePath, ["--version"]).status === 0; |
| 9 | + |
| 10 | +// Boxes Chrome gives these styles once a 640x360 video's metadata has loaded. |
| 11 | +const cases = { |
| 12 | + noSize: { style: "", box: "640x360" }, |
| 13 | + widthOnly: { style: "width:200px", box: "200x112.5" }, |
| 14 | + heightOnly: { style: "height:90px", box: "160x90" }, |
| 15 | + bothSizes: { style: "width:200px;height:300px;object-fit:cover", box: "200x300" }, |
| 16 | + maxWidthOnly: { style: "max-width:100px", box: "100x56.25" }, |
| 17 | + // A flex item may not shrink below its natural width, so its border overflows the 400px row. |
| 18 | + borderedFlexItem: { style: "width:100%;border:8px solid", box: "416x241" }, |
| 19 | +}; |
| 20 | +const ids = Object.keys(cases); |
| 21 | + |
| 22 | +describe.skipIf(!hasBrowser)("applyVideoMetadataHints in Chrome", () => { |
| 23 | + let browser: Browser; |
| 24 | + |
| 25 | + beforeAll(async () => { |
| 26 | + browser = await puppeteer.launch({ |
| 27 | + executablePath: chromePath, |
| 28 | + headless: true, |
| 29 | + args: ["--no-sandbox", "--disable-dev-shm-usage"], |
| 30 | + }); |
| 31 | + }); |
| 32 | + afterAll(async () => { |
| 33 | + await browser?.close(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("lays out a video without loaded metadata as it would with it", async () => { |
| 37 | + const page = await browser.newPage(); |
| 38 | + const videos = Object.entries(cases).map( |
| 39 | + ([id, { style }]) => |
| 40 | + `<div style="display:flex;align-items:center;width:400px"><video id="${id}" style="${style}"></video></div>`, |
| 41 | + ); |
| 42 | + await page.setContent(`<body style="margin:0">${videos.join("")}</body>`); |
| 43 | + |
| 44 | + await applyVideoMetadataHints( |
| 45 | + page, |
| 46 | + ids.map((id) => ({ id, width: 640, height: 360 })), |
| 47 | + ); |
| 48 | + |
| 49 | + const boxes = await page.evaluate((videoIds) => { |
| 50 | + return Object.fromEntries( |
| 51 | + videoIds.map((id) => { |
| 52 | + const rect = document.getElementById(id)!.getBoundingClientRect(); |
| 53 | + return [id, `${rect.width}x${rect.height}`]; |
| 54 | + }), |
| 55 | + ); |
| 56 | + }, ids); |
| 57 | + expect(boxes).toEqual( |
| 58 | + Object.fromEntries(Object.entries(cases).map(([id, { box }]) => [id, box])), |
| 59 | + ); |
| 60 | + await page.close(); |
| 61 | + }); |
| 62 | +}); |
0 commit comments