aboutsummaryrefslogtreecommitdiff
path: root/scripts/tests/session-title-hook.bats
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/tests/session-title-hook.bats')
-rw-r--r--scripts/tests/session-title-hook.bats70
1 files changed, 70 insertions, 0 deletions
diff --git a/scripts/tests/session-title-hook.bats b/scripts/tests/session-title-hook.bats
new file mode 100644
index 0000000..60b633d
--- /dev/null
+++ b/scripts/tests/session-title-hook.bats
@@ -0,0 +1,70 @@
+#!/usr/bin/env bats
+# hooks/session-title.sh — SessionStart hook that titles the session
+# "<host> <project>" (uname -n + git-toplevel basename, cwd basename outside
+# a repo) so remote sessions are identifiable on web/mobile. It only sets a
+# title when the session doesn't have one yet: a /rename or an earlier run
+# must not be clobbered on resume.
+
+setup() {
+ REPO_ROOT="$(cd "$(dirname "$BATS_TEST_FILENAME")/../.." && pwd)"
+ SCRIPT="$REPO_ROOT/hooks/session-title.sh"
+ TMPDIR_T="$(mktemp -d)"
+ HOST="$(uname -n)"
+}
+
+teardown() {
+ rm -rf "$TMPDIR_T"
+}
+
+run_hook() {
+ # $1 = cwd, $2 = optional existing session_title
+ if [ -n "${2:-}" ]; then
+ printf '{"cwd":"%s","source":"startup","session_title":"%s"}' "$1" "$2" | sh "$SCRIPT"
+ else
+ printf '{"cwd":"%s","source":"startup"}' "$1" | sh "$SCRIPT"
+ fi
+}
+
+@test "titles host + repo basename inside a git repo" {
+ git -C "$TMPDIR_T" init -q -b main
+ mkdir -p "$TMPDIR_T/sub/dir"
+ run run_hook "$TMPDIR_T"
+ [ "$status" -eq 0 ]
+ title=$(echo "$output" | jq -r '.hookSpecificOutput.sessionTitle')
+ [ "$title" = "$HOST $(basename "$TMPDIR_T")" ]
+}
+
+@test "uses the repo toplevel basename from a subdirectory" {
+ git -C "$TMPDIR_T" init -q -b main
+ mkdir -p "$TMPDIR_T/sub/dir"
+ run run_hook "$TMPDIR_T/sub/dir"
+ [ "$status" -eq 0 ]
+ title=$(echo "$output" | jq -r '.hookSpecificOutput.sessionTitle')
+ [ "$title" = "$HOST $(basename "$TMPDIR_T")" ]
+}
+
+@test "falls back to cwd basename outside a git repo" {
+ mkdir -p "$TMPDIR_T/chime"
+ run run_hook "$TMPDIR_T/chime"
+ [ "$status" -eq 0 ]
+ title=$(echo "$output" | jq -r '.hookSpecificOutput.sessionTitle')
+ [ "$title" = "$HOST chime" ]
+}
+
+@test "emits valid SessionStart hookSpecificOutput" {
+ run run_hook "$TMPDIR_T"
+ [ "$status" -eq 0 ]
+ event=$(echo "$output" | jq -r '.hookSpecificOutput.hookEventName')
+ [ "$event" = "SessionStart" ]
+}
+
+@test "stays silent when a session title already exists" {
+ run run_hook "$TMPDIR_T" "my custom name"
+ [ "$status" -eq 0 ]
+ [ -z "$output" ]
+}
+
+@test "emits no stderr noise" {
+ err="$(printf '{"cwd":"%s","source":"startup"}' "$TMPDIR_T" | sh "$SCRIPT" 2>&1 >/dev/null)"
+ [ -z "$err" ]
+}