aorecomp

Anarchy Online classic in the browser

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


tests/e2e/native-creation.test.mjs (7100B)


      1 import assert from 'node:assert/strict';
      2 import {before, test} from 'node:test';
      3 import {buildServer, withFixture} from './fixture.mjs';
      4 import {startStarter, enterOmniTraining} from '../../tools/client/create_starter.mjs';
      5 
      6 before(buildServer);
      7 
      8 test('native creation credentials preserve cancellation and the complete starter journey',
      9 {timeout: 900_000}, async () => withFixture('native-creation', async fixture => {
     10   const context = await fixture.visitor();
     11   const claimed = await context.request.post(fixture.url + '/browser-account/claim',
     12     {form: {name: 'Existing', password: 'existing password'}});
     13   assert.equal(claimed.status(), 204);
     14   const original = await fixture.state(context);
     15   const game = await fixture.game(context);
     16   const packets = [];
     17   game.page.on('websocket', socket => {
     18     if (!socket.url().endsWith('/browser-create')) return;
     19     for (const direction of ['framesent', 'framereceived']) socket.on(direction, ({payload}) => {
     20       const packet = Buffer.from(payload);
     21       packets.push({direction, packet});
     22       game.record({kind: 'creation-packet', direction, hex: packet.toString('hex')});
     23     });
     24   });
     25 
     26   await startStarter(game);
     27   await game.click(997, 38);
     28   await game.wait('native quit confirmation', state => state.regions.some(
     29     ([left, top, right, bottom]) => left === 437 && top === 415 && right === 507 && bottom === 435));
     30   await game.save(fixture.output + '/cancel');
     31   // Exit ends the native frame loop; observe the page reload instead of waiting for another frame.
     32   const point = await game.point(472, 425);
     33   await game.page.mouse.move(point.x, point.y);
     34   const sequence = game.sequence;
     35   await game.page.mouse.down();
     36   await game.waitInput(sequence, 1025);
     37   await Promise.all([game.page.waitForEvent('load'), game.page.mouse.up()]);
     38   await game.ready();
     39   assert.equal((await fixture.state(context)).id, original.id);
     40   assert.deepEqual((await fixture.state(context)).items, original.items);
     41   console.log('PASS: native cancellation preserves the existing character');
     42 
     43   await startStarter(game);
     44   await game.click(946, 730);
     45   await game.page.locator('#creation-credentials').waitFor({state: 'visible'});
     46 
     47   // Browser fields validate credentials at the native naming step, before creation commits.
     48   for (const [name, status] of [['ab', 400], ['Existing', 409]]) {
     49     await game.page.locator('#creation-player-name').fill(name);
     50     await game.page.locator('#creation-password').fill('native password');
     51     await game.page.locator('#creation-password-confirm').fill('native password');
     52     const [response] = await Promise.all([
     53       game.page.waitForResponse(response => response.url().endsWith('/browser-create') && response.request().method() === 'POST'),
     54       game.page.locator('#creation-submit').click(),
     55     ]);
     56     assert.equal(response.status(), status);
     57     await game.page.waitForFunction(() => document.querySelector('#creation-error').textContent !== '' &&
     58       !document.querySelector('#creation-submit').disabled);
     59     assert.equal((await fixture.state(context)).id, original.id);
     60     assert.deepEqual((await fixture.state(context)).items, original.items);
     61     assert.equal(packets.filter(({direction, packet}) => direction === 'framesent' && packet.readUInt32BE(16) === 0x0f).length, 0);
     62     await game.save(fixture.output + '/' + (name === 'ab' ? 'invalid-name' : 'duplicate-name'));
     63   }
     64   console.log('PASS: short-name and duplicate-name errors preserve the existing character at native naming');
     65 
     66   await game.page.locator('#creation-player-name').fill('Nativehero');
     67   await game.page.locator('#creation-password').fill('native password');
     68   await game.page.locator('#creation-password-confirm').fill('native password');
     69   await game.page.locator('#creation-submit').click();
     70   await game.ready();
     71   assert.equal(game.local().playfield, 4604);
     72   assert.equal(game.local().stats[4], 4);
     73   assert.equal(game.local().stats[60], 9);
     74   await game.save(fixture.output + '/arrival');
     75   const created = await fixture.state(context);
     76   assert.equal(created.name, 'Nativehero');
     77   assert.equal(created.items.length, 4);
     78   assert.deepEqual(created.items.map(item => item.templates[0]), [116628, 116629, 121564, 43379]);
     79   const account = await context.request.get(fixture.url + '/browser-account');
     80   assert.equal((await account.json()).adopted, true);
     81   await enterOmniTraining(game);
     82   await game.save(fixture.output + '/training');
     83   const departed = await fixture.state(context);
     84   assert.equal(departed.playfield, 950);
     85   assert.equal(departed.items.length, 7);
     86   assert.deepEqual(departed.items.slice(4).map(item => item.templates[0]), [116693, 99719, 29426]);
     87   console.log('PASS: original creator and Arrival Hall reach training with the expected starter items');
     88 
     89   // Verify first-session preference writes belong to the newly created identity.
     90   await game.key('i');
     91   const grid = state => state.grids.find(grid => grid.cells.some(cell => cell.identity[0] === 104));
     92   const firstItem = state => grid(state).cells.find(cell => cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy);
     93   const clickCell = cell => game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2);
     94   assert.equal(firstItem(game.state).index, 0);
     95   await clickCell(firstItem(game.state));
     96   await game.wait('starter item picked up', state => state.dragged[0] === 104);
     97   await clickCell(grid(game.state).cells.find(cell => cell.index === 8));
     98   await game.wait('starter layout saved', state => state.dragged[0] === 0 && firstItem(state).index === 8);
     99   await game.page.reload();
    100   await game.ready();
    101   if (!game.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104))) await game.key('i');
    102   assert.equal(firstItem(game.state).index, 8);
    103   console.log('PASS: newly created character preferences survive reload');
    104 
    105   assert.equal((await fixture.state(context)).id, created.id);
    106   assert.deepEqual((await fixture.state(context)).items, departed.items);
    107   const cookies = await context.cookies();
    108   await fixture.restart();
    109   const restored = await fixture.context();
    110   await restored.addCookies(cookies);
    111   const resumed = await fixture.game(restored);
    112   assert.equal(resumed.local().playfield, 950);
    113   assert.deepEqual((await fixture.state(restored)).items, departed.items);
    114   const recovered = await fixture.visitor();
    115   const login = await recovered.request.post(fixture.url + '/browser-account/login',
    116     {form: {name: 'Existing', password: 'existing password'}});
    117   assert.equal(login.status(), 204);
    118   assert.equal((await fixture.state(recovered)).id, original.id);
    119   assert.deepEqual((await fixture.state(recovered)).items, original.items);
    120   const newLogin = await recovered.request.post(fixture.url + '/browser-account/login',
    121     {form: {name: 'Nativehero', password: 'native password'}});
    122   assert.equal(newLogin.status(), 204);
    123   assert.equal((await fixture.state(recovered)).id, created.id);
    124   assert.deepEqual((await fixture.state(recovered)).items, departed.items);
    125   for (const client of [game, resumed]) assert.deepEqual(client.errors, []);
    126 }));