Files
felhom.eu/hub/internal/monitor/daily_ledger_test.go
T
admin 1886030782
gates / gates (push) Successful in 2m9s
hub (unreleased): four silent failures tell the operator (R-1383, D78)
A hub daily check skipped while the hub was down; an operator mail that was never sent; a signed job a box did not
take; a signed job a box refused, and the guest-power watchdog's give-up (both from the agent). Operator-only.
No new table. Not deployed.

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-14 17:29:53 +01:00

117 lines
4.6 KiB
Go

package monitor
import (
"io"
"log"
"strings"
"testing"
"time"
)
type ledgerFix struct {
state map[string]time.Time
events []string
l *DailyLedger
}
func newLedgerFix(watched ...string) *ledgerFix {
f := &ledgerFix{state: map[string]time.Time{}}
f.l = NewDailyLedger(
func(k string) time.Time { return f.state[k] },
func(k string, t time.Time) error { f.state[k] = t; return nil },
func(cust, typ, sev, msg, details, src string) {
f.events = append(f.events, cust+"|"+typ+"|"+sev+"|"+src+"|"+msg+"|"+details)
},
log.New(io.Discard, "", 0), watched...)
return f
}
func bud(t *testing.T) *time.Location {
t.Helper()
loc, err := time.LoadLocation("Europe/Budapest")
if err != nil {
t.Fatal(err)
}
return loc
}
// R-1383 (D78), the case the ruling names: the hub was DOWN at 05:00. Before the ledger the scheduler's „last run”
// lived in memory, so the next start knew nothing. The stored effect asserted here: ONE operator event that names the
// job and the missed slot — and none for a slot that ran, none twice for the same slot, none for a job nobody watches.
// COMPANION RED-PROOFS (observed, audits/night-build-2026-11-14/B/redproof-hub-daily-ledger.txt).
func TestDailyLedger_HubDownAtTheSlotIsTold(t *testing.T) {
loc := bud(t)
at := func(d, h, m int) time.Time { return time.Date(2026, 11, d, h, m, 0, 0, loc) }
f := newLedgerFix("deadline-check")
// day 10: the first start ever at 04:00 — nothing to compare with, no word
f.l.Started("deadline-check", at(10, 4, 0), 5, 0, loc)
// 05:00 runs
f.l.Ran("deadline-check", at(10, 5, 0))
// a restart the same day at 09:00: the 05:00 slot ran → no word
f.l.Started("deadline-check", at(10, 9, 0), 5, 0, loc)
if len(f.events) != 0 {
t.Fatalf("a slot that ran was told as skipped: %v", f.events)
}
// the hub goes down on day 10 at 23:00 and starts again on day 11 at 05:00:30 — the 05:00 slot passed
f.l.Started("deadline-check", at(11, 5, 0).Add(30*time.Second), 5, 0, loc)
if len(f.events) != 1 {
t.Fatalf("events=%d, want ONE for the slot the hub was down at: %v", len(f.events), f.events)
}
e := f.events[0]
for _, want := range []string{"|hub_daily_job_skipped|warning|hub|", "deadline-check", "2026-11-11 05:00", "2026-11-10 05:00", "1 slot(s) missed", `"missed_slot":"2026-11-11T04:00:00Z"`} {
if !strings.Contains(e, want) {
t.Errorf("the event lacks %q:\n%s", want, e)
}
}
if !strings.HasPrefix(e, "|") {
t.Errorf("a hub-wide event must carry no customer: %s", e)
}
// a second restart the same morning: told once
f.l.Started("deadline-check", at(11, 6, 0), 5, 0, loc)
if len(f.events) != 1 {
t.Fatalf("the same missed slot was told %d times", len(f.events))
}
// day 12 runs; a restart after it is quiet; then two days down → told again, and it counts 2
f.l.Ran("deadline-check", at(12, 5, 0))
f.l.Started("deadline-check", at(12, 12, 0), 5, 0, loc)
if len(f.events) != 1 {
t.Fatalf("a restart after a slot that ran was told: %v", f.events[1:])
}
f.l.Started("deadline-check", at(14, 8, 0), 5, 0, loc)
if len(f.events) != 2 || !strings.Contains(f.events[1], "2 slot(s) missed") || !strings.Contains(f.events[1], "2026-11-14 05:00") {
t.Fatalf("two days down: %v", f.events[1:])
}
}
// A job that is not on the watch list (the snapshot and the prune make up for a missed slot by themselves) is never
// told and leaves no state. And a nil ledger is safe: the scheduler calls it without a check.
func TestDailyLedger_UnwatchedJobAndNilLedger(t *testing.T) {
loc := bud(t)
f := newLedgerFix("deadline-check")
f.l.Ran("prune", time.Date(2026, 11, 1, 3, 0, 0, 0, loc))
f.l.Started("prune", time.Date(2026, 11, 9, 9, 0, 0, 0, loc), 3, 0, loc)
if len(f.events) != 0 || len(f.state) != 0 {
t.Fatalf("an unwatched job: events=%v state=%v", f.events, f.state)
}
var nl *DailyLedger
nl.Started("deadline-check", time.Now(), 5, 0, loc)
nl.Ran("deadline-check", time.Now())
}
// The slot is a wall-clock time in Budapest: on the two days the clocks change it is still „the newest 05:00”.
func TestPrevDailySlot_HourChangeDays(t *testing.T) {
loc := bud(t)
for _, c := range []struct{ now, want string }{
{"2026-10-25T04:30:00+01:00", "2026-10-24T05:00:00+02:00"}, // 25-hour day, before the slot
{"2026-10-25T05:00:00+01:00", "2026-10-25T05:00:00+01:00"}, // at the slot: it is the slot
{"2027-03-28T12:00:00+02:00", "2027-03-28T05:00:00+02:00"}, // 23-hour day
} {
now, _ := time.Parse(time.RFC3339, c.now)
want, _ := time.Parse(time.RFC3339, c.want)
if got := PrevDailySlot(now, 5, 0, loc); !got.Equal(want) {
t.Errorf("PrevDailySlot(%s) = %s, want %s", c.now, got.Format(time.RFC3339), c.want)
}
}
}