package main import ( "encoding/json" "fmt" "strings" "testing" "time" "github.com/novox/mesh-controller/internal/conditions" ) // stalledConditions are n open conditions like those of 2026-10-08: one per delivery held past its bound, // each keeping all its evidence. func stalledConditions(n int) []conditions.Condition { at := time.Date(2026, 10, 8, 9, 0, 0, 0, time.UTC) var out []conditions.Condition for i := 0; i < n; i++ { id := fmt.Sprintf("d-%012x", 0x51a11ed+i) lines := stalledObservations([]stalledLine{{ID: id, State: "checking", For: "13h2m", Bound: "45m", H2: "close: recheck", Says: "the check was asked and nothing has answered"}}) o := lines[0] c := conditions.Condition{Key: o.Key(), Kind: o.Kind, Subject: conditions.Subject{Scope: o.Scope, ID: o.ID}, Severity: o.Severity, Summary: o.Summary, Source: probeDeliveriesID, Raised: at, LastObserved: at, Observations: 160, Count: 1, Resolver: conditions.ResolverSelf} for e := 0; e < conditions.KeptEvidence; e++ { c.Evidence = append(c.Evidence, conditions.Evidence{At: at.Add(time.Duration(e) * 5 * time.Minute), Said: "checking for " + (13*time.Hour + time.Duration(e)*time.Minute).String()}) } out = append(out, c) } return out } // novox/hq issue 314: a list of hundreds of open conditions carries each one's newest evidence, with // how much more it keeps and where to read it, and the verb's answer says it once — as data, not again // as the text the command printed. On 2026-10-08, 689 stalled deliveries made that answer more than the // bus carries in one message; now it is less than half of one. func TestAListOfConditionsCarriesEachInBrief(t *testing.T) { list := stalledConditions(689) printed, _ := json.MarshalIndent(map[string]any{"conditions": inBrief(list), "open": len(list), "counted": counted(list)}, "", " ") var parsed any if err := json.Unmarshal(printed, &parsed); err != nil { t.Fatal(err) } wire, _ := json.Marshal(map[string]any{"result": verbAnswer{OK: true, Answer: parsed, Output: "its answer, as data, is `answer`\n"}}) before, _ := json.MarshalIndent(map[string]any{"conditions": list}, "", " ") t.Logf("689 stalled deliveries: %d bytes on the wire, where the whole list printed was %d and was sent twice", len(wire), len(before)) if len(wire) >= 512<<10 { t.Fatalf("the answer of 689 conditions is %d bytes, not under half of one message of the bus", len(wire)) } var doc struct { Conditions []conditionInBrief `json:"conditions"` Counted map[string]int `json:"counted"` } if err := json.Unmarshal(printed, &doc); err != nil { t.Fatal(err) } c := doc.Conditions[0] if len(doc.Conditions) != 689 || len(c.Evidence) != 1 || c.EvidenceKept != conditions.KeptEvidence || c.Key != list[0].Key || c.Summary != list[0].Summary || !strings.Contains(c.More, "key="+c.Key) { t.Fatalf("a condition in brief reads %+v", c) } if doc.Counted["D14 stalled"] != 689 { t.Fatalf("counted %v", doc.Counted) } if one := inBrief(list[:1])[0]; len(list[0].Evidence) != conditions.KeptEvidence || one.Evidence[0] != list[0].Evidence[0] { t.Fatal("the brief changed the condition it was made from, or kept other than the newest") } } // The self-check's verdict carries at most overviewFindings of one probe's findings, with their count and // how to read them all; `doctor probe=` gives that probe whole (novox/hq issue 314). func TestAVerdictCarriesAProbesFindingsInBrief(t *testing.T) { var found []string for i := 0; i < 689; i++ { found = append(found, fmt.Sprintf("delivery d-%012x has been checking for 13h, past its bound of 45m", i)) } run := doctorRun{Run: "run-1", Probes: []probeVerdict{{ID: "D14", Verdict: "failed", Found: found}, {ID: "D1", Verdict: "passed"}}} answer, err := verdictAnswer(run, time.Now(), "") if err != nil { t.Fatal(err) } brief := answer["run"].(doctorRun).Probes[0] if len(brief.Found) != overviewFindings || brief.FoundCount != 689 || !strings.Contains(brief.More, "probe=D14") { t.Fatalf("the verdict carries %d findings of D14, count %d, more %q", len(brief.Found), brief.FoundCount, brief.More) } if len(run.Probes[0].Found) != 689 { t.Fatal("the overview cut the run it was made from") } whole, err := verdictAnswer(run, time.Now(), "d14") if err != nil { t.Fatal(err) } if p := whole["run"].(doctorRun).Probes; len(p) != 1 || len(p[0].Found) != 689 || p[0].More != "" { t.Fatalf("doctor probe=D14 answered %+v", p) } if _, err := verdictAnswer(run, time.Now(), "D99"); err == nil { t.Fatal("a probe the run does not have was answered") } if !strings.Contains(doctorText(answer), "probe=D14` gives them all") { t.Fatalf("the text does not say how to read them all:\n%s", doctorText(answer)) } }