aorecomp

Anarchy Online classic in the browser

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


tests/e2e/mobile-creation.test.mjs (6783B)


      1 import assert from 'node:assert/strict';
      2 import {before, test} from 'node:test';
      3 import {buildServer, withFixture} from './fixture.mjs';
      4 
      5 before(buildServer);
      6 
      7 test('mobile naming modal blocks native controls, survives refresh and preserves proportions',
      8 {timeout: 480_000}, async () => withFixture('mobile-creation', async fixture => {
      9   const context = await fixture.browser.newContext({viewport: {width: 390, height: 844},
     10     isMobile: true, hasTouch: true, deviceScaleFactor: 2,
     11     userAgent: 'Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 Chrome/130.0.0.0 Mobile Safari/537.36'});
     12   const visitor = await context.request.post(fixture.url + '/browser-launch');
     13   assert.equal(visitor.status(), 200);
     14   assert.equal((await context.request.post(fixture.url + '/_test/visitor')).status(), 204);
     15   const original = await fixture.state(context);
     16   const game = await fixture.game(context);
     17   const page = game.page;
     18   const touch = await context.newCDPSession(page);
     19   const begin = async () => {
     20     await page.locator('#menu-button').tap();
     21     await Promise.all([page.waitForEvent('load'), page.locator('#create-button').tap()]);
     22     await page.waitForFunction(() => typeof Module._browser_test_request === 'function', null, {timeout: 180000});
     23   };
     24   const scene = id => game.wait('native creator scene ' + id,
     25     state => state.creation?.scene === id && state.creation.ready, 60000);
     26   const next = () => game.wait('native Next enabled', state => state.regions.some(
     27     ([left, top, right, bottom]) => left === 888 && top === 710 && right === 1006 && bottom === 749));
     28   const tap = async (x, y) => {
     29     const point = await game.point(x, y);
     30     const sequence = game.sequence;
     31     await touch.send('Input.dispatchTouchEvent', {type: 'touchStart', touchPoints: [point]});
     32     await game.waitInput(sequence, 1024);
     33     await game.snapshot();
     34     await touch.send('Input.dispatchTouchEvent', {type: 'touchEnd', touchPoints: []});
     35     await game.waitInput(sequence, 1026);
     36     await game.snapshot();
     37   };
     38   const proportions = async () => {
     39     await game.snapshot();
     40     assert.deepEqual(game.state.screen, [1024, 768]);
     41     const canvas = await page.locator('#canvas').boundingBox();
     42     assert(Math.abs(canvas.width / canvas.height - 4 / 3) < 0.001);
     43     assert(canvas.x >= 0 && canvas.y >= 0);
     44     assert(canvas.x + canvas.width <= page.viewportSize().width + 1);
     45     assert(canvas.y + canvas.height <= page.viewportSize().height + 1);
     46   };
     47 
     48   await begin();
     49   await scene(1001);
     50   assert.equal(new URL(page.url()).searchParams.has('create'), false);
     51   await page.reload();
     52   await game.ready();
     53   assert.equal((await fixture.state(context)).id, original.id);
     54   assert.equal(game.state.creation, null);
     55 
     56   await begin();
     57   await scene(1001);
     58   await proportions();
     59   await game.save(fixture.output + '/portrait-breed');
     60   await page.setViewportSize({width: 844, height: 390});
     61   await proportions();
     62   await game.save(fixture.output + '/landscape-breed');
     63   await tap(880, 350);
     64   await next();
     65   await tap(946, 730);
     66   await scene(1002);
     67   await page.setViewportSize({width: 390, height: 844});
     68   await proportions();
     69   await tap(404, 130);
     70   await tap(919, 214);
     71   await tap(920, 444);
     72   await tap(946, 730);
     73   await scene(1003);
     74   await tap(850, 514);
     75   await next();
     76   // Observe the native lifetime signal, including its scene-less ending animation.
     77   await page.evaluate(() => {
     78     window.creationPhases = [];
     79     const publish = Module.onCreationState;
     80     Module.onCreationState = state => {
     81       publish(state);
     82       window.creationPhases.push({active: state.active, screen: renderSize(),
     83         width: canvas.getBoundingClientRect().width, height: canvas.getBoundingClientRect().height});
     84     };
     85   });
     86   await tap(946, 730);
     87   await page.locator('#creation-credentials').waitFor({state: 'visible'});
     88   await page.locator('#creation-player-name').tap();
     89   assert.equal(await page.locator('#creation-player-name').evaluate(element => document.activeElement === element), true);
     90   await page.keyboard.insertText('Phonehero');
     91   await page.locator('#creation-password').tap();
     92   await page.keyboard.insertText('phone password');
     93   await page.locator('#creation-password-confirm').tap();
     94   await page.keyboard.insertText('wrong password');
     95   await page.locator('#creation-submit').tap();
     96   assert.equal(await page.locator('#creation-error').textContent(), 'Passwords do not match.');
     97   assert.equal((await fixture.state(context)).id, original.id);
     98   await page.locator('#creation-password-confirm').fill('phone password');
     99   await page.keyboard.press('Escape');
    100   assert.equal(await page.locator('#creation-credentials').isVisible(), true);
    101   assert.deepEqual(await page.locator('#creation-credentials button').allTextContents(), ['OK']);
    102   const dialog = await page.locator('#creation-credentials').boundingBox();
    103   assert(dialog.width <= 326 && dialog.height < 350);
    104   await game.snapshot();
    105   const blockedSequence = game.sequence;
    106   const finish = await game.point(945, 730);
    107   await page.mouse.click(finish.x, finish.y);
    108   await page.keyboard.press('ArrowLeft');
    109   await game.snapshot();
    110   assert.equal(await page.locator('#creation-credentials').isVisible(), true);
    111   assert.equal((await fixture.state(context)).id, original.id);
    112   assert.equal(game.events.some(event => event.sequence > blockedSequence &&
    113     event.kind === 'input' && [768, 769, 1025, 1026].includes(event.type)), false);
    114   await game.save(fixture.output + '/naming-dialog');
    115   await page.locator('#creation-submit').tap();
    116   await game.ready();
    117   await game.wait('full-screen world after native creation', state =>
    118     state.screen[0] === 390 && state.screen[1] === 844);
    119   const phases = await page.evaluate(() => window.creationPhases);
    120   assert(phases.some(state => state.active));
    121   assert.equal(phases.at(-1).active, false);
    122   for (const state of phases.filter(state => state.active)) {
    123     assert.deepEqual(state.screen, [1024, 768]);
    124     assert(Math.abs(state.width / state.height - 4 / 3) < 0.001);
    125   }
    126   const created = await fixture.state(context);
    127   assert.equal(created.name, 'Phonehero');
    128   assert.equal(created.playfield, 4604);
    129   assert.equal(created.items.length, 4);
    130   const account = await context.request.get(fixture.url + '/browser-account');
    131   assert.equal((await account.json()).adopted, true);
    132   await page.locator('#menu-button').tap();
    133   await page.locator('#password-button').waitFor({state: 'visible'});
    134   assert.equal(await page.locator('#claim-button').isVisible(), false);
    135   await page.keyboard.press('Escape');
    136   await page.reload();
    137   await game.ready();
    138   assert.equal((await fixture.state(context)).id, created.id);
    139   assert.equal(game.state.creation, null);
    140   assert.deepEqual(game.errors, []);
    141   await game.save(fixture.output + '/arrival');
    142 }));