// System lifecycle contract for generated systemd units and env files.
//
// Scope: assert install-time unit/env generation remains idempotent and
// restart-safe without touching the live host.
// Targets: `scripts/install/server.sh`.
// Why: the server is a bare-metal appliance; systemd/env drift is one of the
// easiest ways to reintroduce stale codecs or unsafe gadget behavior.

const SERVER_INSTALL: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/install/server.sh"
));

#[test]
fn generated_systemd_units_load_runtime_env_files_in_the_right_places() {
    for marker in [
        "EnvironmentFile=-/etc/lesavka/server.env",
        "EnvironmentFile=-/etc/lesavka/uvc.env",
        "ExecStart=/usr/local/bin/lesavka-server",
        "ExecStart=/usr/local/bin/lesavka-uvc.sh",
        "Requires=lesavka-core.service",
    ] {
        assert!(
            SERVER_INSTALL.contains(marker),
            "generated units should preserve marker {marker}"
        );
    }
    for marker in [
        "ExecStartPre=/usr/local/bin/lesavka-core.sh --attach",
        "Wants=lesavka-uvc.service",
    ] {
        assert!(
            !SERVER_INSTALL.contains(marker),
            "server restarts should not implicitly touch gadget/UVC setup through {marker}"
        );
    }
}

#[test]
fn generated_units_are_restartable_without_leaking_helper_processes() {
    for marker in [
        "Restart=always",
        "RestartSec=5",
        "RestartSec=2",
        "KillMode=control-group",
        "TimeoutStopSec=10",
        "StandardError=append:/var/log/lesavka/server.stderr",
        "StandardError=append:/var/log/lesavka/uvc.stderr",
    ] {
        assert!(
            SERVER_INSTALL.contains(marker),
            "generated units should preserve restart marker {marker}"
        );
    }
    assert!(
        !SERVER_INSTALL.contains("KillMode=process"),
        "helpers should not be left behind by process-only termination"
    );
    assert!(
        !SERVER_INSTALL.contains("RefuseManualStop=yes"),
        "operators need to be able to stop services during recovery"
    );
}

#[test]
fn env_files_are_rendered_to_tempfiles_then_installed_idempotently() {
    for marker in [
        "render_uvc_env_file >\"$UVC_ENV_TMP\"",
        "sudo cmp -s \"$UVC_ENV_TMP\" /etc/lesavka/uvc.env",
        "sudo install -m 0644 \"$UVC_ENV_TMP\" /etc/lesavka/uvc.env",
        "UVC_ENV_CHANGED=0",
        "lesavka-uvc already active; runtime settings unchanged.",
    ] {
        assert!(
            SERVER_INSTALL.contains(marker),
            "UVC env update should preserve idempotence marker {marker}"
        );
    }
}

#[test]
fn server_env_persists_runtime_profile_and_tls_settings() {
    for marker in [
        "LESAVKA_CAM_CODEC=%s",
        "LESAVKA_UPSTREAM_HEVC_VIDEO_PLAYOUT_MODE_OFFSETS_US=%s",
        "LESAVKA_UPSTREAM_MJPEG_VIDEO_PLAYOUT_MODE_OFFSETS_US=%s",
        "LESAVKA_UVC_HEVC_FREEZE_ON_SIZE_DROP=%s",
        "LESAVKA_UVC_DIRECT_MJPEG_NORMALIZE=%s",
        "LESAVKA_UVC_DIRECT_MJPEG_NORMALIZE_MISS_LIMIT=%s",
        "LESAVKA_UVC_DIRECT_MJPEG_NORMALIZE_RSS_LIMIT_MB=%s",
        "LESAVKA_SERVER_BIND_ADDR=%s",
        "LESAVKA_REQUIRE_TLS=%s",
        "LESAVKA_TLS_CLIENT_CA=%s",
    ] {
        assert!(
            SERVER_INSTALL.contains(marker),
            "server env should persist marker {marker}"
        );
    }
}
