aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/test_admin.mjs (16649B)


      1 import assert from 'node:assert/strict';
      2 import fs from 'node:fs/promises';
      3 import {createRequire} from 'node:module';
      4 import {GameTest} from './game_test.mjs';
      5 
      6 const {chromium} = createRequire(import.meta.url)('playwright');
      7 const [url, output] = process.argv.slice(2);
      8 await fs.mkdir(output, {recursive: true});
      9 const browser = await chromium.launch({executablePath: '/usr/bin/chromium',
     10   args: ['--no-sandbox', '--disable-dev-shm-usage', '--enable-gpu', '--use-gl=angle', '--use-angle=gl',
     11     '--disable-background-timer-throttling', '--disable-renderer-backgrounding']});
     12 let game;
     13 try {
     14   const context = await browser.newContext({viewport: {width: 1280, height: 900}, hasTouch: true});
     15   // Password management requires an adopted player and its rotated browser token.
     16   await context.addCookies([{name: 'ao_client', value: 'admin-browser', url}]);
     17   const claimed = await context.request.post(url + 'browser-account/claim', {form: {name: 'Adminbrowser', password: 'old-player-password'}});
     18   assert.equal(claimed.status(), 204, await claimed.text());
     19   const page = await context.newPage();
     20   const profileRequests = [];
     21   context.on('request', request => {
     22     if (request.url().includes('/browser-profile/')) profileRequests.push(request.url());
     23   });
     24   const errors = [];
     25   page.on('pageerror', error => errors.push(error.message));
     26 
     27   await page.goto(url + 'admin/');
     28   await page.getByLabel('Username').fill('Operator');
     29   await page.getByLabel('Password', {exact: true}).fill('long-test-password');
     30   await page.getByLabel('Confirm password', {exact: true}).fill('different-password');
     31   await page.getByRole('button', {name: 'Create administrator'}).click();
     32   await page.getByRole('alert').filter({hasText: 'Passwords do not match.'}).waitFor();
     33   await page.screenshot({path: output + '/setup-confirmation.png'});
     34   await page.getByLabel('Password', {exact: true}).fill('long-test-password');
     35   await page.getByLabel('Confirm password', {exact: true}).fill('long-test-password');
     36   await page.getByRole('button', {name: 'Create administrator'}).click();
     37   await page.getByRole('heading', {name: 'General', exact: true}).waitFor();
     38   await page.waitForFunction(() => /^\d/.test(document.querySelector('#query-rate').textContent));
     39   await page.screenshot({path: output + '/general.png'});
     40 
     41   // Manage accounts through the table and separate forms.
     42   await page.getByRole('link', {name: 'Admins', exact: true}).click();
     43   assert.equal(await page.locator('input[type=password]').count(), 0);
     44   await page.getByRole('link', {name: 'Create admin', exact: true}).click();
     45   await page.getByLabel('Username', {exact: true}).fill('Reviewer');
     46   await page.getByLabel('Password', {exact: true}).fill('valid');
     47   await page.getByRole('button', {name: 'Create admin', exact: true}).click();
     48   await page.getByRole('row').filter({hasText: 'Reviewer'}).getByRole('link', {name: 'Change password'}).click();
     49   await page.getByRole('heading', {name: 'Change password', exact: true}).waitFor();
     50   await page.getByLabel('New password', {exact: true}).fill('other');
     51   await page.getByRole('button', {name: 'Save password', exact: true}).click();
     52   await page.getByRole('status').filter({hasText: 'Password saved.'}).waitFor();
     53   await page.screenshot({path: output + '/admins.png'});
     54   await page.getByRole('button', {name: 'Log out', exact: true}).click();
     55   await page.getByLabel('Username').fill('Reviewer');
     56   await page.getByLabel('Password', {exact: true}).fill('wrong');
     57   await page.getByRole('button', {name: 'Log in', exact: true}).click();
     58   await page.getByRole('alert').filter({hasText: 'Incorrect username or password'}).waitFor();
     59   assert.equal(await page.locator('section.login').count(), 1);
     60   assert.equal(await page.getByLabel('Username').inputValue(), 'Reviewer');
     61   await page.getByLabel('Password', {exact: true}).fill('other');
     62   await page.getByRole('button', {name: 'Log in', exact: true}).click();
     63   await page.getByRole('heading', {name: 'General', exact: true}).waitFor();
     64   await page.getByRole('link', {name: 'Players', exact: true}).click();
     65   assert.equal(await page.getByRole('link', {name: 'Adminbrowser', exact: true}).count(), 1);
     66   assert.equal(await page.getByText('Create user', {exact: true}).count(), 0);
     67   await page.getByRole('link', {name: 'Adminbrowser', exact: true}).click();
     68   assert(page.url().endsWith('/admin/players/1'));
     69   assert.equal(await page.locator('canvas').count(), 0);
     70   assert((await page.getByRole('heading', {name: 'Character data'}).boundingBox()).y < 900);
     71   // Save ordinary controls, then verify the returned form at desktop and phone widths.
     72   assert.equal(await page.locator('textarea, .player-editor details').count(), 0);
     73   assert.equal(await page.getByRole('combobox', {name: 'Profession', exact: true}).locator('option:checked').textContent(), 'Soldier (1)');
     74   assert.equal(await page.getByRole('combobox', {name: 'Breed', exact: true}).locator('option:checked').textContent(), 'Solitus (1)');
     75   await page.getByLabel('Title', {exact: true}).fill('Form & field check');
     76   await page.getByRole('button', {name: 'Save', exact: true}).click();
     77   assert.equal(await page.getByLabel('Title', {exact: true}).inputValue(), 'Form & field check');
     78   await page.getByRole('combobox', {name: 'Profession', exact: true}).selectOption('1');
     79   await page.getByRole('combobox', {name: 'Breed', exact: true}).selectOption('1');
     80   await page.getByLabel('Rifle', {exact: true}).fill('42');
     81   await page.getByRole('button', {name: 'Save', exact: true}).click();
     82   assert.equal(await page.getByLabel('Rifle', {exact: true}).inputValue(), '42');
     83   await page.screenshot({path: output + '/player-form.png', fullPage: true});
     84   await page.setViewportSize({width: 390, height: 844});
     85   await page.getByRole('heading', {name: 'Character data'}).scrollIntoViewIfNeeded();
     86   assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true);
     87   await page.screenshot({path: output + '/mobile-player-form.png'});
     88   await page.setViewportSize({width: 1280, height: 900});
     89   await page.getByRole('link', {name: 'Teleport', exact: true}).click();
     90   assert(page.url().endsWith('/admin/players/1/teleport'));
     91   await page.waitForFunction(() => document.querySelector('[data-map-status]').textContent.includes('connected players'));
     92   await page.setViewportSize({width: 390, height: 844});
     93   const teleportMap = page.locator('canvas');
     94   const teleportBox = await teleportMap.boundingBox();
     95   const teleportButton = await page.getByRole('button', {name: 'Teleport here', exact: true}).boundingBox();
     96   assert(teleportButton.y + teleportButton.height <= teleportBox.y);
     97   assert(teleportBox.y + teleportBox.height <= 844, JSON.stringify({teleportBox, teleportButton}));
     98   assert.equal(await teleportMap.evaluate(element => element.nextElementSibling), null);
     99   await page.screenshot({path: output + '/mobile-teleport.png'});
    100   await page.setViewportSize({width: 1280, height: 900});
    101   await page.screenshot({path: output + '/teleport.png'});
    102   await page.getByLabel("Warp zones", {exact: true}).uncheck();
    103   const desktopTeleport = await teleportMap.boundingBox();
    104   await teleportMap.click({position: {x: desktopTeleport.width / 2 + 15, y: desktopTeleport.height / 2}});
    105   const destination = page.locator('[data-map-zone-choice]');
    106   if (await destination.isVisible()) await page.locator('[data-map-teleport] select').selectOption('705');
    107   assert.equal(await page.getByRole('button', {name: 'Teleport here', exact: true}).isEnabled(), true, await page.locator("[data-map-location]").textContent());
    108   assert(Number(await page.locator('[data-map-teleport] input[name=x]').inputValue()) > 960);
    109   await page.getByRole('link', {name: 'Back to player', exact: true}).click();
    110   await page.getByRole('link', {name: 'Change password', exact: true}).click();
    111   await page.getByLabel('New password', {exact: true}).fill('valid');
    112   await page.getByRole('button', {name: 'Save password', exact: true}).click();
    113   await page.getByRole('status').filter({hasText: 'Password changed.'}).waitFor();
    114   await page.getByRole('link', {name: 'SQL', exact: true}).click();
    115   await page.getByLabel('Query', {exact: true}).fill("CREATE TABLE admin_browser_test (value TEXT); INSERT INTO admin_browser_test VALUES ('a,b'); SELECT * FROM admin_browser_test");
    116   await page.getByRole('button', {name: 'Run query'}).click();
    117   await page.getByRole('cell', {name: 'a,b', exact: true}).waitFor();
    118   await page.getByLabel('Output', {exact: true}).selectOption('csv');
    119   assert.equal(await page.getByLabel('CSV output').textContent(), 'value\n"a,b"\n');
    120   await page.getByLabel('Output', {exact: true}).selectOption('table');
    121 
    122   // Large results extend the document in both output formats.
    123   await page.getByLabel('Query', {exact: true}).fill('WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM n WHERE x<100) SELECT x FROM n');
    124   await page.getByRole('button', {name: 'Run query'}).click();
    125   assert.equal(await page.locator('#result-table tbody tr').count(), 100);
    126   assert(await page.locator('#result-table').evaluate(element => element.clientHeight > innerHeight && element.scrollHeight <= element.clientHeight + 1));
    127   await page.getByLabel('Output', {exact: true}).selectOption('csv');
    128   assert(await page.getByLabel('CSV output').evaluate(element => element.clientHeight > innerHeight && element.scrollHeight <= element.clientHeight + 1));
    129   await page.getByLabel('Output', {exact: true}).selectOption('table');
    130   await page.screenshot({path: output + '/sql.png'});
    131   await page.getByRole('link', {name: 'Map', exact: true}).click();
    132   await page.waitForFunction(() => document.querySelector('[data-map-status]').textContent.includes('connected players'));
    133   assert.equal(await page.getByLabel('Offline players', {exact: true}).isChecked(), false);
    134   assert.equal(await page.getByLabel('NPCs', {exact: true}).isChecked(), false);
    135   assert.equal(await page.getByLabel('Warp zones', {exact: true}).isChecked(), true);
    136   const mapResponses = [];
    137   page.on('response', async response => {
    138     if (response.url().includes('admin/map-entities?') && response.ok()) mapResponses.push(await response.json());
    139   });
    140   await page.getByLabel('NPCs', {exact: true}).check();
    141   await page.locator('[data-map-find]').selectOption('705');
    142   for (let i = 0; i < 5; i++) await page.getByRole('button', {name: 'Zoom in', exact: true}).click();
    143   await page.waitForResponse(response => response.url().includes('admin/map-entities?') && response.ok());
    144   const before = await page.locator('canvas').boundingBox();
    145   await page.screenshot({path: output + '/map.png'});
    146   await page.setViewportSize({width: 390, height: 844});
    147   const canvas = page.locator('canvas');
    148   const box = await canvas.boundingBox();
    149   assert(box.width < before.width && box.width > 300);
    150   assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true);
    151   const session = await context.newCDPSession(page);
    152   const x = box.x + box.width / 2, y = box.y + box.height / 2;
    153   const originalView = new URL((await page.waitForRequest(request => request.url().includes('admin/map-entities?'))).url()).searchParams;
    154   await session.send('Input.dispatchTouchEvent', {type: 'touchStart', touchPoints: [{x: x - 30, y}, {x: x + 30, y}]});
    155   await session.send('Input.dispatchTouchEvent', {type: 'touchMove', touchPoints: [{x: x - 70, y}, {x: x + 70, y}]});
    156   await session.send('Input.dispatchTouchEvent', {type: 'touchEnd', touchPoints: []});
    157   await session.send('Input.dispatchTouchEvent', {type: 'touchStart', touchPoints: [{x, y}]});
    158   await session.send('Input.dispatchTouchEvent', {type: 'touchMove', touchPoints: [{x: x + 35, y: y + 20}]});
    159   await session.send('Input.dispatchTouchEvent', {type: 'touchEnd', touchPoints: []});
    160   const touchedView = new URL((await page.waitForRequest(request => request.url().includes('admin/map-entities?'))).url()).searchParams;
    161   assert(Number(touchedView.get('right')) - Number(touchedView.get('left')) < Number(originalView.get('right')) - Number(originalView.get('left')));
    162   assert.notEqual(touchedView.get('left'), originalView.get('left'));
    163   assert(box.y + box.height <= 844);
    164   await page.screenshot({path: output + '/mobile-map.png'});
    165   await page.getByRole('link', {name: 'General', exact: true}).click();
    166   await page.getByRole('button', {name: 'Log out', exact: true}).click();
    167   await page.getByLabel('Username').fill('Operator');
    168   await page.getByLabel('Password').fill('long-test-password');
    169   await page.getByRole('button', {name: 'Log in', exact: true}).click();
    170 
    171   // Verify weather delivery in the real client, with live simulation refreshes.
    172   const client = await context.newPage();
    173   await client.setViewportSize({width: 1024, height: 768});
    174   game = await GameTest.attach(client);
    175   await client.goto(url);
    176   await game.ready();
    177   const originalStream = game.worldStream;
    178   const editSequence = game.sequence;
    179   const originalRotation = game.local().rotation;
    180   let response = await context.request.post(url + 'admin/players/1/teleport', {form: {playfield: '705', x: '980', z: '510'}});
    181   assert(response.ok(), await response.text());
    182   await game.wait('same-zone teleport', state => Math.abs(game.local(state).position[0] - 980) < 1 && Math.abs(game.local(state).position[2] - 510) < 1);
    183   assert.deepEqual(game.local().rotation, originalRotation);
    184   const players = await (await context.request.get(url + 'admin/map-players')).json();
    185   const teleported = players.players.find(player => player.id === 1);
    186   assert(teleported.rotation.every((value, axis) => Math.abs(value - originalRotation[axis]) < 0.01));
    187   assert(Math.abs(teleported.position[0] - 980) < 1 && Math.abs(teleported.position[2] - 510) < 1);
    188   response = await context.request.post(url + 'admin/items', {form: {template: '121567'}});
    189   assert(response.ok(), await response.text());
    190   const itemId = Number(response.url().split('/').pop());
    191   response = await context.request.post(url + 'admin/items/' + itemId, {form: {action: 'assign', owner: '1', destination: '0:93'}});
    192   assert(response.ok(), await response.text());
    193   await game.wait('live inventory addition at selected slot', state => game.local(state).inventory.some(item => item.id === itemId && item.slot === 77));
    194   response = await context.request.post(url + 'admin/players/1', {form: {'inventory.slots.inventory_slot_30': ''}});
    195   assert(response.ok(), await response.text());
    196   await game.wait('live inventory removal', state => !game.local(state).inventory.some(item => item.id === itemId));
    197   assert.equal(game.worldStream, originalStream);
    198   assert(!game.events.some(event => event.sequence > editSequence && event.direction === 'framereceived' && ['43197d22', '0000003c'].includes(event.opcode)));
    199   for (const [value, name, type] of [['1', 'clear', 3], ['2', 'rain', 0], ['3', 'clouds', 2], ['4', 'fog', 1]]) {
    200     const sequence = game.sequence;
    201     await page.getByLabel('Conditions', {exact: true}).selectOption(value);
    202     await page.getByRole('button', {name: 'Set weather', exact: true}).click();
    203     await game.wait('weather packet ' + name, () => game.events.some(event => event.sequence > sequence && event.opcode === '0c5a5d6d' && event.direction === 'framereceived' && Buffer.from(event.hex, 'hex')[41] === type));
    204     await game.wait('weather refresh ' + name, () => game.events.filter(event => event.sequence > sequence && event.opcode === '0c5a5d6d' && event.direction === 'framereceived' && Buffer.from(event.hex, 'hex')[41] === type).length >= 2);
    205     const frame = game.state.frame;
    206     await game.wait('weather frames ' + name, state => state.frame > frame + 150);
    207     await client.screenshot({path: output + '/weather-' + name + '.png'});
    208   }
    209   assert.equal(await page.getByLabel('Conditions', {exact: true}).locator('option', {hasText: 'Automatic'}).count(), 0);
    210   await game.save(output + '/game');
    211 
    212   assert.equal(profileRequests.length, 0);
    213   assert.equal(client.workers().filter(worker => worker.url().includes('profile-worker.js')).length, 0);
    214   const uploaded = client.waitForResponse(response => response.url().includes('/browser-profile/') && response.status() === 204);
    215   await client.goto(url + '?profile=1');
    216   await uploaded;
    217   assert(profileRequests.length > 0);
    218 
    219   assert(mapResponses.every(response => response.npcs.length <= 5000 && response.warps.length <= 5000));
    220   assert.deepEqual(errors, []);
    221   assert.equal(game.errors.length, 0);
    222   console.log('PASS: admin/player password pages, styled login errors, expanding SQL table/CSV, responsive touch map, live teleport and inventory edits, native weather, profiling opt-in');
    223 } finally {
    224   await browser.close();
    225 }