FIXTURES="$BATS_TEST_DIRNAME/../fixtures"

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

# Helper: run _starlink_parse on a fixture file
_parse_fixture() {
    _starlink_parse < "$FIXTURES/$1"
}

@test "starlink: online dish: lat=28 dl=200000000 obstructed=0 outage=0" {
    run _parse_fixture starlink-online.json
    [ "$status" -eq 0 ]
    lat=$(echo "$output" | awk '{print $1}')
    dl=$(echo "$output" | awk '{print $2}')
    obs=$(echo "$output" | awk '{print $3}')
    outage=$(echo "$output" | awk '{print $4}')
    [ "$lat" -eq 28 ]
    [ "$dl" -gt 100000000 ]
    [ "$obs" -eq 0 ]
    [ "$outage" -eq 0 ]
}

@test "starlink: obstructed dish: obstructed=1" {
    run _parse_fixture starlink-obstructed.json
    [ "$status" -eq 0 ]
    obs=$(echo "$output" | awk '{print $3}')
    [ "$obs" -eq 1 ]
}

@test "starlink: obstructed dish: fractionObstructed>0.01 sets obstructed=1" {
    run _parse_fixture starlink-obstructed.json
    [ "$status" -eq 0 ]
    obs=$(echo "$output" | awk '{print $3}')
    [ "$obs" -eq 1 ]
}

@test "starlink: outage dish: outage=1" {
    run _parse_fixture starlink-outage.json
    [ "$status" -eq 0 ]
    outage=$(echo "$output" | awk '{print $4}')
    [ "$outage" -eq 1 ]
}

@test "starlink: outage dish: dl=0 lat=0" {
    run _parse_fixture starlink-outage.json
    [ "$status" -eq 0 ]
    lat=$(echo "$output" | awk '{print $1}')
    dl=$(echo "$output" | awk '{print $2}')
    [ "$lat" -eq 0 ]
    [ "$dl" -eq 0 ]
}

@test "starlink: grpcurl absent returns exit 1" {
    export GRPCURL=/nonexistent/grpcurl
    run starlink_status
    [ "$status" -eq 1 ]
}
