Files
coredesktop/tests/report.bats
T
davidandClaude Opus 5.5 1cd56f7a90 report: accept a report ID that ends with a newline
Plain-text replies usually end with one, and the ID check counted it as
part of the ID, so a report that was sent was said to have failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 11:36:43 -07:00

158 lines
5.1 KiB
Bash

#!/usr/bin/env bats
load test_helper
# report, with curl replaced by a stub that saves the fields it was given and
# answers with $CURL_REPLY
setup() {
common_setup
export USER=alice
stub uname '
case "$1" in
-n) echo lab ;;
-r) echo 6.99.1-arch1-1 ;;
*) /usr/bin/uname "$@" ;;
esac'
# Save each -F field: name=<file as name=contents, the log as log.gz
stub curl '
fields="$BATS_TEST_TMPDIR/fields"
mkdir -p "$fields"
while (( $# )); do
if [ "$1" = -F ]; then
case "$2" in
log=@*) path="${2#log=@}"; cp "${path%%;*}" "$fields/log.gz" ;;
*=\<*) cp "${2#*=<}" "$fields/${2%%=*}" ;;
esac
shift
fi
shift
done
printf "%b" "${CURL_REPLY:-CD-7Q2K\n200}"
exit "${CURL_STATUS:-0}"'
export COREDESKTOP_TTY="$BATS_TEST_TMPDIR/tty"
LOGS="$HOME/.local/state/coredesktop/logs"
mkdir -p "$LOGS"
LOG="$LOGS/update-2026-11-08-110307.log"
printf '%s\n' "Updating CoreDesktop..." "Cloning into '$HOME/.local/share/x'..." \
"Done, with errors in: 71-siblings.sh" > "$LOG"
}
# answers LINE...: what the user types at the terminal
answers() {
printf '%s\n' "$@" > "$COREDESKTOP_TTY"
}
field() {
cat "$BATS_TEST_TMPDIR/fields/$1"
}
@test "redaction replaces personal details and keeps version numbers" {
printf '%s\n' \
"Cloning into '$HOME/.local/share/nvim/lazy/LuaSnip'..." \
"[sudo] password for alice: " \
"ardop 1.0.4.1.3 is up to date. gridtracker2 2.261108.4-1, python 3.14" \
"ssh alice@lab from 192.168.1.20; mail david@federated.computer" \
"lab.tail1234.ts.net 100.101.102.103 127.0.0.1" \
"alicesmith and labrador stay" > "$LOG"
run bash "$REPO/report" --view "$LOG"
[ "$status" -eq 0 ]
[ "$output" = "$(printf '%s\n' \
"Cloning into '~/.local/share/nvim/lazy/LuaSnip'..." \
"[sudo] password for <user>: " \
"ardop 1.0.4.1.3 is up to date. gridtracker2 2.261108.4-1, python 3.14" \
"ssh <user>@<host> from <ip>; mail <email>" \
"<tailscale-name> <ip> <ip>" \
"alicesmith and labrador stay")" ]
! grep -q curl "$STUB_LOG"
}
@test "redaction also cleans a terminal session's log" {
printf 'Script started on 2026-11-08 [COMMAND="update"]\n\033[1;32mok\033[0m\r\n10%%\r100%%\r\nScript done on 2026-11-08 [COMMAND_EXIT_CODE="1"]\n' > "$LOG"
run bash "$REPO/report" --view "$LOG"
[ "$output" = "$(printf 'ok\n100%%')" ]
}
@test "after a failed update, report offers to send the log and sends it on yes" {
answers y
run bash "$REPO/report" --offer "$LOG" 71-siblings.sh 46-neovim.sh
[ "$status" -eq 0 ]
[[ "$output" == *"Your report's ID is CD-7Q2K"* ]]
[ "$(field kind)" = update ]
[ "$(field failed)" = "71-siblings.sh, 46-neovim.sh" ]
[ "$(field kernel)" = 6.99.1-arch1-1 ]
# The log arrives cleaned and redacted
[ "$(gzip -dc "$BATS_TEST_TMPDIR/fields/log.gz")" = "$(printf '%s\n' "Updating CoreDesktop..." \
"Cloning into '~/.local/share/x'..." "Done, with errors in: 71-siblings.sh")" ]
}
@test "the offer can show the log first, and sends nothing on no" {
answers v n
run bash "$REPO/report" --offer "$LOG" 71-siblings.sh
[ "$status" -eq 0 ]
[[ "$output" == *"Cloning into '~/.local/share/x'"* ]]
[[ "$output" == *"Not sent. To send it later, run: report $LOG"* ]]
! grep -q curl "$STUB_LOG"
}
@test "without a terminal, the offer sends nothing and says nothing" {
export COREDESKTOP_TTY=/nonexistent/tty
run bash "$REPO/report" --offer "$LOG" 71-siblings.sh
[ "$status" -eq 0 ]
[ -z "$output" ]
! grep -q curl "$STUB_LOG"
}
@test "report sends the newest log" {
touch -d '1 hour ago' "$LOG"
printf 'install output\n' > "$LOGS/install-2026-11-08-120000.log"
answers y
run bash "$REPO/report"
[ "$status" -eq 0 ]
[[ "$output" == *"install-2026-11-08-120000.log"* ]]
[ "$(field kind)" = install ]
[ -z "$(field failed)" ]
}
@test "a refused report says why and how to try again" {
answers y
export CURL_REPLY='Too many reports from this address; try again tomorrow.\n429'
run bash "$REPO/report" "$LOG"
[ "$status" -eq 1 ]
[[ "$output" == *"Couldn't send the report: Too many reports from this address"* ]]
[[ "$output" == *"To try again later, run: report $LOG"* ]]
}
@test "an unreachable server is reported too" {
answers y
export CURL_REPLY='curl: (6) Could not resolve host: federated.sh' CURL_STATUS=6
run bash "$REPO/report" "$LOG"
[ "$status" -eq 1 ]
[[ "$output" == *"Couldn't send the report: curl: (6) Could not resolve host"* ]]
}
@test "a value starting with @ or < is sent as text, not read as a file" {
answers y
run bash "$REPO/report" --offer "$LOG" "@/etc/passwd"
[ "$status" -eq 0 ]
[ "$(field failed)" = "@/etc/passwd" ]
}
@test "a page that isn't the receiver's answer isn't taken for a report ID" {
answers y
export CURL_REPLY='#!/bin/bash\necho installing\n200'
run bash "$REPO/report" "$LOG"
[ "$status" -eq 1 ]
[[ "$output" == *"Couldn't send the report"* ]]
[[ "$output" != *"Sent."* ]]
}
@test "a report ID ending with a newline, as replies usually do, is accepted" {
answers y
export CURL_REPLY='CD-7Q2K\n\n200'
run bash "$REPO/report" "$LOG"
[ "$status" -eq 0 ]
[[ "$output" == *"Your report's ID is CD-7Q2K;"* ]]
}