Files
mesh-delivery/modules/mesh-delivery-view/cmd/mesh-delivery-view-tools/owners_test.go
T
jschoubben 0de534c692 Names N3 (view): the status view names its own buckets from its credential's module
Its states answers, reach and waits (viewstate WaitsBucket, ReachBucket, ViewAnswersBucket), its
first views and its marks were named from the constant ViewModule; they are now
`<cred.Module>_<state>` (viewstate.BucketsOf), and the model is made for its module.

Builds: design 47 G A-296

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 19:14:55 +01:00

280 lines
13 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"mesh-delivery-view/internal/view"
"sort"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/graph"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-view/viewstate"
"git.novox.be/novox/mesh-sdk/go/views"
)
// source is a fake owner state as a views.Source: what the owner's read tool answers from.
type source map[string]string
func (s source) Keys() ([]string, error) {
var out []string
for k := range s {
out = append(out, k)
}
sort.Strings(out)
return out, nil
}
func (s source) Get(key string) (*views.Entry, bool, error) {
v, ok := s[key]
if !ok {
return nil, false, nil
}
return &views.Entry{Value: json.RawMessage(v), Revision: uint64(len(key))}, true, nil
}
// kept is a fake owner state that keeps its keys' history, as views.KeptSource does: what the owner's read tool answers a
// `history` from.
type kept struct {
source
history map[string][]views.Revision
}
func (k kept) History(key string) ([]views.Revision, error) { return k.history[key], nil }
// ownersAnswerTheToolsAsBefore is DLV-V1: every view tool over the indexer's and the assigner's states answers through
// the owners' read tools (the real owner side, views.Read, over a fake state) what it answered reading the states.
func ownersAnswerTheToolsAsBefore(t *testing.T) {
cand := `{"v":1,"group":"feat/x","base":3,"heads":"h","pulls":[{"modules":["zsh"],"fps":{"zsh":{"fp2":"` + fp2B + `","manifest":"m"}}}]}`
pkgKey, err := view.PackagesKey("git.novox.be/novox/mesh-sdk/go")
if err != nil {
t.Fatal(err)
}
states := map[string]source{
view.IndexerModule + ".candidates": {"feat/x": cand, "fix": `{"v":1,"group":"fix","base":3,"heads":"h","pulls":[{"modules":["netcheck"]}]}`},
view.AssignerModule + ".wants": {"want.ace.zsh": `{"fp":"` + fp2A + `"}`, "want.novox.zsh": `{"fp":"` + fp2B + `"}`,
"held." + keyB: `{"node":"ace","words":"failed"}`},
view.AssignerModule + ".differences": {"ace.zsh": `{"unexplained":true}`, "novox.zsh": `{"rule":3}`},
view.IndexerModule + "." + view.IndexerPredefinedState: {view.PredefinedListKey: `{"v":1,"keys":["dir:state"]}`},
view.IndexerModule + "." + view.IndexerConfigCandidatesState: {"novox.r.7": `{"v":1,"pull":"novox/r#7"}`},
view.IndexerModule + "." + view.PackagesState: {pkgKey: `{"v":1,"module":"git.novox.be/novox/mesh-sdk/go","commit":"c1"}`},
view.IndexerModule + "." + graph.State: {},
}
// The kept history of the indexer's modules and heard and the assigner's wants (each declared with more than one
// revision in its owner's manifest), which indexer_module_history, indexer_heard and view_tiers' times read.
at := time.Date(2026, 11, 4, 9, 0, 0, 0, time.UTC)
const heard1 = `{"v":1,"subject":"s","sequence":41,"repository":"novox/mesh-catalog","commit":"9cdac09a"}`
states[view.IndexerModule+".heard"] = source{"forge": heard1}
histories := map[string]map[string][]views.Revision{
view.IndexerModule + ".heard": {"forge": {{Revision: 3, At: at, Op: "put", Value: json.RawMessage(heard1)}}},
view.AssignerModule + ".wants": {"want.ace.zsh": {{Revision: 12, At: at, Op: "put", Value: json.RawMessage(`{"fp":"` + fp2A + `"}`)}}},
}
// The direct reading, as the tools read the states before (the reads the manifest no longer has).
st := func(name string) view.StateRead { return fake(states[name]).reader().State }
direct := view.Reader{CandidatesState: st(view.IndexerModule + ".candidates"),
WantsState: st(view.AssignerModule + ".wants"), DifferencesState: st(view.AssignerModule + ".differences"),
PredefinedState: st(view.IndexerModule + "." + view.IndexerPredefinedState),
ConfigCandidatesState: st(view.IndexerModule + "." + view.IndexerConfigCandidatesState),
PackagesState: st(view.IndexerModule + "." + view.PackagesState),
GraphState: st(view.IndexerModule + "." + graph.State)}
// The owners' read tools, answered by the owner's own Read over the same state; any other address is refused.
tools := map[string]string{}
for name := range states {
owner, state, _ := strings.Cut(name, ".")
short := map[string]string{view.IndexerModule: "indexer_read_", view.AssignerModule: "assigner_read_"}[owner]
tools[owner+"."+short+strings.ReplaceAll(state, "-", "_")] = name
}
asked := map[string]int{}
ask := func(key string, body any) (json.RawMessage, error) {
if a, ok := catalogSeats(key, body); ok {
asked[key]++
return a()
}
name, ok := tools[key]
if !ok {
return nil, fmt.Errorf("no tool %s", key)
}
asked[key]++
args, _ := body.(map[string]any)
k, _ := args["key"].(string)
all, _ := args["values"].(bool)
after, _ := args["after"].(string)
owner, state, _ := strings.Cut(name, ".")
if h, _ := args["history"].(bool); h {
// The join (J4): the owner's read tool answers a key's kept history.
a, err := views.ReadHistory(owner, state, kept{states[name], histories[name]}, k)
if err != nil {
return nil, err
}
return json.Marshal(a)
}
a, err := views.Read(owner, state, states[name], nil, k, all, after, time.Unix(0, 0))
if err != nil {
return nil, err
}
return json.Marshal(a)
}
owned := ownersRead(ask, startedBranches(t, context.Background(), ask))
for _, row := range []struct {
tool string
args map[string]any
}{
{view.CandidateReadTool, map[string]any{"group": "feat/x"}},
{view.CandidatesReadTool, map[string]any{}},
{view.CandidatesReadTool, map[string]any{"module": "zsh"}},
{view.WantsReadTool, map[string]any{}},
{view.WantsReadTool, map[string]any{"node": "ace"}},
{view.WantReadTool, map[string]any{"node": "ace", "module": "zsh"}},
{view.DifferencesReadTool, map[string]any{"node": "novox"}},
{view.PredefinedReadTool, map[string]any{}},
{view.ConfigCandidatesReadTool, map[string]any{}},
{view.ConfigCandidatesReadTool, map[string]any{"key": "novox.r.7"}},
{view.PackagesReadTool, map[string]any{"module": "git.novox.be/novox/mesh-sdk/go"}},
{view.PackagesReadTool, nil},
} {
want, werr := run(t, direct, row.tool, row.args)
got, gerr := run(t, owned, row.tool, row.args)
wj, _ := json.Marshal(want)
gj, _ := json.Marshal(got)
if string(wj) != string(gj) || fmt.Sprint(werr) != fmt.Sprint(gerr) {
t.Errorf("%s %v through the owner's read tool = %s, %v; reading the state it was %s, %v", row.tool, row.args, gj, gerr, wj, werr)
}
}
// The modules are the catalog half's, asked of the seat mesh-catalog's verb revision (W16.1): a module is its entry in
// the form of the value the tool always answered, the modules without a fingerprint say why, and the module's history
// is not a thing the catalog half keeps.
a, err := run(t, owned, view.ModuleReadTool, map[string]any{"module": "a"})
if raw, _ := json.Marshal(a); err != nil || !strings.Contains(string(raw), `"fp2":"`+fp2A+`"`) || !strings.Contains(string(raw), `"commit":"c1"`) || !strings.Contains(string(raw), `"src":7`) || !strings.Contains(string(raw), `"manifest":"`+manifestDigestA+`"`) {
t.Errorf("indexer_module a = %s, %v", raw, err)
}
if _, err := run(t, owned, view.ModuleReadTool, map[string]any{"module": "gone"}); err == nil {
t.Errorf("indexer_module gone answered a module the catalog half does not hold")
}
all, err := run(t, owned, view.ModulesReadTool, map[string]any{"none2": true})
if raw, _ := json.Marshal(all); err != nil || !strings.Contains(string(raw), `"b"`) || strings.Contains(string(raw), `"a"`) || !strings.Contains(string(raw), "builder's fingerprint not yet known") {
t.Errorf("indexer_modules none2 = %s, %v", raw, err)
}
if _, err := run(t, owned, view.ModuleHistoryTool, map[string]any{"module": "a"}); err == nil {
t.Errorf("indexer_module_history answered a history the catalog half does not keep")
}
out, err := run(t, owned, view.HeardTool, map[string]any{})
if rows, _ := out.(view.HeardAnswer); err != nil || len(rows.Heard) != 1 || rows.Heard[0].Revision != 3 || string(rows.Heard[0].Heard) != heard1 {
t.Errorf("indexer_heard through the indexer's read tool = %+v, %v", out, err)
}
// view_tiers' Since of a want is its first kept revision's time, read through the assigner's read tool.
if revs, err := owned.WantsState.History("want.ace.zsh"); err != nil || len(revs) != 1 || !revs[0].At.Equal(at) || revs[0].Revision != 12 {
t.Errorf("the want's history through the assigner's read tool = %+v, %v", revs, err)
}
// With no view of the catalog half the modules are not read: the tools say so, never that no module is held.
away := func(key string, body any) (json.RawMessage, error) { return nil, fmt.Errorf("the holder is away") }
gone, cancel := context.WithCancel(context.Background())
cancel()
none := ownersRead(away, startedBranches(t, gone, away))
if out, err := run(t, none, view.ModulesReadTool, map[string]any{}); err == nil || !strings.Contains(err.Error(), "no watched branch") {
t.Errorf("indexer_modules with no view = %v, %v, want an error naming it", out, err)
}
// Each owner state the tools read was asked of its owner's read tool.
for tool, name := range tools {
if name != view.IndexerModule+"."+graph.State && asked[tool] == 0 {
t.Errorf("%s was never asked", tool)
}
}
for _, verb := range []string{meshcatalogclient.RevisionVerb, meshcatalogclient.RegistrationsVerb, meshcatalogclient.SourceVerb} {
if asked[verb] == 0 {
t.Errorf("%s was never asked", verb)
}
}
}
// catalogBranch is the one watched branch of the catalog half in these tests: the registrations name it, the gate's source
// keeps it, and the seat's revision answers it alone, so a reader that names a branch of its own reads nothing.
const catalogBranch = "release"
// catalogSeats answers the seats the branch follower asks (design 47 G §614): the registrations, each module on
// catalogBranch, the gate's source, and the newest trunk revision of catalogBranch; ok is false for any other key.
func catalogSeats(key string, body any) (func() (json.RawMessage, error), bool) {
switch key {
case meshcatalogclient.RegistrationsVerb:
return func() (json.RawMessage, error) {
return json.Marshal(meshcatalog.RegistrationsAnswer{Registrations: []meshcatalog.Registration{
{Module: "a", Version: 1, Branches: []string{catalogBranch}}, {Module: "b", Version: 1, Branches: []string{catalogBranch}}}})
}, true
case meshcatalogclient.SourceVerb:
return func() (json.RawMessage, error) { return json.Marshal(map[string][]string{"branches": {catalogBranch}}) }, true
case meshcatalogclient.RevisionVerb:
return func() (json.RawMessage, error) {
if in, _ := body.(meshcatalog.RevisionInput); in.Branch != catalogBranch {
return nil, fmt.Errorf("no watched branch %q", in.Branch)
}
return json.Marshal(catalogAnswer)
}, true
}
return nil, false
}
// startWiring is the bundle's reader for a test: each view takes its first view as it is adopted, within ctx.
type startWiring struct {
t *testing.T
ctx context.Context
}
func (w startWiring) Adopt(f *views.Follower, _, _ string, _ func(context.Context, []byte) error) error {
if err := f.Start(w.ctx); err != nil && w.ctx.Err() == nil {
w.t.Errorf("the first view of %s.%s: %v", f.Owner, f.State, err)
}
return nil
}
func (startWiring) Drop(string, string) error { return nil }
// startedBranches is the branch follower of meshcatalogclient asking ask, its views started at once.
func startedBranches(t *testing.T, ctx context.Context, ask func(string, any) (json.RawMessage, error)) *meshcatalogclient.Branches {
t.Helper()
b, err := meshcatalogclient.NewBranches(startWiring{t, ctx}, ask)
if err != nil {
t.Fatal(err)
}
return b
}
// catalogAnswer is the seat mesh-catalog's newest trunk revision: a with a fingerprint and the source revision it moved at,
// b with none and the reason.
var catalogAnswer = meshcatalog.RevisionAnswer{
Revision: &meshcatalog.CatalogRevision{Revision: 4, Kind: meshcatalogclient.KindTrunk, Source: 7},
Entries: []meshcatalog.CatalogEntry{
{Module: "a", Repository: "novox/r", Commit: "c1", Path: ".", Manifest: map[string]any{"module": "a"}, ManifestDigest: "sha256:" + manifestDigestA, Fp: fp2A, Inputs: &meshcatalog.EntryInputs{Src: 7}},
{Module: "b", Repository: "novox/r", Commit: "c2", Path: "modules/b", Manifest: map[string]any{"module": "b"}, None: "builder's fingerprint not yet known"},
},
}
// manifestDigestA is the digest the indexer read of a's manifest bytes, which the module's value carries bare (design 47 G
// §506, §578), never one computed again from the manifest whole.
const manifestDigestA = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
// view_read_answers answers the view's own answers as every owner's read tool answers its state (design 47 G A-178): one
// key's value, and every key when none is named.
func TestTheViewReadsItsAnswersAsAnOwner(t *testing.T) {
answers := source{"hold.plex.ab.view": `{"asking":"a1","option":"release"}`}
tool := AnswersReadTool(func(name string) views.Source {
if name == viewstate.ViewAnswersState {
return answers
}
return source{}
}, time.Now)
if tool.Name != "view_read_answers" {
t.Fatalf("the tool is %s", tool.Name)
}
for args, want := range map[string]map[string]any{"release": {"key": "hold.plex.ab.view"}, "hold.plex.ab.view": {}} {
out, err := tool.Run(want)
raw, _ := json.Marshal(out)
if err != nil || !strings.Contains(string(raw), args) {
t.Errorf("%v: %s, %v", args, raw, err)
}
}
}