52 lines
2.6 KiB
JavaScript
Executable File
52 lines
2.6 KiB
JavaScript
Executable File
// Test the launch page without downloading or instantiating the WASM client.
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const vm = require('node:vm');
|
|
const html = fs.readFileSync('static/index.html', 'utf8');
|
|
assert.equal((html.match(/<!doctype html>/gi) || []).length, 1);
|
|
assert.equal((html.match(/\bid="launch"(?=[\s>])/g) || []).length, 0);
|
|
assert(!html.includes('LuantiArgs.fromQueryString'));
|
|
assert(html.includes('Play Luanti · eom.dev'));
|
|
assert(!html.includes('<button'), 'No separate launch button should be shown');
|
|
assert(!html.includes('<select'), 'No separate pre-game language screen');
|
|
assert(!html.includes('type="password"'), 'Player credentials must stay in the game, not a second web form');
|
|
const script = html.match(/<script>([\s\S]*?)<\/script>/)[1];
|
|
|
|
for (const protocol of ['http:', 'https:']) {
|
|
const elements = new Map();
|
|
for (const id of ['status', 'launch-style']) {
|
|
elements.set(id, {value: 'en', add() {}, handlers: {},
|
|
remove() { this.removed = true; },
|
|
addEventListener(event, fn) { this.handlers[event] = fn; }});
|
|
}
|
|
let launcher;
|
|
class Args {}
|
|
class Launcher {
|
|
constructor() { launcher = this; }
|
|
setProxy(value) { this.proxy = value; }
|
|
overrideConf(key, value) { this.override = [key, value]; }
|
|
setConf() {}
|
|
initialLang() { return Promise.resolve('en'); }
|
|
setLang(value) { this.language = value; }
|
|
launch(value) { this.args = value; this.launches = (this.launches || 0) + 1; }
|
|
}
|
|
vm.runInNewContext(script, {
|
|
document: {getElementById: (id) => elements.get(id), body: {}},
|
|
window: {location: {protocol,
|
|
href: `${protocol}//localhost:8080/?address=evil.example&go=1&server=1`}},
|
|
URL, Option: class {}, SUPPORTED_LANGUAGES: [['en', 'English']],
|
|
getDefaultLanguage: () => 'en', LuantiLauncher: Launcher, LuantiArgs: Args,
|
|
});
|
|
assert.equal(launcher.proxy, `${protocol === 'https:' ? 'wss:' : 'ws:'}//localhost:8080/proxy`);
|
|
assert.equal(launcher.override[0], 'main_menu_script');
|
|
assert.equal(launcher.override[1], '');
|
|
launcher.onready();
|
|
assert.equal(launcher.launches, 1, 'Launch automatically when ready');
|
|
launcher.onready();
|
|
assert.equal(launcher.launches, 1, 'Repeated readiness must not launch twice');
|
|
assert.equal(elements.get('launch-style').removed, true);
|
|
assert(launcher.args instanceof Args);
|
|
assert.deepEqual(Object.keys(launcher.args), []);
|
|
}
|
|
console.log('Launcher tests passed');
|