package link import ( "context" "testing" "time" ) // A health event is kept as the machine its subject names says it, whatever its body claims, and taken // (novox/hq ADR 0240): core NATS, nothing to hold. type keptHealths struct{ by map[string]Health } func (k *keptHealths) Stated(_ context.Context, node string, h Health) error { k.by[node] = h return nil } func TestAHealthEventIsKeptAsTheMachineInItsSubject(t *testing.T) { s, in := serving() kept := &keptHealths{by: map[string]Health{}} s.Hears(kept) to := &settled{} m := in.sends(t, to, KindHealth, HealthSaid{Node: "laptop", Health: Health{Contract: LivenessContract, At: time.Now(), Resources: []ResourceHealth{{Module: "letta", Resource: "letta.server", State: StateUnhealthy}}}}).(*fakeControl) m.subject = HealthSubject("anchor") s.act(t.Context(), m) if !to.acked { t.Fatal("a health event was not taken") } if _, lied := kept.by["laptop"]; lied || len(kept.by["anchor"].Resources) != 1 { t.Fatalf("kept %+v: the machine is the subject's, never the body's", kept.by) } if kind, ok := kindOfSubject(HealthSubject("anchor")); !ok || kind != KindHealth { t.Fatalf("%s is not read as a health statement", HealthSubject("anchor")) } }