tests/e2e/fixture.mjs (4832B)
1 import assert from 'node:assert/strict'; 2 import {spawn, execFile} from 'node:child_process'; 3 import {createWriteStream} from 'node:fs'; 4 import fs from 'node:fs/promises'; 5 import {createRequire} from 'node:module'; 6 import os from 'node:os'; 7 import path from 'node:path'; 8 import {createInterface} from 'node:readline'; 9 import {fileURLToPath} from 'node:url'; 10 import {promisify} from 'node:util'; 11 import {GameTest} from '../../tools/client/game_test.mjs'; 12 13 const {chromium} = createRequire(import.meta.url)('playwright'); 14 const root = fileURLToPath(new URL('../../', import.meta.url)); 15 const binary = path.join(root, 'local/e2e-server'); 16 17 export async function buildServer() { 18 await promisify(execFile)('go', ['build', '-tags=e2e', '-o', binary, './server'], {cwd: root}); 19 } 20 21 class Fixture { 22 games = []; 23 starts = 0; 24 25 async start() { 26 const child = spawn(binary, {cwd: this.directory, stdio: ['ignore', 'pipe', 'pipe']}); 27 this.process = child; 28 this.exited = new Promise(resolve => child.once('exit', (code, signal) => resolve({code, signal}))); 29 child.stderr.pipe(createWriteStream(path.join(this.output, `server-${++this.starts}.log`))); 30 const lines = createInterface({input: child.stdout}); 31 try { 32 this.url = await new Promise((resolve, reject) => { 33 const timeout = setTimeout(() => reject(new Error('Test server startup timed out')), 60_000); 34 const finish = callback => value => { clearTimeout(timeout); callback(value); }; 35 child.once('error', finish(reject)); 36 this.exited.then(finish(result => reject(new Error(`Test server exited before ready: ${JSON.stringify(result)}`)))); 37 lines.once('line', finish(line => { 38 try { resolve(JSON.parse(line).url); } 39 catch (error) { reject(error); } 40 })); 41 }); 42 } finally { 43 lines.close(); 44 } 45 assert.match(this.url, /^http:\/\/127\.0\.0\.1:\d+$/); 46 } 47 48 async stop() { 49 if (!this.process?.pid) return; 50 if (this.process.exitCode === null && this.process.signalCode === null) this.process.kill('SIGTERM'); 51 const timeout = setTimeout(() => this.process.kill('SIGKILL'), 15_000); 52 try { 53 const result = await this.exited; 54 assert.deepEqual(result, {code: 0, signal: null}, 'The server must checkpoint and exit cleanly'); 55 } finally { 56 clearTimeout(timeout); 57 } 58 } 59 60 async restart() { 61 for (const context of this.browser.contexts()) await context.close(); 62 await this.stop(); 63 await this.start(); 64 } 65 66 async context() { 67 return this.browser.newContext({viewport: {width: 1024, height: 768}}); 68 } 69 70 async visitor() { 71 const context = await this.context(); 72 const response = await context.request.post(this.url + '/browser-launch'); 73 assert.equal(response.status(), 200); 74 const prepared = await context.request.post(this.url + '/_test/visitor'); 75 assert.equal(prepared.status(), 204, await prepared.text()); 76 return context; 77 } 78 79 async state(context) { 80 const response = await context.request.get(this.url + '/_test/player'); 81 assert.equal(response.status(), 200, await response.text()); 82 return response.json(); 83 } 84 85 async setup(context, operation, data) { 86 const response = await context.request.post(this.url + '/_test/player/' + operation, {data}); 87 assert.equal(response.status(), 200, await response.text()); 88 return response.json(); 89 } 90 91 async game(context) { 92 const page = await context.newPage(); 93 const game = await GameTest.attach(page); 94 this.games.push(game); 95 await page.goto(this.url); 96 await game.ready(); 97 return game; 98 } 99 100 async close() { 101 try { 102 for (let index = 0; index < this.games.length; ++index) { 103 const game = this.games[index]; 104 if (!game.page.isClosed()) await game.save(path.join(this.output, 'final-' + index)); 105 } 106 } finally { 107 try { if (this.browser) await this.browser.close(); } 108 finally { 109 try { await this.stop(); } 110 finally { await fs.rm(this.directory, {recursive: true, force: true}); } 111 } 112 } 113 } 114 } 115 116 export async function withFixture(name, run) { 117 const fixture = new Fixture(); 118 fixture.directory = await fs.mkdtemp(path.join(os.tmpdir(), 'ao-e2e-')); 119 fixture.output = path.join(root, 'local/e2e', Date.now() + '-' + name); 120 try { 121 await fs.mkdir(fixture.output, {recursive: true}); 122 await fs.symlink(path.join(root, 'server/assets'), path.join(fixture.directory, 'assets')); 123 await fixture.start(); 124 fixture.browser = await chromium.launch({executablePath: '/usr/bin/chromium', args: [ 125 '--enable-gpu', '--use-gl=angle', '--use-angle=gl', '--no-sandbox', '--disable-dev-shm-usage', 126 '--disable-background-timer-throttling', '--disable-renderer-backgrounding']}); 127 await run(fixture); 128 } finally { 129 await fixture.close(); 130 } 131 }