SPEED_STATE_DIR="$BATS_TMPDIR/busrouter"
setup() {
    load '../../package/kit-busrouter/files/usr/lib/busrouter/speedtest.sh'
    mkdir -p "$SPEED_STATE_DIR"
    export SPEED_STATE_DIR
}
teardown() { rm -rf "$SPEED_STATE_DIR"; }

@test "speed: CELL_METERED=1 skips cellular, emits 0 0" {
    export CELL_METERED=1 CELLULAR_IFACE=rmnet_mhi0
    run speed_test_iface rmnet_mhi0
    [ "$status" -eq 0 ]
    [ "$output" = "0 0" ]
}

@test "speed: CELL_METERED=0 does not skip cellular (returns non 0 0)" {
    export CELL_METERED=0 CELLULAR_IFACE=rmnet_mhi0
    run speed_test_iface rmnet_mhi0
    [ "$output" != "0 0" ] || [ "$status" -ne 0 ]
}

@test "speed_test_due: no state file -> due (exit 0)" {
    run speed_test_due eth0
    [ "$status" -eq 0 ]
}

@test "speed_test_due: fresh state file -> not due (exit 1)" {
    date +%s > "$SPEED_STATE_DIR/speed_last_eth0"
    export SPEED_INTERVAL=3600
    run speed_test_due eth0
    [ "$status" -eq 1 ]
}

@test "speed_test_due: old state file -> due (exit 0)" {
    echo "0" > "$SPEED_STATE_DIR/speed_last_eth0"
    export SPEED_INTERVAL=300
    run speed_test_due eth0
    [ "$status" -eq 0 ]
}
