package link import ( "strings" "testing" "time" "github.com/nats-io/nats.go/jetstream" "github.com/novox/mesh-controller/internal/broker" ) // The hand-act log against a real server (novox/hq to-be 45 ยง7): an act is written with who, why and // its cause, read back in order, and a cause recorded twice within a fortnight is found. func TestNatsAnActByHandIsKeptWithWhyAndARepeatIsFound(t *testing.T) { js := aBus(t) api, err := jetstream.New(js.Conn()) if err != nil { t.Fatal(err) } _ = api.DeleteKeyValue(t.Context(), broker.HandActsBucket) if err := js.EnsureControllerBuckets(); err != nil { t.Fatal(err) } t.Setenv(CallerVar, "node-tools.g14, through the mesh-controller seat") if _, err := RecordHandAct(t.Context(), js.Conn(), HandAct{Verb: "push", Args: []string{"anchor"}}); err == nil { t.Fatal("an act without why was written") } first, err := RecordHandAct(t.Context(), js.Conn(), HandAct{Verb: "push", Args: []string{"anchor"}, Why: "it never reported the send", Cause: "sent-not-reported"}) if err != nil { t.Fatal(err) } if first.By != "node-tools.g14, through the mesh-controller seat" || first.ID == "" { t.Fatalf("written as %+v", first) } if _, err := RecordHandAct(t.Context(), js.Conn(), HandAct{Verb: "plans close", Args: []string{"plan-1"}, Why: "waiting on the same report"}); err != nil { t.Fatal(err) } if _, err := RecordHandAct(t.Context(), js.Conn(), HandAct{Verb: "push", Args: []string{"ace"}, Why: "again", Cause: "sent-not-reported", At: time.Now().UTC().Add(time.Second)}); err != nil { t.Fatal(err) } acts, err := HandActs(t.Context(), js.Conn(), time.Now().Add(-time.Hour)) if err != nil || len(acts) != 3 || acts[0].ID != first.ID || acts[1].Cause != "plans close" { t.Fatalf("%v %+v", err, acts) } repeated := RepeatedCauses(acts, time.Now()) if len(repeated) != 1 || repeated["sent-not-reported"] != 2 { t.Fatalf("repeated %v", repeated) } if old, _ := HandActs(t.Context(), js.Conn(), time.Now().Add(time.Hour)); len(old) != 0 { t.Fatalf("acts before the moment asked were listed: %+v", old) } if !strings.HasPrefix(acts[2].ID, "act-") { t.Fatalf("%q", acts[2].ID) } }