package main import ( "encoding/json" "fmt" "slices" "testing" catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" "nats-tools/internal/keeper" ) func TestAsksOf(t *testing.T) { m := map[string]any{"module": "m", "claims": []any{map[string]any{"name": "seat-a"}}, "emits": []any{"done"}} a, err := AsksOf(catalog.CatalogEntry{Module: "m", Verdict: "accepted", Manifest: m}) if err != nil { t.Fatal(err) } if len(a.Claims) != 1 || a.Claims[0] != "seat-a" || !a.EmitsOwn || a.Accepted.Module != "m" || a.Accepted.Digest == "" { t.Errorf("read %+v", a) } b, _ := AsksOf(catalog.CatalogEntry{Module: "m", Manifest: map[string]any{"module": "m"}}) if b.EmitsOwn || len(b.Claims) != 0 { t.Errorf("an entry with neither: %+v", b) } if b.Accepted.Digest != a.Accepted.Digest { t.Errorf("the digest is of the bus-state asked alone: %s vs %s", b.Accepted.Digest, a.Accepted.Digest) } // Design 47 H H-A-229, H-A-230: the old sections of the artifact registry and the forge, as their manifests declare // them, are asked for under the bus names the grants part gives them, the names the live orphans of 2026-11-21 hold; // a manifest not accepted asks none. for _, row := range []struct{ module, sections, want string }{ {"distribution", `{"state": ["transfers", {"name": "idle", "key-ttl": true}, "holds", "windows", "mirrors", "findings"], "streams": [{"name": "distribution-events", "events": ["old-name-closed"], "max-age-days": 7, "max-mib": 16}], "objects": [{"name": "incoming", "max-mib": 8192}]}`, "[KV_distribution_transfers KV_distribution_idle KV_distribution_holds KV_distribution_windows KV_distribution_mirrors KV_distribution_findings OBJ_distribution_incoming DISTRIBUTION_EVENTS]"}, {"gitea", `{"state": ["bound", {"name": "idle", "key-ttl": true}, "transfers"], "objects": [{"name": "incoming", "max-mib": 2048}], "logs": [{"name": "calls", "max-mib": 8}]}`, "[KV_gitea_bound KV_gitea_idle KV_gitea_transfers OBJ_gitea_incoming LOG_gitea_calls]"}, } { var m map[string]any if err := json.Unmarshal([]byte(row.sections), &m); err != nil { t.Fatal(err) } m["module"] = row.module a, err := AsksOf(catalog.CatalogEntry{Module: row.module, Verdict: "accepted", Manifest: m}) if err != nil { t.Fatal(err) } var names []string for _, p := range a.Accepted.Old { names = append(names, p.Objects[0].Stream()) } if fmt.Sprint(names) != row.want { t.Errorf("%s asks %v of its old sections, want %s", row.module, names, row.want) } i := slices.IndexFunc(a.Accepted.Old, func(p keeper.Plan) bool { return p.Local == "incoming" }) if i < 0 || a.Accepted.Old[i].Objects[0].Settings.MaxBytes != int64(m["objects"].([]any)[0].(map[string]any)["max-mib"].(float64))*keeper.MiB { t.Errorf("%s's incoming is not capped as declared: %+v", row.module, a.Accepted.Old) } if n, _ := AsksOf(catalog.CatalogEntry{Module: row.module, Verdict: "refused", Manifest: m}); len(n.Accepted.Old) != 0 { t.Errorf("a refused manifest of %s asks %v", row.module, n.Accepted.Old) } } if _, err := AsksOf(catalog.CatalogEntry{}); err == nil { t.Error("an entry with no module was read") } }