Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Keep the full `bun test` well inside the deploy's 600 s limit as the catalogue grows #233
Check · Review · Land · Live done — Landed and live in production.
mason/pixel-plays:mason/227-keep-the-full-bun-test-well-inside-the-d → main 2 commits fork
4 files
changed +45
−19
README.md +1 −1
Diff not shown — a line 2,117 characters wide (limit 2,000). +1 −1
View fileplaytest.mjs +3
| @@ -110,6 +110,9 @@ export function boot(id, { seed = 1 } = {}) { | ||
| 110 | 110 | }, |
| 111 | 111 | // number of distinct colours painted: a blank frame has one |
| 112 | 112 | colours() { return image ? new Set(new Uint32Array(image.data.buffer)).size : 0; }, |
| 113 | // from now on the page's own `draw` is left out, which is most of what a frame costs: for a twin that is | |
| 114 | // only stepped on and read for its text (a game without a `draw` function is unaffected) | |
| 115 | stopDrawing() { try { evaluate('typeof draw === "function" && (draw = () => {})'); } catch (e) { errors.push(e); } }, | |
| 113 | 116 | // advance by `seconds` of play |
| 114 | 117 | frames: 0, |
| 115 | 118 | run(seconds) { api.step(Math.round(seconds * 25)); }, // 25 fps: the games cap a step at 1/25 s, so none runs slower than real time |
playtest.test.mjs +36 −15
| @@ -30,23 +30,35 @@ test('every game has a scripted round, and every script has a game', () => { | ||
| 30 | 30 | |
| 31 | 31 | test('the games folder is not empty', () => { expect(GAMES.length).toBeGreaterThanOrEqual(19); }); |
| 32 | 32 | |
| 33 | // What one game's playtests may cost together, in milliseconds: a slow page fails in its own pull, | |
| 34 | // by name, rather than as the whole suite running out of time. A scripted round plays the game for | |
| 35 | // as long as the game lasts (twice, with its untouched twin), so each second of it adds a little. | |
| 36 | const GAME_BUDGET = 15_000, PER_ROUND_SECOND = 300; | |
| 37 | const budgetOf = seconds => GAME_BUDGET + PER_ROUND_SECOND * seconds; | |
| 38 | const spent = {}, roundSeconds = {}; | |
| 39 | const timed = (id, fn) => () => { const t = performance.now(); try { return fn(); } finally { spent[id] = (spent[id] ?? 0) + performance.now() - t; } }; | |
| 40 | ||
| 33 | 41 | for (const id of GAMES) { |
| 34 | test(`${id}: boots, paints a picture and says what to do`, () => { | |
| 35 | const g = boot(id); | |
| 42 | // the first two tests only read this boot, so they share it: the second carries on from the first | |
| 43 | let shared; | |
| 44 | const idler = () => (shared ??= boot(id)); | |
| 45 | ||
| 46 | test(`${id}: boots, paints a picture and says what to do`, timed(id, () => { | |
| 47 | const g = idler(); | |
| 36 | 48 | g.run(2); |
| 37 | 49 | expect(g.errors.map(e => e.message)).toEqual([]); |
| 38 | 50 | expect(g.colours()).toBeGreaterThan(40); |
| 39 | 51 | expect(g.text().length).toBeGreaterThan(10); |
| 40 | }); | |
| 52 | })); | |
| 41 | 53 | |
| 42 | test(`${id}: untouched, it idles for four seconds without failing`, () => { | |
| 43 | const g = boot(id); | |
| 44 | g.run(4); | |
| 54 | test(`${id}: untouched, it idles for four seconds without failing`, timed(id, () => { | |
| 55 | const g = idler(); | |
| 56 | g.run(2); // two seconds on from the first test: four in all | |
| 45 | 57 | expect(g.errors.map(e => e.message)).toEqual([]); |
| 46 | 58 | expect(g.colours()).toBeGreaterThan(40); |
| 47 | }); | |
| 59 | })); | |
| 48 | 60 | |
| 49 | test(`${id}: pointer play throws nothing, and the picture and the HUD text differ from an untouched twin`, () => { | |
| 61 | test(`${id}: pointer play throws nothing, and the picture and the HUD text differ from an untouched twin`, timed(id, () => { | |
| 50 | 62 | // same seed, same frame count: only the player's input tells the two apart |
| 51 | 63 | const idle = boot(id, { seed: 7 }), g = boot(id, { seed: 7 }), r = rand(11); |
| 52 | 64 | let picture = false, words = false; |
| @@ -60,9 +72,9 @@ for (const id of GAMES) { | ||
| 60 | 72 | expect(picture).toBe(true); |
| 61 | 73 | expect(words).toBe(true); |
| 62 | 74 | expect(g.colours()).toBeGreaterThan(40); |
| 63 | }); | |
| 75 | })); | |
| 64 | 76 | |
| 65 | test(`${id}: the keyboard alone changes the game compared with leaving it be`, () => { | |
| 77 | test(`${id}: the keyboard alone changes the game compared with leaving it be`, timed(id, () => { | |
| 66 | 78 | const idle = boot(id, { seed: 3 }), played = boot(id, { seed: 3 }), r = rand(5); |
| 67 | 79 | let parted = false; |
| 68 | 80 | for (let i = 0; i < 20 && !parted; i++) { |
| @@ -73,17 +85,26 @@ for (const id of GAMES) { | ||
| 73 | 85 | } |
| 74 | 86 | expect(played.errors.map(e => e.message)).toEqual([]); |
| 75 | 87 | expect(parted).toBe(true); |
| 76 | }); | |
| 88 | })); | |
| 77 | 89 | |
| 78 | test(`${id}: a scripted round reaches its end and the HUD names the outcome`, () => { | |
| 90 | test(`${id}: a scripted round reaches its end and the HUD names the outcome`, timed(id, () => { | |
| 79 | 91 | const { ended, seconds, errors } = playRound(id); |
| 80 | 92 | expect(errors.map(e => e.message)).toEqual([]); |
| 81 | 93 | expect(ended).not.toBeNull(); |
| 82 | 94 | expect(ended).toMatch(ROUNDS[id].outcome); // the win or the loss, with its number |
| 83 | 95 | expect(seconds).toBeLessThan(ROUNDS[id].limit ?? 100); |
| 84 | 96 | // an untouched game does not reach that outcome in the same time (it may still end, by failing) |
| 85 | const idle = boot(id); | |
| 86 | idle.run(seconds); | |
| 97 | roundSeconds[id] = seconds; | |
| 98 | // the untouched boot of the first two tests, carried on to the same moment (a fresh one if the round ended sooner than they ran) | |
| 99 | const idle = idler().frames <= seconds * 25 ? idler() : boot(id); | |
| 100 | idle.stopDrawing(); // only its text is read at the end | |
| 101 | idle.step(seconds * 25 - idle.frames); | |
| 87 | 102 | expect(idle.text()).not.toMatch(ROUNDS[id].outcome); |
| 88 | }, ROUNDS[id].budget ?? 120_000); // a timed round (wood-split's 60 s) is played twice in the engine's software renderer | |
| 103 | }), ROUNDS[id].budget ?? 120_000); // a timed round (wood-split's 60 s) is played twice in the engine's software renderer | |
| 104 | ||
| 105 | test(`${id}: its playtests together stay within ${GAME_BUDGET / 1000} s and ${PER_ROUND_SECOND} ms per second of the round`, () => { | |
| 106 | expect(roundSeconds[id]).toBeDefined(); // the game's other tests ran first: run alone, there is nothing to weigh | |
| 107 | const ms = Math.round(spent[id]), budget = budgetOf(roundSeconds[id] ?? 0); | |
| 108 | expect(`${id} took ${Math.min(ms, budget)} ms`).toBe(`${id} took ${ms} ms`); // names the game and its time when over | |
| 109 | }); | |
| 89 | 110 | } |
serve.test.mjs +5 −3
| @@ -240,10 +240,12 @@ test('every game and the home page carry share-card tags and a served PNG card', | ||
| 240 | 240 | test('every committed card is what the games render now, and capture is deterministic', async () => { |
| 241 | 241 | const { games, capture, card, collage } = await import('./scripts/og.mjs'); |
| 242 | 242 | const { readFileSync } = await import('node:fs'); |
| 243 | const frames = []; | |
| 244 | for (const g of games()) { | |
| 243 | const frames = [], all = games(); | |
| 244 | // determinism is checked on the first, middle and last game: a second capture of each costs as much as the first | |
| 245 | const again = new Set([0, Math.floor(all.length / 2), all.length - 1]); | |
| 246 | for (const [i, g] of all.entries()) { | |
| 245 | 247 | const frame = capture(g.id); |
| 246 | expect(Buffer.from(capture(g.id)).equals(Buffer.from(frame))).toBe(true); | |
| 248 | if (again.has(i)) expect(Buffer.from(capture(g.id)).equals(Buffer.from(frame))).toBe(true); | |
| 247 | 249 | frames.push(frame); |
| 248 | 250 | expect(card(frame).equals(readFileSync(new URL(`./og/${g.id}.png`, import.meta.url)))).toBe(true); |
| 249 | 251 | } |
new above
offline