tests/e2e/storage.test.mjs (2840B)
1 import assert from 'node:assert/strict'; 2 import {before, test} from 'node:test'; 3 import {buildServer, withFixture} from './fixture.mjs'; 4 import {GameTest} from '../../tools/client/game_test.mjs'; 5 6 before(buildServer); 7 8 for (const denied of [true, false]) { 9 test(denied ? 'denied browser storage uses temporary preferences' : 'available browser storage retains preferences', 10 {timeout: 120_000}, async () => { 11 await withFixture(denied ? 'storage-denied' : 'storage-persistent', async fixture => { 12 const context = await fixture.visitor(); 13 const items = (await fixture.state(context)).items; 14 if (denied) { 15 // Firefox private browsing rejects this call on both pages and workers. 16 const rejectStorage = `if (navigator.storage) navigator.storage.getDirectory = async () => { 17 throw new DOMException('Security error when calling GetDirectory', 'SecurityError'); 18 };`; 19 await context.addInitScript(rejectStorage); 20 await context.route('**/wasm/game.js', async route => { 21 const response = await route.fetch(); 22 await route.fulfill({response, body: rejectStorage + await response.text()}); 23 }); 24 } 25 const page = await context.newPage(); 26 const game = await GameTest.attach(page); 27 fixture.games.push(game); 28 await page.goto(fixture.url); 29 await page.waitForFunction(() => typeof gameReady !== 'undefined' && gameReady, null, {timeout: 30_000}); 30 await game.ready(); 31 await game.key('i'); 32 const grid = state => state.grids.find(grid => grid.cells.some(cell => cell.identity[0] === 104)); 33 const weapon = state => grid(state).cells.find(cell => cell.identity[0] === 104 && cell.identity[1] === 48 && !cell.dummy); 34 const click = cell => game.click((cell.bounds[0] + cell.bounds[2]) / 2, (cell.bounds[1] + cell.bounds[3]) / 2); 35 assert.equal(weapon(game.state).index, 0); 36 37 // Exercise native preference writes by moving an item to a sparse cell. 38 await click(weapon(game.state)); 39 await game.wait('item picked up', state => state.dragged[0] === 104); 40 await click(grid(game.state).cells.find(cell => cell.index === 8)); 41 await game.wait('item layout saved', state => state.dragged[0] === 0 && weapon(state).index === 8); 42 await page.reload(); 43 await page.waitForFunction(() => typeof gameReady !== 'undefined' && gameReady, null, {timeout: 30_000}); 44 await game.ready(); 45 if (!game.state.grids.some(grid => grid.cells.some(cell => cell.identity[0] === 104))) await game.key('i'); 46 47 assert.equal(weapon(game.state).index, denied ? 0 : 8); 48 assert.deepEqual((await fixture.state(context)).items, items); 49 assert.deepEqual(game.errors, []); 50 }); 51 }); 52 }