session-tools/tests/test_rclaude_helpers.sh

81 lines
3.1 KiB
Bash
Raw Normal View History

# test_rclaude_helpers.sh — unit tests for rclaude's pure helpers.
#
# Strategy: source rclaude with a guard so the dispatch block doesn't fire,
# then call the individual helpers directly. The guard is `RCLAUDE_LIB_ONLY=1`
# — rclaude checks it at the top of its dispatch and returns early.
# Source rclaude as a library. The dispatch block is bypassed by the guard.
RCLAUDE_LIB_ONLY=1 . "$ROOT/bin/rclaude" 2>/dev/null || true
# ---------------------------------------------------------------------------
# claude_slug
# ---------------------------------------------------------------------------
test_claude_slug_basic() {
assert_eq "-Users-natalie-Code--projects--lilith" \
"$(claude_slug "/Users/natalie/Code/@projects/@lilith")"
}
test_claude_slug_no_special() {
# Leading `/` becomes leading `-` (claude's own behavior — every
# non-alphanumeric char is replaced, including the leading slash).
assert_eq "-tmp-foo" "$(claude_slug "/tmp/foo")"
}
test_claude_slug_empty() {
assert_eq "" "$(claude_slug "")"
}
# ---------------------------------------------------------------------------
# is_local
# ---------------------------------------------------------------------------
test_is_local_keywords() {
assert_exit 0 is_local "local"
assert_exit 0 is_local "localhost"
assert_exit 0 is_local "127.0.0.1"
assert_exit 0 is_local "::1"
}
test_is_local_unknown_host() {
assert_exit 1 is_local "definitely-not-a-real-host-12345"
}
# ---------------------------------------------------------------------------
# dedupe_sessions (keeps highest-mtime row per uuid)
# ---------------------------------------------------------------------------
test_dedupe_sessions_keeps_newest() {
# Two rows with the same uuid (col 3), different mtimes (col 6).
# Should retain only the row with the higher mtime.
_in=$(printf 'apricot\tsession\tUUID-A\tsnip\tcwd\t100\nlocal\tsession\tUUID-A\tsnip2\tcwd\t200\n')
_out=$(printf '%s' "$_in" | dedupe_sessions)
_count=$(printf '%s\n' "$_out" | wc -l | tr -d ' ')
assert_eq "1" "$_count" "expected 1 deduped row" || return 1
assert_contains "$_out" "200" "should keep mtime=200 row" || return 1
}
test_dedupe_sessions_passes_unique() {
_in=$(printf 'apricot\tsession\tA\ts\tc\t100\nlocal\tsession\tB\ts\tc\t100\n')
_out=$(printf '%s' "$_in" | dedupe_sessions)
_count=$(printf '%s\n' "$_out" | wc -l | tr -d ' ')
assert_eq "2" "$_count"
}
# ---------------------------------------------------------------------------
# get_home — always returns 0 even on failure (regression test)
# ---------------------------------------------------------------------------
test_get_home_unknown_returns_zero() {
# Use a clearly invalid host. The function must not abort `set -e`
# callers; previously this caused silent exits in cmd_resume.
_v=$(get_home "definitely-not-a-real-host-12345-zzz" 2>/dev/null)
_rc=$?
assert_eq "0" "$_rc" "get_home must return 0 on failure" || return 1
assert_eq "" "$_v" "should produce empty stdout on failure" || return 1
}
test_get_home_local_returns_HOME() {
assert_eq "$HOME" "$(get_home local)"
}