Files
mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient/entry_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

97 lines
3.2 KiB
Go

package meshcatalogclient
import (
"context"
"sync"
"testing"
"time"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/keys"
"git.novox.be/novox/mesh-sdk/go/views"
)
// wireFindings is a part's finding writer in memory: the findings by `<kind>/<id>` it holds.
type wireFindings struct {
mu sync.Mutex
held map[string]bool
}
func (w *wireFindings) WriteWire(_ context.Context, f map[string]any) error {
w.mu.Lock()
defer w.mu.Unlock()
w.held[f["kind"].(string)+"/"+f["id"].(string)] = true
return nil
}
func (w *wireFindings) Clear(_ context.Context, _, id, kind, _ string) error {
w.mu.Lock()
defer w.mu.Unlock()
delete(w.held, kind+"/"+id)
return nil
}
func (w *wireFindings) ModuleNode() (string, string) { return "part", "n1" }
func (w *wireFindings) has(key string) bool {
w.mu.Lock()
defer w.mu.Unlock()
return w.held[key]
}
// A-103 (design 47 G §614): the helpers every part uses over the branch follower: Ready and Entry before a first view, the
// entries by fingerprint as `first_views` rows, the finding entry-not-found written and cleared, and `<module>_entry_again`.
func TestTheBranchFollowerGivesEveryPartItsEntryHelpers(t *testing.T) {
seats := &fakeSeats{entries: map[string]seat.CatalogEntry{"fp:a": {Module: "m", Fp: "fp:a"}}}
b, err := NewBranches(&fakeWiring{adopted: map[string]*views.Follower{}, hears: map[string]func(context.Context, []byte) error{}}, seats.call)
if err != nil {
t.Fatal(err)
}
if err := b.Ready(); err == nil {
t.Error("Ready said ready with no watched branch")
}
if _, _, err := b.Entry("m"); err == nil {
t.Error("Entry answered with no first view: that is an error, never no module")
}
told := make(chan EntryRow, 4)
w := &wireFindings{held: map[string]bool{}}
b.WriteEntryFindings(w, func(r EntryRow) { told <- r })
b.Entries.Need(context.Background(), "fp:none")
<-told
if !w.has(views.EntryNotFoundKind + "/" + keys.EscapeName("fp:none")) {
t.Errorf("entry-not-found not written: %v", w.held)
}
if st := b.EntryStatuses(); len(st) != 1 || st[0].State != EntryNotFound {
t.Errorf("entry statuses = %+v", st)
}
seats.mu.Lock()
seats.entries["fp:none"] = seat.CatalogEntry{Module: "late", Fp: "fp:none"}
seats.mu.Unlock()
var acts []string
tool := b.EntryAgainTool("part", func(what, why, cause string) error { acts = append(acts, what+"|"+why+"|"+cause); return nil })
if tool.Name != "part_entry_again" {
t.Errorf("tool name %s", tool.Name)
}
if _, err := tool.Run(map[string]any{"fingerprint": "fp:none"}); err == nil {
t.Error("entry_again ran without a why")
}
if _, err := tool.Run(map[string]any{"fingerprint": "fp:none", "why": "late"}); err != nil {
t.Fatal(err)
}
if len(acts) != 1 || w.has(views.EntryNotFoundKind+"/"+keys.EscapeName("fp:none")) {
t.Errorf("hand acts %v, findings %v", acts, w.held)
}
select {
case <-told:
case <-time.After(5 * time.Second):
t.Error("the part was not told of the answer")
}
b.Entries.Need(context.Background(), "fp:gone")
<-told
b.Entries.Release("fp:gone")
if w.has(views.EntryNotFoundKind + "/" + keys.EscapeName("fp:gone")) {
t.Error("a released entry kept its finding")
}
}