Files
pokerfreak/tests/pipeline_contract.rs
mmintel 04d5251acf Welt ohne fremden Namen: Pruefordner vollstaendig mitkopieren, Engine nur im eigenen Baum
Die Welt nannte 'pokerfreak' nur noch in zwei Harness-Zeilen - der Rest waren veraltete
Rust-Pruefungen im Pruefordner, die der Aufbau nie auffrischte.
2026-11-01 23:37:11 +01:00

195 lines
6.7 KiB
Rust

//! End-to-end: request line → `wire::respond::respond` → response line, per VERTRAG.md.
mod support;
use rechner::wire::respond::respond;
use support::{action, board_json, cards_json, confidence, short, size};
/// The literal example from VERTRAG.md. Its FLOP lists a single card, so a
/// careful engine must refuse to reason about it.
const CONTRACT_EXAMPLE: &str = r#"{"variant":"NLHE","bigBlind":0.1,"seats":6,"heroSeat":5,"heroCards":[{"rank":"A","suit":"HEARTS"},{"rank":"5","suit":"HEARTS"}],"board":{"FLOP":[{"rank":"A","suit":"CLUBS"}],"TURN":null,"RIVER":null},"street":"FLOP","pot":1.25,"toCall":0.2,"heroStack":2.4,"minRaiseTo":0.5,"maxRaiseTo":20.0,"legalActions":["FOLD","CALL","RAISE"],"allowedSizes":[0.5,1.15,4.6],"villains":[{"seat":2,"stack":10.4,"folded":false}]}"#;
#[allow(clippy::too_many_arguments)]
fn request(
variant: &str,
hero: &str,
board: &str,
street: &str,
pot: f64,
to_call: f64,
legal: &str,
extra: &str,
) -> String {
format!(
r#"{{"variant":"{variant}","bigBlind":0.1,"seats":6,"heroSeat":5,"buttonSeat":5,"heroCards":{},"board":{},"street":"{street}","pot":{pot},"toCall":{to_call},"heroStack":2.4,"minRaiseTo":0.5,"maxRaiseTo":20.0,"legalActions":{legal},"villains":[{{"seat":2,"stack":10.4,"folded":false}}]{extra}}}"#,
cards_json(&short(hero)),
board_json(&short(board)),
)
}
fn is_one_json_line(response: &str) -> bool {
response.starts_with('{') && response.ends_with('}') && !response.contains('\n')
}
#[test]
fn contract_example_with_one_card_flop_is_refused_with_abstain() {
let out = respond(CONTRACT_EXAMPLE);
assert!(is_one_json_line(&out), "{out}");
assert_eq!(action(&out), "ABSTAIN", "{out}");
assert_eq!(confidence(&out), 0.0);
}
#[test]
fn contract_example_with_full_flop_answers_legally_from_allowed_sizes() {
let line = CONTRACT_EXAMPLE.replace(
r#""FLOP":[{"rank":"A","suit":"CLUBS"}]"#,
r#""FLOP":[{"rank":"A","suit":"CLUBS"},{"rank":"9","suit":"DIAMONDS"},{"rank":"4","suit":"SPADES"}]"#,
);
let out = respond(&line);
assert!(is_one_json_line(&out), "{out}");
let act = action(&out);
assert!(["FOLD", "CALL", "RAISE", "ABSTAIN"].contains(&act.as_str()), "{out}");
if act == "RAISE" {
let s = size(&out).expect("raise carries a size");
assert!(s == 0.5 || s == 1.15, "size must come from allowedSizes and be affordable: {out}");
} else {
assert_eq!(size(&out), None, "{out}");
}
// Top pair, weak kicker, getting 0.2 into 1.45: never a fold.
assert_ne!(act, "FOLD", "{out}");
let c = confidence(&out);
assert!((0.0..=1.0).contains(&c));
}
#[test]
fn response_has_contract_keys_in_order() {
let out = respond(CONTRACT_EXAMPLE);
let positions: Vec<usize> = ["\"action\"", "\"size\"", "\"confidence\"", "\"reason\""]
.iter()
.map(|k| out.find(k).unwrap_or_else(|| panic!("missing {k} in {out}")))
.collect();
assert!(positions.windows(2).all(|w| w[0] < w[1]), "{out}");
}
#[test]
fn garbage_and_empty_objects_abstain() {
for line in ["", "not json", "{}", "[1,2]", r#"{"variant":"STUD"}"#] {
let out = respond(line);
assert!(is_one_json_line(&out), "{line:?} → {out}");
assert_eq!(action(&out), "ABSTAIN", "{line:?} → {out}");
}
}
#[test]
fn identical_requests_give_identical_answers() {
let line = request("NLHE", "Ah Qh", "Kh 7h 2c", "FLOP", 1.0, 0.5, r#"["FOLD","CALL","RAISE"]"#, "");
assert_eq!(respond(&line), respond(&line));
}
#[test]
fn preflop_aces_on_the_button_raise() {
let line = request("NLHE", "As Ad", "", "PREFLOP", 0.15, 0.1, r#"["FOLD","CALL","RAISE"]"#, "");
let out = respond(&line);
assert_eq!(action(&out), "RAISE", "{out}");
let s = size(&out).unwrap();
assert!((0.5..=2.4).contains(&s), "{out}");
}
#[test]
fn preflop_trash_folds_or_abstains() {
let line = format!(
r#"{{"variant":"NLHE","bigBlind":0.1,"seats":6,"heroSeat":3,"buttonSeat":6,"heroCards":{},"street":"PREFLOP","pot":0.15,"toCall":0.1,"heroStack":10.0,"minRaiseTo":0.2,"maxRaiseTo":10.0,"legalActions":["FOLD","CALL","RAISE"],"villains":[{{"seat":1,"stack":10.0,"folded":false}}]}}"#,
cards_json(&short("7c 2d"))
);
let out = respond(&line);
assert!(["FOLD", "ABSTAIN"].contains(&action(&out).as_str()), "{out}");
}
#[test]
fn river_nuts_never_folds_and_bets_when_possible() {
let line = request(
"NLHE",
"Ah Kh",
"Qh Jh Th 2c 3d",
"RIVER",
1.0,
0.0,
r#"["CHECK","RAISE"]"#,
"",
);
let out = respond(&line);
assert_eq!(action(&out), "RAISE", "royal flush must bet: {out}");
}
#[test]
fn drawing_dead_on_the_river_facing_a_bet_folds() {
// Board is a straight flush 9h-Kh; hero's cards play nothing and villain can only tie or win.
let line = request(
"NLHE",
"2c 3d",
"9h Th Jh Qh Kh",
"RIVER",
2.0,
2.0,
r#"["FOLD","CALL"]"#,
"",
);
let out = respond(&line);
// Everyone plays the board: a split. Break-even 2/4 = 0.5 against a share ≤ 0.5 → no call.
assert!(["FOLD", "ABSTAIN"].contains(&action(&out).as_str()), "{out}");
}
#[test]
fn check_is_preferred_over_fold_when_free() {
let line = request("NLHE", "7c 2d", "Ah Kh Qs", "FLOP", 1.0, 0.0, r#"["FOLD","CHECK","RAISE"]"#, "");
let out = respond(&line);
assert_ne!(action(&out), "FOLD", "{out}");
}
#[test]
fn k_poker_raises_only_from_menu() {
let line = request(
"K_POKER",
"As Ad",
"Ac Kd 7s",
"FLOP",
1.0,
0.0,
r#"["CHECK","RAISE"]"#,
r#","allowedSizes":[0.6,1.3]"#,
);
let out = respond(&line);
if action(&out) == "RAISE" {
let s = size(&out).unwrap();
assert!(s == 0.6 || s == 1.3, "{out}");
}
}
#[test]
fn aof_is_binary() {
let line = format!(
r#"{{"variant":"AOF","bigBlind":1,"seats":2,"heroSeat":1,"buttonSeat":1,"heroCards":{},"street":"PREFLOP","pot":1.5,"toCall":0.5,"heroStack":10,"minRaiseTo":10,"maxRaiseTo":10,"legalActions":["FOLD","RAISE"],"villains":[{{"seat":2,"stack":10,"folded":false}}]}}"#,
cards_json(&short("Kc Kd"))
);
let out = respond(&line);
assert_eq!(action(&out), "RAISE", "{out}");
assert_eq!(size(&out), Some(10.0), "AOF raise is all-in: {out}");
}
#[test]
fn plo5_requires_five_hole_cards() {
let bad = request("PLO5", "Ah Kh", "Qh Jh 2c", "FLOP", 1.0, 0.0, r#"["CHECK","RAISE"]"#, "");
assert_eq!(action(&respond(&bad)), "ABSTAIN");
let good = request(
"PLO5",
"Ah Kh Ad Kd 9s",
"Qh Jh 2c",
"FLOP",
1.0,
0.0,
r#"["CHECK","RAISE"]"#,
"",
);
assert_ne!(action(&respond(&good)), "FOLD");
}