// Reliability contracts for disruptive local input tests.
//
// Scope: inspect Cargo target metadata and the input transport CI gate.
// Targets: `Cargo.toml`, `scripts/ci/input_transport_gate.sh`.
// Why: tests that create uinput keyboards/mice can type into the active
// desktop, so they must be explicit opt-in work for isolated Jenkins workers.

const CARGO_TOML: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml"));
const INPUT_GATE: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/ci/input_transport_gate.sh"
));
const CLIENT_MOUSE_EVENT_TESTS: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/src/input/mouse_event_contract_tests.rs"
));

const DISRUPTIVE_INPUT_TARGETS: &[&str] = &[
    "client_keyboard_paste_rpc_contract",
    "client_inputs_contract",
    "client_inputs_extra_contract",
    "client_inputs_routing_contract",
    "client_keyboard_include_contract",
    "client_keyboard_include_extra_contract",
    "client_keyboard_process_contract",
    "client_keyboard_shift_contract",
    "client_mouse_include_contract",
    "client_mouse_include_extra_contract",
    "client_mouse_uinput_contract",
    "client_keyboard_activation_contract",
];

fn test_target_block(name: &str) -> &str {
    let needle = format!("name = \"{name}\"");
    let start = CARGO_TOML
        .find(&needle)
        .unwrap_or_else(|| panic!("missing test target {name}"));
    let rest = &CARGO_TOML[start..];
    let end = rest.find("\n[[test]]").unwrap_or(rest.len());
    &rest[..end]
}

#[test]
fn virtual_hid_test_targets_require_explicit_disruptive_feature() {
    assert!(
        CARGO_TOML.contains("disruptive-input-tests = []"),
        "Cargo should declare a named opt-in feature for local input emission"
    );
    for target in DISRUPTIVE_INPUT_TARGETS {
        let block = test_target_block(target);
        assert!(
            block.contains("required-features = [\"disruptive-input-tests\"]"),
            "{target} should not run in default cargo test on a shared desktop"
        );
    }
}

#[test]
fn input_transport_gate_requires_isolated_worker_opt_in() {
    for expected in [
        "LESAVKA_ALLOW_DISRUPTIVE_INPUT_TESTS",
        "Skipping disruptive input transport tests",
        "--features disruptive-input-tests",
    ] {
        assert!(
            INPUT_GATE.contains(expected),
            "input transport gate should make disruptive execution explicit: {expected}"
        );
    }
}

#[test]
fn input_transport_gate_runs_every_feature_gated_hid_target_when_opted_in() {
    for target in DISRUPTIVE_INPUT_TARGETS {
        assert!(
            INPUT_GATE.contains(&format!("--test {target}")),
            "input transport gate should exercise opted-in disruptive target {target}"
        );
    }
}

#[test]
fn crate_local_virtual_mouse_tests_are_env_gated() {
    assert!(
        CLIENT_MOUSE_EVENT_TESTS.contains("LESAVKA_ALLOW_DISRUPTIVE_INPUT_TESTS"),
        "client crate-local virtual mouse tests should not emit events by default"
    );
    assert!(
        CLIENT_MOUSE_EVENT_TESTS.contains("disruptive_input_tests_enabled"),
        "crate-local virtual mouse coverage should use a named safety gate"
    );
}
