Files
ytplayer/tests/helpers/classic-fixture.js
2026-10-03 06:44:22 +08:00

57 lines
3.1 KiB
JavaScript

// Synthetic UI fixtures: exercise real rendering and navigation, not streaming.
async function openClassic(page, { populated = false, settings = {}, standalone = false } = {}) {
await page.route('**/api/**', route => route.fulfill({
contentType: 'application/json',
body: JSON.stringify({ ok: true, results: [], items: [], holders: [], data: null }),
}));
await page.route('**/fixture-cover.svg', route => route.fulfill({
contentType: 'image/svg+xml',
body: '<svg xmlns="http://www.w3.org/2000/svg" width="640" height="360" viewBox="0 0 640 360"><rect width="640" height="360" fill="#343b42"/><text x="42" y="150" fill="#f3f1ed" font-family="sans-serif" font-size="46">Studio session</text><text x="44" y="200" fill="#c3c4c7" font-family="sans-serif" font-size="20">Synthetic preview fixture</text></svg>',
}));
await page.addInitScript(({ populated, settings, standalone }) => {
if (standalone) {
const match = window.matchMedia.bind(window);
window.matchMedia = query => query.includes('display-mode: standalone')
? match(query.replace(/\(display-mode: standalone\)/g, '(min-width: 0px)')) : match(query);
}
const videos = Array.from({ length: 12 }, (_, i) => ({
id: 'preview' + String(i).padStart(4, '0'),
title: ['A quiet morning — live studio session', 'Late afternoon, piano and strings', 'An extended conversation about making music and finding a little room to listen'][i % 3],
channel: 'Preview fixtures', duration: 240 + i * 36, thumbnail: '/fixture-cover.svg',
}));
localStorage.setItem('_ytpdata', JSON.stringify({
settings: { layout: 'classic', reduceMotion: true, p2pShare: false, p2pReceive: false, ...settings },
playlists: populated ? [{ id: 'studio', name: 'Studio sessions', videos }] : [],
history: populated ? videos : [], queue: populated ? videos : [],
}));
}, { populated, settings, standalone });
await page.goto('/');
await page.waitForFunction(() => document.documentElement.dataset.layout === 'classic' && document.querySelectorAll('.smart-pl-list .playlist-item').length > 0);
await page.locator('#searchInput').blur();
}
async function showPlayingFixture(page) {
await page.evaluate(() => {
const videos = data.playlists[0].videos;
current = { meta: videos[0], qualities: [], audioUrl: '' };
queue = videos;
queueIndex = 0;
queueSource = 'playlist:studio';
view = { type: 'playlist', id: 'studio' };
document.getElementById('playerPane').classList.remove('empty');
document.getElementById('playerPlaceholder').classList.add('hidden');
document.getElementById('controls').classList.remove('hidden');
document.getElementById('nowPlayingMeta').classList.remove('hidden');
document.getElementById('npTitle').textContent = videos[0].title;
document.getElementById('npChannel').textContent = videos[0].channel;
document.getElementById('artFallback').classList.remove('hidden');
document.getElementById('artImg').src = '/fixture-cover.svg';
updateNowPlayingActions();
render();
renderUpNext();
document.querySelector('.body').scrollTop = 0;
});
}
module.exports = { openClassic, showPlayingFixture };