fix+test: improve test suite, fix 2 bugs found by tests

Bugs fixed in production code:
- compare.sh: Python truthiness on 0.0 — `if b_val` was False for 0.0 t/s,
  displaying it as a dash instead of "0.0". Fixed with `is not None` checks.
- compare.sh: ZeroDivisionError when computing delta % with 0.0 baseline.

Test improvements (review findings):
- detect.bats: kernel param tests now use real detect_kernel_param logic
  pattern (not a separate reimplementation). Added non-GiB-aligned RAM test,
  device ID without 0x prefix, empty firmware version, llama-bench detection,
  detect_total_physical_ram_kb tests.
- benchmark_compare.bats: assert delta percentages (+20.0%, -25.0%, 0.0%),
  test 0.0 t/s edge case, test per-directory error messages, test config
  change detection with specific field assertions.
- log_metrics.bats: add assert_success, --help test, timestamp format
  validation. Remove unused mock sysfs setup.
- common.bats: fix data_dir test, remove redundant assertion, add cleanup.
- test_helper.sh: remove unused FIXTURES_DIR.
- Remove empty tests/fixtures/ directory.

94 tests, all passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Felipe Cardoso
2026-03-25 22:22:41 +01:00
parent a403dd9ce0
commit e9cb5c491f
6 changed files with 284 additions and 143 deletions

View File

@@ -5,12 +5,10 @@ load test_helper.sh
setup() {
source_lib common.sh
setup_mock_sysfs
OUTPUT_FILE="$(mktemp)"
}
teardown() {
teardown_mock_sysfs
rm -f "$OUTPUT_FILE"
}
@@ -22,19 +20,21 @@ teardown() {
}
@test "log-metrics: produces at least 1 data row in 3 seconds" {
bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
--duration 3 --interval 1 --output "$OUTPUT_FILE" 2>/dev/null
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
--duration 3 --interval 1 --output "$OUTPUT_FILE"
assert_success
local lines
lines=$(wc -l < "$OUTPUT_FILE")
(( lines >= 2 )) # header + at least 1 data row
}
@test "log-metrics: data rows have 8 comma-separated fields" {
bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
--duration 3 --interval 1 --output "$OUTPUT_FILE" 2>/dev/null
# Check second line (first data row)
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
--duration 3 --interval 1 --output "$OUTPUT_FILE"
assert_success
local row
row=$(sed -n '2p' "$OUTPUT_FILE")
[ -n "$row" ] # ensure a data row exists
local field_count
field_count=$(echo "$row" | awk -F, '{print NF}')
[ "$field_count" -eq 8 ]
@@ -55,17 +55,34 @@ teardown() {
}
@test "log-metrics: creates output file in specified path" {
local custom_output
custom_output="$(mktemp -d)/custom-metrics.csv"
bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
--duration 2 --interval 1 --output "$custom_output" 2>/dev/null
local custom_dir
custom_dir="$(mktemp -d)"
local custom_output="$custom_dir/custom-metrics.csv"
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
--duration 2 --interval 1 --output "$custom_output"
assert_success
[ -f "$custom_output" ]
rm -f "$custom_output"
rmdir "$(dirname "$custom_output")"
rm -rf "$custom_dir"
}
@test "log-metrics: warns on unknown argument" {
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
--bogus-flag --duration 1 --interval 1 --output "$OUTPUT_FILE"
assert_success # script continues despite unknown arg
assert_output --partial "Unknown argument"
}
@test "log-metrics: shows help with --help" {
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" --help
assert_success
assert_output --partial "Usage"
}
@test "log-metrics: timestamp column has valid datetime format" {
run bash "$PROJECT_ROOT/scripts/monitor/log-metrics.sh" \
--duration 2 --interval 1 --output "$OUTPUT_FILE"
assert_success
local ts
ts=$(sed -n '2p' "$OUTPUT_FILE" | cut -d, -f1)
[[ "$ts" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]
}