From 6b65665eca8a4b36b0b6eae4d761fccd7b4c1fc4 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 26 Apr 2026 01:08:50 -0500 Subject: refactor: extract get_raid_level fzf preview into raid.sh helper `get_raid_level()` carried a 98-line inline fzf `--preview` shell snippet that contained five nearly-parallel `case` branches emitting per-level preview text, plus three exported variables (`RAID_DISK_COUNT`, `RAID_TOTAL_GB`, `RAID_SMALLEST_GB`) just to pass values into the fzf preview subshell. The inline shell-in-shell had no syntax highlighting, no shellcheck on the inner snippet, and any edit to preview copy meant editing inside a single-quoted argument. I extracted the per-level text into a new `raid_preview(level, disk_count, total_gb, smallest_gb)` helper in `lib/raid.sh`. It reuses the existing `raid_fault_tolerance` and `raid_usable_bytes` primitives for the data lines instead of redoing the arithmetic inline. That keeps the math in one place. The fzf `--preview` argument is now a one-liner that calls `raid_preview` with the sizing values, and the env-var exports are gone. `export -f raid_preview raid_fault_tolerance raid_usable_bytes` makes the functions visible in fzf's preview subshell. I verified this against a fresh `bash -c` subshell, which is what fzf spawns internally. `get_raid_level()` shrinks from 144 to 49 lines. Preview text is now bats-tested. Added 8 unit tests across the 5 RAID levels (headline, fault-tolerance line, computed usable space), mixed-size handling for mirror (smallest disk, not average), unknown level returning 1 with empty output, and a sanity loop confirming every valid level produces non-empty output. No behavior change. The preview pane shows the same text, the same level options, the same selected output. The pure logic in `lib/raid.sh` is unchanged. --- tests/unit/test_raid.bats | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'tests') diff --git a/tests/unit/test_raid.bats b/tests/unit/test_raid.bats index 4e2f842..3972623 100644 --- a/tests/unit/test_raid.bats +++ b/tests/unit/test_raid.bats @@ -197,3 +197,72 @@ raidz3" ] run raid_fault_tolerance bogus 3 [ "$status" -eq 1 ] } + +############################# +# raid_preview +############################# +# Signature: raid_preview LEVEL DISK_COUNT TOTAL_GB SMALLEST_GB +# Returns 1 for unknown level. Output is preview text — assertions +# focus on structural pieces (headlines, computed numbers, key +# labels), not exact prose. + +@test "raid_preview mirror: headline + fault tolerance + smallest disk size" { + run raid_preview mirror 3 300 100 + [ "$status" -eq 0 ] + [[ "$output" == *"MIRROR"* ]] + [[ "$output" == *"Can lose 2 of 3 disks"* ]] + [[ "$output" == *"100"* ]] +} + +@test "raid_preview stripe: headline + no-redundancy warning + total size" { + run raid_preview stripe 2 200 100 + [ "$status" -eq 0 ] + [[ "$output" == *"STRIPE"* ]] + [[ "$output" == *"NO REDUNDANCY"* ]] + [[ "$output" == *"200"* ]] +} + +@test "raid_preview raidz1: headline + can-lose-1 + (n-1)*smallest size" { + run raid_preview raidz1 4 400 100 + [ "$status" -eq 0 ] + [[ "$output" == *"RAIDZ1"* ]] + [[ "$output" == *"Can lose 1 of 4 disks"* ]] + [[ "$output" == *"300"* ]] +} + +@test "raid_preview raidz2: headline + can-lose-2 + (n-2)*smallest size" { + run raid_preview raidz2 5 500 100 + [ "$status" -eq 0 ] + [[ "$output" == *"RAIDZ2"* ]] + [[ "$output" == *"Can lose 2 of 5 disks"* ]] + [[ "$output" == *"300"* ]] +} + +@test "raid_preview raidz3: headline + can-lose-3 + (n-3)*smallest size" { + run raid_preview raidz3 6 600 100 + [ "$status" -eq 0 ] + [[ "$output" == *"RAIDZ3"* ]] + [[ "$output" == *"Can lose 3 of 6 disks"* ]] + [[ "$output" == *"300"* ]] +} + +@test "raid_preview mirror with mixed-size disks honors smallest, not average" { + run raid_preview mirror 3 300 80 + [ "$status" -eq 0 ] + [[ "$output" == *"80"* ]] + [[ "$output" != *"100"* ]] +} + +@test "raid_preview unknown level returns 1 with empty output" { + run raid_preview bogus 3 300 100 + [ "$status" -eq 1 ] + [ -z "$output" ] +} + +@test "raid_preview every valid level produces non-empty output" { + for level in mirror stripe raidz1 raidz2 raidz3; do + run raid_preview "$level" 5 500 100 + [ "$status" -eq 0 ] + [ -n "$output" ] + done +} -- cgit v1.2.3