Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/held_test.go
T
jschoubben 3090a80a09 indexer: indexer_pin_drop's all form drops every pin for the clean state
Review round 1, H-A-121 (2): beside the one-item form, all true drops
every pin of the state pins, each recorded through mesh-hand-acts and
asked of the indexer as the one-item form is; it answers the count
dropped and the pipelines the pins named.

Builds: design 47 H §765, H-A-121, H-A-124

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 05:55:59 +01:00

123 lines
5.1 KiB
Go

package index
import (
"encoding/json"
"errors"
"slices"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// Design 47 H §744, §765, H-A-97, H-A-57: the hand tools of the held take-in and the pin drop write asks the indexer reads
// back, each recorded in the hand-act log before it is asked where it is a hand act.
func TestTheHeldAndPinToolsAskWhatTheIndexerReads(t *testing.T) {
var asked []TakeInAsk
var recorded []string
ask := func(_ string, value []byte) (TakeInAsk, error) {
a, err := DecodeTakeIn(value)
if err != nil {
t.Errorf("the indexer cannot read the ask %s: %v", value, err)
}
asked = append(asked, a)
return a, nil
}
record := func(what, _ string) error { recorded = append(recorded, what); return nil }
tools := map[string]stdio.Tool{}
for _, tl := range append(HeldTools(newMem(), ask, time.Now, record), PinsTools(newMem(), newMemObjects(0), ask, time.Now, record)...) {
tools[tl.Name] = tl
}
for _, c := range []struct {
tool string
args map[string]any
want func(TakeInAsk) bool
}{
{TakeInPendingTool, map[string]any{"branch": "main"}, func(a TakeInAsk) bool { return a.Held == "main" && !a.Skip }},
{TakeInSkipTool, map[string]any{"branch": "main", "source": float64(4), "why": "stale"}, func(a TakeInAsk) bool { return a.Held == "main" && a.HeldSource == 4 && a.Skip }},
{PinDropTool, map[string]any{"key": "main.7", "why": "no pipeline"}, func(a TakeInAsk) bool { return a.PinDrop == "main.7" }},
} {
asked = nil
if _, err := tools[c.tool].Run(c.args); err != nil || len(asked) != 1 || !c.want(asked[0]) {
t.Errorf("%s asked %+v (%v)", c.tool, asked, err)
}
}
if len(recorded) != 2 {
t.Errorf("recorded %v, want the skip and the pin drop", recorded)
}
// Design 47 H §744, §764 (H-A-48): an ask of the earlier take-in (a pending wait, a merge by hand) names no hand act the
// indexer serves, and is refused, not acted on.
for _, old := range []string{`{"v":1,"pending":"m.12","drop":true,"why":"x"}`, `{"v":1,"merge":"` + "0123456789012345678901234567890123456789" + `","sequence":3}`} {
if _, err := DecodeTakeIn([]byte(old)); err == nil {
t.Errorf("the earlier ask %s is read as a hand act", old)
}
}
held := newMem()
ix := &Indexer{Held: held}
ix.hold(t.Context(), meshdelivery.SourceMovedEvent{Branch: "main", Source: 4, Call: "0123456789abcdef"}, "the catalog store is full")
got, err := HeldTools(held, ask, time.Now, record)[0].Run(map[string]any{})
data, _ := json.Marshal(got)
var answer struct {
Held []struct {
Branch string `json:"branch"`
Source int64 `json:"source"`
Why string `json:"why"`
Since string `json:"since"`
} `json:"held"`
}
if err != nil || json.Unmarshal(data, &answer) != nil || len(answer.Held) != 1 || answer.Held[0].Branch != "main" || answer.Held[0].Source != 4 || answer.Held[0].Why == "" {
t.Fatalf("indexer_pending answered %s (%v), want {held: [main 4 and why]}", data, err)
}
if _, err := time.Parse(time.RFC3339, answer.Held[0].Since); err != nil {
t.Errorf("since %q is not RFC 3339: %v", answer.Held[0].Since, err)
}
}
// Design 47 H-A-121 (2), H-A-124: indexer_pin_drop's all form drops every pin of the state pins, each recorded in the hand-act
// log and asked as the one-item form is, and answers the count and the pipelines they named; it names no key, says why, and stops
// at a drop not recorded.
func TestEveryPinIsDroppedByHandAndCounted(t *testing.T) {
pins := newMem()
for k, v := range map[string]string{"main.7": `{"records":["pipeline.main.4","take-in.main.4"]}`, "main.8": `{"records":["pipeline.main.5"]}`, "dev.2": `{"records":[]}`} {
_ = pins.Create(t.Context(), k, []byte(v))
}
var asked []string
ask := func(_ string, value []byte) (TakeInAsk, error) {
a, err := DecodeTakeIn(value)
if err != nil {
t.Errorf("the indexer cannot read the ask %s: %v", value, err)
}
asked = append(asked, a.PinDrop)
a.Result = &TakenIn{Stored: true}
return a, nil
}
for _, c := range []struct {
name string
args map[string]any
refuse error
dropped int
failed bool
}{
{"all", map[string]any{"all": true, "why": "the clean state"}, nil, 3, false},
{"all with a key", map[string]any{"all": true, "key": "main.7", "why": "x"}, nil, 0, true},
{"no why", map[string]any{"all": "true"}, nil, 0, true},
{"not recorded", map[string]any{"all": true, "why": "x"}, errors.New("no responders"), 0, true},
} {
asked = nil
var recorded int
record := func(string, string) error { recorded++; return c.refuse }
got, err := PinsTools(pins, newMemObjects(0), ask, time.Now, record)[1].Run(c.args)
if c.failed != (err != nil) || len(asked) != c.dropped {
t.Fatalf("%s: %+v (%v), asked %v", c.name, got, err, asked)
}
if c.failed {
continue
}
d := got.(PinsDropped)
if d.Dropped != 3 || recorded != 3 || !slices.Equal(d.Pins, []string{"dev.2", "main.7", "main.8"}) || !slices.Equal(d.Pipelines, []string{"main.4", "main.5"}) {
t.Errorf("%s answered %+v with %d hand acts, want 3 pins, 3 acts and the pipelines main.4 and main.5", c.name, d, recorded)
}
}
}