Files
strix-halo-optimizations/tests/common.bats
Felipe Cardoso e9cb5c491f 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>
2026-03-25 22:22:41 +01:00

124 lines
3.4 KiB
Bash

#!/usr/bin/env bats
# Tests for lib/common.sh — core utilities
load test_helper.sh
setup() {
source_lib common.sh
}
# ── PROJECT_ROOT detection ───────────────────────────────
@test "PROJECT_ROOT points to a directory containing Makefile" {
[ -f "$PROJECT_ROOT/Makefile" ]
}
@test "PROJECT_ROOT is not the filesystem root" {
[ "$PROJECT_ROOT" != "/" ]
}
# ── is_cmd ───────────────────────────────────────────────
@test "is_cmd: bash exists" {
run is_cmd bash
assert_success
}
@test "is_cmd: nonexistent command fails" {
run is_cmd this_command_does_not_exist_xyz123
assert_failure
}
@test "is_cmd: empty argument fails" {
run is_cmd ""
assert_failure
}
# ── timestamp ────────────────────────────────────────────
@test "timestamp: returns YYYYMMDD-HHMMSS format" {
run timestamp
assert_success
[[ "$output" =~ ^[0-9]{8}-[0-9]{6}$ ]]
}
@test "timestamp: returns a reasonable date (not epoch)" {
local ts
ts="$(timestamp)"
local year="${ts:0:4}"
(( year >= 2025 ))
}
# ── data_dir ─────────────────────────────────────────────
@test "data_dir: creates directory and returns path" {
local subdir="test-bats-$$-$RANDOM"
local dir
dir="$(data_dir "$subdir")"
[ -d "$dir" ]
rmdir "$dir" 2>/dev/null || true
}
@test "data_dir: returns path under PROJECT_ROOT/data" {
local dir
dir="$(data_dir "testdir")"
[[ "$dir" == "$PROJECT_ROOT/data/testdir" ]]
rmdir "$dir" 2>/dev/null || true
}
# ── logging functions produce output ─────────────────────
@test "log_info: produces output" {
run log_info "test message"
assert_success
assert_output --partial "test message"
}
@test "log_success: produces output" {
run log_success "it worked"
assert_success
assert_output --partial "it worked"
}
@test "log_warn: produces output" {
run log_warn "warning"
assert_success
assert_output --partial "warning"
}
@test "log_error: produces output on stderr" {
run log_error "failure"
assert_success
# log_error writes to stderr but bats captures both
assert_output --partial "failure"
}
@test "log_header: produces output with delimiters" {
run log_header "Section Title"
assert_success
assert_output --partial "Section Title"
}
# ── Color handling ───────────────────────────────────────
@test "colors are empty strings when not a terminal" {
export TERM=dumb
# Re-source to pick up non-terminal detection
unset RED GREEN YELLOW BLUE CYAN BOLD DIM RESET
source_lib common.sh
# When stdout is not a tty (as in bats), colors should be empty
[ -z "$RED" ]
}
# ── require_root ─────────────────────────────────────────
@test "require_root: exits with error when not root" {
# We're not running tests as root
if [ "$EUID" -eq 0 ]; then
skip "Test must not run as root"
fi
run require_root
assert_failure
assert_output --partial "root"
}