tests/e2e/camera.test.mjs (8854B)
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 cameraDistance(game, state = game.state) { 8 return Math.hypot(...state.camera.position.map((value, index) => value - game.local(state).position[index])); 9 } 10 11 test('camera zoom works in both wheel directions and without a numpad', {timeout: 120_000}, async () => { 12 await withFixture('camera-zoom', async fixture => { 13 const context = await fixture.visitor(); 14 await fixture.setup(context, 'position', {playfield: 705, position: [960, 16.5, 510], rotation: [0, 0, 0, 1]}); 15 const game = await fixture.game(context); 16 if (game.state.camera.firstPerson) await game.key('F8'); 17 const pose = await fixture.state(context); 18 const distance = cameraDistance(game); 19 20 await game.hold('Minus', 'main minus zooms out', state => cameraDistance(game, state) > distance + 1); 21 const farther = cameraDistance(game); 22 await game.hold('Equal', 'main equals zooms in', state => cameraDistance(game, state) < farther - 1); 23 const near = cameraDistance(game); 24 await game.hold('NumpadSubtract', 'original numpad minus zooms out', state => cameraDistance(game, state) > near + 2); 25 const numpadOut = cameraDistance(game); 26 await game.hold('NumpadAdd', 'original numpad plus zooms in', state => cameraDistance(game, state) < numpadOut - 0.5); 27 const beforePlus = cameraDistance(game); 28 await game.page.keyboard.down('Shift'); 29 try { 30 await game.hold('Equal', 'main plus zooms in', state => cameraDistance(game, state) < beforePlus - 0.5); 31 } finally { await game.page.keyboard.up('Shift'); } 32 await game.page.mouse.move(500, 300); 33 const beforeWheel = cameraDistance(game); 34 const angles = game.state.camera.angles; 35 await game.page.mouse.wheel(0, 100); 36 await game.wait('wheel zooms out', state => cameraDistance(game, state) > beforeWheel + 0.1); 37 const wheelOut = cameraDistance(game); 38 await game.page.mouse.wheel(0, -100); 39 await game.wait('wheel zooms in', state => cameraDistance(game, state) < wheelOut - 0.1); 40 41 const after = await fixture.state(context); 42 assert.deepEqual(after.position, pose.position); 43 assert.deepEqual(after.rotation, pose.rotation); 44 assert.deepEqual(game.state.camera.angles, angles); 45 assert.deepEqual(game.errors, []); 46 }); 47 }); 48 49 test('laptop shortcuts rotate/reset the camera and toggle autorun', {timeout: 120_000}, async () => { 50 await withFixture('camera-shortcuts', async fixture => { 51 const context = await fixture.visitor(); 52 await fixture.setup(context, 'position', {playfield: 705, position: [960, 16.5, 510], rotation: [0, 0, 0, 1]}); 53 const game = await fixture.game(context); 54 const page = game.page; 55 if (game.state.camera.firstPerson) await game.key('F8'); 56 const pose = await fixture.state(context); 57 58 const initialPosition = game.state.camera.position; 59 await page.keyboard.down('Shift'); 60 try { 61 await game.hold('ArrowLeft', 'choose a preferred camera angle', state => 62 state.camera.position.some((value, index) => Math.abs(value - initialPosition[index]) > 0.5)); 63 await game.key('Digit7'); 64 } finally { await page.keyboard.up('Shift'); } 65 const preferred = game.state.camera.position; 66 await page.keyboard.down('Shift'); 67 try { 68 await game.hold('ArrowRight', 'move away from preferred angle', state => 69 state.camera.position.some((value, index) => Math.abs(value - preferred[index]) > 0.5)); 70 const beforeReset = game.state.camera.position; 71 await game.key('Digit5'); 72 await game.wait('reset moves the camera', state => 73 state.camera.position.some((value, index) => Math.abs(value - beforeReset[index]) > 0.5)); 74 } finally { await page.keyboard.up('Shift'); } 75 for (const key of ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown']) { 76 const position = game.state.camera.position; 77 await page.keyboard.down('Shift'); 78 await page.keyboard.down(key); 79 try { 80 await game.wait(key + ' orbits the camera', state => 81 state.camera.position.some((value, index) => Math.abs(value - position[index]) > 0.05)); 82 } finally { 83 // Release the modifier first, then verify the camera stops. 84 await page.keyboard.up('Shift'); 85 await page.keyboard.up(key); 86 } 87 const releaseFrame = game.state.frame; 88 await game.wait('orbit key released', state => state.frame > releaseFrame + 2); 89 // Vertical orbit can touch the ground; wait for native collision recovery. 90 let settling = game.state; 91 await game.wait('camera settles after release', state => { 92 if (state.frame < settling.frame + 5) return false; 93 const stable = state.camera.position.every((value, index) => Math.abs(value - settling.camera.position[index]) < 0.001); 94 settling = state; 95 return stable; 96 }); 97 const released = game.state; 98 await game.wait('frames after camera release', state => state.frame > released.frame + 5); 99 assert(game.state.camera.position.every((value, index) => Math.abs(value - released.camera.position[index]) < 0.01)); 100 } 101 102 const cameraPose = await fixture.state(context); 103 assert.deepEqual(cameraPose.position, pose.position); 104 assert.deepEqual(cameraPose.rotation, pose.rotation); 105 106 await page.keyboard.down('Shift'); 107 try { await game.key('Digit0'); } 108 finally { await page.keyboard.up('Shift'); } 109 await game.wait('autorun moves the character', state => 110 Math.abs(game.local(state).position[2] - pose.position[2]) > 1); 111 await game.key('w'); 112 113 await game.waitFor('server receives autorun movement', async () => 114 Math.abs((await fixture.state(context)).position[2] - pose.position[2]) > 0.5); 115 const stopped = game.local().position; 116 const frame = game.state.frame; 117 await game.wait('frames after autorun stops', state => state.frame > frame + 5); 118 assert.deepEqual(game.local().position, stopped); 119 assert.deepEqual(game.errors, []); 120 }); 121 }); 122 123 test('zoom punctuation and shortcut digits remain text in native chat', {timeout: 120_000}, async () => { 124 await withFixture('camera-chat', async fixture => { 125 const context = await fixture.visitor(); 126 await fixture.setup(context, 'position', {playfield: 705, position: [960, 16.5, 510], rotation: [0, 0, 0, 1]}); 127 const replies = []; 128 context.on('page', page => page.on('websocket', socket => { 129 if (socket.url().endsWith('/browser-chat')) 130 socket.on('framereceived', ({payload}) => replies.push(Buffer.from(payload).toString())); 131 })); 132 const game = await fixture.game(context); 133 const page = game.page; 134 if (game.state.camera.firstPerson) await game.key('F8'); 135 const camera = game.state.camera; 136 const position = game.local().position; 137 138 await game.key('Enter'); 139 await page.keyboard.type("camera punctuation / ' - ="); 140 for (const key of ['Equal', 'Minus', 'Digit0', 'Digit5', 'Digit7']) { 141 await page.keyboard.press('Space'); 142 await page.keyboard.press('Shift+' + key); 143 } 144 await game.key('Enter'); 145 146 await game.wait('server echoes exact native chat text', () => 147 replies.some(reply => reply.includes("camera punctuation / ' - = + _ ) % &"))); 148 assert.equal(game.state.camera.firstPerson, camera.firstPerson); 149 assert.deepEqual(game.state.camera.angles, camera.angles); 150 assert(game.state.camera.position.every((value, index) => Math.abs(value - camera.position[index]) < 0.01)); 151 assert.deepEqual(game.local().position, position); 152 assert.deepEqual(game.errors, []); 153 }); 154 }); 155 156 test('desktop chat pastes clipboard text into the current draft', {timeout: 120_000}, async () => { 157 await withFixture('chat-paste', async fixture => { 158 const context = await fixture.visitor(); 159 await context.grantPermissions(['clipboard-read', 'clipboard-write'], {origin: fixture.url}); 160 const replies = []; 161 context.on('page', page => page.on('websocket', socket => { 162 if (socket.url().endsWith('/browser-chat')) 163 socket.on('framereceived', ({payload}) => replies.push(Buffer.from(payload).toString())); 164 })); 165 const game = await fixture.game(context); 166 await game.wait('chat login completes', () => replies.some(reply => reply.includes('[Server]'))); 167 await game.key('Enter'); 168 await game.page.keyboard.type('draft '); 169 await game.page.evaluate(() => navigator.clipboard.writeText('caf\u00e9\nsecond line')); 170 await game.page.keyboard.press('Control+v'); 171 await game.page.keyboard.type(' ends'); 172 await game.key('Enter'); 173 await game.wait('server echoes the entire pasted draft', () => 174 replies.some(reply => reply.includes('draft caf\u00e9 second line ends'))); 175 assert.deepEqual(game.errors, []); 176 }); 177 });