v1.0.0 — Hexagonal architecture rewrite
All checks were successful
CI / ci (push) Successful in 7m16s
All checks were successful
CI / ci (push) Successful in 7m16s
Restructure the monolithic 252-line main.rs into a 10-crate workspace with clean hexagonal architecture, swappable adapters, and a production- ready deployment pipeline. Backend architecture: - domain: Canvas, Color/Position/PixelUpdate value objects, port traits (CanvasStore, CanvasPersistence, EventBroadcaster), BroadcastEvent - application: use cases (place_pixel, get_state, save/restore snapshot, connect/disconnect), AppState with Arc snapshot cache - config: AppConfig structs + ConfigSource trait - api-types: shared DTOs, event name constants - adapters: config-env, canvas-file, http-axum (rust-embed), socketio, websocket — all behind port traits, swappable via feature flags - server: composition root with graceful shutdown (SIGTERM/SIGINT) Frontend: - Transport abstraction: Socket.IO and native WebSocket via VITE_TRANSPORT - Canvas zoom/pan with mouse wheel, pinch-to-zoom, and +/- buttons - ImageData rendering (~50x faster than fillRect loop) - Touch support, responsive CSS scaling, mobile-friendly layout - OG/Twitter Card meta tags for rich link previews Production: - Docker: musl static build on scratch — 2.73MB image - CI workflows for Gitea and GitHub Actions (fmt, clippy, test, Docker push) - deploy.sh for private registry - 39 unit tests across domain and application - Zero unwraps, zero unsafe, graceful error handling with tracing - Periodic canvas snapshots with rotation, restored on startup - All config via environment variables with typed defaults
This commit is contained in:
96
crates/application/tests/soldiers.rs
Normal file
96
crates/application/tests/soldiers.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
mod common;
|
||||
|
||||
use domain::BroadcastEvent;
|
||||
|
||||
#[test]
|
||||
fn connect_increments_count() {
|
||||
let (state, _) = common::test_state();
|
||||
assert_eq!(
|
||||
application::soldiers::connect::execute(&state, "a".into()),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
application::soldiers::connect::execute(&state, "b".into()),
|
||||
2
|
||||
);
|
||||
assert_eq!(
|
||||
application::soldiers::connect::execute(&state, "c".into()),
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnect_decrements_count() {
|
||||
let (state, _) = common::test_state();
|
||||
application::soldiers::connect::execute(&state, "a".into());
|
||||
application::soldiers::connect::execute(&state, "b".into());
|
||||
|
||||
assert_eq!(application::soldiers::disconnect::execute(&state, "a"), 1);
|
||||
assert_eq!(application::soldiers::disconnect::execute(&state, "b"), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnect_unknown_user_is_harmless() {
|
||||
let (state, _) = common::test_state();
|
||||
application::soldiers::connect::execute(&state, "a".into());
|
||||
assert_eq!(
|
||||
application::soldiers::disconnect::execute(&state, "unknown"),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connect_publishes_soldier_count() {
|
||||
let (state, spy) = common::test_state();
|
||||
application::soldiers::connect::execute(&state, "a".into());
|
||||
|
||||
let events = spy.events();
|
||||
assert_eq!(events.len(), 1);
|
||||
assert!(matches!(events[0], BroadcastEvent::SoldierCountChanged(1)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnect_publishes_soldier_count() {
|
||||
let (state, spy) = common::test_state();
|
||||
application::soldiers::connect::execute(&state, "a".into());
|
||||
application::soldiers::disconnect::execute(&state, "a");
|
||||
|
||||
let events = spy.events();
|
||||
assert_eq!(events.len(), 2);
|
||||
assert!(matches!(events[1], BroadcastEvent::SoldierCountChanged(0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnect_clears_cooldown() {
|
||||
let (state, _) = common::test_state();
|
||||
application::soldiers::connect::execute(&state, "user-1".into());
|
||||
|
||||
application::canvas::place_pixel::execute(
|
||||
&state,
|
||||
application::canvas::place_pixel::Command {
|
||||
user_id: "user-1",
|
||||
position: domain::Position::new(0, 0),
|
||||
color: domain::Color::new(0xFF),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
application::soldiers::disconnect::execute(&state, "user-1");
|
||||
|
||||
// Reconnect with same ID — cooldown should be gone
|
||||
application::soldiers::connect::execute(&state, "user-1".into());
|
||||
let result = application::canvas::place_pixel::execute(
|
||||
&state,
|
||||
application::canvas::place_pixel::Command {
|
||||
user_id: "user-1",
|
||||
position: domain::Position::new(1, 1),
|
||||
color: domain::Color::new(0xAA),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert!(matches!(
|
||||
result,
|
||||
application::canvas::place_pixel::Outcome::Placed(_)
|
||||
));
|
||||
}
|
||||
Reference in New Issue
Block a user