Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder-tools/toolchains_test.go
T
jschoubben 905a055a1e B2: the builder serves the seat mesh-builds' drop and its queue's tools
A caller whose step ended by its bound could not stop the builder working on
its ask, and nobody could see what waited on the seat's work queue. The
builder now serves the seat's verb drop: an ask it runs is cancelled, one not
pulled yet is kept dropped in its state taken (dropped.<ask>, for
ask-taken-bound); both end the step could-not-run naming the drop and who
asked it. builder_asks reads the waiting asks through the runtime and the
taken ones and the drops from the state taken; builder_ask_drop asks the
seat's drop once the hand act is recorded.

Builds: design 47 H2 §777, §778; H-A-401, H-A-404, H-A-410
2026-11-22 01:08:30 +01:00

113 lines
4.6 KiB
Go

package main
import (
"encoding/json"
"errors"
"reflect"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/buildrun"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
stdio "git.novox.be/novox/mesh-sdk/go"
"mesh-delivery-builder/internal/build"
)
type fakeHeard struct{ m map[string]json.RawMessage }
func (f *fakeHeard) Keys() ([]string, error) {
var out []string
for k := range f.m {
out = append(out, k)
}
return out, nil
}
func (f *fakeHeard) Get(k string) (*stdio.StateEntry, error) {
v, ok := f.m[k]
if !ok {
return nil, nil
}
return &stdio.StateEntry{Key: k, Value: v}, nil
}
func (f *fakeHeard) Delete(k string) error { delete(f.m, k); return nil }
// Design 47 G §604, §546: builder_toolchains lists the keys; builder_toolchains_drop deletes one with its why, after the
// hand act is recorded, and nothing is deleted when it is not.
func TestToolchainsToolsListAndDropAKeyAfterTheHandAct(t *testing.T) {
data, err := buildstep.EncodeToolchain(buildstep.Toolchain{Language: "go", Version: "1.26.0", Recipe: "1", Digest: "sha256:a", At: time.Now()})
if err != nil {
t.Fatal(err)
}
st := &fakeHeard{m: map[string]json.RawMessage{"ace.go": data}}
asked := 0
var failAct error
acts := meshhandactsclient.Recorder{Ask: func(string, any) (json.RawMessage, error) {
asked++
return json.RawMessage(`{"id":"h1"}`), failAct
}}
tools := toolchainsTools(st, acts, time.Now)
run := func(name string, args map[string]any) (any, error) {
for _, tl := range tools {
if tl.Name == name {
return tl.Run(args)
}
}
t.Fatalf("no tool %s", name)
return nil, nil
}
got, err := run(buildrun.ToolchainsTool, nil)
rows := got.(map[string]any)["toolchains"].([]map[string]any)
if err != nil || len(rows) != 1 || rows[0]["key"] != "ace.go" || rows[0]["digest"] != "sha256:a" {
t.Fatalf("listed %v, %v", got, err)
}
if _, err := run(buildrun.ToolchainsDropTool, map[string]any{"key": "ace.go", "why": ""}); err == nil {
t.Fatal("a drop with no why was taken")
}
failAct = json.Unmarshal([]byte("x"), new(int))
if _, err := run(buildrun.ToolchainsDropTool, map[string]any{"key": "ace.go", "why": "ace left"}); err == nil || !strings.Contains(err.Error(), "nothing was dropped") || len(st.m) != 1 {
t.Fatalf("a hand act not recorded still dropped: %v", err)
}
failAct = nil
if _, err := run(buildrun.ToolchainsDropTool, map[string]any{"key": "ace.go", "why": "ace left"}); err != nil || len(st.m) != 0 || asked < 2 {
t.Fatalf("the drop: %v, keys left %d", err, len(st.m))
}
// Design 47 H2 §777, H-A-410: builder_asks answers the waiting asks the runtime reads, held told from waiting again by
// the state taken, and the taken asks and the drops kept; builder_ask_drop asks the seat's drop after the hand act.
taken := &fakeHeard{m: map[string]json.RawMessage{"mesh-builds.a2": json.RawMessage(`{"seat":"mesh-builds","ask":"a2"}`),
"dropped.a4": json.RawMessage(`{"by":"op@shanks","why":"by hand"}`)}}
var dropped []meshbuilds.DropInput
asks := asksTools(func(seat string) (stdio.Waiting, error) {
if seat != meshbuilds.Seat {
t.Errorf("the waiting asks were read of %s", seat)
}
return stdio.Waiting{Asks: []stdio.WaitingAsk{{ID: "a1", Module: "mesh-delivery-pipeline"}, {ID: "a2", Again: true}, {ID: "a3", Again: true}}}, nil
}, taken, func(in meshbuilds.DropInput) (json.RawMessage, error) {
dropped = append(dropped, in)
return json.RawMessage(`{"held":true}`), nil
}, time.Now, acts)
tools = asks
got, err = run(build.AsksTool, nil)
if err != nil {
t.Fatal(err)
}
a := got.(map[string]any)
w := a["waiting"].([]WaitingRow)
if len(w) != 3 || w[0].State != StateWaiting || w[1].State != StateHeld || w[2].State != StateWaitingAgain ||
!reflect.DeepEqual(a["taken"], []TakenAsk{{Ask: "a2"}}) || !reflect.DeepEqual(a["dropped"], []TakenAsk{{Ask: "a4", By: "op@shanks", Why: "by hand"}}) {
t.Errorf("builder_asks = %+v", a)
}
failAct = errors.New("no log")
if _, err := run(build.AskDropTool, map[string]any{"ask": "a1", "why": "stuck"}); err == nil || len(dropped) != 0 {
t.Fatalf("a hand act not recorded still dropped: %v", err)
}
failAct = nil
if got, err := run(build.AskDropTool, map[string]any{"ask": "a1", "why": "stuck"}); err != nil || len(dropped) != 1 || dropped[0].ID != "a1" || dropped[0].Why != "stuck" || got.(map[string]any)["held"] != true {
t.Fatalf("the drop: %v, %v, asked %+v", got, err, dropped)
}
}