Co-op (QA'd with two to three real browsers, TCP-proxy chaos scenarios): - partner's tower changes are never dropped after a render stall or snapshot burst - half-open connections show "reconnecting" in ~6 s instead of 45-65 s - upgrade and next-wave are idempotent (stale commands refused), restart message, promoted host gets Play again, failed joins never kill a live game, listener leaks Audio / keyboard / a11y: - one Audio element per track, fade/stinger supersede tokens, play() retry on the next gesture, 5 s fallback to the synth, mute works everywhere, dialogs with role/aria-modal/focus handling, accessible names for sound/speed/pause New content: - enemies: Daemon (healer), Proxy (regenerating shield), Botnet (flyer swarm); waves weave them in from wave 9 (waves 1-8 unchanged) - towers: Sandbox (stun pulse), Load Balancer (fire-rate aura); hotkeys 1-9 - glyphs/shapes, stun/heal effects, gallery e2e that pixel-checks every kind Orbital Uplink rebalanced after playtesting (was affordable by wave ~6 and buyable in bulk from boss rewards): 1500 credits, upgrades 1200/2000/3200, and every Uplink already owned makes the next one 25% dearer to buy AND to upgrade (each tower remembers its multiplier, carried in the tower list as a 9th field). Shared TD.priceFor/upgradePrice so the UI shows exactly what the sim charges. Also: tablet/foldable viewports in the layout matrix (tolerant colour check), docs (README tower/enemy tables, PROTOCOL shield/stun/pm encodings, NEXT roadmap). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tb8pW2KAN4oFkh1aK1BfD8
397 lines
27 KiB
JavaScript
397 lines
27 KiB
JavaScript
'use strict';
|
||
/*
|
||
* Real audio, real files (public/assets/music via tracks.json), real Chromium. Every check reads the HTMLAudioElement itself
|
||
* (paused, volume, currentTime), not only NW.Audio.now: the old code said "playing" while the element had been paused.
|
||
*
|
||
* flow theme -> battle -> boss -> lose stinger -> Play again -> battle -> win stinger -> Keep going, with and without
|
||
* --autoplay-policy=no-user-gesture-required; one element and one download per track; boss and stingers preloaded (code-12, code-16)
|
||
* rapid five music() calls in one tick; M twice within 90 ms; M during the fade-in; Play again 100 ms after the end (code-7, code-16)
|
||
* mute M and the sound buttons work on the end screen and stop the stinger; state in the labels (code-13, code-27)
|
||
* late a stinger that loads late never plays over the next game (code-12)
|
||
* fallback a track that never answers or fails: synth within seconds, the file is used again later (code-15)
|
||
* gesture play() refused by the browser (iOS / Safari rules): retried at the next tap; strict "only inside a gesture" stub (code-14)
|
||
* coop Play again and Keep going start the music for BOTH players and stop the stinger (code-5)
|
||
*
|
||
* Headless Chromium resolves play() without a gesture whatever --autoplay-policy says, so code-14 is proven with a stub of the
|
||
* browser's rule, not with the flag.
|
||
*/
|
||
const { chromium } = require('playwright-core');
|
||
const { createServer } = require('../../server');
|
||
|
||
const assert = (c, m) => { if (!c) throw new Error('ASSERT: ' + m); console.log('ok -', m); };
|
||
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
||
async function until(fn, ms, what) { const t0 = Date.now(); for (;;) { try { const v = await fn(); if (v) return v; } catch (e) { /* retry */ } if (Date.now() - t0 > ms) throw new Error('TIMEOUT: ' + what); await sleep(40); } }
|
||
|
||
// Injected before the game's scripts: every Audio element is recorded, so a test can look at all of them.
|
||
function instrument() {
|
||
window.__els = [];
|
||
const A = window.Audio;
|
||
window.Audio = function (src) { const el = src === undefined ? new A() : new A(src); window.__els.push(el); return el; };
|
||
window.Audio.prototype = A.prototype;
|
||
const nm = (e) => ((e.src || '').split('/').pop() || '').replace(/\.mp3$/, '');
|
||
window.__info = () => window.__els.map((e) => ({ n: nm(e), playing: !e.paused && !e.ended, vol: +e.volume.toFixed(2), t: +e.currentTime.toFixed(2), rs: e.readyState, err: e.error ? e.error.code : 0 }));
|
||
try { localStorage.setItem('nw.helped', 'true'); } catch (e) { /* ignore */ } // (the first-run help is tested elsewhere)
|
||
}
|
||
// The browser's rule on iOS / Safari, as a stub: play() is only allowed inside the dispatch of a tap, click or key press.
|
||
function strictPolicy() {
|
||
window.__gesture = false; window.__refused = 0;
|
||
for (const ev of ['pointerdown', 'pointerup', 'keydown', 'click', 'touchend']) {
|
||
window.addEventListener(ev, (e) => {
|
||
if (ev === 'pointerdown' && e.pointerType !== 'mouse') return; // (HTML: only a mouse press activates the page; a finger counts when it lifts)
|
||
window.__gesture = true; setTimeout(() => { window.__gesture = false; }, 0);
|
||
}, true);
|
||
}
|
||
const P = HTMLMediaElement.prototype.play;
|
||
HTMLMediaElement.prototype.play = function () {
|
||
if (!window.__gesture) { window.__refused++; return Promise.reject(new DOMException('play() failed: not allowed', 'NotAllowedError')); }
|
||
return P.apply(this, arguments);
|
||
};
|
||
}
|
||
// The refusal is the test's to switch: "the user has not allowed it yet" / "now they have".
|
||
function denyPolicy() {
|
||
window.__deny = true; window.__refused = 0;
|
||
const P = HTMLMediaElement.prototype.play;
|
||
HTMLMediaElement.prototype.play = function () {
|
||
if (window.__deny) { window.__refused++; return Promise.reject(new DOMException('play() failed: not allowed', 'NotAllowedError')); }
|
||
return P.apply(this, arguments);
|
||
};
|
||
}
|
||
|
||
const FILES = { theme: 'menu', battle: 'battle', boss: 'boss', win: 'win', lose: 'lose' };
|
||
const elsOf = async (page, n) => (await page.evaluate(() => window.__info())).filter((e) => e.n === n);
|
||
const audible = async (page, n) => (await elsOf(page, n)).some((e) => e.playing && e.vol > 0.05);
|
||
const silent = async (page, n) => (await elsOf(page, n)).every((e) => !e.playing);
|
||
const nowOf = (page) => page.evaluate(() => { const n = NW.Audio.now; return n ? n.name + ':' + n.kind : null; });
|
||
const state = (page) => page.evaluate(() => NW.app.ctrl.view.state);
|
||
const endShown = (page) => page.evaluate(() => !document.getElementById('end').hidden);
|
||
async function advancing(page, n) { // the file really plays: its clock runs
|
||
const a = (await elsOf(page, n)).find((e) => e.playing); if (!a) return false;
|
||
await sleep(450);
|
||
const b = (await elsOf(page, n)).find((e) => e.playing);
|
||
return !!b && b.t > a.t + 0.2;
|
||
}
|
||
async function newPage(browser, errors, setup, opts) {
|
||
const ctx = await browser.newContext(Object.assign({ viewport: { width: 1100, height: 700 }, serviceWorkers: 'block' }, opts || {}));
|
||
await ctx.addInitScript(instrument);
|
||
if (setup) await ctx.addInitScript(setup);
|
||
const page = await ctx.newPage();
|
||
page.on('pageerror', (e) => errors.push('pageerror: ' + e.message));
|
||
page.on('console', (m) => { if (m.type() === 'error' && !/Failed to load resource|net::ERR/.test(m.text())) errors.push('console.error: ' + m.text()); });
|
||
return { ctx, page };
|
||
}
|
||
async function startSolo(page, url) {
|
||
await page.goto(url); await sleep(350);
|
||
await page.click('#btnSolo');
|
||
await until(() => page.evaluate(() => NW.app.inGame), 2000, 'in game');
|
||
}
|
||
const lose = (page) => page.evaluate(() => { NW.app.ctrl.sim.lives = 0; });
|
||
|
||
// ------------------------------------------------------------------------------------------------ flow
|
||
async function flow(browser, url, errors, tag) {
|
||
console.log('--- flow (' + tag + ')');
|
||
const { ctx, page } = await newPage(browser, errors);
|
||
try {
|
||
await page.goto(url); await sleep(350);
|
||
const manifest = await page.evaluate(() => fetch('assets/music/tracks.json').then((r) => r.json()));
|
||
for (const n of Object.keys(FILES)) assert(typeof manifest[n] === 'string', 'tracks.json names a real file for ' + n);
|
||
assert((await nowOf(page)) === null, 'nothing plays before the first gesture');
|
||
await page.mouse.click(8, 8);
|
||
await until(async () => (await nowOf(page)) === 'theme:file' && await audible(page, 'menu'), 4000, 'menu theme plays from the file after the first tap');
|
||
assert(await advancing(page, 'menu'), 'menu.mp3 clock runs');
|
||
|
||
await page.click('#btnSolo');
|
||
await until(async () => (await nowOf(page)) === 'battle:file' && await audible(page, 'battle'), 4000, 'battle music starts');
|
||
await until(() => silent(page, 'menu'), 3000, 'the menu theme is paused after the crossfade');
|
||
assert(await advancing(page, 'battle'), 'battle.mp3 clock runs');
|
||
// the stingers load in the background once the battle track plays: the end screen has them ready
|
||
await until(async () => (await elsOf(page, 'lose')).length === 1 && (await elsOf(page, 'win')).length === 1, 6000, 'win and lose are preloaded after the battle track starts');
|
||
await until(async () => (await elsOf(page, 'lose'))[0].rs >= 3, 6000, 'lose.mp3 is ready before it is needed');
|
||
|
||
// boss: wave 8 is the wave before it, so the panel announces wave 9 and the boss track is fetched ahead
|
||
await page.evaluate(() => { NW.app.ctrl.sim.wave = 8; });
|
||
await until(async () => (await elsOf(page, 'boss')).length === 1, 3000, 'boss.mp3 is requested a wave or two ahead');
|
||
assert(await silent(page, 'boss'), '... but not played');
|
||
await until(async () => (await elsOf(page, 'boss'))[0].rs >= 3, 8000, 'boss.mp3 is ready before the boss');
|
||
await page.evaluate(() => { const s = NW.app.ctrl.sim; s.wave = 10; s.spawnQ.push({ t: 0, k: 'rootkit', wave: 10 }); });
|
||
await until(async () => (await nowOf(page)) === 'boss:file' && await audible(page, 'boss'), 4000, 'boss music takes over when the rootkit appears');
|
||
await until(() => silent(page, 'battle'), 3000, 'battle is paused while the boss plays');
|
||
await page.evaluate(() => { const s = NW.app.ctrl.sim; s.monsters.length = 0; s.spawnQ.length = 0; });
|
||
await until(async () => (await nowOf(page)) === 'battle:file' && await audible(page, 'battle'), 4000, 'battle music is back when the boss is gone');
|
||
await until(() => silent(page, 'boss'), 3000, 'boss is paused again');
|
||
|
||
// lose: the music stops, the stinger plays
|
||
await lose(page);
|
||
await until(() => endShown(page), 4000, 'end screen');
|
||
await until(async () => (await nowOf(page)) === null && await audible(page, 'lose'), 3000, 'the lose stinger plays, the music is off');
|
||
await until(() => silent(page, 'battle'), 3000, 'battle is paused on the end screen');
|
||
await sleep(700);
|
||
await page.click('#btnAgain');
|
||
await until(async () => (await nowOf(page)) === 'battle:file' && await audible(page, 'battle'), 4000, 'Play again: battle music again');
|
||
await until(() => silent(page, 'lose'), 3000, 'the lose stinger stops with the new game');
|
||
assert(await advancing(page, 'battle'), 'battle clock runs after Play again');
|
||
|
||
// win and Keep going
|
||
await page.evaluate(() => { const s = NW.app.ctrl.sim; s.wave = 1; s.winWave = 1; });
|
||
await until(async () => (await state(page)) === 'won' && await audible(page, 'win'), 4000, 'the win stinger plays');
|
||
await until(() => silent(page, 'battle'), 3000, 'battle is paused on the win screen');
|
||
await sleep(700);
|
||
await page.click('#btnContinue');
|
||
await until(async () => (await nowOf(page)) === 'battle:file' && await audible(page, 'battle'), 4000, 'Keep going: battle music again');
|
||
await until(() => silent(page, 'win'), 3000, 'the win stinger stops');
|
||
|
||
const info = await page.evaluate(() => window.__info());
|
||
for (const n of ['menu', 'battle', 'boss', 'win', 'lose']) assert(info.filter((e) => e.n === n).length === 1, 'exactly one audio element for ' + n + ' in the whole session');
|
||
} finally { await ctx.close(); }
|
||
}
|
||
|
||
// ------------------------------------------------------------------------------------------------ rapid
|
||
async function rapid(browser, url, errors) {
|
||
console.log('--- rapid: calls faster than loads and fades');
|
||
const { ctx, page } = await newPage(browser, errors);
|
||
try {
|
||
await page.goto(url); await sleep(350);
|
||
await page.mouse.click(8, 8);
|
||
await until(async () => (await nowOf(page)) === 'theme:file', 4000, 'theme');
|
||
await page.evaluate(() => { for (const n of ['battle', 'theme', 'battle', 'theme', 'battle']) NW.Audio.music(n); });
|
||
await until(async () => (await nowOf(page)) === 'battle:file' && await audible(page, 'battle'), 4000, 'five music() calls in one tick end on battle');
|
||
await until(() => silent(page, 'menu'), 3000, 'and the theme is stopped');
|
||
const els = await page.evaluate(() => window.__info());
|
||
assert(els.filter((e) => e.n === 'battle').length === 1 && els.filter((e) => e.n === 'menu').length === 1, 'one element per track (battle ' + els.filter((e) => e.n === 'battle').length + ', menu ' + els.filter((e) => e.n === 'menu').length + ')');
|
||
// mute twice within 90 ms: net unmuted, the music must be playing and stay playing
|
||
await page.evaluate(() => { NW.Audio.music(null); NW.Audio.music('battle'); });
|
||
await page.click('#btnSolo'); await sleep(2500);
|
||
await page.keyboard.press('m'); await sleep(90); await page.keyboard.press('m');
|
||
await sleep(2500);
|
||
assert(!(await page.evaluate(() => NW.Audio.muted)), 'M, M: not muted');
|
||
assert(await audible(page, 'battle') && await advancing(page, 'battle'), 'M, M within 90 ms: the battle element still plays (a stale fade-out used to pause it)');
|
||
// mute during the fade-in, then unmute during the fade-out
|
||
await page.evaluate(() => { NW.Audio.music(null); }); await sleep(700);
|
||
await page.evaluate(() => NW.Audio.music('battle')); await sleep(250); // fading in
|
||
await page.keyboard.press('m');
|
||
await until(() => silent(page, 'battle'), 2000, 'M during the fade-in silences the music');
|
||
await sleep(1800);
|
||
assert(await silent(page, 'battle') && (await nowOf(page)) === null, 'and the fade-in does not bring it back');
|
||
await page.keyboard.press('m');
|
||
await until(async () => (await nowOf(page)) === 'battle:file' && await audible(page, 'battle'), 3000, 'unmute brings the music back');
|
||
await page.keyboard.press('m'); await sleep(80); await page.keyboard.press('m'); // mute, and unmute inside the fade-out
|
||
await sleep(2200);
|
||
assert(await audible(page, 'battle') && await advancing(page, 'battle'), 'mute undone during its own fade-out: still playing');
|
||
|
||
// Play again 100 ms after the end screen opens: inside the old fade-out of the music
|
||
await lose(page);
|
||
await until(() => endShown(page), 4000, 'end screen');
|
||
await sleep(100);
|
||
await page.click('#btnAgain');
|
||
await sleep(2500);
|
||
assert(await audible(page, 'battle') && await advancing(page, 'battle'), 'Play again 100 ms after the end screen: battle plays');
|
||
} finally { await ctx.close(); }
|
||
}
|
||
|
||
// ------------------------------------------------------------------------------------------------ mute
|
||
async function mute(browser, url, errors) {
|
||
console.log('--- mute: the end screen and the labels');
|
||
const { ctx, page } = await newPage(browser, errors);
|
||
try {
|
||
await startSolo(page, url);
|
||
await until(async () => await audible(page, 'battle'), 4000, 'battle plays');
|
||
assert((await page.getAttribute('#btnSound', 'aria-label')) === 'Mute sound', 'Sound button: "Mute sound" while the sound is on');
|
||
await page.click('#btnSpeed');
|
||
await until(async () => (await page.getAttribute('#btnSpeed', 'aria-label')) === 'Game speed 2×', 1500, 'the speed button says the speed: Game speed 2×');
|
||
await page.click('#btnSpeed'); await page.click('#btnSpeed');
|
||
await until(async () => (await page.getAttribute('#btnSpeed', 'aria-label')) === 'Game speed 1×', 1500, 'and back to 1×');
|
||
await page.click('#btnPause');
|
||
await until(async () => (await page.getAttribute('#btnPause', 'aria-pressed')) === 'true', 1500, 'Pause is pressed while paused');
|
||
assert((await page.getAttribute('#btnPause', 'aria-label')) === 'Pause', 'and keeps its name "Pause" (a toggle does not rename itself)');
|
||
await page.click('#btnPause');
|
||
await lose(page);
|
||
await until(() => endShown(page), 4000, 'end screen');
|
||
await until(async () => await audible(page, 'lose'), 3000, 'lose stinger plays');
|
||
const covered = await page.evaluate(() => { const b = document.getElementById('btnSound').getBoundingClientRect(); const e = document.elementFromPoint(b.left + b.width / 2, b.top + b.height / 2); return !!e && !!e.closest('#end'); });
|
||
assert(covered, 'the top-bar Sound button is behind the end dialog (so the dialog has its own)');
|
||
await page.keyboard.press('m');
|
||
assert(await page.evaluate(() => NW.Audio.muted), 'M on the end screen mutes');
|
||
await until(() => silent(page, 'lose'), 2000, 'and the stinger stops');
|
||
assert((await page.locator('#btnEndSound').count()) === 1 && (await page.isVisible('#btnEndSound')), 'the end dialog has its own sound button');
|
||
await until(async () => (await page.textContent('#btnEndSound')) === 'Sound: off', 1500, 'the end dialog button follows: Sound: off');
|
||
assert((await page.getAttribute('#btnSound', 'aria-label')) === 'Unmute sound', 'the top-bar button name is now "Unmute sound"');
|
||
await sleep(700);
|
||
await page.click('#btnAgain');
|
||
await sleep(2200);
|
||
assert((await nowOf(page)) === null && await silent(page, 'battle'), 'a new game while muted stays silent');
|
||
await lose(page);
|
||
await until(() => endShown(page), 4000, 'end screen again');
|
||
await sleep(900);
|
||
assert(await silent(page, 'lose'), 'no stinger while muted');
|
||
await page.click('#btnEndSound');
|
||
assert(!(await page.evaluate(() => NW.Audio.muted)), 'the end dialog button unmutes');
|
||
await until(async () => (await page.textContent('#btnEndSound')) === 'Sound: on', 1500, 'and shows it');
|
||
await sleep(700);
|
||
await page.click('#btnAgain');
|
||
await until(async () => await audible(page, 'battle'), 4000, 'unmuted: battle plays again');
|
||
// menu: M and the menu's own button
|
||
await page.click('#btnMenu');
|
||
await page.keyboard.press('m');
|
||
assert(await page.evaluate(() => NW.Audio.muted), 'M mutes in the menu');
|
||
await until(async () => (await page.textContent('#btnMenuSound')) === 'Sound: off', 1500, 'the menu button follows: Sound: off');
|
||
await page.click('#btnMenuSound');
|
||
assert(!(await page.evaluate(() => NW.Audio.muted)), 'the menu button unmutes');
|
||
} finally { await ctx.close(); }
|
||
}
|
||
|
||
// ------------------------------------------------------------------------------------------------ late stinger
|
||
async function late(browser, url, errors) {
|
||
console.log('--- late: a stinger that arrives after Play again');
|
||
const { ctx, page } = await newPage(browser, errors);
|
||
try {
|
||
await page.route('**/assets/music/lose.mp3', async (route) => { await sleep(2500); try { await route.continue(); } catch (e) { /* page closed */ } });
|
||
await startSolo(page, url);
|
||
await until(async () => await audible(page, 'battle'), 4000, 'battle plays');
|
||
await lose(page); // (before the stingers are preloaded: lose.mp3 is first requested now)
|
||
await until(() => endShown(page), 4000, 'end screen');
|
||
await sleep(700);
|
||
await page.click('#btnAgain');
|
||
const t0 = Date.now(); let bad = 0;
|
||
while (Date.now() - t0 < 6500) { // (the first 1.5 s: a stinger that is still loading has `paused` false but makes no sound, and a loaded one is fading out)
|
||
if (Date.now() - t0 > 1500 && (await elsOf(page, 'lose')).some((e) => e.playing && e.vol > 0.05 && e.rs >= 3)) bad++;
|
||
await sleep(100);
|
||
}
|
||
assert(bad === 0, 'the late lose stinger never played over the new game');
|
||
assert(await audible(page, 'battle'), 'the battle music plays');
|
||
} finally { await ctx.close(); }
|
||
}
|
||
|
||
// ------------------------------------------------------------------------------------------------ fallback
|
||
async function fallback(browser, url, errors) {
|
||
console.log('--- fallback: a track that never answers, and one that fails');
|
||
const { ctx, page } = await newPage(browser, errors);
|
||
try {
|
||
let held = null;
|
||
await page.route('**/assets/music/battle.mp3', (route) => { if (!held) held = route; else route.continue().catch(() => {}); }); // the first request hangs: no bytes, no error
|
||
await startSolo(page, url);
|
||
const t0 = Date.now();
|
||
await until(async () => (await nowOf(page)) === 'battle:synth', 9000, 'a file that never answers: the synth takes over');
|
||
const took = Date.now() - t0;
|
||
assert(took < 7500, 'within ' + took + ' ms (it was 10 to 35 s, or never)');
|
||
assert(await silent(page, 'battle'), 'the stalled element is not playing');
|
||
if (held) await held.continue().catch(() => {}); // the server answers at last
|
||
await until(async () => (await elsOf(page, 'battle')).some((e) => e.rs >= 3), 8000, 'the file arrives');
|
||
await page.evaluate(() => { NW.Audio.music('theme'); });
|
||
await sleep(300);
|
||
await page.evaluate(() => { NW.Audio.music('battle'); });
|
||
await until(async () => (await nowOf(page)) === 'battle:file' && await audible(page, 'battle'), 4000, 'the next time the track is wanted, the file plays');
|
||
assert((await elsOf(page, 'battle')).length === 1, 'still one element');
|
||
} finally { await ctx.close(); }
|
||
|
||
const b2 = await newPage(browser, errors);
|
||
try {
|
||
await b2.page.route('**/assets/music/battle.mp3', (route) => route.abort());
|
||
await startSolo(b2.page, url);
|
||
await until(async () => (await nowOf(b2.page)) === 'battle:synth', 3000, 'a file that fails: the synth takes over at once');
|
||
await b2.page.unroute('**/assets/music/battle.mp3');
|
||
await b2.page.evaluate(() => { if (NW.Audio.timing) NW.Audio.timing.retry = 500; });
|
||
await sleep(800);
|
||
await b2.page.evaluate(() => { NW.Audio.music('theme'); });
|
||
await sleep(300);
|
||
await b2.page.evaluate(() => { NW.Audio.music('battle'); });
|
||
await until(async () => (await nowOf(b2.page)) === 'battle:file' && await audible(b2.page, 'battle'), 5000, 'after the server is back the file is tried again (it used to stay synth for the whole session)');
|
||
} finally { await b2.ctx.close(); }
|
||
}
|
||
|
||
// ------------------------------------------------------------------------------------------------ gesture
|
||
async function gesture(browser, url, errors) {
|
||
console.log('--- gesture: play() refused until the user allows it (code-14)');
|
||
let T = await newPage(browser, errors, denyPolicy);
|
||
try {
|
||
const page = T.page;
|
||
await page.goto(url); await sleep(350);
|
||
await page.mouse.click(8, 8);
|
||
await sleep(900);
|
||
assert((await nowOf(page)) === null && (await page.evaluate(() => window.__refused)) >= 1, 'the first tap: play() was refused, nothing plays (refused ' + (await page.evaluate(() => window.__refused)) + 'x)');
|
||
await page.evaluate(() => { window.__deny = false; });
|
||
await page.mouse.click(8, 8);
|
||
await until(async () => (await nowOf(page)) === 'theme:file' && await audible(page, 'menu'), 3000, 'the next tap retries it: the theme plays');
|
||
assert((await elsOf(page, 'menu')).length === 1, 'on the same element');
|
||
} finally { await T.ctx.close(); }
|
||
|
||
console.log('--- gesture: play() only inside a tap (the strict stub)');
|
||
T = await newPage(browser, errors, strictPolicy);
|
||
try {
|
||
const page = T.page;
|
||
await page.goto(url); await sleep(500);
|
||
await page.mouse.click(8, 8);
|
||
await until(async () => (await nowOf(page)) === 'theme:file' && await audible(page, 'menu'), 3000, 'the first tap starts the theme: play() is called inside the tap');
|
||
await page.click('#btnSolo');
|
||
await until(async () => (await nowOf(page)) === 'battle:file' && await audible(page, 'battle'), 3000, 'Play solo starts the battle music inside the tap');
|
||
// the boss track is started by the game loop, outside any tap: refused, and waiting for the next one
|
||
await page.evaluate(() => { const s = NW.app.ctrl.sim; s.wave = 10; s.spawnQ.push({ t: 0, k: 'rootkit', wave: 10 }); });
|
||
await until(async () => (await page.evaluate(() => NW.Audio.blocked === true)) || (await nowOf(page)) === 'boss:file', 4000, 'the boss track is either refused or plays');
|
||
await page.mouse.click(300, 300);
|
||
await until(async () => (await nowOf(page)) === 'boss:file' && await audible(page, 'boss'), 3000, 'the next tap on the board starts it');
|
||
} finally { await T.ctx.close(); }
|
||
|
||
console.log('--- gesture: a finger (pointerdown is no gesture, the lift is)');
|
||
T = await newPage(browser, errors, strictPolicy, { viewport: { width: 390, height: 844 }, deviceScaleFactor: 2, isMobile: true, hasTouch: true });
|
||
try {
|
||
const page = T.page;
|
||
await page.goto(url); await sleep(500);
|
||
await page.touchscreen.tap(195, 30);
|
||
await until(async () => (await nowOf(page)) === 'theme:file' && await audible(page, 'menu'), 3000, 'the first tap of a finger starts the theme (the press was refused, the lift retried it)');
|
||
assert((await elsOf(page, 'menu')).length === 1, 'on one element');
|
||
await page.tap('#btnSolo');
|
||
await until(async () => (await nowOf(page)) === 'battle:file' && await audible(page, 'battle'), 3000, 'Play solo by finger starts the battle music');
|
||
} finally { await T.ctx.close(); }
|
||
}
|
||
|
||
// ------------------------------------------------------------------------------------------------ coop
|
||
async function coop(browser, errors) {
|
||
console.log('--- coop: Play again and Keep going start the music for both players');
|
||
const srv = await createServer({ port: 0, host: '127.0.0.1', log: () => {}, pingMs: 0 }), url = 'http://127.0.0.1:' + srv.port + '/';
|
||
const A = await newPage(browser, errors), B = await newPage(browser, errors);
|
||
try {
|
||
const joined = (P) => P.page.evaluate(() => NW.app.ctrl.kind === 'remote' && NW.app.ctrl.status === 'joined');
|
||
await A.page.goto(url); await sleep(300);
|
||
await A.page.click('#btnCoop'); await A.page.fill('#inName', 'Alice'); await A.page.click('#btnCreate');
|
||
await until(() => joined(A), 6000, 'host joined');
|
||
const room = await A.page.evaluate(() => NW.app.ctrl.room);
|
||
await B.page.goto(url + '?room=' + room); await sleep(300);
|
||
await B.page.fill('#inName', 'Bob'); await B.page.click('#btnJoin');
|
||
await until(() => joined(B), 6000, 'guest joined');
|
||
const R = srv.rooms.get(room);
|
||
const both = async (fn) => (await fn(A.page)) && (await fn(B.page));
|
||
await until(() => both(async (p) => (await nowOf(p)) === 'battle:file' && await audible(p, 'battle')), 5000, 'both hear the battle music');
|
||
|
||
R.sim.lives = 0;
|
||
await until(() => both(endShown), 5000, 'both see the end screen');
|
||
await until(() => both(async (p) => (await nowOf(p)) === null && await audible(p, 'lose')), 4000, 'both: stinger on, music off');
|
||
await sleep(700);
|
||
await A.page.click('#btnAgain');
|
||
await until(() => both(async (p) => (await state(p)) === 'playing' && !(await endShown(p))), 4000, 'new game on both');
|
||
await until(() => both(async (p) => (await nowOf(p)) === 'battle:file' && await audible(p, 'battle')), 4000, 'Play again: the battle music plays on the host AND on the guest');
|
||
await until(() => both((p) => silent(p, 'lose')), 3000, 'and the lose stinger stopped on both');
|
||
|
||
R.sim.wave = 1; R.sim.winWave = 1;
|
||
await until(() => both(async (p) => (await state(p)) === 'won' && await audible(p, 'win')), 5000, 'both: win stinger');
|
||
await sleep(700);
|
||
await A.page.click('#btnContinue');
|
||
await until(() => both(async (p) => (await state(p)) === 'playing' && !(await endShown(p))), 4000, 'the game goes on for both');
|
||
await until(() => both(async (p) => (await nowOf(p)) === 'battle:file' && await audible(p, 'battle')), 4000, 'Keep going: the battle music plays on the host AND on the guest');
|
||
await until(() => both((p) => silent(p, 'win')), 3000, 'and the win stinger stopped on both');
|
||
} finally { await A.ctx.close(); await B.ctx.close(); await srv.close(); }
|
||
}
|
||
|
||
module.exports = async function ({ url, out }) {
|
||
const errors = [];
|
||
const run = async (flag, fn) => { const b = await chromium.launch({ args: ['--no-sandbox'].concat(flag ? [flag] : []) }); try { await fn(b); } finally { await b.close(); } };
|
||
await run('--autoplay-policy=no-user-gesture-required', (b) => flow(b, url, errors, 'no-user-gesture-required'));
|
||
await run('--autoplay-policy=document-user-activation-required', (b) => flow(b, url, errors, 'document-user-activation-required'));
|
||
await run(null, async (b) => {
|
||
await rapid(b, url, errors);
|
||
await mute(b, url, errors);
|
||
await late(b, url, errors);
|
||
await fallback(b, url, errors);
|
||
await gesture(b, url, errors);
|
||
await coop(b, errors);
|
||
});
|
||
if (errors.length) throw new Error('console errors:\n' + errors.join('\n'));
|
||
console.log('no console errors');
|
||
};
|