End-to-end spec v2: short deck, history, position inference, multiway, small-confidence abstain (red)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 12:29:20 +01:00
co-authored by Claude Opus 5.5
parent 30aaee6e1b
commit d5aa53f1a4
2 changed files with 237 additions and 3 deletions
+208
View File
@@ -0,0 +1,208 @@
//! End-to-end spec for the postflop upgrade, short deck and position inference.
mod support;
use pokerfreak::wire::respond::respond;
use support::{action, board_json, cards_json, confidence, field, short, size};
struct Request<'a> {
variant: &'a str,
hero: &'a str,
board: &'a str,
street: &'a str,
pot: f64,
to_call: f64,
legal: &'a str,
villains: usize,
extra: &'a str,
}
impl Request<'_> {
fn line(&self) -> String {
let villains: Vec<String> = (0..self.villains)
.map(|i| format!(r#"{{"seat":{},"stack":40.0,"folded":false}}"#, i + 1))
.collect();
format!(
r#"{{"variant":"{}","bigBlind":1,"seats":6,"heroSeat":6,"buttonSeat":6,"heroCards":{},"board":{},"street":"{}","pot":{},"toCall":{},"heroStack":40,"minRaiseTo":{},"maxRaiseTo":40,"legalActions":{},"villains":[{}]{}}}"#,
self.variant,
cards_json(&short(self.hero)),
board_json(&short(self.board)),
self.street,
self.pot,
self.to_call,
(self.to_call * 2.0).max(1.0),
self.legal,
villains.join(","),
self.extra,
)
}
}
fn base() -> Request<'static> {
Request {
variant: "NLHE",
hero: "Ah Kd",
board: "Ks 7c 2d",
street: "FLOP",
pot: 10.0,
to_call: 0.0,
legal: r#"["CHECK","RAISE"]"#,
villains: 1,
extra: "",
}
}
fn reason(out: &str) -> String {
field(out, "reason").unwrap_or("").to_string()
}
// ---------- Short deck ----------
#[test]
fn short_deck_and_its_alias_are_accepted() {
for name in ["SHORT_DECK", "6+"] {
let out = respond(&Request { variant: name, hero: "Ah Ad", board: "As 9c 7d", ..base() }.line());
assert_ne!(action(&out), "ABSTAIN", "{name}: {out}");
}
}
#[test]
fn short_deck_rejects_cards_below_six() {
let out = respond(&Request { variant: "SHORT_DECK", hero: "5h 5d", board: "As 9c 7d", ..base() }.line());
assert_eq!(action(&out), "ABSTAIN", "{out}");
assert_eq!(confidence(&out), 0.0);
}
#[test]
fn short_deck_flush_is_played_as_a_monster_over_a_full_house_draw() {
// Hero holds the made flush; in short deck a flush beats a full house, so a
// villain's set that fills up is still behind — the flush must bet.
let out = respond(
&Request { variant: "SHORT_DECK", hero: "Ah 9h", board: "Kh 8h 6h 8c", street: "TURN", ..base() }
.line(),
);
assert_eq!(action(&out), "RAISE", "{out}");
}
#[test]
fn short_deck_respects_allowed_sizes() {
let out = respond(
&Request {
variant: "SHORT_DECK",
hero: "Ah Ad",
board: "As 9c 7d",
extra: r#","allowedSizes":[3.5,7.25]"#,
..base()
}
.line(),
);
if action(&out) == "RAISE" {
let s = size(&out).unwrap();
assert!(s == 3.5 || s == 7.25, "{out}");
}
}
// ---------- Action history ----------
#[test]
fn malformed_history_abstains_with_its_path() {
let out = respond(
&Request { extra: r#","history":[{"street":"FLOP","seat":1,"action":"DANCE"}]"#, ..base() }.line(),
);
assert_eq!(action(&out), "ABSTAIN", "{out}");
assert!(reason(&out).contains("history[0]"), "{out}");
}
#[test]
fn preflop_raiser_betting_flop_and_turn_narrows_range_so_middle_pair_gives_up() {
// Hero 9-9 on K-7-2-4 facing a turn bet from a player who raised preflop and bet both streets.
let history = r#","history":[{"street":"PREFLOP","seat":1,"action":"RAISE","amount":3},{"street":"PREFLOP","seat":6,"action":"CALL","amount":3},{"street":"FLOP","seat":1,"action":"BET","amount":5},{"street":"FLOP","seat":6,"action":"CALL","amount":5},{"street":"TURN","seat":1,"action":"BET","amount":14}]"#;
let narrowed = respond(
&Request {
hero: "9h 9c",
board: "Ks 7c 2d 4h",
street: "TURN",
pot: 30.0,
to_call: 14.0,
legal: r#"["FOLD","CALL","RAISE"]"#,
extra: history,
..base()
}
.line(),
);
assert!(["FOLD", "ABSTAIN"].contains(&action(&narrowed).as_str()), "{narrowed}");
}
#[test]
fn position_is_derived_from_history_when_button_is_missing() {
let line = format!(
r#"{{"variant":"NLHE","bigBlind":1,"seats":6,"heroSeat":4,"heroCards":{},"street":"PREFLOP","pot":1.5,"toCall":1,"heroStack":100,"minRaiseTo":2,"maxRaiseTo":100,"legalActions":["FOLD","CALL","RAISE"],"villains":[{{"seat":1,"stack":100,"folded":false}},{{"seat":2,"stack":100,"folded":false}}],"history":[{{"street":"PREFLOP","seat":1,"action":"POST_SB","amount":0.5}},{{"street":"PREFLOP","seat":2,"action":"POST_BB","amount":1}}]}}"#,
cards_json(&short("Ah Kh"))
);
let out = respond(&line);
assert!(!reason(&out).contains("pos?"), "{out}");
}
// ---------- Multiway and semibluff ----------
#[test]
fn a_call_heads_up_is_not_a_call_four_way() {
let spot = |villains| Request {
hero: "Qh Jd",
board: "Qs 8c 3d",
pot: 12.0,
to_call: 6.0,
legal: r#"["FOLD","CALL","RAISE"]"#,
villains,
..base()
};
let heads_up = respond(&spot(1).line());
let four_way = respond(&spot(4).line());
assert_eq!(action(&heads_up), "CALL", "{heads_up}");
assert!(["FOLD", "ABSTAIN"].contains(&action(&four_way).as_str()), "{four_way}");
}
#[test]
fn reasons_name_the_hand_read() {
let out = respond(&base().line());
let text = reason(&out).to_lowercase();
assert!(text.contains("top pair"), "{out}");
}
#[test]
fn no_semibluff_on_the_river() {
let out = respond(
&Request {
hero: "Jh Th",
board: "9h 8h 2c 3d Ks",
street: "RIVER",
legal: r#"["CHECK","RAISE"]"#,
..base()
}
.line(),
);
assert_eq!(action(&out), "CHECK", "missed draw on the river: {out}");
}
#[test]
fn uncertain_abstain_carries_small_but_positive_confidence() {
// Scan pot sizes around the breakpoint; any uncertainty ABSTAIN must have 0 < c <= 0.3.
(1..40).for_each(|i| {
let to_call = i as f64 * 0.5;
let out = respond(
&Request {
hero: "Ad 5c",
board: "Ks 9c 4h",
pot: 10.0,
to_call,
legal: r#"["FOLD","CALL"]"#,
..base()
}
.line(),
);
if action(&out) == "ABSTAIN" && reason(&out).contains("uncertain") {
let c = confidence(&out);
assert!(c > 0.0 && c <= 0.3, "{out}");
}
});
}
+29 -3
View File
@@ -33,6 +33,28 @@ fn chips(rng: Rng, low: f64, high: f64) -> (f64, Rng) {
(((low + u * (high - low)) * 100.0).round() / 100.0, rng)
}
/// A random but plausible `history` extension field: blinds, then villain actions per street.
fn history(villain_count: usize, street: Street, rng: Rng) -> String {
let actions = ["FOLD", "CHECK", "CALL", "BET", "RAISE", "ALL_IN"];
let blinds = [r#"{"street":"PREFLOP","seat":1,"action":"POST_SB","amount":0.05}"#.to_string(),
r#"{"street":"PREFLOP","seat":2,"action":"POST_BB","amount":0.1}"#.to_string()];
let streets: Vec<Street> = Street::ALL.into_iter().filter(|s| *s <= street).collect();
let (entries, _) = streets.iter().flat_map(|s| (0..villain_count).map(move |v| (*s, v))).fold(
(blinds.to_vec(), rng),
|(mut entries, rng), (s, v)| {
let (action, rng) = pick(&actions, rng);
let (amount, rng) = chips(rng, 0.1, 5.0);
entries.push(format!(
r#"{{"street":"{}","seat":{},"action":"{action}","amount":{amount}}}"#,
s.name(),
v + 1
));
(entries, rng)
},
);
format!(r#","history":[{}]"#, entries.join(","))
}
fn generate(seed: u64) -> Generated {
let rng = Rng::seeded(seed);
let (variant, rng) = pick(&Variant::ALL, rng);
@@ -45,7 +67,9 @@ fn generate(seed: u64) -> Generated {
let (villain_count, rng) = rng.below(4);
let villain_count = villain_count as usize + 1;
let hole = rules(variant).hole_cards;
let (cards, rng) = deck::draw(&deck::full(), hole + street.board_len(), rng).unwrap();
let in_deck: Vec<_> =
deck::full().into_iter().filter(|c| rules(variant).deck.contains(c.rank)).collect();
let (cards, rng) = deck::draw(&in_deck, hole + street.board_len(), rng).unwrap();
let (hero, board) = cards.split_at(hole);
let (pot, rng) = chips(rng, 0.15, 30.0);
@@ -76,7 +100,9 @@ fn generate(seed: u64) -> Generated {
} else {
(None, rng)
};
let (folded_mask, _) = rng.below(1 << villain_count);
let (folded_mask, rng) = rng.below(1 << villain_count);
let (with_history, rng) = rng.below(2);
let history_json = if with_history == 1 { history(villain_count, street, rng) } else { String::new() };
let villains: Vec<String> = (0..villain_count)
.map(|i| {
@@ -90,7 +116,7 @@ fn generate(seed: u64) -> Generated {
format!(r#","allowedSizes":[{}]"#, inner.join(","))
});
let line = format!(
r#"{{"variant":"{}","bigBlind":0.1,"seats":6,"heroSeat":6,"buttonSeat":{},"heroCards":{},"board":{},"street":"{}","pot":{pot},"toCall":{to_call},"heroStack":{hero_stack},"minRaiseTo":{min_raise_to},"maxRaiseTo":{max_raise_to},"legalActions":[{}]{allowed_json},"villains":[{}],"seed":{seed}}}"#,
r#"{{"variant":"{}","bigBlind":0.1,"seats":6,"heroSeat":6,"buttonSeat":{},"heroCards":{},"board":{},"street":"{}","pot":{pot},"toCall":{to_call},"heroStack":{hero_stack},"minRaiseTo":{min_raise_to},"maxRaiseTo":{max_raise_to},"legalActions":[{}]{allowed_json},"villains":[{}]{history_json},"seed":{seed}}}"#,
variant.name(),
seed % 6 + 1,
cards_json(hero),