Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions packages/studio/tests/e2e/preview-video-regression.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ async function open(beforeNavigate) {
return { page, iframe, frame };
}

// A sealed frame gets Studio's seeks as posted messages, which an in-frame seek can overtake. Studio's own message
// queue to the frame is ordered: once a message posted after them arrives, they have all been applied.
async function afterStudioMessages(host) {
await host.frame.evaluate((studio) => {
window.__regressionBarrier = new Promise((resolve) => {
const isBarrier = (event) =>
event.source === parent && event.data?.type === "preview-regression-barrier";
const onMessage = (event) => {
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
if (event.origin !== studio || !isBarrier(event)) return;
removeEventListener("message", onMessage);
resolve();
};
addEventListener("message", onMessage);
});
}, new URL(host.page.url()).origin);
const iframe = await host.frame.frameElement();
await iframe.evaluate((el) =>
el.contentWindow.postMessage({ type: "preview-regression-barrier" }, "*"),
);
await host.frame.evaluate(() => window.__regressionBarrier);
}

async function seek(host, time) {
const start = performance.now();
await host.frame.evaluate(async (time) => {
Expand Down Expand Up @@ -153,6 +175,18 @@ async function collectGarbage(host) {
await session.detach();
}

// The preview server's own CORS answer to `request`: a sealed frame plays its media only with that grant.
async function serverCorsFor(request) {
const origin = request.headers().origin;
if (!origin) return {};
const res = await fetch(request.url(), {
headers: { origin, "sec-fetch-dest": "video", range: "bytes=0-0" },
});
await res.body?.cancel();
const cors = /^(access-control-|vary$|x-content-type-options$)/;
return Object.fromEntries([...res.headers].filter(([header]) => cors.test(header)));
}

async function withBlockedNative(test) {
const assets = new Map(
await Promise.all(
Expand All @@ -168,17 +202,18 @@ async function withBlockedNative(test) {
const host = await open(async (page) => {
await page.setRequestInterception(true);
page.on("requestfailed", (request) => held.delete(request));
const respondPrefix = (request, name, data) => {
const respondPrefix = async (request, name, data) => {
if (prefixed.has(name)) {
held.add(request);
return;
}
prefixed.add(name);
const prefix = data.subarray(0, 16 * 1024);
void request.respond({
await request.respond({
status: 206,
contentType: "video/mp4",
headers: {
...(await serverCorsFor(request)),
"Content-Range": `bytes 0-${prefix.length - 1}/${data.length}`,
"Content-Length": String(prefix.length),
"Accept-Ranges": "bytes",
Expand All @@ -189,7 +224,8 @@ async function withBlockedNative(test) {
page.on("request", (request) => {
const name = new URL(request.url()).pathname.split("/").at(-1);
const data = assets.get(name);
if (request.resourceType() === "media" && data && blocked) respondPrefix(request, name, data);
if (request.resourceType() === "media" && data && blocked)
void respondPrefix(request, name, data);
else void request.continue();
});
});
Expand Down Expand Up @@ -399,6 +435,7 @@ try {
await picture(host, `drag-${fraction}`);
}
await host.page.mouse.up();
await afterStudioMessages(host);
await seek(host, 7);
await picture(host, "paused-b");
await seek(host, 5.5);
Expand Down
Loading