#!/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= "$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 : " \ "ardop 1.0.4.1.3 is up to date. gridtracker2 2.261108.4-1, python 3.14" \ "ssh @ from ; mail " \ " " \ "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;"* ]] }