From d79b9f1518577143dd7fb3d716730e0e9db3fc62 Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 22 Nov 2026 12:53:22 +0100 Subject: [PATCH] Indexer: test a registration through the history stream its manifest declares (H-A-470 (3)) The tests wrote registrations straight into their state or through a fake history, so none used the stream's real subject and the one-token subject that dropped source.novox.hq went unseen. The new integration test makes registration-history from the manifest as the grants part does and writes a module's and a source-only registration through the program's registry; with the old subject it fails with the live journal's words. Builds: design 47 H2 H-A-470 (3), H-A-408 --- .../registry_integration_test.go | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry_integration_test.go diff --git a/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry_integration_test.go b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry_integration_test.go new file mode 100644 index 000000000..e1d177d5b --- /dev/null +++ b/modules/mesh-delivery-indexer/cmd/mesh-delivery-indexer/registry_integration_test.go @@ -0,0 +1,153 @@ +//go:build integration + +package main + +import ( + "context" + "encoding/json" + "os" + "strings" + "testing" + "time" + + "github.com/nats-io/nats-server/v2/server" + "github.com/nats-io/nats.go" + "github.com/nats-io/nats.go/jetstream" + + occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient" + oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" + gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + "git.novox.be/novox/mesh-sdk/go/buscompose" + "git.novox.be/novox/mesh-sdk/go/catalogue" + + "mesh-delivery-indexer/internal/index" +) + +// TestARegistrationIsKeptOnTheHistoryStreamItsManifestDeclares (design 47 H2 H-A-470 (3); the kind of fix-a-bug-met-twice: a +// test that does not use the real stream's subject): the program's registry, as newRegistry wires it, writes a warranted +// registration of a module (one token after registration.) and of a source-only repository (source.., H-A-408's +// key) through the stream registration-history on an embedded bus, made from the indexer's manifest's streams section as the +// grants part makes it (buscompose); each version lands on its subject, the name whole. +func TestARegistrationIsKeptOnTheHistoryStreamItsManifestDeclares(t *testing.T) { + srv, err := server.NewServer(&server.Options{Host: "127.0.0.1", Port: -1, JetStream: true, StoreDir: t.TempDir(), NoLog: true, NoSigs: true}) + if err != nil { + t.Fatal(err) + } + go srv.Start() + if !srv.ReadyForConnections(10 * time.Second) { + t.Fatal("the bus did not come up") + } + t.Cleanup(srv.Shutdown) + conn, err := nats.Connect(srv.ClientURL()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(conn.Close) + js, err := jetstream.New(conn) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + t.Cleanup(cancel) + + // The stream as the grants part makes it from the manifest: its events spelled as the bus spells them, on the module's subjects. + raw, err := os.ReadFile("../../module.json") + if err != nil { + t.Fatal(err) + } + var m struct { + Streams []struct { + Name string `json:"name"` + Events []string `json:"events"` + } `json:"streams"` + } + if err := json.Unmarshal(raw, &m); err != nil { + t.Fatal(err) + } + var wire []string + for _, s := range m.Streams { + if s.Name == index.RegistrationHistoryStream { + for _, e := range s.Events { + wire = append(wire, buscompose.WirePattern(e)) + } + } + } + subjects := (buscompose.ModuleStream{Module: testIndexer, Events: wire}).Subjects() + stream, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: catalogue.StreamBusName(index.RegistrationHistoryStream), Subjects: subjects}) + if err != nil { + t.Fatalf("the stream of %v: %v", subjects, err) + } + + // The seat git answers the default branch's head and a tree holding no module.json; the router's record holds the warrant + // of each ask the indexer publishes. + const head = "0123456789abcdef0123456789abcdef01234567" + call := func(key string, body any) (json.RawMessage, error) { + var out any + switch key { + case "seat:" + gitseat.Seat + "." + gitseat.VerbBranch: + in := body.(gitseat.BranchInput) + out = gitseat.BranchAnswer{Branch: "main", Repository: in.Repository, Head: head} + case gitVerb(gitseat.VerbTrees): + out = gitseat.TreesAnswer{Commit: head, Trees: map[string]string{"tools": "t1"}} + default: + t.Errorf("asked %s", key) + } + return json.Marshal(out) + } + asks := make(chan oc.Ask, 4) + sub, err := conn.Subscribe(oc.AcceptSubjectAsk(testIndexer), func(msg *nats.Msg) { + var a oc.Ask + if err := json.Unmarshal(msg.Data, &a); err == nil { + asks <- a + } + }) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = sub.Unsubscribe() }) + warrants := map[string]oc.Warrant{} + where := func(id string) (string, *oc.Warrant, error) { + if w, ok := warrants[id]; ok { + return "chosen", &w, nil + } + return "open", nil, nil + } + reg := newRegistry(js, conn, testAccount, call, index.MemoryStore(), index.MemoryStore(), where, &index.Indexer{Module: testIndexer}, t.Logf) + + check := &meshcatalog.SourceCheck{Seat: meshcatalog.SourceCheckSeatNodeBuildGo, Step: meshcatalog.SourceCheckStepUnit} + for name, r := range map[string]meshcatalog.Registration{ + "avahi": {Module: "avahi", Origin: "novox/mesh-catalog", Path: "modules/avahi/module.json", Branches: []string{"main"}}, + "source.novox.hq": {Origin: "novox/hq", Check: check, Policy: meshcatalog.Policy{Steps: meshcatalog.StepStatuses{Unit: "required"}}}, + } { + change, _ := json.Marshal(meshcatalog.RegisterInput{Registration: r}) + got, err := reg.Propose(ctx, "register", "s@n", change) + if err != nil || got.Ask == "" || got.Module != name { + t.Fatalf("register %s: %+v, %v; want an ask", name, got, err) + } + var a oc.Ask + select { + case a = <-asks: + case <-ctx.Done(): + t.Fatalf("the ask of %s was not published", name) + } + o, _ := occ.OptionOf(a, "approve") + w := oc.Warrant{Ask: a.ID, Asker: testIndexer, Outcome: occ.OutcomeChosen, Option: "approve", Level: o.Level, Label: o.Label, + AskDigest: occ.Digest(a), At: occ.FormatTime(time.Now()), By: &oc.Person{Who: occ.Operator, Kind: "telegram", Verified: "user id verified"}} + warrants[a.ID] = w + if err := reg.Heard(ctx, w); err != nil { + t.Fatalf("the warrant of %s was not applied: %v", name, err) + } + msg, err := stream.GetLastMsgForSubject(ctx, historySubject(testIndexer, name)) + var kept meshcatalog.Registration + if err == nil { + err = json.Unmarshal(msg.Data, &kept) + } + if err != nil || kept.Version != 1 || index.RegistrationName(kept) != name { + t.Fatalf("registration-history of %s holds %+v (%v); want its version 1 kept on %v", name, kept, err, subjects) + } + if one, _, _ := reg.Registration(ctx, name); one.Version != 1 || !strings.Contains(one.Origin, "novox/") { + t.Errorf("the registration of %s is %+v, want version 1 written", name, one) + } + } +}