// Include-based coverage for relay control CLI parsing and helpers.
//
// Scope: include `client/src/bin/lesavka-relayctl.rs` and exercise helper
// branches that do not require a live relay server.
// Targets: `client/src/bin/lesavka-relayctl.rs`.
// Why: relay power recovery controls need parser coverage without depending on
// a live relay endpoint.

#[allow(warnings)]
mod relayctl_binary {
    include!(env!("LESAVKA_CLIENT_RELAYCTL_BIN_SRC"));

    #[test]
    fn command_aliases_and_usage_are_stable() {
        assert_eq!(CommandKind::parse("status"), Some(CommandKind::Status));
        assert_eq!(CommandKind::parse("get"), Some(CommandKind::Status));
        assert_eq!(
            CommandKind::parse("calibration"),
            Some(CommandKind::CalibrationStatus)
        );
        assert_eq!(
            CommandKind::parse("calibration-status"),
            Some(CommandKind::CalibrationStatus)
        );
        assert_eq!(
            CommandKind::parse("calibrate"),
            Some(CommandKind::CalibrationAdjust)
        );
        assert_eq!(
            CommandKind::parse("calibration-save-default"),
            Some(CommandKind::CalibrationSaveDefault)
        );
        assert_eq!(CommandKind::parse("auto"), Some(CommandKind::Auto));
        assert_eq!(CommandKind::parse("on"), Some(CommandKind::On));
        assert_eq!(CommandKind::parse("force-on"), Some(CommandKind::On));
        assert_eq!(CommandKind::parse("off"), Some(CommandKind::Off));
        assert_eq!(CommandKind::parse("force-off"), Some(CommandKind::Off));
        assert_eq!(CommandKind::parse("reset-usb"), Some(CommandKind::ResetUsb));
        assert_eq!(
            CommandKind::parse("recover-usb"),
            Some(CommandKind::RecoverUsb)
        );
        assert_eq!(
            CommandKind::parse("recover-uac"),
            Some(CommandKind::RecoverUac)
        );
        assert_eq!(
            CommandKind::parse("recover-uvc"),
            Some(CommandKind::RecoverUvc)
        );
        assert_eq!(
            CommandKind::parse("hard-reset-usb"),
            Some(CommandKind::ResetUsb)
        );
        assert_eq!(CommandKind::parse("bad"), None);
        assert!(usage().contains("lesavka-relayctl"));
        assert!(usage().contains("recover-uac"));
        assert!(usage().contains("recover-uvc"));
        assert!(usage().contains("calibrate <audio_delta_us> <video_delta_us>"));
    }

    #[tokio::test(flavor = "current_thread")]
    async fn connect_rejects_invalid_endpoint_without_network_retry() {
        let err = connect("http://[::1").await.expect_err("invalid endpoint");
        assert!(err.to_string().contains("invalid relay server address"));
    }

    #[test]
    fn print_state_is_non_panicking_for_populated_payload() {
        print_state(lesavka_common::lesavka::CapturePowerState {
            available: true,
            enabled: true,
            mode: "manual".to_string(),
            detected_devices: 2,
            active_leases: 1,
            unit: "lesavka-capture.service".to_string(),
            detail: "ok".to_string(),
        });
    }

    #[test]
    fn version_output_keeps_release_version_and_revision_separate() {
        let source = include_str!(env!("LESAVKA_CLIENT_RELAYCTL_BIN_SRC"));
        assert!(source.contains("client_version="));
        assert!(source.contains("client_revision="));
        assert!(source.contains("server_version="));
        assert!(source.contains("server_revision="));
        assert!(
            !source.contains("client_full_version="),
            "release version output must not append the git revision to semver"
        );
    }
}
