package main import ( "encoding/json" "errors" "strings" "testing" "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" "mesh-delivery-builder/internal/build" "mesh-delivery-builder/internal/versions" ) // Design 47 G §276, §672: builder_waits lists the held entries; builder_release records the hand act first and asks the // builder through reasks, whose refusal is the tool's error. func TestWaitsToolsListTheWaitsAndReleaseAfterTheHandAct(t *testing.T) { wait, _ := json.Marshal(build.ToolchainWait{Module: "m", Fingerprint: "src2:" + strings.Repeat("1", 64), Language: "go", Version: "go1.26.0", Since: time.Now()}) rr := versions.Reader{Keys: func() ([]string, error) { return []string{"m.k"}, nil }, Get: func(string) (json.RawMessage, bool, error) { return wait, true, nil }} var asked []build.ReAsk answer := func(key string, value []byte) (json.RawMessage, error) { a, _ := build.DecodeReAsk(value) asked = append(asked, a) a.Answered, a.Answer = time.Now(), "released: built" return json.Marshal(a) } recorded := 0 acts := meshhandactsclient.Recorder{Ask: func(string, any) (json.RawMessage, error) { recorded++; return json.RawMessage(`{}`), nil }} tools := waitsTools(rr, answer, time.Now, acts) byName := map[string]func(map[string]any) (any, error){} for _, tl := range tools { byName[tl.Name] = tl.Run } got, err := byName[build.WaitsTool](nil) if rows := got.(map[string]any)["waits"].([]build.ToolchainWait); err != nil || len(rows) != 1 || rows[0].On() != "toolchain:go@go1.26.0" { t.Fatalf("waits = %v, %v", got, err) } if _, err := byName[build.ReleaseTool](map[string]any{"module": "m", "fp": "src2:" + strings.Repeat("1", 64), "why": ""}); err == nil || recorded != 0 { t.Fatalf("a release with no why was taken: %v, %d recorded", err, recorded) } if _, err := byName[build.ReleaseTool](map[string]any{"module": "m", "fp": "src2:" + strings.Repeat("1", 64), "why": "the build module runs"}); err != nil || recorded != 1 || len(asked) != 1 || asked[0].Mode != build.ModeRelease || asked[0].Module != "m" { t.Fatalf("release: %v, %d recorded, asked %+v", err, recorded, asked) } } // Design 47 G A-455: builder_reserve_done records the hand act first and only then asks the builder through reasks; the builder's // answer (the version and its queued publish) is the tool's answer, its refusal the tool's error, and an act that was not // recorded asks nothing. func TestReserveDoneRecordsTheHandActFirstAndAnswersTheBuilders(t *testing.T) { var order []string var asked []build.ReAsk reply := func(refuse bool) func(string, []byte) (json.RawMessage, error) { return func(key string, value []byte) (json.RawMessage, error) { order = append(order, "asked") a, _ := build.DecodeReAsk(value) asked = append(asked, a) a.Answered, a.Answer, a.Refused = time.Now(), "reserved 0.1.13 for node-tools and queued 1 versions to publish: node-tools@0.1.13", refuse if refuse { a.Answer = "the build key is not done" } return json.Marshal(a) } } record := func(fail bool) meshhandactsclient.Recorder { return meshhandactsclient.Recorder{Ask: func(string, any) (json.RawMessage, error) { order = append(order, "recorded") if fail { return nil, errors.New("no responders") } return json.RawMessage(`{}`), nil }} } run := func(ask func(string, []byte) (json.RawMessage, error), acts meshhandactsclient.Recorder, args map[string]any) (any, error) { for _, tl := range reservedTools(versions.Reader{}, ask, time.Now, acts) { if tl.Name == versions.ReserveDoneTool { return tl.Run(args) } } t.Fatal("the bundle serves no builder_reserve_done") return nil, nil } args := map[string]any{"module": "node-tools", "fp": "src2:7df8", "why": "its build ended done with no version"} out, err := run(reply(false), record(false), args) if err != nil || len(order) != 2 || order[0] != "recorded" || order[1] != "asked" || asked[0].Mode != build.ModeReserveDone || asked[0].Name != "node-tools" || asked[0].FP != "src2:7df8" { t.Fatalf("answer %v, %v; order %v, asked %+v", out, err, order, asked) } if ans := out.(map[string]any)["answer"].(string); !strings.Contains(ans, "reserved 0.1.13") || !strings.Contains(ans, "queued 1 versions") { t.Errorf("the answer %q names no version and queued publish", ans) } if _, err := run(reply(true), record(false), args); err == nil || !strings.Contains(err.Error(), "not done") { t.Errorf("the builder's refusal is not the tool's error: %v", err) } order = nil if _, err := run(reply(false), record(true), args); err == nil || len(order) != 1 || order[0] != "recorded" { t.Errorf("an unrecorded act was asked of the builder: %v, %v", err, order) } }