The delivery repository's contracts and SDK live under modules/ as mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths renamed to match the folders (the TypeScript names already did), and every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools and mesh-sdk follows in the same group: a design as it should be, one path per module, checked by the group's check building every importer. Builds: design 47 G §427, §88 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
302 lines
14 KiB
Go
302 lines
14 KiB
Go
package index
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
|
||
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"
|
||
)
|
||
|
||
// regRig is a Registry on memory states with a fake router: asks it was given, the cancelled ones, the history and the events.
|
||
type regRig struct {
|
||
r *Registry
|
||
asks map[string]oc.Ask
|
||
cancelled []string
|
||
history []meshcatalog.Registration
|
||
events []meshcatalog.RegistrationChangedEvent
|
||
now time.Time
|
||
unprotect map[string]bool
|
||
whereErr error
|
||
chosen map[string]string // ask id -> option the router's record holds
|
||
missing map[string]bool
|
||
placed map[string][]mma.ModulePlacement // module -> its placements, as the seat mesh-module-assigner's placement answers
|
||
}
|
||
|
||
func newRegRig() *regRig {
|
||
g := ®Rig{asks: map[string]oc.Ask{}, now: time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC), unprotect: map[string]bool{}, chosen: map[string]string{}, missing: map[string]bool{}}
|
||
g.r = &Registry{Registrations: MemoryStore(), Proposals: MemoryStore(),
|
||
History: func(_ context.Context, r meshcatalog.Registration) error {
|
||
g.history = append(g.history, r)
|
||
return nil
|
||
},
|
||
Say: func(_ context.Context, e meshcatalog.RegistrationChangedEvent) error {
|
||
g.events = append(g.events, e)
|
||
return nil
|
||
},
|
||
Branch: func(repo, branch string) (gitseat.BranchAnswer, error) {
|
||
if branch == "" {
|
||
branch = "main"
|
||
}
|
||
return gitseat.BranchAnswer{Branch: branch, Repository: repo, Protected: !g.unprotect[branch]}, nil
|
||
},
|
||
Placement: func(module string) ([]mma.ModulePlacement, error) { return g.placed[module], nil },
|
||
Ask: func(a oc.Ask) error { g.asks[a.ID] = a; return nil },
|
||
Cancel: func(id string) error { g.cancelled = append(g.cancelled, id); return nil },
|
||
Where: func(id string) (string, *oc.Warrant, error) {
|
||
if g.whereErr != nil {
|
||
return "", nil, g.whereErr
|
||
}
|
||
if g.missing[id] {
|
||
return "", nil, nil
|
||
}
|
||
if opt, ok := g.chosen[id]; ok {
|
||
w := g.warrant(id, opt)
|
||
return "chosen", &w, nil
|
||
}
|
||
return "open", nil, nil
|
||
},
|
||
Now: func() time.Time { return g.now }, Logf: func(string, ...any) {}}
|
||
return g
|
||
}
|
||
|
||
// warrant is the warrant of the operator's choice of option on an ask, as the router would say it.
|
||
func (g *regRig) warrant(id, option string) oc.Warrant {
|
||
a := g.asks[id]
|
||
o, _ := occ.OptionOf(a, option)
|
||
return oc.Warrant{Ask: id, Asker: Module, Outcome: occ.OutcomeChosen, Option: option, Level: o.Level, Label: o.Label,
|
||
AskDigest: occ.Digest(a), At: occ.FormatTime(g.now.Add(time.Minute)), By: &oc.Person{Who: occ.Operator, Kind: "telegram", Verified: "user id verified"}}
|
||
}
|
||
|
||
func (g *regRig) decide(t *testing.T, ask, option string) {
|
||
t.Helper()
|
||
g.chosen[ask] = option
|
||
if err := g.r.Heard(context.Background(), g.warrant(ask, option)); err != nil {
|
||
t.Fatalf("hearing the warrant of %s: %v", ask, err)
|
||
}
|
||
}
|
||
|
||
func reg(module string, steps meshcatalog.StepStatuses, branches ...string) json.RawMessage {
|
||
b, _ := json.Marshal(meshcatalog.RegisterInput{Registration: meshcatalog.Registration{Module: module, Origin: "novox/" + module, Path: "module.json", Branches: branches, Policy: meshcatalog.Policy{Steps: steps}}})
|
||
return b
|
||
}
|
||
|
||
func pol(module string, s meshcatalog.StepStatuses) json.RawMessage {
|
||
b, _ := json.Marshal(meshcatalog.PolicyInput{Module: module, Steps: s})
|
||
return b
|
||
}
|
||
|
||
func TestRegistrationsAreWrittenOnTheirWarrantOnly(t *testing.T) {
|
||
ctx := context.Background()
|
||
g := newRegRig()
|
||
// Refusals raise no ask: a code review that is required (§3), a branch named candidate or entry (§343), an unprotected branch (§5).
|
||
g.unprotect["beta"] = true
|
||
for _, c := range []struct {
|
||
name, why string
|
||
change json.RawMessage
|
||
}{
|
||
{"review", meshcatalog.RegistrationAskedRefusedReviewRequired, reg("a", meshcatalog.StepStatuses{Review: "required"}, "main")},
|
||
{"unprotected", meshcatalog.RegistrationAskedRefusedUnprotectedBranch, reg("a", meshcatalog.StepStatuses{}, "beta")},
|
||
{"candidate", meshcatalog.RegistrationAskedRefusedReservedBranchName, reg("a", meshcatalog.StepStatuses{}, "candidate")},
|
||
{"entry", meshcatalog.RegistrationAskedRefusedReservedBranchName, reg("a", meshcatalog.StepStatuses{}, "entry")},
|
||
} {
|
||
got, err := g.r.Propose(ctx, "register", "session@node", c.change)
|
||
if err != nil || got.Refused == "" || got.Ask != "" || got.Words == "" || got.Refused != c.why {
|
||
t.Fatalf("%s: answer %+v, %v: want a refusal for %q naming why and no ask (design 47 G §704)", c.name, got, err, c.why)
|
||
}
|
||
}
|
||
if len(g.asks) != 0 {
|
||
t.Fatalf("a refusal raised %d asks", len(g.asks))
|
||
}
|
||
// A registration with no branch takes the default branch through the seat git (§342) and asks, naming each value and carrying the digest.
|
||
got, err := g.r.Propose(ctx, "register", "session@node", reg("a", meshcatalog.StepStatuses{Unit: "required", Lint: "bypass"}))
|
||
if err != nil || got.Ask == "" || len(got.Lines) < 3 {
|
||
t.Fatalf("register: %+v, %v", got, err)
|
||
}
|
||
ask := g.asks[got.Ask]
|
||
if !strings.Contains(ask.Explanation, "watching main") || !strings.Contains(ask.Explanation, "sha256:") || ask.About == "" {
|
||
t.Fatalf("the ask does not name the values and the digest: %+v", ask)
|
||
}
|
||
if all, _ := g.r.RegistrationsAnswer(ctx, meshcatalog.RegistrationsInput{}); len(all.Registrations) != 0 {
|
||
t.Fatalf("a registration was written before its warrant: %+v", all)
|
||
}
|
||
g.decide(t, got.Ask, "approve")
|
||
one, _ := g.r.RegistrationsAnswer(ctx, meshcatalog.RegistrationsInput{Module: "a"})
|
||
if len(one.Registrations) != 1 || one.Registrations[0].Version != 1 || one.Registrations[0].Warrant != got.Ask || one.Registrations[0].By != "session@node" {
|
||
t.Fatalf("after the warrant: %+v", one)
|
||
}
|
||
if len(g.history) != 1 || len(g.events) != 1 || g.events[0].Version != 1 || g.events[0].Warrant != got.Ask {
|
||
t.Fatalf("history %d, events %+v", len(g.history), g.events)
|
||
}
|
||
// A policy from required to bypass asks, showing that line; a decline writes nothing and ends the key declined.
|
||
p, err := g.r.Propose(ctx, "policy", "s@n", pol("a", meshcatalog.StepStatuses{Unit: "bypass"}))
|
||
if err != nil || len(p.Lines) != 1 || p.Lines[0] != "a unit tests: required -> bypass" {
|
||
t.Fatalf("policy: %+v, %v", p, err)
|
||
}
|
||
g.decide(t, p.Ask, "decline")
|
||
if now, _, _ := g.r.Registration(ctx, "a"); now.Version != 1 {
|
||
t.Fatalf("a declined change was written: %+v", now)
|
||
}
|
||
rows, _ := ListProposals(ctx, g.r.Proposals)
|
||
if len(rows) != 2 || rows[1].State != ProposalDeclined && rows[0].State != ProposalDeclined {
|
||
t.Fatalf("proposals: %+v", rows)
|
||
}
|
||
// Two changes of one key leave one open ask; the first ask's late warrant writes nothing.
|
||
g.now = g.now.Add(time.Hour)
|
||
first, _ := g.r.Propose(ctx, "policy", "s@n", pol("a", meshcatalog.StepStatuses{Unit: "bypass"}))
|
||
g.now = g.now.Add(time.Hour)
|
||
second, _ := g.r.Propose(ctx, "policy", "s@n", pol("a", meshcatalog.StepStatuses{Unit: "optional"}))
|
||
if first.Ask == second.Ask {
|
||
t.Fatal("a newer change of the key kept the older ask")
|
||
}
|
||
g.chosen[first.Ask] = "approve"
|
||
_ = g.r.Heard(ctx, g.warrant(first.Ask, "approve"))
|
||
if now, _, _ := g.r.Registration(ctx, "a"); now.Version != 1 {
|
||
t.Fatalf("the replaced ask's warrant wrote: %+v", now)
|
||
}
|
||
// Where the router's record cannot be read the warrant is left unapplied, said as an error.
|
||
g.whereErr = errors.New("no runtime")
|
||
if err := g.r.Heard(ctx, g.warrant(second.Ask, "approve")); err == nil {
|
||
t.Fatal("a warrant was applied with the router's record unread")
|
||
}
|
||
g.whereErr = nil
|
||
g.decide(t, second.Ask, "approve")
|
||
if now, _, _ := g.r.Registration(ctx, "a"); now.Version != 2 || now.Policy.Steps.Unit != "optional" {
|
||
t.Fatalf("the second change: %+v", now)
|
||
}
|
||
// watch adds a branch only when protected; unwatch removes it; each on its warrant.
|
||
w, _ := g.r.Propose(ctx, "watch", "s@n", mustJSON(meshcatalog.WatchInput{Module: "a", Branch: "beta"}))
|
||
if w.Refused != meshcatalog.RegistrationAskedRefusedUnprotectedBranch {
|
||
t.Fatalf("watch of an unprotected branch: %+v", w)
|
||
}
|
||
delete(g.unprotect, "beta")
|
||
w, _ = g.r.Propose(ctx, "watch", "s@n", mustJSON(meshcatalog.WatchInput{Module: "a", Branch: "beta"}))
|
||
g.decide(t, w.Ask, "approve")
|
||
// A branch a placement names is not unwatched: refused as placed, naming the placement, and no ask is raised (§704, A-62).
|
||
g.placed = map[string][]mma.ModulePlacement{"a": {{Branch: "beta", Nodes: []string{"n1", "n2"}}}}
|
||
if p, err := g.r.Propose(ctx, "unwatch", "s@n", mustJSON(meshcatalog.WatchInput{Module: "a", Branch: "beta"})); err != nil || p.Refused != meshcatalog.RegistrationAskedRefusedPlaced ||
|
||
p.Ask != "" || !strings.Contains(p.Words, "beta") || !strings.Contains(p.Words, "n1, n2") {
|
||
t.Fatalf("unwatch of a placed branch: %+v, %v; want refused placed naming the placement", p, err)
|
||
}
|
||
g.placed = nil
|
||
u, _ := g.r.Propose(ctx, "unwatch", "s@n", mustJSON(meshcatalog.WatchInput{Module: "a", Branch: "beta"}))
|
||
g.decide(t, u.Ask, "approve")
|
||
if now, _, _ := g.r.Registration(ctx, "a"); now.Version != 4 || len(now.Branches) != 1 {
|
||
t.Fatalf("watch and unwatch: %+v", now)
|
||
}
|
||
if un, _ := g.r.Propose(ctx, "policy", "s@n", pol("zz", meshcatalog.StepStatuses{Unit: "bypass"})); un.Refused != meshcatalog.RegistrationAskedRefusedUnknownModule {
|
||
t.Fatalf("a policy of an unregistered module: %+v", un)
|
||
}
|
||
}
|
||
|
||
func mustJSON(v any) json.RawMessage { b, _ := json.Marshal(v); return b }
|
||
|
||
func TestProposalsSurviveARestartAndAreDropped(t *testing.T) {
|
||
ctx := context.Background()
|
||
g := newRegRig()
|
||
a, _ := g.r.Propose(ctx, "register", "s@n", reg("a", meshcatalog.StepStatuses{}, "main"))
|
||
b, _ := g.r.Propose(ctx, "register", "s@n", reg("b", meshcatalog.StepStatuses{}, "main"))
|
||
c, _ := g.r.Propose(ctx, "register", "s@n", reg("c", meshcatalog.StepStatuses{}, "main"))
|
||
// While the indexer was down the operator approved a and the router lost c; b is still open.
|
||
g.chosen[a.Ask] = "approve"
|
||
g.missing[c.Ask] = true
|
||
if err := g.r.Restart(ctx); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
rows, _ := ListProposals(ctx, g.r.Proposals)
|
||
states := map[string]string{}
|
||
for _, row := range rows {
|
||
states[row.Module] = row.State
|
||
}
|
||
if states["a"] != ProposalApplied || states["b"] != ProposalOpen || states["c"] != ProposalLost {
|
||
t.Fatalf("after a restart: %v", states)
|
||
}
|
||
// indexer_proposal_drop cancels the open ask and ends the key dropped; it asks why.
|
||
if _, err := g.r.Drop(ctx, ProposalKey("register", "b", ""), "", "s@n"); err == nil {
|
||
t.Fatal("a drop with no why was taken")
|
||
}
|
||
if _, err := g.r.Drop(ctx, ProposalKey("register", "b", ""), "not wanted", "s@n"); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if len(g.cancelled) != 1 || g.cancelled[0] != b.Ask {
|
||
t.Fatalf("cancelled %v", g.cancelled)
|
||
}
|
||
rows, _ = ListProposals(ctx, g.r.Proposals)
|
||
for _, row := range rows {
|
||
if row.Module == "b" && row.State != ProposalDropped {
|
||
t.Fatalf("a dropped key: %+v", row)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestSeedIsOneAskAndWaitsForEveryBuildSeat(t *testing.T) {
|
||
ctx := context.Background()
|
||
g := newRegRig()
|
||
g.unprotect["main"] = false
|
||
mods := []SeedModule{{Module: "a", Origin: "novox/a", Path: "module.json", Artifacts: []SeedArtifact{{Name: "program", Seat: "node-build-go", Answered: false}}}}
|
||
if _, err := g.r.ProposeSeed(ctx, mods, false, "indexer"); err == nil || !strings.Contains(err.Error(), "node-build-go") {
|
||
t.Fatalf("an unanswered build seat did not hold the seed: %v", err)
|
||
}
|
||
mods[0].Artifacts[0].Answered, mods[0].Artifacts[0].Steps = true, []string{"unit"}
|
||
waits := ""
|
||
g.r.SeedWaits = func(ask string, waiting bool) {
|
||
if waiting {
|
||
waits = ask
|
||
} else {
|
||
waits = ""
|
||
}
|
||
}
|
||
ask, err := g.r.ProposeSeed(ctx, mods, false, "indexer")
|
||
if err != nil || ask == "" {
|
||
t.Fatalf("seed: %q, %v", ask, err)
|
||
}
|
||
// A step the seat answers none for is bypass; the statuses the check judged are the rest.
|
||
g.decide(t, ask, "decline")
|
||
if waits != ask {
|
||
t.Fatalf("a declined seed does not keep seed-waits: %q", waits)
|
||
}
|
||
if _, err := g.r.ProposeSeed(ctx, mods, false, "indexer"); err == nil {
|
||
t.Fatal("the seed was asked again with no indexer_seed_again")
|
||
}
|
||
g.now = g.now.Add(time.Hour)
|
||
ask, err = g.r.ProposeSeed(ctx, mods, true, "s@n")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
g.decide(t, ask, "approve")
|
||
got, _, _ := g.r.Registration(ctx, "a")
|
||
if got.Version != 1 || got.Policy.Steps.Review != "optional" || got.Policy.Steps.Approval != "bypass" || got.Policy.Artifacts["program"].Lint != "bypass" || got.Policy.Artifacts["program"].Integration != "bypass" || got.Policy.Artifacts["program"].Unit != "" {
|
||
t.Fatalf("the seeded registration: %+v", got)
|
||
}
|
||
if waits != "" {
|
||
t.Fatalf("seed-waits is not cleared by the warrant: %q", waits)
|
||
}
|
||
// An unprotected default branch is left out and named.
|
||
g2 := newRegRig()
|
||
g2.unprotect["main"] = true
|
||
seed, err := g2.r.ComposeSeed(mods)
|
||
if err != nil || len(seed.Registrations) != 0 || len(seed.Left) != 1 || !strings.Contains(seed.Left[0], "not protected") {
|
||
t.Fatalf("seed with an unprotected default branch: %+v, %v", seed, err)
|
||
}
|
||
}
|
||
|
||
func TestCanonicalIsRFC8785(t *testing.T) {
|
||
got, err := Canonical(map[string]any{"b": []any{1, "x
"}, "a": map[string]any{"z": true, "y": nil}, "é": "<&>"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if want := "{\"a\":{\"y\":null,\"z\":true},\"b\":[1,\"x
\"],\"é\":\"<&>\"}"; string(got) != want {
|
||
t.Fatalf("canonical = %s, want %s", got, want)
|
||
}
|
||
if _, err := Canonical(map[string]any{"n": 1.5}); err == nil {
|
||
t.Fatal("a fraction has no canonical form here")
|
||
}
|
||
}
|