package api import ( "fmt" "net/http" "strings" "testing" "time" "gitea.dooplex.hu/admin/felhom-hub/internal/store" ) func r1383Events(t *testing.T, st *store.Store, typ string) []store.Event { t.Helper() evs, err := st.GetEventsByType("c1", typ, time.Now().Add(-time.Hour)) if err != nil { t.Fatal(err) } return evs } // R-1383 (D78): an agent that clears a signed job says HOW it ended. A refused job (here: expired — the box's clock // is ahead, so an agent update never arrives) leaves one operator event that names the job, the op and the cause. // A job that ran, and an agent that says nothing (older), leave none. The stored effect is the event row; the queue // is emptied in every case. COMPANION RED-PROOFS (observed, audits/night-build-2026-11-14/B/redproof-hub-box-told.txt). func TestSignedJobOutcome_RefusedIsToldDoneIsNot(t *testing.T) { h, st := trustSetup(t) del := func(job, query string) { t.Helper() if err := st.EnqueueSignedJob("h1", job, []byte("opaque")); err != nil { t.Fatal(err) } if rr := do(h, http.MethodDelete, "/hosts/h1/jobs/"+job+query, "HKEY", ""); rr.Code != 200 { t.Fatalf("DELETE %s: %d %s", job, rr.Code, rr.Body.String()) } if n, _ := st.CountSignedJobs("h1"); n != 0 { t.Fatalf("job %s is still queued", job) } } del("ran", "?outcome=done&op=agent_update") del("old-agent", "") if evs := r1383Events(t, st, eventSignedJobRefused); len(evs) != 0 { t.Fatalf("a job that ran, or an agent that says nothing, raised %d event(s): %+v", len(evs), evs) } del("j-exp", "?outcome=rejected&reason=expired&op=agent_update") // the agent repeats a DELETE whose answer it lost: told once if rr := do(h, http.MethodDelete, "/hosts/h1/jobs/j-exp?outcome=rejected&reason=expired&op=agent_update", "HKEY", ""); rr.Code != 200 { t.Fatalf("repeat DELETE: %d", rr.Code) } evs := r1383Events(t, st, eventSignedJobRefused) if len(evs) != 1 { t.Fatalf("events=%d, want ONE for the refused job", len(evs)) } for _, want := range []string{"j-exp", "agent_update", "h1", "REFUSED", "clock is AHEAD"} { if !strings.Contains(evs[0].Message, want) { t.Errorf("the event lacks %q: %s", want, evs[0].Message) } } if evs[0].Severity != "error" || !strings.Contains(evs[0].DetailsJSON, `"reason":"expired"`) { t.Errorf("event = %+v", evs[0]) } // a box „clears” job ids that were never in its queue: no event — a box cannot mint operator mails for _, id := range []string{"never-queued", "never-queued-2", "x%0Ay"} { if rr := do(h, http.MethodDelete, "/hosts/h1/jobs/"+id+"?outcome=rejected&reason=expired&op=agent_update", "HKEY", ""); rr.Code != 200 { t.Fatalf("DELETE of an unknown job: %d", rr.Code) } } if evs := r1383Events(t, st, eventSignedJobRefused); len(evs) != 1 { t.Fatalf("clearing ids that were never queued raised events: %d, want still 1", len(evs)) } // a failed run, and words the hub does not know: told, and the box's own text never reaches the mail del("j-fail", "?outcome=failed&reason=execution_failed&op=agent_config_update") del("j-odd", "?outcome=rejected&reason=%3Cscript%3E&op=%3Cb%3EX") evs = r1383Events(t, st, eventSignedJobRefused) if len(evs) != 3 { t.Fatalf("events=%d, want 3", len(evs)) } all := "" for _, e := range evs { all += e.Message + e.DetailsJSON + "\n" } if !strings.Contains(all, "ran and FAILED") || !strings.Contains(all, `"reason":"other"`) || !strings.Contains(all, `"op":"unknown"`) { t.Errorf("the failed run or the unknown words are not told as designed:\n%s", all) } if strings.Contains(all, "script") || strings.Contains(all, "") { t.Errorf("the box's own text reached an event:\n%s", all) } } func gaveUpReport(guestStatus, stanza string) string { b := validReportBody("h1") b = strings.Replace(b, `{"vmid":100,"name":"acme","status":"running"`, `{"vmid":100,"name":"acme","status":"`+guestStatus+`"`, 1) if stanza == "" { return b } return b[:strings.LastIndex(b, "}")] + `,"guest_power_gave_up":` + stanza + "}" } // R-1383 (D78): the guest-power watchdog gave up — the household's apps are off and the agent wrote one journal line. // One operator event per down period; the agent's list is memory, so the entry vanishing (an agent restart) while // the guest is still stopped must neither clear nor alarm a second time; the recovery is the guest RUNNING. func TestGuestPowerGaveUp_ToldOnceClearedOnlyByARunningGuest(t *testing.T) { h, st := trustSetup(t) post := func(status, stanza string) { t.Helper() if rr := do(h, http.MethodPost, "/host-report", "HKEY", gaveUpReport(status, stanza)); rr.Code != 200 { t.Fatalf("report: %d %s", rr.Code, rr.Body.String()) } } counts := func() string { return fmt.Sprintf("gave_up=%d ok=%d", len(r1383Events(t, st, eventGuestPowerGaveUp)), len(r1383Events(t, st, eventGuestPowerRunning))) } post("running", "") post("stopped", "") // stopped, the watchdog still tries: nothing if c := counts(); c != "gave_up=0 ok=0" { t.Fatalf("before a give-up: %s", c) } stanza := `[{"vmid":100,"since":"2026-11-14T02:00:00Z","attempts":3}]` post("stopped", stanza) post("stopped", stanza) if c := counts(); c != "gave_up=1 ok=0" { t.Fatalf("two reports of the same give-up: %s, want ONE alarm", c) } e := r1383Events(t, st, eventGuestPowerGaveUp)[0] if e.Severity != "error" || !strings.Contains(e.Message, "Guest 100 on h1 is STOPPED") || !strings.Contains(e.Message, "3 failed attempts") { t.Errorf("alarm = %+v", e) } post("stopped", "") // the agent restarted: its list is empty, the guest is still down if c := counts(); c != "gave_up=1 ok=0" { t.Fatalf("the entry vanished while the guest is still stopped: %s — that is not a recovery", c) } post("stopped", `[{"vmid":100,"since":"2026-11-14T02:20:00Z","attempts":3}]`) // it gave up again: same down period if c := counts(); c != "gave_up=1 ok=0" { t.Fatalf("the second give-up of the same down period: %s", c) } post("running", "") post("running", "") if c := counts(); c != "gave_up=1 ok=1" { t.Fatalf("after the guest runs again: %s, want one recovery", c) } post("stopped", stanza) // a new down period is told again if c := counts(); c != "gave_up=2 ok=1" { t.Fatalf("a new down period: %s", c) } // the list is the box's: a vmid the same report does not list is not read, and a long list is cut many := "[" for i := 0; i < 500; i++ { many += fmt.Sprintf(`{"vmid":%d,"since":"2026-11-14T02:00:00Z","attempts":3},`, 5000+i) } many += `{"vmid":101,"since":"2026-11-14T02:00:00Z","attempts":3}]` // 101 IS a guest of the report (stopped), but entry 501 post("stopped", many) if c := counts(); c != "gave_up=2 ok=1" { t.Fatalf("500 invented guests and a list past the cap: %s, want no new alarm", c) } post("stopped", `[{"vmid":101,"since":"2026-11-14T02:00:00Z","attempts":3},{"vmid":7777,"since":"x","attempts":3}]`) if c := counts(); c != "gave_up=3 ok=1" { t.Fatalf("a real second guest (101) within the cap: %s, want one more alarm and none for vmid 7777", c) } }