Files
mesh-delivery/modules/mesh-delivery-checker/cmd/mesh-delivery-checker-tools/main_test.go
T
jschoubben 71a35a6376 MV1: move contracts and sdk under modules/, importers follow
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
2026-11-12 05:09:56 +01:00

351 lines
16 KiB
Go

package main
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"math/rand"
"os"
"regexp"
"slices"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-checker/checkermsg"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
"mesh-delivery-checker/internal/check"
)
func TestTheToolsServedAreTheFiveReadTools(t *testing.T) {
var names []string
for _, tl := range Tools(check.Reader{}) {
names = append(names, tl.Name)
if tl.Description == "" || tl.Run == nil {
t.Errorf("%s has no description or no run", tl.Name)
}
}
want := []string{check.VerdictsReadTool, check.PullReadTool, check.KeptReadTool, check.InputsReadTool, check.PreviewsReadTool, check.StatusReadTool}
if !slices.Equal(names, want) {
t.Errorf("tools = %v, want %v", names, want)
}
// The recovery tool beside them asks a lost check again; with nothing to write it refuses.
recovery := RecoveryTools(check.Reader{}, nil)
if len(recovery) != 1 || recovery[0].Name != check.AskAgainTool || recovery[0].Description == "" {
t.Errorf("recovery tools = %+v", recovery)
}
if _, err := recovery[0].Run(map[string]any{"group": "feat/x", "why": "lost"}); err == nil {
t.Errorf("%s answered with no state to write", check.AskAgainTool)
}
// Issue 1921: the composition requests kept by the checker are read by check and node; the tool refuses a node with no check
// and a state it cannot read, and lists a check's nodes without their bodies.
store := &memStore{vals: map[string]json.RawMessage{}}
keeper := check.RequestKeeper{Store: memPut{store}}
for _, k := range [][2]string{{"g.0123456789ab", "anchor"}, {"g.0123456789ab", "ace"}, {"h.0123456789ab", "ace"}} {
if err := keeper.Keep(context.Background(), k[0], k[1], []byte(`{"input":{"nodes":[]}}`)); err != nil {
t.Fatal(err)
}
}
// Issue 1966: a request whose compressed bytes pass the state's value limit is kept in pieces and read back whole; the
// pieces are no requests in the list.
rnd := make([]byte, 600<<10)
_, _ = rand.New(rand.NewSource(1)).Read(rnd)
big := []byte(`{"input":"` + base64.StdEncoding.EncodeToString(rnd) + `"}`)
if err := keeper.Keep(context.Background(), "big.0123456789ab", "anchor", big); err != nil {
t.Fatalf("a request of %d bytes: %v", len(big), err)
}
rt := RequestTools(check.StateRead{Keys: store.Keys, Get: store.Get})
if one, err := rt[0].Run(map[string]any{"check": "big.0123456789ab", "node": "anchor"}); err != nil || !strings.Contains(func() string { b, _ := json.Marshal(one); return string(b) }(), base64.StdEncoding.EncodeToString(rnd)[:64]) {
t.Errorf("the big request read back: %v", err)
}
// Listing reads no piece, and a request of another version is refused by name.
store.vals["v1.0123456789ab/anchor"] = json.RawMessage(`{"v":1,"check":"v1.0123456789ab","node":"anchor","gzip":"x"}`)
noPieces := check.StateRead{Keys: store.Keys, Get: func(k string) (json.RawMessage, error) {
if strings.HasPrefix(k, "pieces/") {
return nil, errors.New("a piece was read")
}
return store.Get(k)
}}
if _, err := RequestTools(noPieces)[0].Run(map[string]any{}); err != nil {
t.Errorf("listing: %v", err)
}
if _, err := rt[0].Run(map[string]any{"check": "v1.0123456789ab", "node": "anchor"}); err == nil || !strings.Contains(err.Error(), "version 1") {
t.Errorf("a version 1 request: %v", err)
}
delete(store.vals, "v1.0123456789ab/anchor")
if all, _ := rt[0].Run(map[string]any{}); strings.Count(func() string { b, _ := json.Marshal(all); return string(b) }(), `"node"`) != 4 {
t.Errorf("the list holds a piece as a request: %v", all)
}
if len(rt) != 1 || rt[0].Name != check.RequestsReadTool || !strings.Contains(rt[0].Description, "composition request a check sent to a node") {
t.Fatalf("request tools = %+v", rt)
}
one, err := rt[0].Run(map[string]any{"check": "g.0123456789ab", "node": "anchor"})
if b, _ := json.Marshal(one); err != nil || !strings.Contains(string(b), `"request":{"input":{"nodes":[]}}`) {
t.Errorf("one request = %s, %v", b, err)
}
list, _ := rt[0].Run(map[string]any{"check": "g.0123456789ab"})
if b, _ := json.Marshal(list); strings.Count(string(b), `"node"`) != 2 || strings.Contains(string(b), "h.0123") || strings.Contains(string(b), `"request"`) {
t.Errorf("list of a check = %s", b)
}
for _, args := range []map[string]any{{"node": "anchor"}, {"check": "g.0123456789ab", "node": "gone"}} {
if _, err := rt[0].Run(args); err == nil {
t.Errorf("%v answered", args)
}
}
if _, err := RequestTools(check.StateRead{})[0].Run(map[string]any{}); err == nil {
t.Errorf("answered with no state to read")
}
// Issue 1966: another module's state is asked of its read tool, never read from its bus state (ADR 0596). Every
// stdio.State this program names of another module is one its manifest declares under reads, which the runtime grants;
// the indexer's candidates and modules are asked of indexer_read_candidates and indexer_read_modules, which it invokes.
t.Run("1966: another module's state is read only where the manifest declares it", func(t *testing.T) {
raw, err := os.ReadFile("../../module.json")
if err != nil {
t.Fatal(err)
}
var man struct {
Reads []string `json:"reads"`
Invokes []string `json:"invokes"`
}
if err := json.Unmarshal(raw, &man); err != nil {
t.Fatal(err)
}
src, err := os.ReadFile("main.go")
if err != nil {
t.Fatal(err)
}
for _, m := range regexp.MustCompile(`stdio\.State\("([^"]+\.[^"]+)"\)`).FindAllStringSubmatch(string(src), -1) {
if !slices.Contains(man.Reads, m[1]) {
t.Errorf("main.go reads %s, which the manifest does not declare under reads: the runtime refuses it", m[1])
}
}
for _, tool := range []string{indexerCandidatesTool, indexerModulesTool} {
if !slices.Contains(man.Invokes, tool) {
t.Errorf("the manifest does not invoke %s", tool)
}
}
})
for _, c := range []struct {
name, key, answer, want string
failed bool
}{
{name: "the keys", answer: `{"owner":"mesh-delivery-indexer","state":"candidates","keys":["a","b"]}`, want: "[a b]"},
{name: "one key, wrapped as the tool runner hands it", key: "a",
answer: `{"content":[{"type":"text","text":"{\"key\":\"a\",\"found\":true,\"value\":{\"commit\":\"c\"}}"}]}`, want: `{"commit":"c"}`},
{name: "a key the indexer does not hold", key: "gone", answer: `{"key":"gone","found":false}`, want: ""},
{name: "the tool refused", key: "a", answer: `{"content":[{"type":"text","text":"refused"}],"isError":true}`, failed: true},
} {
t.Run("1966: the indexer's read tool, "+c.name, func(t *testing.T) {
var asked []string
r := ownerRead(func(key string, body any) (json.RawMessage, error) {
b, _ := json.Marshal(body)
asked = append(asked, key+" "+string(b))
return json.RawMessage(c.answer), nil
}, indexerCandidatesTool)
var got string
var err error
if c.key == "" {
var keys []string
keys, err = r.Keys()
got = strings.Trim(strings.Join([]string{"[", strings.Join(keys, " "), "]"}, ""), " ")
} else {
var raw json.RawMessage
raw, err = r.Get(c.key)
got = string(raw)
}
want := []string{indexerCandidatesTool + " {}"}
if c.key != "" {
want = []string{indexerCandidatesTool + ` {"key":"` + c.key + `"}`}
}
if (err != nil) != c.failed || (!c.failed && got != c.want) || !slices.Equal(asked, want) {
t.Errorf("read %q, %v, asking %v; want %q, failed %v, asking %v", got, err, asked, c.want, c.failed, want)
}
})
}
// With nothing to read from a tool says so and changes nothing.
for _, tl := range Tools(check.Reader{}) {
if tl.Name == check.StatusReadTool {
continue // it answers from what it can read, and says what it could not
}
if _, err := tl.Run(map[string]any{"repository": "novox/x", "number": float64(1)}); err == nil {
t.Errorf("%s answered with no state to read", tl.Name)
}
}
}
// memStore is a tries state in memory.
type memStore struct{ vals map[string]json.RawMessage }
func (m *memStore) Keys() ([]string, error) {
var out []string
for k := range m.vals {
out = append(out, k)
}
return out, nil
}
func (m *memStore) Get(key string) (json.RawMessage, error) { return m.vals[key], nil }
func (m *memStore) Create(key string, value []byte) error {
if _, ok := m.vals[key]; ok {
return errors.New("exists")
}
m.vals[key] = value
return nil
}
func (m *memStore) Entry(key string) (json.RawMessage, uint64, error) { return m.vals[key], 1, nil }
func (m *memStore) Update(key string, value []byte, revision uint64) error {
m.vals[key] = value
return nil
}
func TestTheTryToolsAskAndReadATry(t *testing.T) { // ADR 0501 §5, the agreed interface with mesh-controller's try
store := &memStore{vals: map[string]json.RawMessage{}}
tools := TryTools(Trying{Tries: store, Ender: store, Now: func() time.Time { return time.Unix(1, 5) },
Trunks: func() (map[string]string, error) {
return map[string]string{"novox/mesh-delivery": strings.Repeat("a", 40)}, nil
}})
if len(tools) != 3 || tools[0].Name != "checker_try" || tools[1].Name != "checker_try_read" || tools[2].Name != "checker_try_end" {
t.Fatalf("tools = %+v", tools)
}
out, err := tools[0].Run(map[string]any{"repository": "novox/mesh-delivery", "head": strings.Repeat("1", 40), "trunk": "main"})
if err != nil {
t.Fatal(err)
}
id := out.(map[string]any)["id"].(string)
if id != "try-1000000005" {
t.Errorf("id = %q", id)
}
raw, err := tools[1].Run(map[string]any{"id": id})
if err != nil || !strings.Contains(string(raw.(json.RawMessage)), `"state":"queued"`) {
t.Errorf("read = %s, %v", raw, err)
}
// The seat's verbs answer as the tools do (ADR 0547 §10, issue 774): the same ask is the same try, and the read is alike.
verbs := SeatTryTools(Trying{Tries: store, Ender: store, Now: func() time.Time { return time.Unix(1, 5) },
Trunks: func() (map[string]string, error) {
return map[string]string{"novox/mesh-delivery": strings.Repeat("a", 40)}, nil
}})
if len(verbs) != 3 || verbs[0].Name != Seat+".try" || verbs[1].Name != Seat+".try-read" || verbs[2].Name != Seat+".try-end" {
t.Fatalf("verbs = %+v", verbs)
}
again, err := verbs[0].Run(map[string]any{"repository": "novox/mesh-delivery", "head": strings.Repeat("1", 40), "trunk": "main"})
if err != nil || again.(map[string]any)["id"] != id {
t.Errorf("verb try = %v, %v, want the waiting try %s", again, err, id)
}
if viaVerb, err := verbs[1].Run(map[string]any{"id": id}); err != nil || string(viaVerb.(json.RawMessage)) != string(raw.(json.RawMessage)) {
t.Errorf("verb try-read = %s, %v; tool = %s", viaVerb, err, raw)
}
if _, err := verbs[0].Run(map[string]any{"repository": "novox/hq", "head": strings.Repeat("1", 40)}); err == nil {
t.Error("the verb tried a repository nothing checks")
}
// The seat's definition of the verb try gives the members the verb serves (ADR 0637 §6), as the tool takes them, and says so.
def, err := os.ReadFile("../../../mesh-delivery-contracts/seats/mesh-delivery-checker/seat.json")
if err != nil {
t.Fatal(err)
}
var seat struct {
Verbs []struct {
Name string `json:"name"`
Description string `json:"description"`
Input struct {
Properties map[string]any `json:"properties"`
Required []string `json:"required"`
} `json:"input"`
} `json:"verbs"`
}
if err := json.Unmarshal(def, &seat); err != nil {
t.Fatal(err)
}
for _, v := range seat.Verbs {
if v.Name != "try" {
continue
}
toolProps := verbs[0].Input["properties"].(map[string]any)
for name := range toolProps {
if _, ok := v.Input.Properties[name]; !ok {
t.Errorf("the verb try serves no %q, which the tool takes", name)
}
}
const limit = "at most 4 run on a build node, and 8 wait for one, mesh-wide"
if !strings.Contains(v.Description, limit) || !strings.Contains(verbs[0].Description, limit) {
t.Errorf("the verb and the tool state one limit %q", limit)
}
if len(v.Input.Required) != 0 || !strings.Contains(v.Description, "members") {
t.Errorf("the verb try requires %v and says %q: want members as an alternative to one repository and head", v.Input.Required, v.Description)
}
}
// The seat's verb input (issue 1921, ADR 0596 §2): a checking program's input as kept, which the Go build module asks.
body := []byte(`{"v":1}`)
in, _ := buildstep.RunInputOf(check.InputVerb, body)
inputs := SeatInputTools(check.Reader{InputState: check.StateRead{Get: func(key string) (json.RawMessage, error) {
if key == in.Digest {
return body, nil
}
return nil, nil
}}})
if len(inputs) != 1 || inputs[0].Name != Seat+".input" {
t.Fatalf("input verbs = %+v", inputs)
}
if a, err := inputs[0].Run(map[string]any{"digest": in.Digest}); err != nil || a.(meshdeliverychecker.InputAnswer).Input != base64.StdEncoding.EncodeToString(body) {
t.Errorf("verb input = %+v, %v", a, err)
}
// A group try (issue 1921): the members, each an exact commit; members and one repository together are refused; the
// description says what it does in the words a search for "try a group" finds.
later := TryTools(Trying{Tries: store, Ender: store, Now: func() time.Time { return time.Unix(2, 5) },
Trunks: func() (map[string]string, error) {
return map[string]string{"novox/mesh-delivery": strings.Repeat("a", 40)}, nil
}})
g, err := later[0].Run(map[string]any{"members": []any{map[string]any{"repository": "novox/mesh-delivery", "head": strings.Repeat("2", 40)}}})
if err != nil || g.(map[string]any)["id"] == id {
t.Errorf("a group try = %v, %v, want a try of its own", g, err)
}
if _, err := tools[0].Run(map[string]any{"members": []any{map[string]any{"repository": "novox/mesh-delivery", "head": strings.Repeat("2", 40)}},
"repository": "novox/mesh-delivery", "head": strings.Repeat("1", 40)}); err == nil {
t.Error("members and a repository were both taken")
}
if _, err := tools[0].Run(map[string]any{"members": "novox/mesh-delivery"}); err == nil {
t.Error("members that are no list were taken")
}
for _, tl := range tools {
for _, words := range []string{"group"} {
if !strings.Contains(strings.ToLower(tl.Description), words) {
t.Errorf("%s says nothing of a %s: %q", tl.Name, words, tl.Description)
}
}
}
if d := strings.ToLower(tools[0].Description); !strings.Contains(d, "try a group") || !strings.Contains(d, "exact commit") || !strings.Contains(d, "every failure") ||
!strings.Contains(d, "composition on every placed node") || !strings.Contains(d, "owes the forge nothing") || !strings.Contains(d, "kept") {
t.Errorf("the try's description does not say what a group try does: %q", d)
}
if open := OpenTriesTools(Trying{Tries: store}); len(open) != 1 || open[0].Name != check.TriesTool || !strings.Contains(open[0].Description, "try.started") {
t.Errorf("open tries tool = %+v", open)
} else if out, err := open[0].Run(nil); err != nil || len(out.(map[string]any)["tries"].([]checkermsg.Open)) != 2 {
t.Errorf("open tries = %v, %v, want the two tries asked", out, err)
}
// Issue 734: a try whose ask was lost is ended by hand with why, and its head is asked anew.
ended, err := tools[2].Run(map[string]any{"id": id, "why": "its ask was lost"})
if err != nil || ended.(map[string]any)["state"] != "error" {
t.Errorf("end = %v, %v", ended, err)
}
if _, err := tools[2].Run(map[string]any{"id": id, "why": "again"}); err == nil {
t.Error("an ended try was ended again")
}
if _, err := tools[0].Run(map[string]any{"repository": "novox/hq", "head": strings.Repeat("1", 40)}); err == nil ||
err.Error() != "no level of novox/hq is checked by the checker" {
t.Errorf("hq: %v", err)
}
}
// memPut writes a memStore as the checker's kept-request state is written.
type memPut struct{ m *memStore }
func (p memPut) Put(_ context.Context, key string, value []byte) (uint64, error) {
if len(value) > 262144 { // the state's value limit, headers included (issue 1966)
return 0, errors.New("the value is over the 262144 bytes the bucket takes")
}
p.m.vals[key] = value
return 1, nil
}