Every module in the dealer ends with a `module.exports` guard, so they were all written to be testable, and none of them were ever tested. The correctness fixes that went in recently were checked by hand in a browser console that has since been closed. This puts a real safety net under them and clears out the duplication that made those bugs possible in the first place. Tests (58, ~300ms, no dependencies) Node's built-in runner, no package.json and no install step: the modules are plain script-tag files, so `load.js` just requires them and puts them on globalThis under the names they use to find each other in the page. The randomised suite is the part that matters. Every bug found in this tool so far has been shape-dependent — blinds posted to empty chairs, the button skipping players, heads-up acting out of order — and none of them show up on the full six-handed table anyone would write a fixed example for. It deals 1000 hands per property across random seat counts, empty chairs, stack depths and dealer input, checking that chips are never created or destroyed, that the pot breakdown plus uncalled returns equals the money in, that paying out settles to the cent, and that the button and blinds always land on real players. The generator is seeded so a failure reproduces instead of flaking. One implementation each - `Math.round(x * 100) / 100` was written out 16 times across 6 files. It's `Money.round` / `toCents` / `fromCents` now, in the module both pages already load first. - The chip epsilon was declared twice with the same value under two names (`PotCalculator.EPSILON`, `Betting.EPSILON_CHIPS`). Both now point at `Money.EPSILON`. - `Betting.seatAt` and `HoldemState.nextSeatIndex` were the same modular arithmetic written twice. Walking the seats is exactly where the button, the blinds and the action drifted apart from each other before, so there is one of it now. - The undo snapshot/restore dance appeared three times in main.js, each one re-deleting every key on `state` in place. It's `snapshotState` / `restoreState` once, with the reason it can't just reassign written down. A bug the tests found `layers()` and `uncalledReturns()` both decide what money was never contested, separately, and disagreed. `uncalledReturns` measured each player against the biggest live contribution *including their own*, which makes the biggest bet its own yardstick — so the one case it exists for came back empty, and `layers()` announced the overbet as a side pot only the bettor could win. With Ann shoving 200 into Ben's 60, the dealer was told "main pot 4, side pot 3, side pot 116, side pot 140" — four pots, one of them fictional. It now reads "main pot 123, uncalled 140 back to Ann", which is what a dealer says out loud. Layers are derived from contributions with the uncalled money already removed, so the two can't disagree again. A band every payer folded out of is now merged into the pot below rather than dropped, which was quietly shrinking the announced pot in another edge case. Validated with Playwright against a real hand on both surfaces — the table's live pot readout and Quick Split's all-in mode — plus undo, with no console errors. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_015bnp7xKUPFFSpwkYNFaPgb
125 lines
6.1 KiB
JavaScript
125 lines
6.1 KiB
JavaScript
const { test, describe } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const { PotCalculator } = require("./load");
|
|
|
|
const p = (id, contribution, folded = false) => ({ id, contribution, folded });
|
|
|
|
describe("PotCalculator.layers", () => {
|
|
test("one pot when everyone put in the same", () => {
|
|
const layers = PotCalculator.layers([p("a", 50), p("b", 50), p("c", 50)]);
|
|
assert.equal(layers.length, 1);
|
|
assert.equal(layers[0].amount, 150);
|
|
assert.equal(layers[0].label, "Main pot");
|
|
assert.deepEqual(layers[0].eligible.sort(), ["a", "b", "c"]);
|
|
});
|
|
|
|
test("a short all-in opens a side pot only the deeper players can win", () => {
|
|
// a is all-in for 20, b and c keep betting to 60.
|
|
const layers = PotCalculator.layers([p("a", 20), p("b", 60), p("c", 60)]);
|
|
assert.equal(layers.length, 2);
|
|
|
|
assert.equal(layers[0].amount, 60); // 20 x 3
|
|
assert.deepEqual(layers[0].eligible.sort(), ["a", "b", "c"]);
|
|
|
|
assert.equal(layers[1].amount, 80); // 40 x 2
|
|
assert.deepEqual(layers[1].eligible.sort(), ["b", "c"]);
|
|
assert.equal(layers[1].label, "Side pot 1");
|
|
});
|
|
|
|
test("folded money stays in the pot as dead money but wins nothing", () => {
|
|
const layers = PotCalculator.layers([p("a", 50), p("b", 50), p("folder", 50, true)]);
|
|
assert.equal(layers.length, 1);
|
|
assert.equal(layers[0].amount, 150, "the folder's chips are still in the middle");
|
|
assert.ok(!layers[0].eligible.includes("folder"));
|
|
});
|
|
|
|
test("layers with the same eligible players are announced as one pot", () => {
|
|
// Two folders at different heights would each open a band, but a dealer
|
|
// announces one pot between the two players who can actually win it.
|
|
const layers = PotCalculator.layers([
|
|
p("a", 100),
|
|
p("b", 100),
|
|
p("f1", 20, true),
|
|
p("f2", 40, true),
|
|
]);
|
|
const eligibleSets = layers.map(l => l.eligible.slice().sort().join(","));
|
|
assert.deepEqual(eligibleSets, ["a,b"], "one announceable pot, not three");
|
|
assert.equal(layers[0].amount, 260);
|
|
});
|
|
|
|
test("layer amounts always sum to the contested money", () => {
|
|
const players = [p("a", 12.5), p("b", 33.25), p("c", 33.25), p("d", 5, true)];
|
|
const total = players.reduce((s, x) => s + x.contribution, 0);
|
|
const uncalled = PotCalculator.uncalledReturns(players).reduce((s, r) => s + r.amount, 0);
|
|
const layered = PotCalculator.layers(players).reduce((s, l) => s + l.amount, 0);
|
|
assert.equal(Math.round((layered + uncalled) * 100), Math.round(total * 100));
|
|
});
|
|
|
|
test("nobody contributed means no pots, not an empty main pot", () => {
|
|
assert.deepEqual(PotCalculator.layers([p("a", 0), p("b", 0)]), []);
|
|
});
|
|
});
|
|
|
|
describe("PotCalculator.uncalledReturns", () => {
|
|
test("an overbet nobody called goes back to whoever bet it", () => {
|
|
// b shoves 200, a calls only 50 and everyone else folds.
|
|
const returns = PotCalculator.uncalledReturns([p("a", 50), p("b", 200)]);
|
|
assert.deepEqual(returns, [{ id: "b", amount: 150 }]);
|
|
});
|
|
|
|
test("a called bet leaves nothing to return", () => {
|
|
assert.deepEqual(PotCalculator.uncalledReturns([p("a", 50), p("b", 50)]), []);
|
|
});
|
|
|
|
test("the cap is the biggest live contribution, so a folder's overbet comes back", () => {
|
|
const returns = PotCalculator.uncalledReturns([p("live", 30), p("bully", 90, true)]);
|
|
assert.deepEqual(returns, [{ id: "bully", amount: 60 }]);
|
|
});
|
|
|
|
test("with nobody live it stays quiet rather than refunding the table", () => {
|
|
// Every fold flag set is a dealer mistake to correct, not a signal that
|
|
// every chip on the felt was uncalled.
|
|
assert.deepEqual(PotCalculator.uncalledReturns([p("a", 10, true), p("b", 20, true)]), []);
|
|
});
|
|
});
|
|
|
|
describe("PotCalculator.resolveRound", () => {
|
|
test("a single winner takes the layer they were matched for", () => {
|
|
const entries = PotCalculator.buildEntries([p("a", 50), p("b", 50)]);
|
|
const result = PotCalculator.resolveRound(entries, ["a"]);
|
|
assert.equal(result.payoutPool, 100);
|
|
assert.deepEqual(result.perWinner, [{ id: "a", amount: 100 }]);
|
|
assert.equal(result.remainingContenders.length, 0);
|
|
});
|
|
|
|
test("an all-in winner only takes what everyone matched, the rest rolls forward", () => {
|
|
const entries = PotCalculator.buildEntries([p("short", 20), p("b", 60), p("c", 60)]);
|
|
const result = PotCalculator.resolveRound(entries, ["short"]);
|
|
assert.equal(result.capApplied, 20);
|
|
assert.equal(result.payoutPool, 60, "20 from each of three players");
|
|
// b and c still have 40 each left to play for.
|
|
assert.deepEqual(result.remainingContenders.map(e => e.id).sort(), ["b", "c"]);
|
|
assert.equal(PotCalculator.totalPot(result.updatedEntries), 80);
|
|
});
|
|
|
|
test("a chopped pot splits evenly", () => {
|
|
const entries = PotCalculator.buildEntries([p("a", 30), p("b", 30)]);
|
|
const result = PotCalculator.resolveRound(entries, ["a", "b"]);
|
|
assert.deepEqual(result.perWinner, [{ id: "a", amount: 30 }, { id: "b", amount: 30 }]);
|
|
});
|
|
|
|
test("odd cents go to the winners in selection order and nothing is lost", () => {
|
|
// A $10.01 pot three ways doesn't divide: 3.34 / 3.34 / 3.33.
|
|
const entries = PotCalculator.buildEntries([p("a", 3.34), p("b", 3.34), p("c", 3.33)]);
|
|
const result = PotCalculator.resolveRound(entries, ["a", "b", "c"]);
|
|
const paid = result.perWinner.reduce((s, w) => s + w.amount, 0);
|
|
assert.equal(Math.round(paid * 100), Math.round(result.payoutPool * 100), "no chips lost to rounding");
|
|
assert.ok(result.perWinner[0].amount >= result.perWinner[2].amount, "odd cent goes to the first selected");
|
|
});
|
|
|
|
test("selecting nobody who can win is an error, not a silent no-op", () => {
|
|
const entries = PotCalculator.buildEntries([p("a", 50), p("b", 50)]);
|
|
assert.throws(() => PotCalculator.resolveRound(entries, ["ghost"]), /No valid winners/);
|
|
});
|
|
});
|