use rechner::fault::Fault; use rechner::game::{ActionRecord, SeenAction, Spot, Street}; use rechner::json::parse; 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}] }"#; fn base(extra: &str) -> String { format!( r#"{{"variant":"NLHE","bigBlind":1,"seats":6,"heroSeat":0, "heroCards":[{{"rank":"ACE","suit":"SPADES"}},{{"rank":"KING","suit":"SPADES"}}], "street":"PREFLOP","pot":1.5,"toCall":1,"heroStack":100,"legalActions":["FOLD","CALL"]{extra}}}"# ) } fn run(history: &str) -> Result { decode(&parse(&base(&format!(r#","history":{history}"#))).expect("test json")) } fn failing_path(history: &str) -> String { match run(history).expect_err("must fail") { Fault::Invalid { field, .. } => field, Fault::Missing(field) => field, other => panic!("unexpected fault {other:?}"), } } #[test] fn contract_example_without_history_has_an_empty_history() { let spot = decode(&parse(EXAMPLE).unwrap()).unwrap(); assert!(spot.history.is_empty()); } #[test] fn null_and_empty_history_are_empty() { assert!(run("null").unwrap().history.is_empty()); assert!(run("[]").unwrap().history.is_empty()); } #[test] fn decodes_every_field_in_order() { let spot = run( r#"[{"street":"PREFLOP","seat":4,"action":"POST_SB","amount":0.5}, {"street":"PREFLOP","seat":5,"action":"POST_BB","amount":1}, {"street":"PREFLOP","seat":0,"action":"RAISE","amount":3}, {"street":"PREFLOP","seat":1,"action":"FOLD"}, {"street":"FLOP","seat":5,"action":"CHECK","amount":null}, {"street":"TURN","seat":0,"action":"BET","amount":4.5}, {"street":"RIVER","seat":0,"action":"ALL_IN","amount":40}]"#, ) .unwrap(); let expected = vec![ ActionRecord { street: Street::Preflop, seat: 4, action: SeenAction::PostSmallBlind, amount: Some(0.5) }, ActionRecord { street: Street::Preflop, seat: 5, action: SeenAction::PostBigBlind, amount: Some(1.0) }, ActionRecord { street: Street::Preflop, seat: 0, action: SeenAction::Raise, amount: Some(3.0) }, ActionRecord { street: Street::Preflop, seat: 1, action: SeenAction::Fold, amount: None }, ActionRecord { street: Street::Flop, seat: 5, action: SeenAction::Check, amount: None }, ActionRecord { street: Street::Turn, seat: 0, action: SeenAction::Bet, amount: Some(4.5) }, ActionRecord { street: Street::River, seat: 0, action: SeenAction::AllIn, amount: Some(40.0) }, ]; assert_eq!(spot.history, expected); } #[test] fn every_action_name_is_accepted() { for action in SeenAction::ALL { let text = format!(r#"[{{"street":"FLOP","seat":1,"action":"{}"}}]"#, action.name()); assert_eq!(run(&text).unwrap().history[0].action, action); } } #[test] fn history_must_be_an_array() { assert_eq!(failing_path(r#"{"street":"FLOP"}"#), "history"); assert_eq!(failing_path("\"x\""), "history"); } #[test] fn entry_must_be_an_object() { assert_eq!(failing_path("[3]"), "history[0]"); } #[test] fn faults_carry_the_path_of_the_entry() { let ok = r#"{"street":"FLOP","seat":1,"action":"CHECK"}"#; assert_eq!( failing_path(&format!(r#"[{ok},{ok},{{"street":"FLOP","seat":1,"action":"LIMP"}}]"#)), "history[2].action" ); assert_eq!( failing_path(&format!(r#"[{ok},{{"street":"BEACH","seat":1,"action":"CHECK"}}]"#)), "history[1].street" ); assert_eq!(failing_path(r#"[{"street":"FLOP","action":"CHECK"}]"#), "history[0].seat"); assert_eq!(failing_path(r#"[{"seat":1,"action":"CHECK"}]"#), "history[0].street"); assert_eq!(failing_path(r#"[{"street":"FLOP","seat":1}]"#), "history[0].action"); } #[test] fn bad_types_are_faults() { assert_eq!( failing_path(r#"[{"street":"FLOP","seat":"one","action":"CHECK"}]"#), "history[0].seat" ); assert_eq!( failing_path(r#"[{"street":"FLOP","seat":1.5,"action":"CHECK"}]"#), "history[0].seat" ); assert_eq!( failing_path(r#"[{"street":"FLOP","seat":-1,"action":"CHECK"}]"#), "history[0].seat" ); assert_eq!( failing_path(r#"[{"street":"FLOP","seat":300,"action":"CHECK"}]"#), "history[0].seat" ); assert_eq!( failing_path(r#"[{"street":7,"seat":1,"action":"CHECK"}]"#), "history[0].street" ); assert_eq!( failing_path(r#"[{"street":"FLOP","seat":1,"action":null}]"#), "history[0].action" ); } #[test] fn amount_must_be_a_non_negative_number() { assert_eq!( failing_path(r#"[{"street":"FLOP","seat":1,"action":"BET","amount":"big"}]"#), "history[0].amount" ); assert_eq!( failing_path(r#"[{"street":"FLOP","seat":1,"action":"BET","amount":-2}]"#), "history[0].amount" ); }