Files
archie/internal/releaseupdate/pending_report_test.go
T
sam 7c409aadb5 feat(update): stream live progress and roll back failed updates automatically
The /update command went silent between "starting" and one final message,
gave no version summary, and required manual intervention on failure.

CommandInstaller now streams the installer script's stdout live instead of
buffering it, and parses a structured result (previous/installed versions,
per-component status) from a sentinel line. Because the reference install
script hands the daemon restart off to a detached systemd unit specifically
so restarting archied.service doesn't kill the process that requested the
update, the outcome is reported in two phases: the synchronous build/install
result comes back through Install() itself, and the post-restart health
check/rollback outcome is written by a new watchdog script and relayed by
whichever archied process boots next, via a small JSON handoff file cleared
once relayed.

Also fixes three defects an adversarial review pass found in the first
draft: the caller's request/handler context was passed straight into the
install child process, so a concurrent /restart or a browser disconnect
could SIGKILL it mid binary-copy; Telegram and the web dashboard each
tracked "install in progress" with their own private flag despite sharing
one Service, allowing two installs to race the same .prev backups; and the
watchdog reported rolled_back:true even when no backup existed to restore,
which would have told an operator to stand down while the daemon was still
broken.

internal/webui gains an unauthenticated /healthz the watchdog polls after
restarting archied.
2026-08-19 01:47:29 +10:00

81 lines
2.9 KiB
Go

package releaseupdate
import (
"os"
"path/filepath"
"testing"
)
// TestPendingReportRoundTrip covers the cross-restart handoff: the watchdog
// script decides the phase-2 outcome and writes it; whichever archied
// process boots next reads it, relays it, and clears it so it is never
// reported twice.
func TestPendingReportRoundTrip(t *testing.T) {
path := filepath.Join(t.TempDir(), "update-report.json")
want := Report{
Channel: "telegram",
ChatID: 42,
ThreadID: 7,
Previous: map[string]string{"daemon": "1.0.0", "agent": "1.0.0"},
Installed: map[string]string{"daemon": "1.1.0", "agent": "1.0.0"},
HealthCheck: "passed",
RolledBack: false,
}
if err := WritePendingReport(path, want); err != nil {
t.Fatalf("WritePendingReport() error = %v", err)
}
got, found, err := ReadPendingReport(path)
if err != nil {
t.Fatalf("ReadPendingReport() error = %v", err)
}
if !found {
t.Fatal("ReadPendingReport() found = false, want true")
}
if got.Channel != want.Channel || got.ChatID != want.ChatID || got.ThreadID != want.ThreadID ||
got.HealthCheck != want.HealthCheck || got.RolledBack != want.RolledBack ||
got.Installed["daemon"] != want.Installed["daemon"] || got.Previous["daemon"] != want.Previous["daemon"] {
t.Fatalf("ReadPendingReport() = %#v, want %#v", got, want)
}
if err := ClearPendingReport(path); err != nil {
t.Fatalf("ClearPendingReport() error = %v", err)
}
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Fatalf("report file still exists after ClearPendingReport(): err = %v", err)
}
}
// TestReadPendingReportMissingFileReturnsNotFound: no update in flight is
// the overwhelmingly common case (every normal startup), so it must be a
// plain "not found", never an error a caller has to specially ignore.
func TestReadPendingReportMissingFileReturnsNotFound(t *testing.T) {
path := filepath.Join(t.TempDir(), "does-not-exist.json")
_, found, err := ReadPendingReport(path)
if err != nil {
t.Fatalf("ReadPendingReport() error = %v, want nil", err)
}
if found {
t.Fatal("ReadPendingReport() found = true, want false")
}
}
// TestClearPendingReportMissingFileIsNotAnError: startup may race a report
// that was already cleared by a previous boot attempt; clearing twice must
// be harmless.
func TestClearPendingReportMissingFileIsNotAnError(t *testing.T) {
path := filepath.Join(t.TempDir(), "does-not-exist.json")
if err := ClearPendingReport(path); err != nil {
t.Fatalf("ClearPendingReport() error = %v, want nil for a missing file", err)
}
}
func TestWritePendingReportRejectsRolledBackWithoutHealthCheckFailure(t *testing.T) {
path := filepath.Join(t.TempDir(), "update-report.json")
bad := Report{Channel: "telegram", ChatID: 1, RolledBack: true, HealthCheck: "passed"}
if err := WritePendingReport(path, bad); err == nil {
t.Fatal("WritePendingReport() error = nil, want a validation error for rolled_back+passed")
}
}