Files
magicka-vm/crates/web_tests/tests/resilience.rs
T
linus-dandClaude Opus 4.8 9d9d5ce41c Add web game (plan2.md) on the independent runtime, merge-blocking gates
Builds the browser game around the existing Rust runtime: a window into the
universe, not a second simulation. Pure std, no external crates.

New crates:
- protocol: versioned, hashable client/server messages + hand-rolled JSON value
  and total parser (malformed packet -> Err, never panic).
- game_runtime: authoritative match state. Resolves turns through the
  INDEPENDENT interpreter (runtime_under_test::native_resolve), not the
  reference engine; filters visibility/knowledge; records and regenerates
  replays. A match is a pure function of (seed, roster, ordered inputs).
- web_assets/web_client: embedded browser client (arena, rune editor,
  knowledge panels, replay viewer) + static HTTP delivery.
- server: std::net HTTP + WebSocket (hand-rolled SHA-1/base64/RFC-6455 framing),
  turn timer, disconnect handling, panic-proof dispatch, poison-tolerant lock.
- web_tests: dependency-free WebSocket test client + Phase H gates.

Trust hardening per review:
- game_runtime no longer delegates to reference_runtime::execute; it runs the
  independent interpreter that the runtime-equivalence gate proves correct.
- Protocol/socket/replay/visibility/resilience gates are merge-blocking
  (added to the merge_group-required job in merge-gates.yml): 1k matches/0
  drift, 10k fuzz/0 panics, 100 headless socket E2E, 0 hidden-state leaks.
- Rendered-browser E2E is marked EXTERNAL-BLOCKED: Playwright runs advisory-only
  (continue-on-error, artifacts) until CI infrastructure with a browser exists;
  it is treated as unsatisfied, not green. The headless 100-match gate is
  labeled protocol-level coverage, not rendered-browser coverage.
- README documents the hand-rolled crypto/parser audit risk explicitly.

Fixes an integer-overflow panic in observed-volatility inference (i64 sum /
abs near i64::MIN) that could poison the server mutex.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-21 20:20:46 -07:00

95 lines
3.9 KiB
Rust

//! Phase H gates: disconnect/reconnect and timer edges. A dropped connection
//! must not corrupt a match, and a late or wrong-turn submission must be
//! rejected deterministically.
use protocol::{Action, ClientMessage, ServerMessage};
use web_tests::{spawn_test_server, WsClient};
#[test]
fn wrong_turn_submission_is_rejected_deterministically() {
let addr = spawn_test_server(2000); // long turn so we control timing
let mut c = WsClient::connect(&addr).unwrap();
c.send(&ClientMessage::JoinMatch { name: "timer".into(), match_id: None }).unwrap();
c.recv_until(|m| matches!(m, ServerMessage::MatchState { .. })).unwrap();
// Submit for a turn that is not live.
c.send(&ClientMessage::SubmitTurn { turn: 999, action: Action::Wait }).unwrap();
let report = c.recv_until(|m| matches!(m, ServerMessage::ValidationReport { .. })).unwrap();
match report {
ServerMessage::ValidationReport { accepted, detail, .. } => {
assert!(!accepted, "wrong-turn submission should be rejected");
assert!(detail.contains("wrong turn"), "reason: {detail}");
}
_ => unreachable!(),
}
// A correct, in-time submission is accepted.
c.send(&ClientMessage::SubmitTurn { turn: 0, action: Action::Wait }).unwrap();
let ok = c.recv_until(|m| matches!(m, ServerMessage::ValidationReport { .. })).unwrap();
match ok {
ServerMessage::ValidationReport { accepted, .. } => assert!(accepted),
_ => unreachable!(),
}
}
#[test]
fn disconnect_does_not_corrupt_an_ongoing_duel() {
let addr = spawn_test_server(10);
// Two players share a duel match by id.
let mut a = WsClient::connect(&addr).unwrap();
a.send(&ClientMessage::JoinMatch { name: "a".into(), match_id: Some(protocol::MatchId(7)) })
.unwrap();
let _ = a.recv_until(|m| matches!(m, ServerMessage::MatchState { .. })).unwrap();
let mut b = WsClient::connect(&addr).unwrap();
b.send(&ClientMessage::JoinMatch { name: "b".into(), match_id: Some(protocol::MatchId(7)) })
.unwrap();
let _ = b.recv_until(|m| matches!(m, ServerMessage::MatchState { .. })).unwrap();
// Player A plays a couple of turns.
let mut live = 0u64;
for _ in 0..2 {
a.send(&ClientMessage::SubmitTurn { turn: live, action: Action::Cast }).unwrap();
let r = a
.recv_until(|m| matches!(m, ServerMessage::TurnResolved { turn, .. } if *turn == live + 1))
.unwrap();
if let ServerMessage::TurnResolved { turn, .. } = r {
live = turn;
}
}
// Player B disconnects abruptly.
drop(b);
// The match continues for A without corruption: more turns still resolve.
for _ in 0..2 {
a.send(&ClientMessage::SubmitTurn { turn: live, action: Action::Cast }).unwrap();
let r = a
.recv_until(|m| matches!(m, ServerMessage::TurnResolved { turn, .. } if *turn == live + 1))
.unwrap();
if let ServerMessage::TurnResolved { turn, runtime_hash, .. } = r {
assert!(!runtime_hash.is_empty());
live = turn;
}
}
assert!(live >= 4, "match advanced past a mid-match disconnect");
}
#[test]
fn reconnect_into_open_duel_slot_succeeds() {
let addr = spawn_test_server(50);
let mid = protocol::MatchId(21);
let mut a = WsClient::connect(&addr).unwrap();
a.send(&ClientMessage::JoinMatch { name: "host".into(), match_id: Some(mid) }).unwrap();
a.recv_until(|m| matches!(m, ServerMessage::MatchState { .. })).unwrap();
// A second player takes the open slot.
let mut b = WsClient::connect(&addr).unwrap();
b.send(&ClientMessage::JoinMatch { name: "guest".into(), match_id: Some(mid) }).unwrap();
let ms = b.recv_until(|m| matches!(m, ServerMessage::MatchState { .. })).unwrap();
if let ServerMessage::MatchState { player_id, .. } = ms {
assert_eq!(player_id.0, 2, "second human should take slot 2");
}
}