diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-08 03:29:35 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-08 03:29:35 +0100 |
| commit | 058432130628d00764663ecf819a9adc6793768d (patch) | |
| tree | d3f49f111fbfaa48647cca2600adcf9017af2d30 /tests/web_types.rs | |
| parent | web: serve latest.php through PHP and refuse stray PHP files (diff) | |
| download | zagan-bet-058432130628d00764663ecf819a9adc6793768d.tar.gz zagan-bet-058432130628d00764663ecf819a9adc6793768d.zip | |
v5.0.0: protocol v5 and the browser frontend
Protocol v5 replaces newline lines with bounded [u32 len][text] frames and REQ/RES/EVT envelopes. Requests carry correlation ids and the server replays the last 256 replies per connection, so a gateway retry cannot double-debit and a late reply cannot clobber a newer request. HELLO/PONG carry server time; crash rounds announce started_at and push coalesced 10 Hz ticks. Crash now commits sha256(seed) before a round and reveals the seed when it ends, so clients recompute the crash point.
Adds the webd/ gateway: one blocking thread, one ClientCore and one pinned upstream TLS connection per session, an HttpOnly session cookie, takeover, and a 60-second grace window. The browser sends Command JSON and renders View JSON; it never speaks the game protocol or stores credentials.
Adds the vanilla TypeScript browser app at /play/ (login, lobby, all four games, history, notices, crash verification), the nginx /play and /ws routes, and the headless DOM smoke test. webd and the app stay out of the TUI/GUI/server build graph.
The client/server version moves to 5.0.0 in lockstep; v4 peers are refused at HELLO.
Diffstat (limited to 'tests/web_types.rs')
| -rw-r--r-- | tests/web_types.rs | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/tests/web_types.rs b/tests/web_types.rs new file mode 100644 index 0000000..21af474 --- /dev/null +++ b/tests/web_types.rs | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | //! The browser and gateway exchange serde JSON for `Command`/`View`. These | ||
| 2 | //! tests pin the wire shape the TypeScript app renders. | ||
| 3 | |||
| 4 | use zagan_bet::client::games::roulette::{Cursor, Direction}; | ||
| 5 | use zagan_bet::client::view::{ | ||
| 6 | BlackjackView, Command, CrashView, HistoryView, LobbyView, RoulettePhase, RouletteView, | ||
| 7 | SlotsPhase, SlotsView, View, | ||
| 8 | }; | ||
| 9 | use zagan_bet::roulette::{BetTarget, Bets, Outside}; | ||
| 10 | |||
| 11 | #[test] | ||
| 12 | fn browser_commands_deserialize() { | ||
| 13 | let login: Command = | ||
| 14 | serde_json::from_str(r#"{"Login":{"user":"alice","credential":"abc"}}"#).unwrap(); | ||
| 15 | assert_eq!( | ||
| 16 | login, | ||
| 17 | Command::Login { | ||
| 18 | user: "alice".to_string(), | ||
| 19 | credential: "abc".to_string() | ||
| 20 | } | ||
| 21 | ); | ||
| 22 | |||
| 23 | let play: Command = serde_json::from_str(r#"{"Play":{"game":"crash","bet":25}}"#).unwrap(); | ||
| 24 | assert_eq!( | ||
| 25 | play, | ||
| 26 | Command::Play { | ||
| 27 | game: "crash".to_string(), | ||
| 28 | bet: Some(25) | ||
| 29 | } | ||
| 30 | ); | ||
| 31 | |||
| 32 | let bet: Command = | ||
| 33 | serde_json::from_str(r#"{"PlaceBetAt":{"bet":"outside","kind":"red"}}"#).unwrap(); | ||
| 34 | assert_eq!( | ||
| 35 | bet, | ||
| 36 | Command::PlaceBetAt(BetTarget::Outside { kind: Outside::Red }) | ||
| 37 | ); | ||
| 38 | |||
| 39 | let split: Command = | ||
| 40 | serde_json::from_str(r#"{"PlaceBetAt":{"bet":"split","a":1,"b":2}}"#).unwrap(); | ||
| 41 | assert_eq!(split, Command::PlaceBetAt(BetTarget::Split { a: 1, b: 2 })); | ||
| 42 | |||
| 43 | assert_eq!( | ||
| 44 | serde_json::from_str::<Command>(r#""Spin""#).unwrap(), | ||
| 45 | Command::Spin | ||
| 46 | ); | ||
| 47 | assert_eq!( | ||
| 48 | serde_json::from_str::<Command>(r#""ShowLobby""#).unwrap(), | ||
| 49 | Command::ShowLobby | ||
| 50 | ); | ||
| 51 | assert_eq!( | ||
| 52 | serde_json::from_str::<Command>(r#"{"SetBet":50}"#).unwrap(), | ||
| 53 | Command::SetBet(50) | ||
| 54 | ); | ||
| 55 | assert_eq!( | ||
| 56 | serde_json::from_str::<Command>(r#"{"MoveCursor":"Left"}"#).unwrap(), | ||
| 57 | Command::MoveCursor(Direction::Left) | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | |||
| 61 | #[test] | ||
| 62 | fn views_serialize_with_a_screen_tag() { | ||
| 63 | let lobby = View::Lobby(LobbyView { | ||
| 64 | connected: true, | ||
| 65 | user: Some("alice".to_string()), | ||
| 66 | account: Some("1".to_string()), | ||
| 67 | balance: Some(1000), | ||
| 68 | games: vec![("slots".to_string(), "Slots".to_string())], | ||
| 69 | login_error: None, | ||
| 70 | }); | ||
| 71 | let json = serde_json::to_value(&lobby).unwrap(); | ||
| 72 | assert_eq!(json["screen"], "lobby"); | ||
| 73 | assert_eq!(json["user"], "alice"); | ||
| 74 | |||
| 75 | let slots = View::Slots(SlotsView { | ||
| 76 | bet: 10, | ||
| 77 | balance: Some(990), | ||
| 78 | reels: "CLOGD".chars().collect(), | ||
| 79 | win_cells: vec![0], | ||
| 80 | win_lines: vec![1], | ||
| 81 | win_type: "win".to_string(), | ||
| 82 | winnings: Some(20), | ||
| 83 | phase: SlotsPhase::Result, | ||
| 84 | error: None, | ||
| 85 | }); | ||
| 86 | let json = serde_json::to_value(&slots).unwrap(); | ||
| 87 | assert_eq!(json["screen"], "slots"); | ||
| 88 | assert_eq!(json["phase"], "result"); | ||
| 89 | assert_eq!(json["reels"][0], "C"); | ||
| 90 | |||
| 91 | let roulette = View::Roulette(RouletteView { | ||
| 92 | bet_amount: 10, | ||
| 93 | balance: Some(1000), | ||
| 94 | chip: 25, | ||
| 95 | bets: Bets::default(), | ||
| 96 | cursor: Cursor::default(), | ||
| 97 | phase: RoulettePhase::Placing, | ||
| 98 | number: None, | ||
| 99 | color: None, | ||
| 100 | winnings: None, | ||
| 101 | error: None, | ||
| 102 | }); | ||
| 103 | let json = serde_json::to_value(&roulette).unwrap(); | ||
| 104 | assert_eq!(json["screen"], "roulette"); | ||
| 105 | assert_eq!(json["phase"], "placing"); | ||
| 106 | |||
| 107 | let blackjack = View::Blackjack(BlackjackView { | ||
| 108 | status: "ready".to_string(), | ||
| 109 | balance: Some(1000), | ||
| 110 | bet: 10, | ||
| 111 | hand: None, | ||
| 112 | error: None, | ||
| 113 | }); | ||
| 114 | assert_eq!( | ||
| 115 | serde_json::to_value(&blackjack).unwrap()["screen"], | ||
| 116 | "blackjack" | ||
| 117 | ); | ||
| 118 | |||
| 119 | let crash = View::Crash(CrashView { | ||
| 120 | status: "running".to_string(), | ||
| 121 | balance: Some(990), | ||
| 122 | bet: 10, | ||
| 123 | multiplier: 1.5, | ||
| 124 | potential: 15, | ||
| 125 | crash_point: None, | ||
| 126 | payout: 0, | ||
| 127 | started_at: 1_700_000_000_000, | ||
| 128 | commit: Some("ab".repeat(32)), | ||
| 129 | seed: None, | ||
| 130 | error: None, | ||
| 131 | }); | ||
| 132 | let json = serde_json::to_value(&crash).unwrap(); | ||
| 133 | assert_eq!(json["screen"], "crash"); | ||
| 134 | assert_eq!(json["commit"], "ab".repeat(32)); | ||
| 135 | |||
| 136 | let history = View::History(HistoryView { records: vec![] }); | ||
| 137 | assert_eq!(serde_json::to_value(&history).unwrap()["screen"], "history"); | ||
| 138 | } | ||
