Files
JoshHeaps.Net/tests/holdem/readingTheShowdown.test.js
jheapsandClaude Opus 5 cd57c16050 Let the table tell the dealer who won
An option — off by default, and off means the tool is exactly what it was.

Turn it on and the community cards go on the felt. Each street asks for the cards as
it is dealt: the picker opens on the flop the moment the flop button is pressed, and
again on the turn and the river, because the moment a dealer is holding those cards is
the only moment they know what they are without turning the board round. Three taps,
then one, then one. The picker is the same sheet as the hole cards, which now knows
about the middle of the table too — the seven of clubs cannot be on the flop and in
somebody's hand, and until now it could.

Turning the players' cards over is unchanged. That already worked the way it should.

What is new is what happens next. Resolve Pot stops asking who won and says so:

    Alexandra wins $50 with three of a kind, nines.

with every hand at the table read out under its player's name, the winner already
ticked, and the button reading "Pay Alexandra". A chop comes out as "Christopher and
Mike from work split it, $22.50 each, with a pair of aces", both ticked. The dealer
still confirms — the tool reads the cards, a person still says the pot is good, and a
hand entered wrong is corrected by unticking rather than by starting over.

It declines rather than guesses. A board that isn't five cards, a player who hasn't
turned over, the same card entered in two places: each says which of those it is and
hands the question back, and the wizard is the one it always was. Who is *ahead* and
who *won* are different claims and only the second one can pay somebody.

Side pots are read per round, not once. The best hand takes the main pot and is then
out of the question; the side pot goes to the best of who is left, which is a different
answer and the one the tool has to ask for again.

handRank.js is the only module here that can pay the wrong person without anybody
noticing, so it is built to be checkable rather than clever: rank five cards, then take
the best of the twenty-one five-card hands in seven. Beside the written cases — every
category against every other, the wheel and the steel wheel, three kickers deep, the
counterfeited two pair, the board that plays — it is checked against a second evaluator
written a different way, on twenty thousand random seven-card hands and four thousand
random pairs of them. An ace that keeps its value in the wheel, a dropped two-pair
kicker and a flush read only by its top card were each introduced on purpose and each
caught.

The board takes the middle of the felt, sized from the room the seat ring actually
leaves — measured against every seat rather than the ring's widest point, which is only
the answer for a seat sitting exactly on the horizontal axis and had two pucks on top
of the board while claiming 77px of clearance either side. Where there is no middle to
take, it moves to a strip above the table: a phone on its side has a felt 528x83 with
55px pucks, so the ring closes over the centre entirely and a board in it is a board
with players painted on top.

That strip found an older bug. It costs the felt height, and a felt that changes size
had only ever been re-measured, never laid out again: the seats follow it on their own,
being placed in percentages, but a bet chip anchored inside a seat is placed in pixels
off that seat's centre. Measured at 640x360, the chips were positioned for a felt 83px
tall and painted on one of 60 — seat 3's $5 came to rest on seat 4's puck, and two
pairs of seat pucks were overlapping. The bar growing mid-hand does the same thing: a
side pot appearing is 55px off the felt. Both fixed.

The sweep grew a third table — the same five players in the same nine chairs as the
second, with the board on, so the only thing that differs between those two rows is the
board itself. It earned its place on the first run.

728 unit tests. 216 audits: what is left is the seat ring hanging past the rail on a
landscape phone, the known residual, plus five or six pixels more of it on the layouts
where the strip takes the felt's height, and a blind badge over a stack on a packed
twelve-seat portrait table.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_015bnp7xKUPFFSpwkYNFaPgb
2026-09-04 11:18:33 -06:00

275 lines
11 KiB
JavaScript

// Reading the showdown: the picker that takes the cards in, and the pot maths that
// pays somebody on the strength of them.
//
// The evaluator is tested to death next door in handRank.test.js. What is tested here
// is the join — the two places where a correct ranking can still pay the wrong player:
// a side pot, where the question is asked again of a smaller field, and a card entered
// in two places, where the ranking is of a deck that does not exist.
const test = require("node:test");
const { describe } = require("node:test");
const assert = require("node:assert");
const { CardPicker, HandRank, HoldemState, PotCalculator, Money, makeTable } = require("./load");
describe("the picker, filling the board", () => {
const table = (board = ["", "", "", "", ""]) => {
const state = makeTable({ seatCount: 4, stack: 100 });
state.board = board;
return state;
};
// The sheet's own drawing is the DOM's business; the slot bookkeeping under it is
// not, and that is what these ask about.
const headless = () => { CardPicker.render = () => {}; CardPicker.close = () => {}; };
const onBoard = (state, cardIndex = 0, limit = 5) => {
headless();
CardPicker.state = state;
CardPicker.hooks = { onPick: () => {} };
CardPicker.target = "board";
CardPicker.boardLimit = limit;
CardPicker.cardIndex = cardIndex;
return CardPicker;
};
const onSeat = (state, seatIndex, cardIndex = 0) => {
headless();
CardPicker.state = state;
CardPicker.hooks = { onPick: () => {} };
CardPicker.target = "seat";
CardPicker.seatIndex = seatIndex;
CardPicker.cardIndex = cardIndex;
return CardPicker;
};
test("it only offers the slots that have actually been turned", () => {
const state = table();
assert.equal(onBoard(state, 0, 3).slotValues().length, 3, "the flop is three");
assert.equal(onBoard(state, 0, 4).slotValues().length, 4, "the turn makes four");
assert.equal(onBoard(state, 0, 5).slotValues().length, 5);
});
test("a card goes into the board, not into a seat", () => {
const state = table();
onBoard(state, 1, 3).assign("KD");
assert.deepEqual(state.board, ["", "KD", "", "", ""]);
assert.deepEqual(state.seats[0].cards, ["", ""]);
});
test("it walks on to the next empty slot, so a flop is three taps", () => {
const state = table();
const picker = onBoard(state, 0, 3);
picker.assign("KD");
assert.equal(picker.cardIndex, 1);
picker.assign("9C");
assert.equal(picker.cardIndex, 2);
picker.assign("4S");
assert.deepEqual(state.board.slice(0, 3), ["KD", "9C", "4S"]);
});
test("and it does not walk past the slots it was given", () => {
// Filling the turn must not run on to the river: that card is still in the deck.
const state = table(["KD", "9C", "4S", "", ""]);
const picker = onBoard(state, 3, 4);
picker.assign("AH");
assert.deepEqual(state.board, ["KD", "9C", "4S", "AH", ""]);
});
test("clearing a slot empties that one and nothing else", () => {
const state = table(["KD", "9C", "4S", "", ""]);
onBoard(state, 1, 3).clearSlot();
assert.deepEqual(state.board, ["KD", "", "4S", "", ""]);
});
});
describe("a card cannot be in two places", () => {
const table = () => {
const state = makeTable({ seatCount: 4, stack: 100 });
state.board = ["KD", "9C", "4S", "", ""];
state.seats[0].cards = ["AH", "AS"];
state.seats[1].cards = ["", ""];
return state;
};
const picker = (state, setup) => {
CardPicker.state = state;
CardPicker.hooks = { onPick: () => {} };
setup();
return CardPicker;
};
test("the board's cards are unavailable to a hand", () => {
// The half of this that did not exist before: the picker knew about every
// player's cards and nothing about the middle of the table, so the king of
// diamonds on the flop could be dealt to somebody as well.
const state = table();
const used = picker(state, () => {
CardPicker.target = "seat";
CardPicker.seatIndex = 1;
CardPicker.cardIndex = 0;
}).cardsInUse();
assert.equal(used.get("KD"), "the board");
assert.equal(used.get("9C"), "the board");
assert.ok(used.has("AH"), "and the other players' are still in there");
});
test("and a hand's cards are unavailable to the board", () => {
const state = table();
const used = picker(state, () => {
CardPicker.target = "board";
CardPicker.boardLimit = 5;
CardPicker.cardIndex = 3;
}).cardsInUse();
assert.ok(used.has("AH"), "the ace in seat 1's hand");
assert.ok(used.has("KD"), "and the rest of the board");
});
test("but the slot being edited is not competing with itself", () => {
const state = table();
const used = picker(state, () => {
CardPicker.target = "board";
CardPicker.boardLimit = 3;
CardPicker.cardIndex = 0;
}).cardsInUse();
assert.equal(used.has("KD"), false, "re-picking what is already there is not a duplicate");
assert.equal(used.has("9C"), true);
});
test("the same goes for a seat's own slot", () => {
const state = table();
const used = picker(state, () => {
CardPicker.target = "seat";
CardPicker.seatIndex = 0;
CardPicker.cardIndex = 0;
}).cardsInUse();
assert.equal(used.has("AH"), false);
assert.equal(used.has("AS"), true, "the player's other card still is");
});
});
describe("who wins a side pot", () => {
// The case a single ranking of the whole table gets wrong. The best hand takes the
// main pot and is then out of the question: the side pot goes to the best of who is
// left, which is a different answer, and it is the answer the wizard has to ask for
// again each round rather than reading off one list.
//
// Three players, and the short stack has the best hand of the three.
const board = ["KD", "9C", "4S", "AH", "2H"];
const hands = {
short: ["9H", "9S"], // trips — the best hand at the table
big: ["AS", "KC"], // two pair, aces and kings
other: ["AC", "3D"], // a pair of aces
};
const entries = () => PotCalculator.buildEntries([
{ id: "short", contribution: 20, folded: false },
{ id: "big", contribution: 100, folded: false },
{ id: "other", contribution: 100, folded: false },
]);
// What the wizard does: read the hands still in for *this* round.
const readRound = state => HandRank.showdown(board,
PotCalculator.getContenders(state).map(c => ({ id: c.id, cards: hands[c.id] })));
test("the short stack takes the main pot with the best hand", () => {
const round = readRound(entries());
assert.equal(round.complete, true);
assert.deepEqual(round.winners, ["short"]);
});
test("and the side pot goes to the best of the two who are left", () => {
const first = PotCalculator.resolveRound(entries(), ["short"]);
// 20 from each of three: the short stack is paid 60 and is out of it.
assert.equal(Money.round(first.perWinner[0].amount), 60);
const round = readRound(first.updatedEntries);
assert.deepEqual(round.winners, ["big"],
"not the trips, which are no longer eligible for this money");
assert.equal(round.players.length, 2, "and the short stack is not even offered");
const second = PotCalculator.resolveRound(first.updatedEntries, ["big"]);
assert.equal(Money.round(second.perWinner[0].amount), 160);
});
test("every chip is accounted for across the two rounds", () => {
const first = PotCalculator.resolveRound(entries(), ["short"]);
const second = PotCalculator.resolveRound(first.updatedEntries, ["big"]);
assert.equal(Money.round(first.payoutPool + second.payoutPool), 220);
assert.equal(PotCalculator.getContenders(second.updatedEntries).length, 0);
});
});
describe("what it refuses to answer", () => {
const board = ["KD", "9C", "4S", "AH", "2H"];
test("a folded player's cards are not in the running", () => {
// They are not a contender, so they are never handed in — which means a hand
// that would have won cannot take a pot its owner threw away.
const table = PotCalculator.buildEntries([
{ id: "live", contribution: 50, folded: false },
{ id: "mucked", contribution: 50, folded: true },
]);
const contenders = PotCalculator.getContenders(table);
assert.deepEqual(contenders.map(c => c.id), ["live"]);
const round = HandRank.showdown(board, contenders.map(c => ({ id: c.id, cards: ["7C", "5D"] })));
assert.deepEqual(round.winners, ["live"], "even holding nothing");
});
test("one hand not turned over and it hands the question back", () => {
const round = HandRank.showdown(board, [
{ id: "a", cards: ["9H", "9S"] },
{ id: "b", cards: ["", ""] },
]);
assert.equal(round.complete, false);
assert.deepEqual(round.winners, []);
assert.deepEqual(round.unknown, ["b"]);
});
test("a board still being dealt is not a showdown", () => {
const round = HandRank.showdown(["KD", "9C", "4S", "", ""], [
{ id: "a", cards: ["9H", "9S"] },
{ id: "b", cards: ["AS", "KC"] },
]);
assert.equal(round.complete, false);
assert.deepEqual(round.winners, []);
});
test("a card entered twice stops it rather than guessing", () => {
// The failure mode that matters: a mis-typed card produces a hand that ranks
// perfectly well and is not the one on the table. Refusing is the only honest
// answer, and the dealer picks the winner the way they always could.
const round = HandRank.showdown(board, [
{ id: "a", cards: ["9H", "9S"] },
{ id: "b", cards: ["KD", "QC"] }, // the king of diamonds is on the board
]);
assert.equal(round.duplicated, true);
assert.equal(round.complete, false);
assert.deepEqual(round.winners, []);
});
test("and two players holding the same card is caught as well", () => {
const round = HandRank.showdown(board, [
{ id: "a", cards: ["QH", "QS"] },
{ id: "b", cards: ["QH", "JC"] },
]);
assert.equal(round.duplicated, true);
assert.equal(round.complete, false);
});
});
describe("a table that never asked for any of this", () => {
test("has no board, tracks nothing, and reads no showdown", () => {
const state = HoldemState.createDefault();
assert.equal(state.trackBoard, false);
// Which is what the wizard checks before it reads anything at all — see
// readShowdown() in potWizard.js. With it off, an empty board would decline
// anyway, so the tool cannot start answering a question nobody asked.
const round = HandRank.showdown(state.board, [
{ id: "a", cards: ["9H", "9S"] },
{ id: "b", cards: ["AS", "KC"] },
]);
assert.equal(round.complete, false);
assert.deepEqual(round.winners, []);
});
});