diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go index 7d6726788..4e29de34f 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/bus_test.go @@ -19,6 +19,7 @@ import ( "time" oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel" + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" stdio "git.novox.be/novox/mesh-sdk/go" "github.com/nats-io/nats-server/v2/server" "github.com/nats-io/nats.go" @@ -54,6 +55,10 @@ type testBus struct { answers map[string][]byte // the gate's ledger `records`, as the tool runner keeps it: it outlives one run of the gate, as the bus's does. ledger *ledgerFake + // registrations are what the seat mesh-catalog's verb registrations answers, and heads what the seat git's verb branch + // answers, by `@` (the start pass and the seed read them, design 47 H §768). + registrations []meshcatalog.Registration + heads map[string]string } func newTestBus(t *testing.T) *testBus { @@ -355,6 +360,22 @@ func (r *runtime) answer(m rpcMessage) { switch a.Key { case "": return nil, errors.New("an ask with no key") + case "seat:" + meshcatalog.Seat + "." + meshcatalog.VerbRegistrations: + r.b.mu.Lock() + defer r.b.mu.Unlock() + return toolAnswer(meshcatalog.RegistrationsAnswer{Registrations: append([]meshcatalog.Registration{}, r.b.registrations...)}), nil + case "seat:git.branch": + repository, _ := a.Body["repository"].(string) + branch, _ := a.Body["branch"].(string) + r.b.mu.Lock() + defer r.b.mu.Unlock() + head, ok := r.b.heads[repository+"@"+branch] + if !ok { + return nil, fmt.Errorf("the forge of this test holds no %s@%s", repository, branch) + } + return toolAnswer(map[string]any{"repository": repository, "branch": branch, "head": head}), nil + case "seat:mesh-hand-acts.hand-act": + return toolAnswer(map[string]any{"id": "hand-act-1"}), nil case "gitea.gitea_note_append", "gitea.gitea_delivery_view", "gitea.gitea_commit_status", "gitea.gitea_merge_pull_request", "gitea.gitea_add_comment": return toolAnswer(map[string]any{}), nil diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go new file mode 100644 index 000000000..129567db4 --- /dev/null +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/mergecall_bus_test.go @@ -0,0 +1,132 @@ +//go:build integration + +package main + +import ( + "context" + "encoding/json" + "strings" + "testing" + "time" + + mdc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient" + meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg" + "git.novox.be/novox/mesh-sdk/go/keys" + "github.com/nats-io/nats.go/jetstream" +) + +// The gate whole, over a real bus (design 47 H §737, §738, §740, §742, §768; acceptance tests 110 and 111's gate half): +// a head held past untaken-bound from before a stop raises head-untaken at the start; a merge-called the forge module says +// names one source revision of a branch holding `/` and `.`, opens its delivery record and says source-moved, and the +// head-moved of that merge then holds nothing, the source key read with the escaping it is written with (H-A-87 (2)); a +// head-moved no call delivered is held; the indexer's take-in.taken of the revision writes its movers into the record; +// after gate_source_seed a head-moved at the seeded head holds nothing. The gate asks the controller nothing. +const eventWithin = 20 * time.Second + +func TestAGateWholeTakesAMergeCallAndWatchesTheHeadsOverARealBus(t *testing.T) { + b := newTestBus(t) + ctx := context.Background() + branch := "release/1.2" // a `/` and a `.`: the dot is escaped in every key + escaped := keys.EscapeName(branch) + heldKey := func(repository, branch string) string { return headKey(repository, branch) } + old := strings.Repeat("0", 40) + // A head held from before a stop, past the bound: the state outlives the gate, as the bus's does. + raw, _ := json.Marshal(HeldHead{Head: old, Since: time.Now().Add(-time.Hour).UTC()}) + if _, err := b.kvs["heads"].Put(ctx, heldKey("novox/old", branch), raw); err != nil { + t.Fatal(err) + } + rt := b.start(t) + evctx, cancel := context.WithCancel(ctx) + defer cancel() + rt.deliverEvents(evctx) + rt.untilListening() + rt.until("head-untaken for the head held past the bound", eventWithin, func() bool { + return findingHeld(ctx, b, "head-untaken", heldKey("novox/old", branch)) + }) + + merge := strings.Repeat("c", 40) + call := "0123456789abcdef" + instance, _ := RecordInstance(recordMilestone(branch, 1)) + member := map[string]any{"repository": "novox/app", "number": 5, "branch": branch, "head": strings.Repeat("d", 40), "commit": merge} + for range 2 { // heard twice: one revision + b.publish(t, "mesh.seat.git.event.merge-called."+call, map[string]any{"call": call, "by": "lead@shanks", "account": "acct-s", + "at": "2026-11-20T03:00:00Z", "members": []any{member}}) + } + sourceKey := mdc.SourceKey(escaped) + rt.until("the record of the merge call and its source revision", eventWithin, func() bool { + e, err := b.kvs["source"].Get(ctx, sourceKey) + if err != nil || !strings.Contains(string(e.Value()), `"call":"`+call+`"`) || !strings.Contains(string(e.Value()), `"source":1`) { + return false + } + b.mu.Lock() + defer b.mu.Unlock() + return b.ledger != nil && b.ledger.states[instance] != nil + }) + headMoved(t, b, "novox/app", branch, merge) // the merge call's head: taken + headMoved(t, b, "novox/push", branch, merge) // a push no call delivered: held + rt.until("the pushed head held", eventWithin, func() bool { return kvHas(ctx, b, "heads", heldKey("novox/push", branch)) }) + if kvHas(ctx, b, "heads", heldKey("novox/app", branch)) { + t.Fatalf("the head of the merge call on %s is held: its source key is not read as it is written", branch) + } + + b.publish(t, "mesh.mod.mesh-delivery-indexer.event.take-in.taken", map[string]any{"branch": branch, "source": 1, "revision": 1, + "call": call, "entries": []any{map[string]any{"module": "app", "commit": merge, "fingerprint": "f", "repository": "novox/app"}}}) + rt.until("the take-in's mover in the record", eventWithin, func() bool { + b.mu.Lock() + defer b.mu.Unlock() + return strings.Contains(string(b.ledger.states[instance]), `"module":"app"`) + }) + if e, _ := b.kvs["source"].Get(ctx, sourceKey); e.Revision() != 1 { + t.Fatalf("the merge call heard twice wrote source.%s %d times", escaped, e.Revision()) + } + + // The seed (§768): every watched branch's first source revision at its head; its head-moved then holds nothing. + seeded := strings.Repeat("5", 40) + b.mu.Lock() + b.registrations = []meshcatalog.Registration{{Module: "seeded", Origin: "novox/seeded", Branches: []string{"main"}}} + b.heads = map[string]string{"novox/seeded@main": seeded} + b.mu.Unlock() + if out := rt.call(t, "gate_source_seed", map[string]any{"why": "the clean state", "_caller": "lead@shanks"}); !strings.Contains(out, `\"branches\":1`) { + t.Fatalf("gate_source_seed answered %s", out) + } + headMoved(t, b, "novox/seeded", "main", seeded) + headMoved(t, b, "novox/push", "main", seeded) + rt.until("the barrier head held", eventWithin, func() bool { return kvHas(ctx, b, "heads", heldKey("novox/push", "main")) }) + if kvHas(ctx, b, "heads", heldKey("novox/seeded", "main")) { + t.Fatal("the seeded head is held after the seed") + } + rt.noControllerAsk() +} + +// headMoved publishes the forge bridge's head-moved of a repository's branch. +func headMoved(t *testing.T, b *testBus, repository, branch, head string) { + t.Helper() + name, err := forgemsg.HeadMovedEvent(repository, branch) + if err != nil { + t.Fatal(err) + } + b.publish(t, "mesh.mod.mesh-delivery-forge-bridge.event."+name, forgemsg.HeadMoved{V: 1, Repository: repository, Branch: branch, Head: head}) +} + +func kvHas(ctx context.Context, b *testBus, state, key string) bool { + _, err := b.kvs[state].Get(ctx, key) + return err == nil +} + +// findingHeld is whether the gate's state findings holds a finding of a kind and id. +func findingHeld(ctx context.Context, b *testBus, kind, id string) bool { + names, err := b.kvs["findings"].Keys(ctx) + if err != nil && err != jetstream.ErrNoKeysFound { + return false + } + for _, k := range names { + if strings.Contains(k, kind) { + e, err := b.kvs["findings"].Get(ctx, k) + if err == nil && strings.Contains(string(e.Value()), id) { + return true + } + } + } + return false +} diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go index 265960ee5..0f7098c5f 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/merges.go @@ -79,8 +79,9 @@ func (kvSource) Set(branch string, update func(prev *Source) (Source, bool)) (ui return 0, fmt.Errorf("source.%s was written by another writer first %d times: %w", branch, changeAttempts, stdio.ErrChanged) } +// Read reads a branch's key with the escaping Set writes it with (design 47 H H-A-87 (2)). func (kvSource) Read(branch string) (*Source, uint64, error) { - e, err := stdio.State("source").Get(mdc.SourceKey(branch)) + e, err := stdio.State("source").Get(mdc.SourceKey(keys.EscapeName(branch))) if err != nil || e == nil { return nil, 0, err } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go deleted file mode 100644 index 0edbfcc53..000000000 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/takenin_bus_test.go +++ /dev/null @@ -1,54 +0,0 @@ -//go:build integration - -package main - -import ( - "context" - "strings" - "testing" - "time" -) - -// The gate whole, over a real bus (design 47 H §737, §738; acceptance test 110's gate half): a merge-called the forge module -// says names one source revision, opens its delivery record and says source-moved; the indexer's take-in.taken of that -// revision writes its movers into the record; the gate asks the controller nothing, and no state it no longer declares is -// needed (the template states are buckets under their local names). -const eventWithin = 20 * time.Second - -func TestAGateWholeTakesAMergeCallIntoASourceRevisionAndARecordOverARealBus(t *testing.T) { - b := newTestBus(t) - ctx := context.Background() - rt := b.start(t) - evctx, cancel := context.WithCancel(ctx) - defer cancel() - rt.deliverEvents(evctx) - rt.untilListening() - merge := strings.Repeat("c", 40) - call := "0123456789abcdef" - instance, _ := RecordInstance(recordMilestone("main", 1)) - member := map[string]any{"repository": "novox/app", "number": 5, "branch": "main", "head": strings.Repeat("d", 40), "commit": merge} - for range 2 { // heard twice: one revision - b.publish(t, "mesh.seat.git.event.merge-called."+call, map[string]any{"call": call, "by": "lead@shanks", "account": "acct-s", - "at": "2026-11-20T03:00:00Z", "members": []any{member}}) - } - rt.until("the record of the merge call and its source revision", eventWithin, func() bool { - e, err := b.kvs["source"].Get(ctx, "main") - if err != nil || !strings.Contains(string(e.Value()), `"call":"`+call+`"`) || !strings.Contains(string(e.Value()), `"source":1`) { - return false - } - b.mu.Lock() - defer b.mu.Unlock() - return b.ledger != nil && b.ledger.states[instance] != nil - }) - b.publish(t, "mesh.mod.mesh-delivery-indexer.event.take-in.taken", map[string]any{"branch": "main", "source": 1, "revision": 1, - "call": call, "entries": []any{map[string]any{"module": "app", "commit": merge, "fingerprint": "f", "repository": "novox/app"}}}) - rt.until("the take-in's mover in the record", eventWithin, func() bool { - b.mu.Lock() - defer b.mu.Unlock() - return strings.Contains(string(b.ledger.states[instance]), `"module":"app"`) - }) - if e, _ := b.kvs["source"].Get(ctx, "main"); e.Revision() != 1 { - t.Fatalf("the merge call heard twice wrote source.main %d times", e.Revision()) - } - rt.noControllerAsk() -}