//! Contract tests for server install-time operational defaults.
//!
//! Scope: statically guard the generated `/etc/lesavka/server.env` values.
//! Targets: `scripts/install/server.sh`.
//! Why: HDMI capture adapter settings should be reproducible after reboot or
//! reinstall instead of living as one-off shell state.

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

#[test]
fn server_install_pins_hdmi_camera_and_display_defaults() {
    for expected in [
        "LESAVKA_CAM_OUTPUT=%s",
        "LESAVKA_CAM_WIDTH=%s",
        "LESAVKA_CAM_HEIGHT=%s",
        "LESAVKA_CAM_FPS=%s",
        "LESAVKA_HDMI_WIDTH=%s",
        "LESAVKA_HDMI_HEIGHT=%s",
        "LESAVKA_HDMI_SINK=%s",
        "LESAVKA_HDMI_FBDEV=%s",
        "LESAVKA_UPSTREAM_PLAYOUT_DELAY_MS=%s",
        "LESAVKA_UPSTREAM_AUDIO_PLAYOUT_OFFSET_US=%s",
        "LESAVKA_UPSTREAM_VIDEO_PLAYOUT_OFFSET_US=%s",
        "LESAVKA_UPSTREAM_PAIR_SLACK_US=%s",
        "LESAVKA_UPSTREAM_STALE_DROP_MS=%s",
    ] {
        assert!(
            SERVER_INSTALL.contains(expected),
            "install script should emit {expected}"
        );
    }
    assert!(SERVER_INSTALL.contains("${LESAVKA_CAM_WIDTH:-1920}"));
    assert!(SERVER_INSTALL.contains("${LESAVKA_CAM_HEIGHT:-1080}"));
    assert!(SERVER_INSTALL.contains("${LESAVKA_CAM_FPS:-30}"));
    assert!(SERVER_INSTALL.contains("${LESAVKA_HDMI_WIDTH:-1920}"));
    assert!(SERVER_INSTALL.contains("${LESAVKA_HDMI_HEIGHT:-1080}"));
    assert!(SERVER_INSTALL.contains("${LESAVKA_HDMI_SINK:-fbdevsink}"));
    assert!(SERVER_INSTALL.contains("${LESAVKA_UPSTREAM_PLAYOUT_DELAY_MS:-1000}"));
    assert!(SERVER_INSTALL.contains("${LESAVKA_UPSTREAM_PAIR_SLACK_US:-20000}"));
    assert!(SERVER_INSTALL.contains("${LESAVKA_UPSTREAM_STALE_DROP_MS:-80}"));
}

#[test]
fn server_install_reports_installed_version_and_revision() {
    assert!(
        SERVER_INSTALL.contains("Installed:"),
        "install script should print the installed build identity at the end"
    );
    assert!(
        SERVER_INSTALL.contains("manifest_package_version"),
        "install script should read the package version from the synced source manifest"
    );
    assert!(
        SERVER_INSTALL.contains("\"$SRC_DIR/server/Cargo.toml\""),
        "install script should report the server package semver from the server manifest"
    );
    assert!(
        SERVER_INSTALL.contains("git -C \"$SCRIPT_REPO_ROOT\" rev-parse --short HEAD"),
        "install script should print the installed git revision for operator clarity"
    );
}

#[test]
fn server_install_keeps_uac_sanity_helper_available() {
    assert!(
        SERVER_INSTALL.contains("run_uac_output_sanity.sh"),
        "install script should ship the root UAC sanity helper"
    );
    assert!(
        SERVER_INSTALL.contains("/usr/local/bin/lesavka-uac-sanity"),
        "install script should install the UAC sanity helper to a stable path"
    );
}

#[test]
fn server_install_grants_operator_audio_group_access() {
    assert!(
        SERVER_INSTALL.contains("usermod -aG audio"),
        "install script should grant the invoking operator access to ALSA devices"
    );
    assert!(
        SERVER_INSTALL.contains("SUDO_USER"),
        "install script should target the sudo-invoking operator instead of a hard-coded account"
    );
}

#[test]
fn server_install_builds_eye_links_from_capture_nodes_only() {
    assert!(
        SERVER_INSTALL.contains("/sys/class/video4linux/$(basename \"$dev\")/index"),
        "install script should inspect the kernel video index so it only links real capture nodes"
    );
    assert!(
        SERVER_INSTALL.contains("GC_CAPTURE_PAIRS"),
        "install script should assemble eye-link candidates from udev-tagged capture devices"
    );
    assert!(
        SERVER_INSTALL.contains("discover_gc_capture_pairs"),
        "install script should retry capture-node discovery instead of trusting a single early snapshot"
    );
    assert!(
        SERVER_INSTALL.contains("for _ in {1..20}; do"),
        "install script should wait briefly for both GC311 capture nodes to finish enumerating"
    );
    assert!(
        SERVER_INSTALL.contains("borrowing relay GPIO power for capture discovery"),
        "install script should temporarily power capture cards for discovery when relay GPIO is available"
    );
    assert!(
        SERVER_INSTALL.contains("returning relay GPIO power control to Lesavka auto mode"),
        "install script should restore relay power control after capture discovery"
    );
    assert!(
        SERVER_INSTALL.contains("trap restore_capture_power_after_discovery EXIT"),
        "install script should restore borrowed capture power even if discovery exits early"
    );
    assert!(
        SERVER_INSTALL.contains("[ \"$LEFT_TAG\" = \"$RIGHT_TAG\" ]"),
        "install script should refuse duplicated path tags instead of assigning both eyes to one card"
    );
}
