// Regression contract for preserving calibration across installs.
//
// Scope: guard the shipped calibration maps and ensure the installer does not
// overwrite site calibration state during upgrades.
// Targets: `scripts/install/server.sh` and server calibration modules.
// Why: the server-to-RCT timing values are hard-won hardware facts; installer
// reruns should refresh defaults without erasing local calibration decisions.

const SERVER_INSTALL: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/install/server.sh"
));
const CALIBRATION: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/server/src/calibration.rs"
));
const PROFILE_OFFSETS: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/server/src/calibration/profile_offsets.rs"
));

#[test]
fn installer_persists_both_mjpeg_and_hevc_factory_offset_maps() {
    for marker in [
        "DEFAULT_MJPEG_UPSTREAM_VIDEO_PLAYOUT_MODE_OFFSETS_US=1280x720@20=162659,1280x720@30=135090,1920x1080@20=160045,1920x1080@30=127952",
        "DEFAULT_MJPEG_OPUS_UPSTREAM_AUDIO_PLAYOUT_MODE_OFFSETS_US=1280x720@20=-34199,1280x720@30=-34199,1920x1080@20=-34199,1920x1080@30=-34199",
        "DEFAULT_MJPEG_OPUS_UPSTREAM_VIDEO_PLAYOUT_MODE_OFFSETS_US=1280x720@20=541419,1280x720@30=513850,1920x1080@20=538805,1920x1080@30=506712",
        "DEFAULT_HEVC_UPSTREAM_VIDEO_PLAYOUT_MODE_OFFSETS_US=1280x720@20=173852,1280x720@30=110000,1920x1080@20=160045,1920x1080@30=127952",
        "DEFAULT_HEVC_OPUS_UPSTREAM_AUDIO_PLAYOUT_MODE_OFFSETS_US=$DEFAULT_HEVC_UPSTREAM_AUDIO_PLAYOUT_MODE_OFFSETS_US",
        "LESAVKA_UPSTREAM_MJPEG_VIDEO_PLAYOUT_MODE_OFFSETS_US=%s",
        "LESAVKA_UPSTREAM_MJPEG_OPUS_VIDEO_PLAYOUT_MODE_OFFSETS_US=%s",
        "LESAVKA_UPSTREAM_HEVC_VIDEO_PLAYOUT_MODE_OFFSETS_US=%s",
        "LESAVKA_UPSTREAM_HEVC_OPUS_AUDIO_PLAYOUT_MODE_OFFSETS_US=%s",
        "LESAVKA_UPSTREAM_VIDEO_PLAYOUT_MODE_OFFSETS_US=%s",
    ] {
        assert!(
            SERVER_INSTALL.contains(marker),
            "server installer should preserve calibration map marker {marker}"
        );
    }
}

#[test]
fn explicit_install_offsets_win_over_stale_ambient_runtime_values() {
    for marker in [
        "LESAVKA_INSTALL_UPSTREAM_AUDIO_PLAYOUT_OFFSET_US",
        "LESAVKA_INSTALL_UPSTREAM_VIDEO_PLAYOUT_OFFSET_US",
        "migrating stale upstream audio playout offset",
        "migrating stale upstream video playout offset",
        "Use LESAVKA_INSTALL_UPSTREAM_VIDEO_PLAYOUT_OFFSET_US to intentionally keep an older value.",
    ] {
        assert!(
            SERVER_INSTALL.contains(marker),
            "installer should preserve stale-baseline migration marker {marker}"
        );
    }
}

#[test]
fn installer_does_not_delete_or_rewrite_site_calibration_file() {
    assert!(
        !SERVER_INSTALL.contains("calibration.toml"),
        "installer should not write the persisted site calibration file directly"
    );
    assert!(
        !SERVER_INSTALL.contains("/var/lib/lesavka/calibration"),
        "installer should leave calibration storage to the runtime store"
    );
    assert!(CALIBRATION.contains("/var/lib/lesavka/calibration.toml"));
    assert!(CALIBRATION.contains("persist_snapshot"));
}

#[test]
fn runtime_calibration_selects_profile_from_ingress_codec() {
    for marker in [
        "LESAVKA_CALIBRATION_PROFILE",
        "LESAVKA_UPLINK_AUDIO_CODEC",
        "format!(\"{camera_profile}+{audio_profile}\")",
        "LESAVKA_CALIBRATION_AUDIO_CODEC",
        "LESAVKA_CAM_CODEC",
        "mjpeg",
        "hevc",
        "factory_video_mode_offsets_us",
    ] {
        assert!(
            PROFILE_OFFSETS.contains(marker),
            "profile offset code should preserve marker {marker}"
        );
    }
}
