From 7b1764b0a63f45267c8d2d1130b2c6e12570bf8a Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Thu, 24 Sep 2026 10:04:19 -0700 Subject: [PATCH] fix(transmission-done): send log output to stderr, not stdout Outside test mode log() tee'd every line to stdout, so any function that returns a value by echo and also logs handed its caller the log lines too. media_type=$(detect_media_type_heuristic ...) never equalled "tv", the preview ran strict auto-detection instead of TheTVDB, and SNW S04E10 was triaged to failed as an ambiguous "Star Trek" match. The existing tests run with TEST_MODE=true, where log() never touches stdout, so they could not see it. Regression exposed by #197's heuristic-gated preview. --- app-setup/templates/transmission-done.sh | 5 ++-- .../unit/test_type_detection.bats | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app-setup/templates/transmission-done.sh b/app-setup/templates/transmission-done.sh index 4216ac0..e4d4dd0 100755 --- a/app-setup/templates/transmission-done.sh +++ b/app-setup/templates/transmission-done.sh @@ -231,11 +231,12 @@ log() { fi local timestamp timestamp=$(date '+%Y-%m-%d %H:%M:%S') - # In test mode, only write to log file (don't output to stdout) + # stderr, never stdout: functions that return data by echo are captured with + # $(...), and a log line in that capture corrupts the returned value. if [[ "${TEST_MODE}" == "true" ]]; then printf '[%s] %s\n' "${timestamp}" "$1" >>"${LOG_FILE}" else - printf '[%s] %s\n' "${timestamp}" "$1" | tee -a "${LOG_FILE}" + printf '[%s] %s\n' "${timestamp}" "$1" | tee -a "${LOG_FILE}" >&2 fi } diff --git a/tests/transmission-filebot/unit/test_type_detection.bats b/tests/transmission-filebot/unit/test_type_detection.bats index bf7335d..6e1fe16 100755 --- a/tests/transmission-filebot/unit/test_type_detection.bats +++ b/tests/transmission-filebot/unit/test_type_detection.bats @@ -199,3 +199,31 @@ load ../test_helper run cat "${LOG_FILE}" assert_output_contains "Pattern counts" "${output}" } + +# Production runs with TEST_MODE=false, where log() also prints to the +# terminal. That output must not reach stdout: callers capture this function +# with $(...), and on 2026-09-24 the captured log lines made media_type differ +# from "tv", so an SNW episode previewed with strict auto-detection and failed. +@test "detect_media_type_heuristic: returns only the type when TEST_MODE=false" { + local file="${TEST_TEMP_DIR}/star.trek.strange.new.worlds.s04e10.1080p.web.h264-cakes[EZTVx.to].mkv" + touch "${file}" + export TR_TORRENT_FILES="${file}" + export TEST_MODE=false + + local result + result=$(detect_media_type_heuristic "${file}") + + assert_equal "tv" "${result}" +} + +@test "log: writes to LOG_FILE and not to stdout when TEST_MODE=false" { + export TEST_MODE=false + : >"${LOG_FILE}" + + local captured + captured=$(log "probe message" 2>/dev/null) + + assert_equal "" "${captured}" + run cat "${LOG_FILE}" + assert_output_contains "probe message" "${output}" +}