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)
This commit is contained in:
2026-01-03 22:41:28 +01:00
parent 4befafd0a5
commit 1f552dbaf8
3 changed files with 56 additions and 1 deletions

View File

@@ -1,2 +1,5 @@
mod relayid; mod relayid;
mod relaystate;
pub use relayid::RelayId; pub use relayid::RelayId;
pub use relaystate::RelayState;

View File

@@ -0,0 +1,52 @@
/// State of a relay (on or off).
///
/// Represents the binary state of a relay channel in the Modbus controller.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RelayState {
/// Relay is energized (circuit closed).
On,
/// Relay is de-energized (circuit open).
Off,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_relay_state_on_serializes_to_on() {
// Test: RelayState::On → serializes to "on"
let state = RelayState::On;
let json = serde_json::to_string(&state).unwrap();
assert_eq!(json, r#""on""#);
}
#[test]
fn test_relay_state_off_serializes_to_off() {
// Test: RelayState::Off → serializes to "off"
let state = RelayState::Off;
let json = serde_json::to_string(&state).unwrap();
assert_eq!(json, r#""off""#);
}
#[test]
fn test_parse_on_from_string() {
// Test: Parse "on" from string
let state: RelayState = serde_json::from_str(r#""on""#).unwrap();
assert_eq!(state, RelayState::On);
}
#[test]
fn test_parse_off_from_string() {
// Test: Parse "off" from string
let state: RelayState = serde_json::from_str(r#""off""#).unwrap();
assert_eq!(state, RelayState::Off);
}
#[test]
fn test_parse_invalid_string_fails() {
// Test: Parse invalid string fails
let result: Result<RelayState, _> = serde_json::from_str(r#""invalid""#);
assert!(result.is_err());
}
}

View File

@@ -218,7 +218,7 @@
- **File**: src/domain/relay.rs - **File**: src/domain/relay.rs
- **Complexity**: Low | **Uncertainty**: Low - **Complexity**: Low | **Uncertainty**: Low
- [ ] **T019** [P] [US1] [TDD] Write tests for RelayState enum - [x] **T019** [P] [US1] [TDD] Write tests for RelayState enum
- Test: RelayState::On → serializes to "on" - Test: RelayState::On → serializes to "on"
- Test: RelayState::Off → serializes to "off" - Test: RelayState::Off → serializes to "off"
- Test: Parse "on"/"off" from strings - Test: Parse "on"/"off" from strings