tools/client/test_status.mjs (4104B)
1 import assert from 'node:assert/strict'; 2 import fs from 'node:fs/promises'; 3 import {createRequire} from 'node:module'; 4 5 const {chromium} = createRequire(import.meta.url)('playwright'); 6 const [url, output] = process.argv.slice(2); 7 await fs.mkdir(output, {recursive: true}); 8 9 const browser = await chromium.launch({executablePath: '/usr/bin/chromium', args: ['--no-sandbox', '--disable-dev-shm-usage']}); 10 try { 11 const page = await browser.newPage({viewport: {width: 1280, height: 900}}); 12 const errors = []; 13 const requests = []; 14 page.on('pageerror', error => errors.push(error.message)); 15 page.on('request', request => requests.push(request.url())); 16 const tile = page.waitForResponse(response => response.url().includes('/status/map-assets/') && response.url().endsWith('.png') && response.ok()); 17 18 await page.goto(url + 'status'); 19 await page.getByRole('heading', {name: '5 players online', exact: true}).waitFor(); 20 await tile; 21 await page.getByRole('button', {name: 'Alice (Old Athen)', exact: true}).click(); 22 await page.waitForFunction(() => { 23 const canvas = document.querySelector('canvas'); 24 const pixel = canvas.getContext('2d').getImageData(canvas.width / 2, canvas.height / 2, 1, 1).data; 25 return pixel[0] === 0 && pixel[1] === 207 && pixel[2] === 255; 26 }); 27 const map = page.locator('canvas'); 28 const box = await map.boundingBox(); 29 await map.click({position: {x: box.width / 2, y: box.height / 2}}); 30 await page.getByRole('button', {name: 'Bob (Clan Basic Shop)', exact: true}).click(); 31 await page.getByRole('button', {name: 'Carol (Private mission in Old Athen #65536)', exact: true}).click(); 32 await map.click({position: {x: box.width / 2, y: box.height / 2}}); 33 await page.screenshot({path: output + '/desktop.png', fullPage: true}); 34 await page.setViewportSize({width: 390, height: 844}); 35 await page.getByRole('button', {name: 'Alice (Old Athen)', exact: true}).click(); 36 await page.screenshot({path: output + '/mobile.png', fullPage: true}); 37 38 assert.equal(await page.getByRole('button', {name: 'NoMap (Playfield 99999)', exact: true}).isDisabled(), true); 39 assert.deepEqual(await page.locator('[data-status-roster] button').allTextContents(), [ 40 'Alice (Old Athen)', 'Bob (Clan Basic Shop)', 'Carol (Private mission in Old Athen #65536)', 41 'Dave (Private mission in Old Athen #65537)', 'NoMap (Playfield 99999)', 42 ]); 43 assert.equal(await page.locator('[data-map-location], [data-status-roster] h2').count(), 0); 44 assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true); 45 assert.equal(await page.locator('a[href*="admin"], form').count(), 0); 46 assert.equal(requests.some(request => request.includes('/admin/')), false); 47 assert.equal(page.url(), url + 'status'); 48 assert.deepEqual(errors, []); 49 50 // A changed feed must refresh both the roster and total without a navigation. 51 await page.route('**/status/map-players', route => route.fulfill({json: {players: []}})); 52 await page.getByRole('heading', {name: '0 players online', exact: true}).waitFor(); 53 await page.getByText('No players are connected right now.', {exact: true}).waitFor(); 54 assert.equal(await page.getByRole('button', {name: 'Alice (Old Athen)', exact: true}).count(), 0); 55 assert.deepEqual(errors, []); 56 57 // The shared renderer must keep the authenticated admin map working too. 58 const login = await page.request.post(url + 'admin/login', { 59 form: {username: 'Operator', password: 'status-test-password', password_confirm: 'status-test-password'}, 60 }); 61 assert(login.ok()); 62 await page.goto(url + 'admin/map'); 63 await page.waitForFunction(() => document.querySelector('[data-map-status]').textContent.includes('5 connected players')); 64 await page.getByLabel('Find zone').selectOption('540'); 65 await page.getByLabel('Offline players').check(); 66 const offline = await page.waitForResponse(response => response.url().includes('admin/map-players?offline=1')); 67 assert((await offline.json()).players.some(player => player.name === 'Offline')); 68 assert.deepEqual(errors, []); 69 } finally { 70 await browser.close(); 71 }