const fs = require('fs'); const path = require('path'); const vm = require('vm'); const ts = require('typescript'); const read = (...p) => fs.readFileSync(path.join(__dirname, '..', ...p), 'utf8'); const assert = (cond, msg) => { if (!cond) throw new Error(msg); }; // Pull one `const name = ... };` block out of a page and run it with the given stand-ins. const load = (src, name, sandbox) => { const block = src.match(new RegExp(`const ${name} = [\\s\\S]*?\\n \\};`)); assert(block, `${name} not found`); const js = ts.transpileModule(block[0], { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText; return vm.runInNewContext(`${js}\n${name}`, sandbox); }; // GameDetails manual download: the preselected platform must be one the select can show. const details = read('frontend', 'src', 'pages', 'GameDetails.tsx'); const availablePlatforms = [ { id: 1, name: 'PC (Windows)', folder: 'windows' }, { id: 20, name: 'Nintendo Switch', folder: 'switch' }, { id: 31, name: 'Xbox 360', folder: 'xbox360' }, ]; const preselect = (platformFolder, platformId) => { let selected = 'untouched'; const open = load(details, 'handleDownloadWithPlatform', { availablePlatforms, game: platformId === undefined ? null : { platformId }, setPendingDownload: () => {}, setSelectedPlatform: v => { selected = v; }, setShowPlatformModal: () => {}, }); open('magnet:?xt=x', 'torrent', 'Detected', platformFolder, 'Some.Release'); return selected; }; assert(preselect('switch', 31) === 'switch', 'a detected platform that is an option must be preselected'); for (const folder of ['unknown', 'mobile', 'psx', '', undefined]) { assert(preselect(folder, 31) === 'xbox360', `release folder ${JSON.stringify(folder)} is not an option, the game's own platform must be preselected`); assert(preselect(folder, 999) === '', `release folder ${JSON.stringify(folder)} with a game platform that is not an option must preselect nothing`); assert(preselect(folder, undefined) === '', 'no game loaded must preselect nothing'); } const modal = details.slice(details.indexOf('{/* Platform Selection Modal */}')); assert(modal.includes('