fertig-classic-games/tools/smokeMahjongMatchBgVideo.mjs

131 lines
5.7 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.

// Smoke test — Mahjong Match "Background: Image | Video" toggle.
// node tools/smokeMahjongMatchBgVideo.mjs [baseURL]
//
// Verifies:
// 1. The toggle appears on the layout-select screen and defaults to Video.
// 2. The selection persists in localStorage.
// 3. Video mode: a looping, muted backdrop video plays behind the tiles.
// 4. Image mode: the classic image backdrop is used (no video).
// Exits non-zero on any failure.
let chromium;
try { chromium = (await import('playwright')).chromium; }
catch { chromium = (await import('file:///home/brianfertig/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/index.mjs')).chromium; }
const BASE = process.argv[2] || 'http://localhost:8123';
let failures = 0;
function check(ok, msg) {
console.log(` ${ok ? '✓' : '✗'} ${msg}`);
if (!ok) failures++;
}
const browser = await chromium.launch({
headless: true,
executablePath: '/home/brianfertig/.cache/ms-playwright/chromium-1208/chrome-linux64/chrome',
args: ['--no-sandbox', '--disable-gpu'],
});
const page = await browser.newPage({ viewport: { width: 1280, height: 720 } });
const errors = [];
page.on('pageerror', (e) => errors.push('pageerror: ' + e.message));
await page.goto(BASE + '/', { waitUntil: 'load', timeout: 30000 });
await page.waitForFunction(
() => window.game && (window.game.isRunning === true || window.game.isRunning === 'running'),
{ timeout: 25000 },
);
// Boot the scene directly, then load its lazy assets (videos + images).
const r = await page.evaluate(async () => {
const { ensureGameAssets } = await import('/src/services/assetLoader.js');
window.game.scene.start('MahjongMatchGame', { game: { slug: 'mahjongmatch', name: 'Mahjong Match' } });
const scene = window.game.scene.getScene('MahjongMatchGame');
await ensureGameAssets(scene, 'mahjongmatch');
const videoKeys = ['bg-mm-video-01', 'bg-mm-video-02', 'bg-mm-video-03'].filter(
(k) => scene.cache.video?.exists(k));
return { videoKeys, imageKeys: ['bg-mm-01', 'bg-mm-02', 'bg-mm-03'].filter((k) => scene.textures.exists(k)) };
});
console.log('Assets:');
check(r.videoKeys.length === 3, `video assets loaded: ${r.videoKeys.join(', ')}`);
check(r.imageKeys.length === 3, `image assets loaded: ${r.imageKeys.join(', ')}`);
console.log('Toggle:');
const t = await page.evaluate(() => {
const scene = window.game.scene.getScene('MahjongMatchGame');
return {
exists: !!scene.bgToggle,
defaultMode: scene._bgMode(),
stored: localStorage.getItem('mahjongmatch-bg-mode'),
};
});
check(t.exists, 'toggle present on layout-select screen');
check(t.defaultMode === 'video', `default mode is Video (got ${t.defaultMode})`);
await page.evaluate(() => window.game.scene.getScene('MahjongMatchGame').bgToggle.set('image'));
const stored = await page.evaluate(() => localStorage.getItem('mahjongmatch-bg-mode'));
check(stored === 'image', 'Image selection persisted to localStorage');
// ── Video mode game ───────────────────────────────────────────────────────────
await page.evaluate(() => {
const scene = window.game.scene.getScene('MahjongMatchGame');
scene.bgToggle.set('video');
scene.startGame('pyramid');
});
console.log('Video background:');
const v = await page.evaluate(async () => {
const scene = window.game.scene.getScene('MahjongMatchGame');
const vid = scene.layer.list.find((o) => o.type === 'Video');
if (!vid) return null;
const el = vid.video;
// Headless loading is slow; wait for playback to actually start.
const deadline = Date.now() + 20000;
while (Date.now() < deadline && !(el.currentTime > 0 || el.readyState >= 2)) {
await new Promise((res) => setTimeout(res, 250));
}
return {
src: el?.currentSrc?.split('/').pop() ?? null,
playing: vid.isPlaying() && !el.paused,
muted: el.muted,
loop: el?.loop === true,
readyState: el?.readyState ?? 0,
currentTime: el?.currentTime ?? 0,
// The zoom regression: a fit computed against Phaser's 256×256 placeholder
// frame survives the real 864×480 frame arriving and renders 3× too big.
display: [vid.displayWidth, vid.displayHeight],
};
});
check(!!v, 'a backdrop video is present behind the tiles');
if (v) {
check(v.src !== null && /^mm-background-0\d\.mp4$/.test(v.src), `video is one of the three mahjong loops (${v.src})`);
check(v.playing && v.currentTime > 0, 'video is actually playing (currentTime > 0)');
check(v.muted && v.loop, 'video is muted and looping');
const [dw, dh] = v.display;
check(dw <= 1921 && dh <= 1081, `video fits the screen (display ${dw}×${dh}, max 1920×1080)`);
}
// ── Image mode game ───────────────────────────────────────────────────────────
await page.evaluate(() => {
const scene = window.game.scene.getScene('MahjongMatchGame');
scene.showLayoutSelect(); // back to the select screen (toggle recreated)
scene.bgToggle.set('image');
scene.startGame('garden');
});
await page.waitForTimeout(500);
console.log('Image background:');
const i = await page.evaluate(() => {
const scene = window.game.scene.getScene('MahjongMatchGame');
return {
video: scene.layer.list.some((o) => o.type === 'Video'),
image: scene.layer.list.some((o) => o.type === 'Image' && /^bg-mm-0\d$/.test(o.texture?.key)),
};
});
check(!i.video, 'no video backdrop in Image mode');
check(i.image, 'image backdrop (bg-mm-0x) present in Image mode');
if (errors.length) console.log(errors.join('\n'));
await browser.close();
if (failures || errors.length) { console.error(`FAILED (${failures})`); process.exit(1); }
console.log('OK');