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:
91
crates/application/tests/place_pixel.rs
Normal file
91
crates/application/tests/place_pixel.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
mod common;
|
||||
|
||||
use application::canvas::place_pixel::{Command, Outcome};
|
||||
use domain::{BroadcastEvent, Color, Position};
|
||||
|
||||
macro_rules! place {
|
||||
($state:expr, $user:expr, $x:expr, $y:expr, $color:expr) => {
|
||||
application::canvas::place_pixel::execute(
|
||||
&$state,
|
||||
Command {
|
||||
user_id: $user,
|
||||
position: Position::new($x, $y),
|
||||
color: Color::new($color),
|
||||
},
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn successful_placement_returns_update() {
|
||||
let (state, _) = common::test_state();
|
||||
let result = place!(state, "user-1", 3, 4, 0xFF0000).unwrap();
|
||||
|
||||
let Outcome::Placed(update) = result else {
|
||||
panic!("expected Placed outcome");
|
||||
};
|
||||
assert_eq!(update.position(), Position::new(3, 4));
|
||||
assert_eq!(update.color(), Color::new(0xFF0000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placement_updates_canvas() {
|
||||
let (state, _) = common::test_state();
|
||||
place!(state, "user-1", 5, 5, 0xAA).unwrap();
|
||||
|
||||
let pixels = application::canvas::get_state::execute(&state);
|
||||
let idx = 5 * 10 + 5;
|
||||
assert_eq!(pixels[idx], Color::new(0xAA));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn placement_publishes_broadcast_event() {
|
||||
let (state, spy) = common::test_state();
|
||||
place!(state, "user-1", 0, 0, 0xFF).unwrap();
|
||||
|
||||
let events = spy.events();
|
||||
assert_eq!(events.len(), 1);
|
||||
assert!(matches!(events[0], BroadcastEvent::PixelUpdated(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cooldown_blocks_rapid_placement() {
|
||||
let (state, _) = common::test_state();
|
||||
place!(state, "user-1", 0, 0, 0xFF).unwrap();
|
||||
|
||||
let result = place!(state, "user-1", 1, 1, 0xAA).unwrap();
|
||||
assert!(matches!(result, Outcome::CooldownActive));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cooldown_is_per_user() {
|
||||
let (state, _) = common::test_state();
|
||||
place!(state, "user-1", 0, 0, 0xFF).unwrap();
|
||||
|
||||
let result = place!(state, "user-2", 1, 1, 0xAA).unwrap();
|
||||
assert!(matches!(result, Outcome::Placed(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_cooldown_allows_rapid_placement() {
|
||||
let (state, _) = common::test_state_no_cooldown();
|
||||
place!(state, "user-1", 0, 0, 0xFF).unwrap();
|
||||
|
||||
let result = place!(state, "user-1", 1, 1, 0xAA).unwrap();
|
||||
assert!(matches!(result, Outcome::Placed(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_bounds_returns_error() {
|
||||
let (state, _) = common::test_state();
|
||||
assert!(place!(state, "user-1", 99, 99, 0xFF).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_placement_does_not_trigger_cooldown() {
|
||||
let (state, _) = common::test_state();
|
||||
let _ = place!(state, "user-1", 99, 99, 0xFF);
|
||||
|
||||
let result = place!(state, "user-1", 0, 0, 0xFF).unwrap();
|
||||
assert!(matches!(result, Outcome::Placed(_)));
|
||||
}
|
||||
Reference in New Issue
Block a user