Files
HGhub/scripts/test-watch-experience.mjs
WEP-56 51c0cb640a feat: add startup and browsing preferences
Add configurable startup page and category columns, optional homepage autoplay, empty source selection, selective history removal, and persisted desktop window size. Fix legacy WebView poster proportions and translated playback controls. Update documentation and focused regression coverage. Refs #82, #79, #88.
2026-11-28 11:09:53 +08:00

170 lines
12 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from "node:assert/strict";
import { mkdir } from "node:fs/promises";
import { join } from "node:path";
// Real Rust plugin host and browser decoding: verifies the journey, not just CSS.
export async function testWatchExperience({ page, browser, dispatch, base, ui, root, report }) {
const output = join(root, "output/playwright/watch-experience");
await mkdir(output, { recursive: true });
const code = `export const meta={id:"fixture.experience",name:"光影放映室",version:"1.0.0",protocol:1,allowInsecureHttp:true,defaultHeaders:{"X-Fixture-Token":"local-fixture"}};
export default hg=>{
const item=(i)=>({id:"film-"+i,title:["城市的另一面","山海之间","有风的日子"][i],entryEpisodeId:"episode-1",cover:${JSON.stringify(base)}+"/cover.svg",type:"short",year:2026,score:9.4,tags:["都市","成长","治愈"],episodeCount:79,description:"在城市与山海之间,几个年轻人重新发现生活的方向。一段关于相遇与陪伴的故事,从此刻开始。"});
const episodes=Array.from({length:79},(_,i)=>({id:"episode-"+(i+1),index:i+1,label:"第"+(i+1)+"集",locked:i===78,lockReason:i===78?"暂不可用":undefined}));
return {feed:{list(){return {items:[item(0),item(1),item(2)],next:null};}},
search:{search(){return {items:[item(0)],next:null};}},
catalog:{getCategories(){return [{id:"short",label:"短剧"}];},list(){return {items:[item(0),item(1)],next:null};}},
home:{getSections(){return {items:[{id:"featured",title:"精选作品",layout:"row",items:[item(0),item(1)]}],next:null};}},
media:{getDetail({id}){return {...item(Number(id.split("-")[1])),playlists:[{id:"main",label:"正片",episodes}]};},resolvePlayback(){return {streams:[{url:${JSON.stringify(base)}+"/clip.mp4",format:"progressive",label:"原画"}]};}}};};`;
const imported = await dispatch({ action: "import", params: { code } });
assert(imported.ok, JSON.stringify(imported));
const errors = [];
page.on("pageerror", e => errors.push(e.message));
await page.evaluate(() => localStorage.setItem("hpp.preferences", JSON.stringify({ autoNext: false })));
await page.reload();
await page.waitForFunction(() => document.querySelector("video")?.currentTime > .2);
const shot = (p, name) => p.screenshot({ animations: "disabled", path: join(output, name + ".png") });
const button = (p, name) => p.getByRole("button", { name, exact: true });
const details = p => p.getByRole("dialog", { name: "作品详情" });
const start = async p => { await details(p).locator(".hg-watch-action").click(); await p.locator(".mode-full").waitFor(); await p.waitForFunction(() => document.querySelector(".mode-full video") && !document.querySelector(".mode-full video").paused); };
assert.equal(await page.locator(".desktop-controls").count(), 0, "preview has no full controller");
const canvas = await page.locator(".win-main").boundingBox();
for (const selector of [".hg-preview-footer", ".hg-preview-actions", ".hg-preview-heading"]) {
const r = await page.locator(selector).boundingBox();
assert(r.x >= canvas.x && r.y >= canvas.y && r.x+r.width <= canvas.x+canvas.width+1 && r.y+r.height <= canvas.y+canvas.height+1, selector+" remains in canvas");
}
await shot(page, "desktop-home");
const src = await page.locator("video").getAttribute("src");
await page.locator("video").click();
await page.waitForFunction(() => document.querySelector("video").paused);
await button(page, "作品详情").click();
await details(page).getByRole("button", { name: "第1集", exact: true }).waitFor();
await page.waitForFunction(() => document.querySelector("video").paused);
await button(details(page), "51–79").click();
assert(await button(details(page), "第79集").isDisabled());
await button(details(page), "1–50").click();
await shot(page, "desktop-detail");
await button(details(page), "关闭详情").click();
assert(await page.locator("video").evaluate(v => v.paused), "closing details preserves an intentional preview pause");
await button(page, "作品详情").click();
await start(page);
assert.equal(await page.locator("video").getAttribute("src"), src, "preview session survives entry to full playback");
assert.equal(await page.locator(".win-titlebar,.win-rail").count(), 0);
const full = await page.locator(".win-main").boundingBox();
assert.deepEqual(full, { x: 0, y: 0, width: 1180, height: 820 });
await page.locator("video").hover();
const shortcuts = page.locator(".desktop-control-row");
await page.evaluate(()=>window.__HPP_TEST_WINDOW_COMMANDS__.splice(0));
await page.locator(".hg-watch-title h1").click();
await page.locator(".hg-watch-title p").click();
assert.equal(await page.evaluate(()=>window.__HPP_TEST_WINDOW_COMMANDS__.filter(c=>c.endsWith("start_dragging")).length),2,"title and subtitle both drag exactly once");
for (const label of ["画中画", "锁定播放控制", "本地收藏", "取消本地收藏"]) { if(label.includes("收藏"))continue; assert(await button(shortcuts,label).isVisible()); }
await button(page,"窗口置顶").click(); await button(page,"取消窗口置顶").waitFor();
await button(page,"取消窗口置顶").click();
assert.equal(await page.evaluate(()=>window.__HPP_TEST_WINDOW_COMMANDS__.filter(c=>c.endsWith("start_dragging")).length),2,"window action buttons do not drag");
await shot(page, "desktop-playing");
await button(page, "更多").click();
await button(page, "画质与线路").click();
await button(page, "刷新播放").waitFor();
await page.keyboard.press("Escape");
assert.equal(await page.locator(".mode-full").count(), 1, "Escape closes menu first");
await page.locator("video").hover();
await button(page, "更多").click();
await shot(page, "desktop-more");
await button(page.locator(".hg-more-menu"), "锁定播放控制").click();
await button(page, "解锁播放控制").click();
await button(page, "选集").click();
await button(page, "第2集").click();
await page.waitForFunction(old => document.querySelector("video")?.getAttribute("src") !== old, src);
await page.keyboard.press("Escape");
await page.locator("video").hover();
await button(page, "全屏").click();
await page.waitForFunction(() => !!document.fullscreenElement);
await button(page, "退出全屏").click();
await button(page, "返回").click();
await page.locator(".mode-preview").waitFor();
assert.equal(await details(page).count(), 0, "exit returns directly to home");
await page.getByRole("navigation", { name: "主导航" }).getByRole("button", { name: "分类", exact: true }).click();
await page.locator(".legacy-row-card").first().click();
await details(page).waitFor();
assert.equal(await page.locator("video:visible").count(), 0, "category opens details without autoplay");
assert(await page.locator(".mode-preview video").evaluate(v => v.paused), "retained homepage video pauses while browsing");
assert(await page.locator(".hpp-category-preview").count() > 0, "origin remains mounted behind detail");
await start(page);
await button(page, "返回").click();
await page.locator(".hpp-category-preview").first().waitFor();
await page.getByRole("navigation", { name: "主导航" }).getByRole("button", { name: "搜索", exact: true }).click();
await page.getByRole("textbox", { name: "搜索所有插件" }).fill("城市");
await page.getByRole("textbox", { name: "搜索所有插件" }).press("Enter");
await page.locator(".legacy-search-poster").first().click();
await details(page).waitFor();
assert.equal(await page.locator("video:visible").count(), 0, "search opens details without autoplay");
await button(details(page), "本地收藏").click();
await button(details(page), "取消本地收藏").waitFor();
await button(details(page), "关闭详情").click();
await page.getByRole("navigation", { name: "主导航" }).locator(".win-account").click();
await button(page, "查看作品").first().click();
await details(page).waitFor();
assert.equal(await page.locator("video:visible").count(), 0, "favorites opens details");
await button(details(page), "关闭详情").click();
await page.getByRole("navigation", {name:"片库分栏"}).getByRole("button", {name:/^历史/}).click();
await button(page, "继续观看").first().click();
await details(page).waitFor();
assert.equal(await page.locator("video:visible").count(), 0, "history opens details");
await button(details(page), "关闭详情").click();
await page.getByRole("navigation",{name:"主导航"}).getByRole("button",{name:"设置",exact:true}).click();
await page.getByRole("button",{name:/代理访问/}).click();
const proxyDialog=page.getByRole("dialog",{name:"代理访问"});
await button(proxyDialog,"自定义").click();
await proxyDialog.getByLabel("代理地址",{exact:true}).fill("http://127.0.0.1:1");
await button(proxyDialog,"保存代理").click();
await page.getByRole("button",{name:/代理访问/}).click();
assert.equal(await proxyDialog.getByLabel("代理地址",{exact:true}).inputValue(),"http://127.0.0.1:1");
await button(proxyDialog,"直连").click();
await button(proxyDialog,"保存代理").click();
assert.equal((await dispatch({action:"proxy.get",params:{}})).value.mode,"off");
report.passed.push("Proxy UI saves/reloads custom address and switches to direct mode");
report.passed.push("Desktop: preview confined to canvas; detail gates category/search/favorites/history; same session handoff; full-window playback; menu/lock/episode/fullscreen; direct return restores shell");
const context = await browser.newContext({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true, userAgent: "Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 Chrome/130.0.0.0 Mobile Safari/537.36" });
const mobile = await context.newPage();
mobile.on("pageerror", e => errors.push(e.message));
await mobile.route("**/__hpp_test_ipc", async route => route.fulfill({ contentType: "application/json", body: JSON.stringify(await dispatch(JSON.parse(route.request().postData()))) }));
await mobile.addInitScript(() => { localStorage.setItem("hpp.preferences", JSON.stringify({ autoNext: false })); localStorage.setItem("hpp.theme", JSON.stringify("light")); });
await mobile.goto(ui + "tests/plugins.html");
await mobile.waitForFunction(() => document.querySelector("video")?.currentTime > .2);
await shot(mobile, "mobile-home");
await button(mobile, "作品详情").click();
await button(details(mobile), "第1集").waitFor();
await shot(mobile, "mobile-detail-light");
await start(mobile);
assert.equal(await mobile.locator(".hg-tabbar").count(), 0);
await shot(mobile, "mobile-playing");
await button(mobile, "更多").click();
await mobile.getByLabel("播放速度", { exact: true }).selectOption("1.5");
assert.equal(await mobile.locator("video").evaluate(v => v.playbackRate), 1.5);
await shot(mobile, "mobile-more");
await button(mobile, "关闭面板").click();
await button(mobile, "锁定播放控制").click();
await button(mobile, "解锁播放控制").click();
await button(mobile, "全屏").click();
await mobile.waitForFunction(()=>window.__HPP_TEST_WINDOW_COMMANDS__.includes("orientation:true"));
await mobile.setViewportSize({width:844,height:390});
await mobile.locator(".fs-immersive").waitFor();
await button(mobile, "退出全屏").click();
await mobile.waitForFunction(()=>window.__HPP_TEST_WINDOW_COMMANDS__.includes("orientation:false"));
await mobile.setViewportSize({width:390,height:844});
await mobile.locator(".hg-watch-heading").waitFor();
await button(mobile, "返回").click();
await mobile.locator(".hg-tabbar").waitFor();
await button(mobile.locator(".hg-tabbar"), "分类").click();
await mobile.locator(".legacy-row-card").first().click();
await details(mobile).waitFor();
assert.equal(await mobile.locator("video:visible").count(), 0);
assert(await mobile.locator(".mode-preview video").evaluate(v => v.paused));
await button(details(mobile), "关闭详情").click();
await mobile.locator(".hpp-category-preview").first().waitFor();
await context.close();
assert.deepEqual(errors, []);
report.passed.push("Mobile: home-only preview, themed bottom detail sheet, full playback hides navigation, speed/menu/lock, direct return and category detail gate");
}