setup(){ load '../../package/kit-busrouter/files/usr/lib/busrouter/lib-decide.sh'; }

@test "equal scores -> 50/50" { run wan_weight 70 70; [ "$output" -eq 50 ]; }
# 50 + 2*(80-60) = 90 -> clamp to 80 (max). So 80 is the true formula result.
@test "weight = 50 + 2*diff, rounded to 10" { run wan_weight 80 60; [ "$output" -eq 80 ]; }
@test "clamped to 20..80 (high)" { run wan_weight 100 0; [ "$output" -eq 80 ]; }
@test "clamped to 20..80 (low)"  { run wan_weight 0 100; [ "$output" -eq 20 ]; }
# 50 + 2*(58-55) = 56 -> round to nearest 10 -> 60
@test "rounds to nearest 10" { run wan_weight 58 55; [ "$output" -eq 60 ]; }
@test "no change if within hysteresis band" { run weight_changed 50 54; [ "$status" -eq 1 ]; }
@test "change if crosses a 10% step" { run weight_changed 50 65; [ "$status" -eq 0 ]; }
@test "change is symmetric (decrease)" { run weight_changed 60 50; [ "$status" -eq 0 ]; }
@test "sim switch only after N bad cycles" { run sim_should_switch 3 3; [ "$status" -eq 0 ]; }
@test "sim no switch before threshold" { run sim_should_switch 2 3; [ "$status" -eq 1 ]; }

@test "gw_verdict: 3/3 -> alive" { run gw_verdict 1 1 1; [ "$status" -eq 0 ]; }
@test "gw_verdict: 2/3 no http -> alive" { run gw_verdict 1 1 0; [ "$status" -eq 0 ]; }
@test "gw_verdict: 2/3 no ping -> alive" { run gw_verdict 1 0 1; [ "$status" -eq 0 ]; }
@test "gw_verdict: 2/3 no dns -> alive" { run gw_verdict 0 1 1; [ "$status" -eq 0 ]; }
@test "gw_verdict: 1/3 -> dead" { run gw_verdict 1 0 0; [ "$status" -eq 1 ]; }
@test "gw_verdict: 0/3 -> dead" { run gw_verdict 0 0 0; [ "$status" -eq 1 ]; }
