opentalk-roomserver-types-timer 0.0.7

OpenTalk RoomServer Types Timer
Documentation
// SPDX-FileCopyrightText: OpenTalk GmbH <mail@opentalk.eu>
//
// SPDX-License-Identifier: EUPL-1.2

use opentalk_roomserver_signaling::signaling_module::CreateReplica;
use serde::{Deserialize, Serialize};

use crate::{Start, event::TimerEvent};

/// Incoming websocket messages
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum TimerCommand {
    /// Start a new timer
    Start(Start),
    /// Stop a running timer
    Stop { reason: Option<String> },
    /// Update the ready status
    UpdateReadyStatus { status: bool },
}

impl CreateReplica<TimerEvent> for TimerCommand {
    fn replicate(&self) -> Option<TimerEvent> {
        None
    }
}

impl From<Start> for TimerCommand {
    fn from(value: Start) -> Self {
        Self::Start(value)
    }
}

#[cfg(test)]
mod serde_tests {
    use pretty_assertions::assert_eq;
    use serde_json::json;

    use super::*;
    use crate::command::kind::Kind;

    #[test]
    fn countdown_start() {
        let json = json!({
            "action": "start",
            "kind": "countdown",
            "duration": 5,
            "style": "coffee_break",
            "title": null,
            "enable_ready_check": false
        });

        assert_eq!(
            json,
            serde_json::to_value(TimerCommand::Start(Start {
                kind: Kind::Countdown { duration: 5 },
                style: Some("coffee_break".into()),
                title: None,
                enable_ready_check: false
            }))
            .unwrap()
        );
    }

    #[test]
    fn stopwatch_start() {
        let json = json!({
            "action": "start",
            "kind": "stopwatch",
            "title": "Testing the timer!",
            "style": null,
            "enable_ready_check": false
        });

        assert_eq!(
            json,
            serde_json::to_value(TimerCommand::Start(Start {
                kind: Kind::Stopwatch,
                style: None,
                title: Some("Testing the timer!".into()),
                enable_ready_check: false
            }))
            .unwrap()
        );
    }

    #[test]
    fn stop() {
        let json = json!({
            "action": "stop",
            "reason": "test"
        });

        assert_eq!(
            json,
            serde_json::to_value(TimerCommand::Stop {
                reason: Some("test".into())
            })
            .unwrap()
        );
    }

    #[test]
    fn update_ready_status() {
        let json = json!({
            "action": "update_ready_status",
            "status": true
        });

        assert_eq!(
            json,
            serde_json::to_value(TimerCommand::UpdateReadyStatus { status: true }).unwrap()
        )
    }
}