Commit Graph

20 Commits

Author SHA1 Message Date
1842ca25e3 test(modbus): implement working MockRelayController tests
Replace 6 stubbed test implementations with fully functional tests that validate:
- read_relay_state() returns correctly mocked state
- write_relay_state() updates internal mocked state
- read_all_states() returns 8 relays in known state
- Independent relay state management for all 8 channel indices
- Thread-safe concurrent state access with Arc<Mutex<>>

Tests now pass after T029-T031 completed MockRelayController implementation.

TDD phase: GREEN - tests validate implementation

Ref: T032 (specs/001-modbus-relay-control/tasks.md)
2026-01-22 00:57:11 +01:00
e8e6a1e702 feat(domain): implement RelayController trait and error handling
Add RelayController async trait (T030) defining interface for Modbus
relay operations with methods for read/write state, bulk operations,
connection checks, and firmware queries.

Implement ControllerError enum (T031) with variants for connection
failures, timeouts, Modbus exceptions, and invalid relay IDs.

Provide MockRelayController (T029) in-memory implementation using
Arc<Mutex<HashMap>> for thread-safe state storage with timeout
simulation for error handling tests.

Add RelayLabelRepository trait abstraction.

Ref: T029 T030 T031 (specs/001-modbus-relay-control/tasks.md)
2026-01-22 00:57:11 +01:00
036be64d3c test(modbus): implement MockRelayController with failing tests
Ref: T028 (specs/001-modbus-relay-control)
2026-01-22 00:57:11 +01:00
2b913ba049 style: format backend 2026-01-22 00:57:11 +01:00
7c89d48ac0 feat(domain): add ModbusAddress type and HealthStatus enum
Implements T025-T027 from TDD workflow (red-green-refactor):
- T025 (red): Tests for ModbusAddress with From<RelayId> conversion
- T026 (green): ModbusAddress newtype (#[repr(transparent)]) with offset mapping
- T027 (red+green): HealthStatus enum with state transitions

ModbusAddress wraps u16 and converts user-facing relay IDs (1-8) to
Modbus addresses (0-7) at the domain boundary. HealthStatus tracks
relay health with Healthy, Degraded, and Unhealthy states supporting
error tracking and recovery monitoring.

Ref: T025, T026, T027 (specs/001-modbus-relay-control)
2026-01-22 00:57:11 +01:00
39a609dec6 feat(domain): implement Relay aggregate and RelayLabel newtype
Implemented the Relay aggregate as the primary domain entity for relay
control operations. Added RelayLabel newtype for validated human-readable
relay labels.

Relay aggregate features:
- Construction with id, state, and optional label
- State control methods: toggle(), turn_on(), turn_off()
- Accessor methods: id(), state(), label()
- All methods use const where possible for compile-time optimization

RelayLabel newtype features:
- Validation: non-empty, max 50 characters
- Smart constructor with Result-based error handling
- Default implementation: "Unlabeled"
- Transparent representation for zero-cost abstraction

Additional changes:
- Made RelayId derive Copy for ergonomic value semantics
- All public APIs include documentation and #[must_use] attributes

TDD phase: GREEN - Tests pass for Relay aggregate (T021 tests now pass)

Ref: T022, T024 (specs/001-modbus-relay-control/tasks.md)
2026-01-22 00:57:11 +01:00
a7a7e7ef18 test(domain): write failing tests for Relay aggregate
Created test suite for Relay entity covering construction, state toggling,
and explicit state control methods. Tests intentionally fail as Relay
struct is not yet implemented.

Tests cover:
- Relay::new() with id, state, and optional label
- toggle() flipping state between On/Off
- turn_on() setting state to On
- turn_off() setting state to Off

TDD phase: RED - Tests written, implementation pending (T022)

Ref: T021 (specs/001-modbus-relay-control/tasks.md)
2026-01-22 00:57:11 +01:00
e81a128e7f feat(domain): implement RelayState enum with serialization support
Add RelayState enum to domain layer with:
- Display, Debug, Clone, Copy, PartialEq, Eq derives
- serde Serialize/Deserialize traits for API JSON handling
- Type-safe representation of relay on/off states

TDD green phase: Tests from T019 now pass.

Ref: T020 (specs/001-modbus-relay-control/tasks.md)
2026-01-22 00:57:11 +01:00
d6cbf0f4ae test(domain/relay): write failing tests for RelayState serialization
Tests verify serialization and deserialization of RelayState enum with
"on" and "off" states. Red phase of TDD - tests define expected behavior
before implementation.

Ref: T019 (specs/001-modbus-relay-control/tasks.md)
2026-01-22 00:57:11 +01:00
0ec7fdf11b feat(domain): implement RelayId newtype with validation
Implement smart constructor that validates relay IDs are within valid 
range (1-8 for 8-channel relay controller). Add accessor method as_u8() 
for safe access to inner value. Add comprehensive documentation to satisfy 
clippy requirements.

TDD green phase: Tests from T017 now pass.

Ref: T018 (specs/001-modbus-relay-control/tasks.md)
2026-01-22 00:57:11 +01:00
86b194ad74 test(domain): write failing tests for RelayId newtype validation
Tests cover validation requirements for the RelayId newtype:
- Valid relay IDs (1-8 for 8-channel controller)
- Invalid IDs outside valid range
- Smart constructor error handling
- Type-safe ID representation

TDD red phase: Tests fail until RelayId is implemented.

Ref: T017 (specs/001-modbus-relay-control/tasks.md)
2026-01-22 00:57:11 +01:00
4929266a8e style: format backend 2026-01-22 00:57:11 +01:00
e16e214b74 test(cors): write integration tests for CORS headers
Added 9 comprehensive integration tests covering:
- Preflight OPTIONS requests
- Actual requests with CORS headers
- Max-age header validation
- Credentials configuration
- Allowed methods configuration
- Wildcard origins
- Multiple origins
- Unauthorized origin rejection

All tests pass successfully.

Ref: T016
2026-01-22 00:57:11 +01:00
b620c3d638 feat(middleware): configure CORS from settings in middleware chain
Replace Cors::new() with Cors::from(value.settings.cors.clone()) in the
From<Application> for RunnableApplication implementation to use CORS
settings from configuration instead of hardcoded defaults.

Changes:
- Use From<CorsSettings> for Cors trait to build CORS middleware
- Add unit test verifying CORS middleware uses settings
- Maintain correct middleware order: RateLimit → CORS → Data

Ref: T015
2026-01-22 00:57:11 +01:00
5d6c3208cc feat(cors): implement CORS configuration with From trait
Implement From<CorsSettings> for Cors trait to configure CORS middleware
with production-ready security validation.

- Move CorsSettings to backend/src/settings/cors.rs module
- Validate wildcard + credentials constraint (browser security policy)
- Configure allowed methods, headers, credentials, and max_age
- Add structured logging for CORS configuration
- Move tests from settings/mod.rs and startup.rs to cors module

Ref: T014
2026-01-22 00:57:11 +01:00
e577fb5095 test(cors): write tests for build_cors() function (TDD red)
Add failing test cases for the CORS configuration builder function.
Tests verify correct initialization of CorsSettings with allowed origins,
credentials, and max age configuration. These tests fail until build_cors()
is implemented in the green phase.

Ref: T013 (specs/001-modbus-relay-control)
2026-01-22 00:57:11 +01:00
5f0aaacb74 chore(config): configure CORS and update frontend URL in development settings
Set up CORS policy to allow requests from frontend development server and
update development.yaml with proper frontend origin URL configuration.

Ref: T011 (specs/001-modbus-relay-control)
2026-01-22 00:57:11 +01:00
9a55aa433c feat(settings): add CorsSettings struct for CORS configuration
Implements CorsSettings struct with validation and deserialization support
for configuring Cross-Origin Resource Sharing in the application settings.

Ref: T010 (specs/001-modbus-relay-control)
2026-01-22 00:57:10 +01:00
b1fd30af67 test(settings): write tests for CorsSettings struct deserialization
Add 5 comprehensive tests for CorsSettings in TDD red phase:
- cors_settings_deserialize_from_yaml: YAML deserialization
- cors_settings_default_has_empty_origins: Default values
- cors_settings_with_wildcard_deserializes: Wildcard origin handling
- settings_loads_cors_section_from_yaml: Settings integration
- cors_settings_deserialize_with_defaults: Partial deserialization

Tests are intentionally failing (red phase) as CorsSettings struct
will be implemented in T010. Follows TDD requirement to write tests
before implementation.

Ref: T009 (specs/001-modbus-relay-control/tasks.md)
2026-01-22 00:57:10 +01:00
837a49fc58 refactor: reorganize project into monorepo with frontend scaffolding
Convert project from single backend to monorepo structure with separate
frontend (Vue 3 + TypeScript + Vite) and backend directories. Updates
all configuration files and build system to support both workspaces.

Ref: T007 (specs/001-modbus-relay-control)
2026-01-22 00:57:10 +01:00