test: add comprehensive bash test suite

- Implement tests/run_tests.sh with mocked environment
- Cover color conversion, multi-format output, and config loading
- Verify clipboard and notification integrations via mocks
- Document testing procedure in README.md
This commit is contained in:
2026-04-28 22:31:57 -04:00
parent 344fc140dc
commit 37a5497336
2 changed files with 161 additions and 0 deletions
+149
View File
@@ -0,0 +1,149 @@
#!/bin/bash
set -euo pipefail
# ── Setup ─────────────────────────────────────────────────────────────────────
# Using a static-ish path since we can't use command substitution in cat
TEST_DIR="/tmp/color-tool-tests-repro"
BIN_DIR="$TEST_DIR/bin"
rm -rf "$TEST_DIR"
mkdir -p "$BIN_DIR"
# Mock notify-send
cat <<'MOCK' > "$BIN_DIR/notify-send"
#!/bin/bash
echo "NOTIFY: $*" >> "/tmp/color-tool-tests-repro/notify.log"
MOCK
# Mock wl-copy
cat <<'MOCK' > "$BIN_DIR/wl-copy"
#!/bin/bash
cat > "/tmp/color-tool-tests-repro/clipboard.txt"
MOCK
# Mock python3
cat <<'MOCK' > "$BIN_DIR/python3"
#!/bin/bash
if [[ "$*" == *wl-colorpicker*.py ]]; then
echo "#aabbcc"
exit 0
fi
exec /usr/bin/python3 "$@"
MOCK
chmod +x "$BIN_DIR"/*
# Symlink essential tools
ln -s /usr/bin/bash "$BIN_DIR/bash"
ln -s /usr/bin/cat "$BIN_DIR/cat"
ln -s /usr/bin/printf "$BIN_DIR/printf"
ln -s /usr/bin/echo "$BIN_DIR/echo"
ln -s /usr/bin/sed "$BIN_DIR/sed"
ln -s /usr/bin/grep "$BIN_DIR/grep"
ln -s /usr/bin/awk "$BIN_DIR/awk"
ln -s /usr/bin/jq "$BIN_DIR/jq"
ln -s /usr/bin/readlink "$BIN_DIR/readlink"
ln -s /usr/bin/dirname "$BIN_DIR/dirname"
ln -s /usr/bin/mkdir "$BIN_DIR/mkdir"
ln -s /usr/bin/mktemp "$BIN_DIR/mktemp"
ln -s /usr/bin/chmod "$BIN_DIR/chmod"
ln -s /usr/bin/rm "$BIN_DIR/rm"
ln -s /usr/bin/curl "$BIN_DIR/curl"
export PATH="$BIN_DIR"
export HOME="$TEST_DIR"
export WAYLAND_DISPLAY=wayland-0
export XDG_CURRENT_DESKTOP=KDE
# Resolve the absolute path of color-tool
COLOR_TOOL="$(/usr/bin/readlink -f ./color-tool)"
# ── Test Helpers ──────────────────────────────────────────────────────────────
total=0
passed=0
it() {
local label="$1"
total=$((total + 1))
printf "Test: %s... " "$label"
}
assert_contains() {
local haystack="$1"
local needle="$2"
if [[ "$haystack" == *"$needle"* ]]; then
echo "PASS"
passed=$((passed + 1))
else
echo "FAIL"
echo " Expected to find: $needle"
echo " Actual output: $haystack"
return 1
fi
}
# ── Tests ─────────────────────────────────────────────────────────────────────
# 1. Basic Conversion
it "converts hex to rgb"
output=$("$COLOR_TOOL" "#ffffff" --output rgb --no-copy --no-notify)
assert_contains "$output" "rgb(255, 255, 255)"
# 2. Multiple Formats
it "handles multiple formats"
output=$("$COLOR_TOOL" "#000000" --output hex,rgba --no-copy --no-notify)
assert_contains "$output" "#000000 rgba(0, 0, 0, 1.0)"
# 3. Clipboard Integration
it "copies to clipboard (mocked)"
rm -f "$TEST_DIR/clipboard.txt"
"$COLOR_TOOL" "#ff0000" --copy --no-notify >/dev/null
if [[ -f "$TEST_DIR/clipboard.txt" ]]; then
assert_contains "$(cat "$TEST_DIR/clipboard.txt")" "#ff0000"
else
echo "FAIL (clipboard.txt not created)"
fi
# 4. Notification Integration
it "sends notifications (mocked)"
rm -f "$TEST_DIR/notify.log"
"$COLOR_TOOL" "#00ff00" --notify --no-copy >/dev/null
if [[ -f "$TEST_DIR/notify.log" ]]; then
assert_contains "$(cat "$TEST_DIR/notify.log")" "NOTIFY: -i color-picker color-tool #00ff00"
else
echo "FAIL (notify.log not created)"
fi
# 5. Color Picker (Mocked)
it "launches color picker and processes result"
output=$("$COLOR_TOOL" --pick --no-copy --no-notify)
assert_contains "$output" "#aabbcc"
# 6. Config Loading
it "loads defaults from config.toml"
mkdir -p "$HOME/.config/color-tool"
cat <<'CONF' > "$HOME/.config/color-tool/config.toml"
[defaults]
output = "rgba"
CONF
output=$("$COLOR_TOOL" "#ffffff" --no-copy --no-notify)
assert_contains "$output" "rgba(255, 255, 255, 1.0)"
# 7. Error Notification
it "notifies on missing clipboard utility"
rm -f "$BIN_DIR/wl-copy"
rm -f "$TEST_DIR/notify.log"
"$COLOR_TOOL" "#123456" --copy --notify 2>/dev/null >/dev/null
if [[ -f "$TEST_DIR/notify.log" ]]; then
assert_contains "$(cat "$TEST_DIR/notify.log")" "NOTIFY: -u normal -i dialog-warning color-tool Missing clipboard utility"
else
echo "FAIL (notify.log not created)"
fi
# ── Cleanup ───────────────────────────────────────────────────────────────────
echo "---------------------------------------"
echo "Result: $passed/$total tests passed."
rm -rf "$TEST_DIR"
if [[ $passed -ne $total ]]; then
exit 1
fi