use rechner::card::{Card, Rank, Suit}; use rechner::fault::Fault; use rechner::game::spot::DEFAULT_SEED; use rechner::game::{ActionKind, Spot, Street}; use rechner::json::{parse, Value}; use rechner::variant::Variant; use rechner::wire::decode::decode; const 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}] }"#; const HERO: &str = r#""heroCards":[{"rank":"ACE","suit":"SPADES"},{"rank":"KING","suit":"SPADES"}],"#; const LEGAL: &str = r#"["FOLD","CALL"]"#; fn base() -> String { format!( r#"{{"variant":"NLHE","bigBlind":1,"seats":6,"heroSeat":0,{HERO} "street":"PREFLOP","pot":1.5,"toCall":1,"heroStack":100,"legalActions":{LEGAL}}}"# ) } fn run(text: &str) -> Result { decode(&parse(text).expect("test json")) } fn with(from: &str, to: &str) -> String { let b = base(); assert!(b.contains(from), "{from}"); b.replace(from, to) } /// Inserts extra members right before `"legalActions"`. fn plus(extra: &str) -> String { with(r#""legalActions""#, &format!(r#"{extra},"legalActions""#)) } fn field_of(f: Fault) -> String { match f { Fault::Invalid { field, .. } => field, Fault::Missing(field) => field, other => panic!("{other:?}"), } } #[test] fn decodes_the_contract_example() { let s = run(EXAMPLE).unwrap(); assert_eq!(s.variant, Variant::Nlhe); assert_eq!(s.big_blind, 0.1); assert_eq!(s.seats, 6); assert_eq!(s.hero_seat, 5); assert_eq!(s.button_seat, None); assert_eq!( s.hero_cards, vec![Card::new(Rank::Ace, Suit::Hearts), Card::new(Rank::Five, Suit::Hearts)] ); assert_eq!(s.board, vec![Card::new(Rank::Ace, Suit::Clubs)]); assert_eq!(s.street, Street::Flop); assert_eq!(s.pot, 1.25); assert_eq!(s.to_call, 0.2); assert_eq!(s.hero_stack, 2.4); assert_eq!(s.min_raise_to, Some(0.5)); assert_eq!(s.max_raise_to, Some(20.0)); assert_eq!(s.legal, vec![ActionKind::Fold, ActionKind::Call, ActionKind::Raise]); assert_eq!(s.allowed_sizes, Some(vec![0.5, 1.15, 4.6])); assert_eq!(s.villains.len(), 1); assert_eq!((s.villains[0].seat, s.villains[0].stack, s.villains[0].folded), (2, 10.4, false)); assert_eq!(s.villains[0].profile, None); assert_eq!(s.seed, DEFAULT_SEED); } #[test] fn optional_fields_default() { let s = run(&base()).unwrap(); assert!(s.board.is_empty()); assert_eq!(s.min_raise_to, None); assert_eq!(s.max_raise_to, None); assert_eq!(s.allowed_sizes, None); assert!(s.villains.is_empty()); assert_eq!(s.button_seat, None); assert_eq!(s.seed, DEFAULT_SEED); } #[test] fn null_optionals_are_absent() { let s = run(&plus( r#""minRaiseTo":null,"allowedSizes":null,"board":null,"villains":null,"seed":null,"buttonSeat":null"#, )) .unwrap(); assert_eq!(s.min_raise_to, None); assert_eq!(s.allowed_sizes, None); assert!(s.board.is_empty()); assert!(s.villains.is_empty()); assert_eq!(s.button_seat, None); } #[test] fn board_streets_concatenate_in_deal_order() { let board = r#""board":{"RIVER":[{"rank":"2","suit":"c"}],"TURN":[{"rank":"9","suit":"d"}], "FLOP":[{"rank":"A","suit":"s"},{"rank":"K","suit":"s"},{"rank":"Q","suit":"s"}]}"#; let s = run(&plus(board)).unwrap(); let shorts: Vec = s.board.iter().map(|c| c.short()).collect(); assert_eq!(shorts, vec!["As", "Ks", "Qs", "9d", "2c"]); } #[test] fn empty_board_object_is_empty() { assert!(run(&plus(r#""board":{}"#)).unwrap().board.is_empty()); } #[test] fn extension_fields() { let ext = r#""buttonSeat":3,"seed":12345,"villains":[ {"seat":1,"stack":50,"folded":true,"profile":{"vpip":0.45,"pfr":0.08,"foldToBet":0.3}}, {"seat":2,"stack":5,"folded":false,"profile":{"vpip":0.2}}]"#; let s = run(&plus(ext)).unwrap(); assert_eq!(s.button_seat, Some(3)); assert_eq!(s.seed, 12345); let p0 = s.villains[0].profile.unwrap(); assert_eq!((p0.vpip, p0.pfr, p0.fold_to_bet), (Some(0.45), Some(0.08), Some(0.3))); assert!(s.villains[0].folded); let p1 = s.villains[1].profile.unwrap(); assert_eq!((p1.vpip, p1.pfr, p1.fold_to_bet), (Some(0.2), None, None)); } /// The HUD extras the client already delivers (Phase E, `lesen/profil.py` DURCHGEREICHT) decode /// under their client names; absent ones stay `None`. #[test] fn hud_extra_fields() { let ext = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{ "cbet":0.58,"foldToCbet":0.34,"threeBet":0.09,"foldToThreeBet":0.62, "steal":0.41,"checkRaise":0.07,"wtsd":0.28,"wsd":0.54}}]"#; let p = run(&plus(ext)).unwrap().villains[0].profile.unwrap(); assert_eq!((p.cbet, p.fold_to_cbet), (Some(0.58), Some(0.34))); assert_eq!((p.three_bet, p.fold_to_three_bet), (Some(0.09), Some(0.62))); assert_eq!((p.steal, p.check_raise), (Some(0.41), Some(0.07))); assert_eq!((p.wtsd, p.wsd), (Some(0.28), Some(0.54))); let sparse = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"wtsd":0.6}}]"#; let q = run(&plus(sparse)).unwrap().villains[0].profile.unwrap(); assert_eq!(q.wtsd, Some(0.6)); assert_eq!((q.cbet, q.fold_to_cbet, q.three_bet, q.wsd), (None, None, None, None)); } #[test] fn unknown_fields_are_ignored() { let extra = r#""whatever":{"x":[1]},"villains":[{"seat":1,"stack":1,"folded":false,"nick":"x"}]"#; assert!(run(&plus(extra)).is_ok()); } #[test] fn card_forms() { let cards = r#""heroCards":[{"rank":"10","suit":"clubs"},{"rank":"jack","suit":"D"}],"#; let s = run(&with(HERO, cards)).unwrap(); assert_eq!( s.hero_cards, vec![Card::new(Rank::Ten, Suit::Clubs), Card::new(Rank::Jack, Suit::Diamonds)] ); } #[test] fn every_variant_street_and_action_name_decodes() { for v in ["NLHE", "PLO5", "K_POKER", "AOF"] { assert!(run(&with(r#""NLHE""#, &format!("\"{v}\""))).is_ok(), "{v}"); } for st in ["PREFLOP", "FLOP", "TURN", "RIVER"] { assert!(run(&with(r#""PREFLOP""#, &format!("\"{st}\""))).is_ok(), "{st}"); } let s = run(&with(LEGAL, r#"["FOLD","CHECK","CALL","RAISE"]"#)).unwrap(); assert_eq!(s.legal.len(), 4); } #[test] fn root_must_be_an_object() { assert_eq!(field_of(run("[1]").unwrap_err()), "request"); assert_eq!(field_of(run("5").unwrap_err()), "request"); } #[test] fn each_required_field_reports_missing() { let required = [ "variant", "bigBlind", "seats", "heroSeat", "heroCards", "street", "pot", "toCall", "heroStack", "legalActions", ]; for name in required { let Value::Object(fields) = parse(&base()).unwrap() else { panic!() }; let kept: Vec<_> = fields.into_iter().filter(|(k, _)| k != name).collect(); let err = decode(&Value::Object(kept)).unwrap_err(); assert_eq!(err, Fault::Missing(name.to_string()), "{name}"); } } #[test] fn wrong_types_name_the_field() { let cases: Vec<(String, &str)> = vec![ (with(r#""NLHE""#, "5"), "variant"), (with(r#""NLHE""#, r#""HOLDEM""#), "variant"), (with(r#""bigBlind":1"#, r#""bigBlind":"1""#), "bigBlind"), (with(r#""seats":6"#, r#""seats":6.5"#), "seats"), (with(r#""seats":6"#, r#""seats":-6"#), "seats"), (with(r#""seats":6"#, r#""seats":300"#), "seats"), (with(r#""heroSeat":0"#, r#""heroSeat":"0""#), "heroSeat"), (with(r#""PREFLOP""#, r#""SHOWDOWN""#), "street"), (with(r#""pot":1.5"#, r#""pot":null"#), "pot"), (with(r#""toCall":1"#, r#""toCall":true"#), "toCall"), (with(r#""heroStack":100"#, r#""heroStack":[]"#), "heroStack"), (with(LEGAL, r#""FOLD""#), "legalActions"), (with(LEGAL, r#"["FOLD","JUMP"]"#), "legalActions[1]"), (with(LEGAL, r#"["FOLD",3]"#), "legalActions[1]"), (plus(r#""minRaiseTo":"x""#), "minRaiseTo"), (plus(r#""maxRaiseTo":{}"#), "maxRaiseTo"), (plus(r#""allowedSizes":0.5"#), "allowedSizes"), (plus(r#""allowedSizes":[0.5,"b"]"#), "allowedSizes[1]"), (plus(r#""buttonSeat":-1"#), "buttonSeat"), (plus(r#""seed":1.5"#), "seed"), (plus(r#""board":[]"#), "board"), (plus(r#""board":{"FLOP":"As"}"#), "board.FLOP"), (plus(r#""villains":{}"#), "villains"), (with(HERO, r#""heroCards":"As Ks","#), "heroCards"), ]; for (text, field) in cases { let err = run(&text).unwrap_err(); assert_eq!(field_of(err), field, "{text}"); } } #[test] fn card_errors_name_the_path() { let bad_rank = with(r#""rank":"ACE""#, r#""rank":"ONE""#); assert_eq!(field_of(run(&bad_rank).unwrap_err()), "heroCards[0].rank"); let bad_suit = with(r#""rank":"KING","suit":"SPADES""#, r#""rank":"KING","suit":"STARS""#); assert_eq!(field_of(run(&bad_suit).unwrap_err()), "heroCards[1].suit"); let missing = with(r#"{"rank":"KING","suit":"SPADES"}"#, r#"{"rank":"KING"}"#); assert_eq!(run(&missing).unwrap_err(), Fault::Missing("heroCards[1].suit".into())); let not_obj = with(r#"{"rank":"KING","suit":"SPADES"}"#, r#""Ks""#); assert_eq!(field_of(run(¬_obj).unwrap_err()), "heroCards[1]"); let board = plus(r#""board":{"TURN":[{"rank":"Z","suit":"c"}]}"#); assert_eq!(field_of(run(&board).unwrap_err()), "board.TURN[0].rank"); } #[test] fn villain_errors_name_the_path() { let miss = plus(r#""villains":[{"seat":1,"folded":false}]"#); assert_eq!(run(&miss).unwrap_err(), Fault::Missing("villains[0].stack".into())); let bad = plus( r#""villains":[{"seat":1,"stack":2,"folded":false},{"seat":2,"stack":3,"folded":"no"}]"#, ); assert_eq!(field_of(run(&bad).unwrap_err()), "villains[1].folded"); let prof = plus( r#""villains":[{"seat":1,"stack":2,"folded":false,"profile":{"pfr":"x"}}]"#, ); assert_eq!(field_of(run(&prof).unwrap_err()), "villains[0].profile.pfr"); let seat = plus(r#""villains":[{"seat":1.5,"stack":2,"folded":false}]"#); assert_eq!(field_of(run(&seat).unwrap_err()), "villains[0].seat"); } #[test] fn decoding_does_not_check_consistency() { // Hero seat beyond the table: structurally fine, guard::validate rejects it later. assert!(run(&with(r#""heroSeat":0"#, r#""heroSeat":9"#)).is_ok()); } /// `aggressionActions`: the aggression read is weighed on its OWN sample (postflop moves seen), not /// on the HUD hand count, and carries that weight as `aggression_confidence`; the overall confidence /// still comes from `hands`. Without the count nothing changes. #[test] fn aggression_trusts_its_own_sample() { let thin = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{ "vpip":0.6,"hands":500,"aggression":0.0,"aggressionActions":2}}]"#; let p = run(&plus(thin)).unwrap().villains[0].profile.unwrap(); let w = 2.0 / (2.0 + 30.0); assert!((p.aggression_confidence.unwrap() - w).abs() < 1e-12); assert!((p.aggression.unwrap() - 0.35 * (1.0 - w)).abs() < 1e-12, "pulled to the population, not 0"); assert!(p.confidence.unwrap() > 0.9, "the HUD read itself is strong"); assert!((p.bluff_read_confidence().unwrap() - w).abs() < 1e-12, "a bluff read is only as good as its aggression"); let old = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.6,"hands":500,"aggression":0.0}}]"#; let q = run(&plus(old)).unwrap().villains[0].profile.unwrap(); assert_eq!(q.aggression_confidence, None); assert_eq!(q.bluff_read_confidence(), q.confidence); } /// `riverBluffShare`/`riverBluffShown` (lesen/profil.py, switch GEGNER_SHOWDOWN): the showdown read /// lands raw on the profile, through `hands` shrinking untouched; the river gearbox weighs it by its count. #[test] fn showdown_read_reaches_the_profile() { let shown = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{ "vpip":0.3,"hands":80,"riverBluffShare":0.25,"riverBluffShown":8}}]"#; let p = run(&plus(shown)).unwrap().villains[0].profile.unwrap(); assert_eq!(p.river_bluff_share, Some(0.25)); assert_eq!(p.river_bluff_shown, Some(8.0)); let none = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.3,"hands":80}}]"#; let q = run(&plus(none)).unwrap().villains[0].profile.unwrap(); assert_eq!((q.river_bluff_share, q.river_bluff_shown), (None, None)); } #[test] fn street_reads_are_pulled_to_the_pooled_read() { // One river bet seen: the river belief moves only 1/16 of the way off the pooled aggression. let one = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{ "vpip":0.3,"foldToBet":0.5,"aggression":0.2, "riverAggression":1.0,"riverAggressionActions":1, "turnFoldToBet":1.0,"turnFacingBet":5}}]"#; let p = run(&plus(one)).unwrap().villains[0].profile.unwrap(); assert!((p.river_aggression.unwrap() - (1.0 + 15.0 * 0.2) / 16.0).abs() < 1e-12); assert!((p.street_fold_to_bet[1].unwrap() - (5.0 + 15.0 * 0.5) / 20.0).abs() < 1e-12); assert_eq!(p.turn_aggression, None, "no turn sample: falls back to the pooled read"); assert_eq!(p.street_fold_to_bet[0], None); // A rate without its sample, or without a pooled read to anchor on, is not trusted at all. let bare = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{ "vpip":0.3,"riverAggression":1.0,"riverAggressionActions":3,"riverFoldToBet":0.0}}]"#; let q = run(&plus(bare)).unwrap().villains[0].profile.unwrap(); assert_eq!(q.river_aggression, None); assert_eq!(q.street_fold_to_bet[2], None); } #[test] fn hud_line_stats_rest_on_their_spots_not_on_all_hands() { // 100 HUD hands are ~15 c-bets faced: the 0.9 fold-to-c-bet is pulled halfway to the 0.4 pool. let p = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{ "foldToBet":0.4,"foldToCbet":0.9,"foldToThreeBet":0.9,"checkRaise":0.3,"hands":100}}]"#; let q = run(&plus(p)).unwrap().villains[0].profile.unwrap(); let fb = q.fold_to_bet.unwrap(); // already hand-shrunk let n = 100.0 * 0.15; assert!((q.fold_to_cbet.unwrap() - (0.9 * n + 15.0 * fb) / (n + 15.0)).abs() < 1e-12); let n3 = 100.0 * 0.04; assert!((q.fold_to_three_bet.unwrap() - (0.9 * n3 + 15.0 * 0.55) / (n3 + 15.0)).abs() < 1e-12); assert!(q.check_raise.unwrap() < 0.2, "10 chances barely move it off 0.08"); } /// The memory handover (`profile.reads`, FORMAT-gegner-uebergabe): counted reads with their true sample /// replace the HUD rates as posteriors; a delivered `prior` wins over the core's anchor; confidence /// rises to the counted sample; extra fields (`post`, `prior_d2`, ...) are ignored; no `reads` = as before. #[test] fn memory_reads_become_posteriors() { let hud = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.3,"foldToBet":0.6,"hands":24}}]"#; let base = run(&plus(hud)).unwrap().villains[0].profile.unwrap(); let empty = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.3,"foldToBet":0.6,"hands":24,"reads":{}}}]"#; assert_eq!(run(&plus(empty)).unwrap().villains[0].profile.unwrap(), base); let counted = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.3,"foldToBet":0.6,"hands":24, "reads":{"vpip":{"n":443,"k":85,"prior_d2":[3.25,6.79],"post":0.195}, "foldToThreeBet":{"n":20,"k":16,"prior":[5.5,4.5]}, "foldToCbet":{"n":30,"k":3}, "aggression":{"n":60,"k":30}}}}]"#; let p = run(&plus(counted)).unwrap().villains[0].profile.unwrap(); assert!((p.vpip.unwrap() - (85.0 + 0.28 * 30.0) / 473.0).abs() < 1e-12, "core anchor, prior_d2 ignored"); assert!((p.fold_to_three_bet.unwrap() - 21.5 / 30.0).abs() < 1e-12, "delivered prior wins"); assert!(p.fold_to_cbet.unwrap() < 0.3, "a counted non-folder overrides the HUD"); assert!((p.aggression.unwrap() - (30.0 + 0.35 * 30.0) / 90.0).abs() < 1e-12); assert!((p.aggression_confidence.unwrap() - 60.0 / 90.0).abs() < 1e-12); assert!((p.confidence.unwrap() - 443.0 / 473.0).abs() < 1e-12); } /// The raise-facing-bet read (`raiseToBet`, RERAISE_TIEFE): absent = None (live bit-identical while the /// switch is off), a counted read becomes a posterior on the anchor 0.30 with strength 10, a direct /// top-level rate is taken as given (forced read, LBR_PROFIL). #[test] fn raise_to_bet_read() { let hud = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.3,"foldToBet":0.6,"hands":24}}]"#; assert_eq!(run(&plus(hud)).unwrap().villains[0].profile.unwrap().raise_to_bet, None); let counted = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.3,"foldToBet":0.6,"hands":24, "reads":{"raiseToBet":{"n":40,"k":4}}}}]"#; let p = run(&plus(counted)).unwrap().villains[0].profile.unwrap(); assert!((p.raise_to_bet.unwrap() - (4.0 + 3.0) / 50.0).abs() < 1e-12); let direct = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.3,"foldToBet":0.6,"hands":24,"raiseToBet":0.15}}]"#; assert!((run(&plus(direct)).unwrap().villains[0].profile.unwrap().raise_to_bet.unwrap() - 0.15).abs() < 1e-12); } /// The conditional re-raise read (`reraiseToRaise`: re-raises when raised after an own postflop /// bet/raise on the street), the one RERAISE_TIEFE narrows by: same anchor and strength as raiseToBet. #[test] fn reraise_to_raise_read() { let hud = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.3,"foldToBet":0.6,"hands":24}}]"#; assert_eq!(run(&plus(hud)).unwrap().villains[0].profile.unwrap().reraise_to_raise, None); let counted = r#""villains":[{"seat":1,"stack":50,"folded":false,"profile":{"vpip":0.3,"foldToBet":0.6,"hands":24, "reads":{"reraiseToRaise":{"n":10,"k":8}}}}]"#; let p = run(&plus(counted)).unwrap().villains[0].profile.unwrap(); assert!((p.reraise_to_raise.unwrap() - (8.0 + 3.0) / 20.0).abs() < 1e-12); assert_eq!(p.raise_to_bet, None); }