Files
mesh-delivery/modules/mesh-delivery-view/cmd/mesh-delivery-view-tools/owners_test.go
T
jschoubben 33c6fc4f7f CT15 (take-in bodies, none-yet): the seat mesh-catalog's bodies give take-in.taken and take-in.pending; revision answers none-yet in a field
The seat's bodies hold TakeInTaken (branch, source, entries with repository and pull) and TakeInPending (branch); revision's
answer gains none-yet and its revision is optional; meshcatalogclient reads the field, never the error's words. The holders'
and readers' uses of the answer follow (a pointer revision).

Builds: design 47 G §476, §493, §530, §454, §648

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 21:32:40 +01:00

196 lines
9.7 KiB
Go

package main
import (
"encoding/json"
"fmt"
"git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient"
"mesh-delivery-view/internal/view"
"sort"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/sdk/graph"
"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 key == meshcatalogclient.RevisionVerb {
asked[key]++
return json.Marshal(catalogAnswer)
}
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)
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`) {
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.
catalogWithin = 20 * time.Millisecond
none := ownersRead(func(key string, body any) (json.RawMessage, error) { return nil, fmt.Errorf("the holder is away") })
if out, err := run(t, none, view.ModulesReadTool, map[string]any{}); err == nil || !strings.Contains(err.Error(), "no first view") {
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)
}
}
if asked[meshcatalogclient.RevisionVerb] == 0 {
t.Errorf("%s was never asked", meshcatalogclient.RevisionVerb)
}
}
// 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"}, 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"},
},
}