package main import ( "encoding/json" "errors" "fmt" "reflect" "strings" "testing" "time" "mesh-delivery-view/internal/view" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-indexer/takein" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-view/viewstate" ) // encoded is v as its contract's encoder writes it, failing the test when it refuses. func encoded(t *testing.T, v []byte, err error) string { t.Helper() if err != nil { t.Fatal(err) } return string(v) } // short is a fingerprint as the assigner's words shorten it. func short(fp string) string { return fp[:len("src2:")+12] } // tiersToolSaysEachModulesWait: view_tiers reads a source revision's tiers from the assigner's difference of the kept revision // carrying it against the one before (design 47 G §512, A-242) and names each module's waits as the parts said them in // wait-held (the view's state waits), in the sayer's own words, beside the indexer's pending revisions. func tiersToolSaysEachModulesWait(t *testing.T) { fp := func(c string) string { return "src2:" + strings.Repeat(c, 64) } since := time.Date(2026, 11, 3, 16, 40, 0, 0, time.UTC) r := fake{}.reader() // db and lib first, web after db by its provisioning edge; the view orders nothing. ref := func(m string) meshwants.NodeEntryRef { return meshwants.NodeEntryRef{Node: "ace", Module: m, Digest: "sha256:" + m} } kept := map[int64]meshwants.AssignmentRevision{ 3: {Revision: 3, Catalogs: []meshwants.CatalogOf{{Branch: "release", Revision: 2, Source: 5}}}, 4: {Revision: 4, Catalogs: []meshwants.CatalogOf{{Branch: "release", Revision: 3, Source: 8}}}, 5: {Revision: 5, Catalogs: []meshwants.CatalogOf{{Branch: "release", Revision: 4, Source: 9}}}, 6: {Revision: 6, Catalogs: []meshwants.CatalogOf{{Branch: "release", Revision: 4, Source: 9}}}, } differences := map[[2]int64]meshwants.Difference{{4, 5}: {From: 4, To: 5, Changes: []meshwants.Change{{Node: "ace", Module: "db", To: fp("a")}, {Node: "ace", Module: "lib", To: fp("c")}, {Node: "ace", Module: "web", To: fp("b"), After: []meshwants.ChangeAfterItem{{Node: "ace", Module: "db", Kind: "provisioning"}}}}, Tiers: [][]meshwants.NodeEntryRef{{ref("db"), ref("lib")}, {ref("web")}}}} r.Assignment = func(n int64) (meshwants.AssignmentRevision, error) { if n == 0 { n = 6 } if rev, ok := kept[n]; ok { return rev, nil } return meshwants.AssignmentRevision{}, errors.New("not kept") } r.Difference = func(from, to int64) (meshwants.Difference, error) { if d, ok := differences[[2]int64{from, to}]; ok { return d, nil } return meshwants.Difference{}, fmt.Errorf("no difference of %d and %d", from, to) } // The waits the parts said, as the view's process keeps them: web's delivery wait by the assigner, lib's toolchain wait by // the builder; db holds none. Their words are the sayers' own. wait := func(w viewstate.Wait) string { raw, err := json.Marshal(w) if err != nil { t.Fatal(err) } return string(raw) } webWait := viewstate.Wait{Owner: "mesh-wants", Step: "delivery", Node: "ace", Module: "web", Part: "db", Machines: []string{"g14"}, Since: since, Bound: "60m", Words: "waiting for delivery of db to g14"} libWait := viewstate.Wait{Owner: "mesh-delivery-builder", Step: "building", Module: "lib", On: "toolchain:go@1.27", Since: since, Words: "the toolchain go 1.27 is not agreed yet"} r.WaitsState = fake{webWait.Key(): wait(webWait), libWait.Key(): wait(libWait)}.reader().State out, err := run(t, r, view.TiersTool, map[string]any{"revision": float64(9)}) got, _ := out.(view.Tiers) if err != nil || got.Revision != 9 || got.Modules != 3 || len(got.Tiers) != 2 || got.Graph != "" { t.Fatalf("view_tiers 9 = %+v, %v", out, err) } if len(got.Tiers[0]) != 2 || got.Tiers[0][0].Module != "db" || got.Tiers[0][0].FP != fp("a") || len(got.Tiers[0][0].Waits) != 0 || got.Tiers[0][1].Module != "lib" || !reflect.DeepEqual(got.Tiers[0][1].Waits, []view.TierWait{{Owner: "mesh-delivery-builder", Step: "building", On: "toolchain:go@1.27", Since: since, Words: "the toolchain go 1.27 is not agreed yet"}}) { t.Errorf("the first tier = %+v, want db with no wait and lib's toolchain wait in its sayer's words", got.Tiers[0]) } web := got.Tiers[1][0] if web.Module != "web" || !reflect.DeepEqual(web.After, []view.TierAfter{{Module: "db", Kind: "provisioning"}}) || !reflect.DeepEqual(web.Waits, []view.TierWait{{Owner: "mesh-wants", Step: "delivery", Node: "ace", Bound: "60m", Since: since, Words: "waiting for delivery of db to g14"}}) { t.Errorf("web = %+v, want after db, waiting for the delivery of db in the assigner's words", web) } // ADR 0633 §5 (1585/T24): a module the indexer holds back, read through indexer_pending, waits on the edge take-in from the // wait's since; a held module the revision does not move is named in the first tier with that wait alone, a group's wait // says its group, and a tool that cannot be asked is said and names no module. heldSince := time.Date(2026, 11, 4, 22, 0, 0, 0, time.UTC) r.Pending = func() (takein.PendingAnswer, error) { return takein.PendingAnswer{Waits: []takein.PendingWait{ {Key: "m.00000000000000000042", What: takein.TakeInRevision, WaitsFor: "src2:abc", Since: heldSince, Holds: []string{"web", "newmod"}}, {Key: "g.feat", What: takein.TakeInGroup, Group: "feat", Since: heldSince.Add(time.Hour), Holds: []string{"lib"}}}}, nil } out, err = run(t, r, view.TiersTool, map[string]any{"revision": float64(9)}) held, _ := out.(view.Tiers) if err != nil || len(held.Tiers) != 2 || held.Pending != "" { t.Fatalf("view_tiers with waits = %+v, %v", out, err) } web = held.Tiers[1][0] if last := web.Waits[len(web.Waits)-1]; last.Step != view.TakeInStep || !last.Since.Equal(heldSince) || !strings.Contains(last.Words, "src2:abc") { t.Errorf("web = %+v, want its last wait on the edge take-in since %s", web, heldSince) } if lib := held.Tiers[0][1]; lib.Module != "lib" || lib.Waits[len(lib.Waits)-1].Step != view.TakeInStep || !strings.Contains(lib.Waits[len(lib.Waits)-1].Words, "group feat") { t.Errorf("lib = %+v, want its last wait the group's", lib) } if nm := held.Tiers[0][len(held.Tiers[0])-1]; nm.Module != "newmod" || len(nm.Waits) != 1 || nm.Waits[0].Step != view.TakeInStep { t.Errorf("the first tier = %+v, want newmod, which the revision does not move, named with its take-in wait alone", held.Tiers[0]) } r.Pending = func() (takein.PendingAnswer, error) { return takein.PendingAnswer{}, errors.New("no answer") } if out, err = run(t, r, view.TiersTool, map[string]any{"revision": float64(9)}); err != nil || !strings.Contains(out.(view.Tiers).Pending, "no answer") || strings.Contains(fmt.Sprint(out.(view.Tiers).Tiers), view.TakeInStep) { t.Errorf("view_tiers with no answer of indexer_pending = %+v, %v", out, err) } r.Pending = nil // No revision named: per watched branch, the newest source revision the gate's verb source answers for it (design 47 G // A-173); a branch with no source revision yet is left out. asked := map[string]bool{} call := func(key string, body any) (json.RawMessage, error) { in, _ := body.(meshdelivery.SourceInput) asked[key+" "+in.Branch] = true rev := map[string]int64{"release": 9}[in.Branch] return json.Marshal(meshdelivery.SourceAnswer{Branch: in.Branch, Revision: rev}) } r.Sources = view.SourcesOf(call, func() []string { return []string{"release", "beta"} }) if out, err := run(t, r, view.TiersTool, map[string]any{}); err != nil || len(out.(view.BranchTiers).Branches) != 1 || out.(view.BranchTiers).Branches[0].Branch != "release" || out.(view.BranchTiers).Branches[0].Revision != 9 || out.(view.BranchTiers).Branches[0].Modules != 3 { t.Errorf("view_tiers of each branch's newest = %+v, %v", out, err) } if !asked["seat:mesh-delivery.source release"] || !asked["seat:mesh-delivery.source beta"] { t.Errorf("the gate's verb source was asked %v, want once per watched branch", asked) } // No revision kept before the one carrying the source: said, and no module named; past 500 modules the answer says it is cut // short. delete(kept, 4) delete(kept, 3) if out, err := run(t, r, view.TiersTool, map[string]any{"revision": float64(9)}); err != nil || len(out.(view.Tiers).Tiers) != 0 || !strings.Contains(out.(view.Tiers).Graph, "no assignment revision is kept before") { t.Errorf("view_tiers with no earlier revision = %+v, %v", out, err) } kept[4] = meshwants.AssignmentRevision{Revision: 4, Catalogs: []meshwants.CatalogOf{{Branch: "release", Source: 8}}} big := meshwants.Difference{From: 4, To: 5, Tiers: [][]meshwants.NodeEntryRef{{}}} for i := 0; i < view.TiersMost+2; i++ { m := fmt.Sprintf("m%03d", i) big.Changes = append(big.Changes, meshwants.Change{Node: "ace", Module: m, To: fp("a")}) big.Tiers[0] = append(big.Tiers[0], ref(m)) } differences[[2]int64{4, 5}] = big if out, err := run(t, r, view.TiersTool, map[string]any{"revision": "9"}); err != nil || out.(view.Tiers).Modules != view.TiersMost+2 || !strings.Contains(out.(view.Tiers).Cut, "the first 500") { t.Errorf("view_tiers past its bound = %+v, %v", out.(view.Tiers).Cut, err) } }