From 1a8c74f55e936ca2afd023077032e2449f2766bc Mon Sep 17 00:00:00 2001 From: goldjohn6 Date: Sat, 21 Nov 2026 09:25:51 -0800 Subject: [PATCH] Seed the bots in the Spain & Portugal Step 2 test (#175) The test seeded the game but not the bots, which choose with Math.random, so a game could now and then draw the Step 3 card during Step 1 and skip Step 2 (Engine - Test failed this way on master after #173, then passed on re-run). A sweep of 400 bot seeds: deck2 skips Step 2 in 13 games (3.3%), deck1 in 1. Seeding Math.random per deck, as wrapper.spec does, makes the test the same on every run. Co-authored-by: Claude Opus 5.5 --- engine/src/decks.spec.ts | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/engine/src/decks.spec.ts b/engine/src/decks.spec.ts index 78862e1..f594122 100644 --- a/engine/src/decks.spec.ts +++ b/engine/src/decks.spec.ts @@ -279,21 +279,33 @@ describe('power plant decks on maps with deck-specific changes', () => { }); it('holds the Spain & Portugal ecological plants back until Step 2', () => { - for (const deck of decks) { - let G = setup(4, { map: 'Spain & Portugal', variant: 'recharged', powerPlantDeck: deck }, `spain-${deck}`); - G.players.forEach((p, i) => { - p.name = `AI ${i}`; - p.isAI = true; - }); - const held = SPAIN_STEP_2_PLANTS[deck]; - for (const number of held) expect(numbers(visible(G)), deck).to.not.include(number); - for (let turn = 0; turn < 5000 && G.step == 1 && G.currentPlayers.length > 0; turn++) { - G = moveAI(G, G.currentPlayers[0]); + // The bots choose with Math.random, so seed it: unseeded, a game could now and then + // draw the Step 3 card during Step 1 and skip Step 2 (seen once on CI, 2026-11-20). + const realRandom = Math.random; + try { + for (const deck of decks) { + Math.random = seedrandom(`spain-bots-${deck}`); + let G = setup( + 4, + { map: 'Spain & Portugal', variant: 'recharged', powerPlantDeck: deck }, + `spain-${deck}` + ); + G.players.forEach((p, i) => { + p.name = `AI ${i}`; + p.isAI = true; + }); + const held = SPAIN_STEP_2_PLANTS[deck]; + for (const number of held) expect(numbers(visible(G)), deck).to.not.include(number); + for (let turn = 0; turn < 5000 && G.step == 1 && G.currentPlayers.length > 0; turn++) { + G = moveAI(G, G.currentPlayers[0]); + } + expect(G.step, deck).to.equal(2); + // The first replaces the plant removed at the start of Step 2; the other two follow. + const known = numbers([...G.actualMarket, ...G.futureMarket, ...G.powerPlantsDeck.slice(0, 2)]); + expect(known, deck).to.include.members(held); } - expect(G.step).to.equal(2); - // The first replaces the plant removed at the start of Step 2; the other two follow. - const known = numbers([...G.actualMarket, ...G.futureMarket, ...G.powerPlantsDeck.slice(0, 2)]); - expect(known, deck).to.include.members(held); + } finally { + Math.random = realRandom; } });