tests/e2e/visitor.test.mjs (5045B)
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 function receivedTitle(game, id) { 8 const event = game.events.findLast(event => event.direction === 'framereceived' && event.opcode === '734e5a7b' && 9 Buffer.from(event.hex, 'hex').readUInt32BE(24) === id); 10 assert(event, 'the server must publish the title for this actor'); 11 const packet = Buffer.from(event.hex, 'hex'); 12 assert.equal(packet.readUInt32BE(29), 4); 13 assert.equal(packet[33], 0); 14 assert.equal(packet.readUInt32BE(34), packet.length - 38); 15 return packet.subarray(38).toString(); 16 } 17 18 test('visitor prefix works in chat and is removed on adoption', {timeout: 180_000}, async () => { 19 await withFixture('visitor-name', async fixture => { 20 const context = await fixture.visitor(); 21 const replies = []; 22 context.on('page', page => page.on('websocket', socket => { 23 if (socket.url().endsWith('/browser-chat')) 24 socket.on('framereceived', ({payload}) => replies.push(Buffer.from(payload).toString())); 25 })); 26 await fixture.setup(context, 'position', {playfield: 705, position: [960, 16.5, 510], rotation: [0, 0, 0, 1]}); 27 const before = await fixture.state(context); 28 const game = await fixture.game(context); 29 await game.key('F1'); 30 await game.save(fixture.output + '/visitor-self'); 31 assert.match(before.name, /^Visitor[A-Z][a-z]{3,4}$/); 32 assert.equal(before.items.length, 7); 33 assert.equal(receivedTitle(game, 1), ''); 34 35 const observer = await fixture.visitor(); 36 const observerReplies = []; 37 observer.on('page', page => page.on('websocket', socket => { 38 if (socket.url().endsWith('/browser-chat')) 39 socket.on('framereceived', ({payload}) => observerReplies.push(Buffer.from(payload).toString())); 40 })); 41 await fixture.setup(observer, 'position', {playfield: 705, position: [960, 16.5, 505], rotation: [0, 0, 0, 1]}); 42 const watching = await fixture.game(observer); 43 44 await watching.key('Enter'); 45 await watching.page.keyboard.type(`/tell ${before.name} visitor name test`); 46 await watching.key('Enter'); 47 48 await game.wait('tell reaches the generated visitor name', () => replies.some(reply => reply.includes('visitor name test'))); 49 assert(observerReplies.some(reply => reply.includes(before.name)), 'chat must receive the full camelcase name'); 50 await watching.save(fixture.output + '/visitor-chat'); 51 const select = async () => { 52 const [x, y] = await watching.pick(50000, 1); 53 await watching.click(x, y); 54 await watching.wait('visitor selected', state => state.selected[0] === 50000 && state.selected[1] === 1); 55 }; 56 await select(); 57 assert.equal(receivedTitle(watching, 1), ''); 58 await watching.save(fixture.output + '/visitor-observed'); 59 const sequence = watching.sequence; 60 await watching.key('t'); 61 await watching.wait('visitor inspection', () => watching.events.some(event => event.sequence > sequence && 62 event.direction === 'framereceived' && event.opcode === '4d38242e')); 63 await watching.save(fixture.output + '/visitor-inspected'); 64 65 await game.page.keyboard.press('Control+Alt+M'); 66 await game.page.locator('#claim-button').click(); 67 assert.equal(await game.page.locator('#account-name').inputValue(), before.name.slice(7)); 68 assert.equal(await game.page.locator('#account-name').isEditable(), true); 69 await game.page.locator('#account-password').fill('visitor name test'); 70 await Promise.all([game.page.waitForEvent('load'), game.page.locator('#account-submit').click()]); 71 await game.ready(); 72 assert.equal(receivedTitle(game, 1), ''); 73 await game.key('F1'); 74 await game.save(fixture.output + '/adopted-self'); 75 await watching.key('Escape'); 76 await select(); 77 assert.equal(receivedTitle(watching, 1), ''); 78 await watching.save(fixture.output + '/adopted-observed'); 79 assert.deepEqual((await fixture.state(context)).items, before.items); 80 assert.equal((await fixture.state(context)).name, before.name.slice(7)); 81 82 const cookies = await context.cookies(); 83 const observerCookies = await observer.cookies(); 84 await fixture.restart(); 85 const restored = await fixture.context(); 86 await restored.addCookies(cookies); 87 const adopted = await fixture.game(restored); 88 assert.equal(receivedTitle(adopted, 1), ''); 89 await adopted.key('F1'); 90 await adopted.save(fixture.output + '/adopted-cold'); 91 assert.deepEqual((await fixture.state(restored)).items, before.items); 92 assert.equal((await fixture.state(restored)).name, before.name.slice(7)); 93 const returned = await fixture.context(); 94 await returned.addCookies(observerCookies); 95 const visitor = await fixture.game(returned); 96 assert.equal(receivedTitle(visitor, 2), ''); 97 await visitor.key('F1'); 98 await visitor.save(fixture.output + '/visitor-cold'); 99 for (const client of [game, watching, adopted, visitor]) assert.deepEqual(client.errors, []); 100 }); 101 });