A poker engine test used to look like a page of addresses, big integers and action indexes. Now it reads like a hand history: "BTN raises to 3bb", "BB calls", AssertStacks(...). We've open-sourced the language as poker-scene-dsl, and the first time we pointed it at someone else's engine, it found two rules bugs on the first hand.
Why a DSL
Poker rules are written in English. "The big blind has the option." "A raise must be at least the size of the previous raise." "An all-in for less than a full raise doesn't reopen the betting." Engine tests are written in code, and the gap between the two is where bugs hide. A test that says raise(addr3, 200, idx 7) can be wrong in three ways before it tests anything: wrong player, wrong amount semantics, wrong index.
So the DSL lets a test say what a dealer would say:
sc.Act(
"SB posts small blind",
"BB posts big blind",
"deal",
"BTN raises to 3bb",
"SB folds",
"BB calls",
)
sc.AssertPot("650")
sc.Rejects("BB checks") // not BB's turn
sc.AssertLegal("BB", "check", "bet")What it handles for you
- Positions.
UTG,HJ,CO,BTNand the blinds are worked out from the button for 2 to 9 players, so a test never hard-codes which seat is under the gun. - "Raises to" means what it says at a real table.
"raises to 300"is the street total. If the player already has 100 in, the DSL works out that 200 goes in now."raises by 200"is there when you mean the increment. - Shoves go through whichever action the engine offers (all-in, raise, bet or call), at its maximum.
- Amounts can be chips or blinds:
"3bb","2sb". - Assertions use the same vocabulary: stacks by name or position, legal actions, ranges, pot, board, and chip conservation.
Rejects(sentence)checks that the engine refuses an action and that the table didn't change.
Engine-agnostic
The DSL doesn't know anything about our engine. It talks to any engine through two methods: tell me the table and apply this action. Every bet, raise and call carries both the chips going in now and the street total, so an adapter can send whichever its engine expects. Our own Go engine has an adapter that drives it through the same JSON path our chain uses.
The first outside engine
To see whether the abstraction held up, we wrote an adapter for poker-engine-ts, an open-source, event-sourced TypeScript engine with careful side-pot handling and property-tested chip conservation. It's written in TypeScript and the DSL in Go, so the adapter drives it through a small Node bridge.
Thirteen scenarios pass:
- blinds and folds;
- refusal of out-of-turn and illegal actions;
- showdowns;
- a three-way all-in with a main pot, a side pot and an unmatched remainder returned;
- a split pot;
- button and blind movement;
- four-handed postflop play with a short all-in.
The side-pot logic is genuinely solid.
Two scenarios fail, and they fail on the first preflop decision:
- Posted blinds aren't credited towards a call. With 50 posted, the small blind is charged 100 to complete a 100 big blind. The big blind then faces a bet instead of having the option to check. When the blinds call a raise to 300, the pot comes to 1,050 instead of 900.
- The first raise is sized from zero, not from the big blind. After a raise to 300, the minimum re-raise should be to 500 (300 plus the 200 raise). The engine requires 600. Later raises are sized correctly.
Both look like the same root cause: the posted blinds aren't counted as bets in the preflop round. And both would slip past a chip-conservation property test, because no chips go missing. The pot is just the wrong size. A rules-level scenario catches what an accounting invariant can't. We've reported both upstream (#1, #2), each with a standalone repro, and the adapter keeps them as skipped scenarios that will guard the fix.
One correction worth admitting: our first report said the engine always doubled the bet. When we checked a second re-raise, it was sized correctly, so we rewrote the issue within minutes. A DSL makes it cheap to try one more case, and we should have tried it before posting.
Bugs as scenarios
We now write every open engine bug as a scenario that asserts the correct behaviour. It's skipped by default, so CI stays green. The fix PR deletes one line, and from then on the scenario guards the fix. A bug report that ships with an executable, readable repro gets fixed faster, and it can't quietly come back.
poker-scene-dsl is MIT-licensed. If you build a poker engine, write an adapter: it takes an afternoon, and you'll learn something about your engine on the first hand.
A worked adapter, in Go and in TypeScript
Here is the whole surface a scenario asks an engine for. Two methods, and the one detail every betting adapter trips on: whether your engine thinks in chips going in now or in street totals. The DSL carries both on every action so you send whichever yours wants. Everything below is in the poker-scene-dsl README.
// What a scenario needs from any engine.
type Table interface {
State() scene.State // round, positions, blind amounts, pot, board, players with legal actions
Apply(scene.Action) error // reject with an error, leaving the table unchanged
}
// Every bet, raise and call carries both:
// Amount — the chips going in now
// Total — the player's street total afterwards
// so the adapter sends whichever its engine expects. Player.StreetBet is what
// the player already has in on this street; "raises to" needs it.For an engine written in Go, the adapter is one file in your test package: a projection of your state into the DSL's, and a switch that forwards each action. The names on the right are whatever your engine calls things. Once it exists, a test is a cast list and a hand history.
// mytable_test.go — adapt your own Go engine. Names on the right are yours.
type myTable struct{ g *myengine.Game }
func (t *myTable) State() scene.State {
s := scene.State{
Round: round(t.g.Street()), NextToAct: t.g.ToAct(),
Dealer: t.g.Button(), SmallBlind: t.g.SBSeat(), BigBlind: t.g.BBSeat(),
SmallBlindAmount: t.g.SB(), BigBlindAmount: t.g.BB(),
Pot: t.g.Pot(), Board: t.g.Board(), // cards as "AS", "KD", … (upper-case suit)
}
for _, p := range t.g.Players() {
s.Players = append(s.Players, scene.Player{
Address: p.ID, Seat: p.Seat, Stack: p.Stack,
StreetBet: p.InFrontThisStreet, // "raises to 300" subtracts this
Status: status[p.Status],
Legal: legal(p), // min/max as chips going in NOW
})
}
return s
}
func (t *myTable) Apply(a scene.Action) error {
switch a.Action {
case scene.ActJoin:
return t.g.Seat(a.Player, a.Seat, a.Amount)
case scene.ActBet, scene.ActRaise:
return t.g.Act(a.Player, a.Action, a.Total) // this engine thinks in street totals
case scene.ActNewHand:
return t.g.NewHand(a.Deck) // "new hand with deck AS-KD-…" lands here
default:
return t.g.Act(a.Player, a.Action, a.Amount) // fold, check, call, all-in, sit-in, leave, …
}
}
// Then a test reads like a hand history.
func TestMinRaiseReopens(t *testing.T) {
sc := scene.NewT(t, &myTable{g: myengine.New(6, 50, 100)},
map[string]string{"Alice": "addr-a", "Bob": "addr-b", "Carol": "addr-c"})
sc.MustJoin("Alice", 1, "1000")
sc.MustJoin("Bob", 2, "1000")
sc.MustJoin("Carol", 3, "1000")
sc.Act("Alice sits in", "Bob sits in", "Carol sits in")
sc.Act("SB posts small blind", "BB posts big blind", "deal")
sc.Act("BTN raises to 3bb", "SB calls")
sc.AssertRange("BB", "raise", "500", "1000") // min re-raise = 300 + 200, not 600
sc.Rejects("SB checks") // not SB's turn
sc.Act("BB calls")
sc.AssertRound(scene.Flop)
sc.AssertPot("900")
sc.AssertChips("3000") // stacks + pot: nothing created or lost
}An engine written in TypeScript plugs in through a small bridge that runs it in Node: one JSON request per line on stdin, one response per line on stdout, with the mapping done in TypeScript so your engine's types never reach Go. The Go side is a table that starts node once and translates scene.Action into those requests. That Go side already exists in the repository and is engine-agnostic, so for a TypeScript engine the bridge is the only thing you write.
// bridge.ts — your TypeScript engine behind a JSON-lines bridge.
// Requests: {"op":"create",…} {"op":"act","actor","type","amount","to"} {"op":"newHand","deck"} {"op":"state"}
import { createInterface } from "node:readline";
import { MyEngine } from "./engine";
let table: MyEngine;
function view() { // the same shape as scene.State
return {
stage: table.street(), // "preflop" | "flop" | "turn" | "river" | "showdown" | "end"
actor: table.toAct()?.id ?? "",
button: table.button(), smallBlind: table.sbPlayer(), bigBlind: table.bbPlayer(),
pot: table.pot(), board: table.board(),
seats: table.players().map(p => ({
id: p.id, seat: p.seat, stack: p.stack, streetBet: p.inFrontThisStreet, status: p.status,
})),
legal: table.legalActions().map(a => ({ type: a.kind, min: a.min, max: a.max })),
};
}
for await (const line of createInterface({ input: process.stdin })) {
const req = JSON.parse(line);
let out;
try {
switch (req.op) {
case "create": table = new MyEngine(req.seats, req.smallBlind, req.bigBlind); break;
case "act": table.act(req.actor, req.type, req.to ?? req.amount); break; // "to" = street total
case "newHand": table.newHand(req.deck); break;
}
out = { state: view() };
} catch (e) {
out = { error: String(e) }; // the DSL reports this as a rejection
}
process.stdout.write(JSON.stringify(out) + "\n");
}This is the exact shape of the adapter we wrote for poker-engine-ts: adapters/pokerenginets holds bridge.mjs, the Go table that drives it, and the thirteen passing and two skipped scenarios from the article above. Copy the pair, swap in your engine, and run:
go get github.com/block52/poker-scene-dsl
go test ./... # your scenarios
# the shipped TypeScript example, end to end
git clone https://github.com/block52/poker-scene-dsl && cd poker-scene-dsl
cd adapters/pokerenginets && npm ci && cd ../..
go test ./adapters/pokerenginets # scenarios that pass
POKER_ENGINE_TS_BUGS=1 go test -v ./adapters/pokerenginets # include the two known-bug scenariosPair it with poker-session-fuzzer and every fuzz failure arrives as one of these scenarios, minimised and ready to commit.
Want to build a poker platform?
We build provably fair, real-money poker where every hand is shuffled by verifiable randomness and settled on a public blockchain. We'd love to hear about your project.
Get in Touch