Test network helper boundaries
Write portable assertions that reject broken diagnostic helpers.
Put a small contract around reusable logic
A helper function is easier to reuse when its inputs, output and exit status are explicit. The supplied classify_probe function accepts a result, latency and limit, prints one assessment and returns success for valid input. Invalid arguments return a nonzero status instead of a plausible-looking assessment.
Sourcing lib/probe.sh is appropriate here because it is trusted course code. That is different from sourcing device exports or policy data. Keep your own test functions separate, capture helper output, and compare the exact expected result as well as the exit status.
Test decisions that could be subtly wrong
Equality at the latency threshold matters, as does the first value above it. A timeout must remain unknown. Refused and unreachable have their own labels. Invalid latency text must fail rather than being coerced to zero.
The automatic checker runs your test script against the correct helper and several deliberately broken helpers. Printing a success line without exercising the contract will not pass. This approach tests whether your assertions can detect a mistake, not just whether they agree with one implementation. Use simple shell functions and test commands so the same test harness runs on macOS Bash 3.2 and in the browser.
. ./lib/probe.sh
classify_probe ok 100 100
classify_probe timeout - 100
Put it into practice.
Source trusted lib/probe.sh and write main.sh as a portable test harness. classify_probe accepts result latency limit: for ok with canonical latency 0–10000 and limit 1–10000, equality is reachable and greater is slow; timeout/- is unknown, refused/- is rejected and unreachable/- is route_error. Invalid arguments return nonzero. Test at least ok 100 100, ok 101 100, timeout - 100, refused - 100, unreachable - 100 and invalid ok oops 100. Print exactly All network checks passed on stdout only if all assertions pass. Otherwise exit nonzero and include Check failed on stderr. Tests replace the helper with faulty versions; do not inspect its source to determine results.
See an explained solution
. ./lib/probe.sh || exit 1
fail(){ printf 'Check failed: %s\n' "$1" >&2;exit 1; }
expect(){
expected=$1;shift
actual=$(classify_probe "$@") || fail 'unexpected exit status'
[ "$actual" = "$expected" ] || fail 'unexpected assessment'
}
expect reachable ok 100 100
expect slow ok 101 100
expect unknown timeout - 100
expect rejected refused - 100
expect route_error unreachable - 100
if classify_probe ok oops 100 >/dev/null 2>&1;then fail 'invalid input succeeded';fi
printf 'All network checks passed\n'
The harness checks exact output and status at decision boundaries, then verifies a rejected input. Mutated-helper cases demonstrate that these assertions catch incorrect behavior instead of simply printing a success marker.