package meshcatalogclient import ( "errors" "strings" "testing" seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog" "git.novox.be/novox/mesh-sdk/go/keys" ) func hex64(c string) string { return strings.Repeat(c, 64) } // The digest and the candidate's fingerprint set equal what the trunk's go/delivery computed for the same input (the // expected strings are its output), so a key or a verdict written before is read after. func TestDigestAndFingerprintSetKeepTheirForms(t *testing.T) { if got := ManifestDigest([]byte(`{"module":"nats"}`)); got != "19a7450f557153c318558acac91cd6972321a192f6f95c3698a0e4c57edd4359" { t.Errorf("ManifestDigest = %s", got) } pull := func(fps map[string]seat.CandidatesByGroupValuePullsItemFpsValue) []seat.CandidatesByGroupValuePullsItem { return []seat.CandidatesByGroupValuePullsItem{{Fps: fps}} } fps := map[string]seat.CandidatesByGroupValuePullsItemFpsValue{"nats": {Fp2: SourcePrefix2 + hex64("a")}, "zsh": {None2: "no manifest"}} set := FingerprintSet(pull(fps)) if set != "50a3f57cc4e2" { t.Errorf("FingerprintSet = %s", set) } fps["zsh"] = seat.CandidatesByGroupValuePullsItemFpsValue{None2: "another reason"} if FingerprintSet(pull(fps)) == set { t.Error("a changed reason leaves the set as it was") } if FingerprintSet(pull(nil)) != "" { t.Error("no fingerprint gives a set") } } // A manifest and a module are read back as written, and each rule of the old values is kept: a refusal names its reason. func TestManifestAndModuleValuesKeepTheirRules(t *testing.T) { if _, err := ManifestsKey("abc"); err == nil || !strings.Contains(err.Error(), `"abc"`) { t.Errorf("ManifestsKey(abc) = %v", err) } if k, err := ParseManifestsKey(hex64("b")); err != nil || k != hex64("b") { t.Errorf("ParseManifestsKey = %q, %v", k, err) } raw, err := EncodeManifest(seat.ManifestsByDigestValue{Module: "nats", Manifest: `{"module":"nats"}`}) if err != nil { t.Fatal(err) } if m, err := DecodeManifest(raw); err != nil || m.Module != "nats" || m.V != keys.Version { t.Errorf("DecodeManifest = %+v, %v", m, err) } for _, bad := range []seat.ManifestsByDigestValue{{Module: "a.b", Manifest: "{}"}, {Module: "nats"}, {Module: "nats", Manifest: "not json"}, {Module: "nats", Manifest: "{\"a\":\"\xff\"}"}} { if _, err := EncodeManifest(bad); err == nil { t.Errorf("EncodeManifest(%+v) is accepted", bad) } } if _, err := DecodeManifest([]byte(`{"module":"nats","manifest":"{}"}`)); !errors.Is(err, keys.ErrUnknownVersion) { t.Errorf("a manifest with no version: %v", err) } good := seat.ModulesByModuleValue{Fp2: SourcePrefix2 + hex64("a"), Repository: "novox/mesh-catalog", Commit: "c", Manifest: hex64("b"), Path: RootPath} raw, err = EncodeModule(good) if err != nil { t.Fatal(err) } if m, err := DecodeModule(raw); err != nil || m.Fp2 != good.Fp2 || m.Path != RootPath { t.Errorf("DecodeModule = %+v, %v", m, err) } refused := good refused.Verdict, refused.Problems = VerdictRefused, []string{"p"} if _, err := EncodeModule(refused); err != nil { t.Errorf("a refused module: %v", err) } for name, mutate := range map[string]func(m *seat.ModulesByModuleValue){ "neither a fingerprint nor a reason": func(m *seat.ModulesByModuleValue) { m.Fp2 = "" }, "both fp2 and none2": func(m *seat.ModulesByModuleValue) { m.None2 = "x" }, "an old fingerprint of another form": func(m *seat.ModulesByModuleValue) { m.Fp = "src2:" + hex64("a") }, "a fingerprint of the old key form": func(m *seat.ModulesByModuleValue) { m.Fp2 = "src1:" + hex64("a") }, "inputs and no manifest": func(m *seat.ModulesByModuleValue) { m.Manifest = "" m.Inputs2 = &seat.ModulesByModuleValueInputs2{Tree: "t"} }, "a refusal with no problem": func(m *seat.ModulesByModuleValue) { m.Verdict = VerdictRefused }, "problems with no refusal": func(m *seat.ModulesByModuleValue) { m.Problems = []string{"p"} }, "unknown fields with no refusal": func(m *seat.ModulesByModuleValue) { m.Unknown = []string{"series"} }, "a path outside the directory": func(m *seat.ModulesByModuleValue) { m.Paths = map[string][]string{"tools": {"../x"}} }, "a toolchain with no recipe": func(m *seat.ModulesByModuleValue) { m.Toolchains = map[string]seat.ToolchainUsed{"go": {Language: "go", Version: "1.26"}} }, "an import form with no imports": func(m *seat.ModulesByModuleValue) { m.Inputs2 = &seat.ModulesByModuleValueInputs2{Tree: "t", ImportForm: SourcePrefix2 + hex64("a")} }, } { m := good mutate(&m) if _, err := EncodeModule(m); err == nil { t.Errorf("%s: EncodeModule accepts %+v", name, m) } } } // A verdict is taken as the readers have always taken it: a missing one accepted, a refusal held with its problems, a wait // held with the fields it waits on, and a module's own verdict field gives the same. func TestVerdictsAreTakenAsTheReadersTookThem(t *testing.T) { if refused, problems := TakeVerdict(nil); refused || problems != nil { t.Errorf("no verdict: %v %v", refused, problems) } waits := seat.VerdictsByDigestValue{Module: "nats", State: VerdictManifestWaits, Unknown: []string{"series"}, Judge: "j"} refused, problems := TakeVerdict(&waits) if !refused || len(problems) != 1 || problems[0] != "it carries series, which the indexer's judge does not know yet" { t.Errorf("a wait: %v %v", refused, problems) } if got := VerdictWhy(waits); got != "the indexer's judge (j) holds the manifest of nats: "+problems[0] { t.Errorf("VerdictWhy(wait) = %q", got) } if as := VerdictAsRefused(waits); as.State != VerdictRefused || len(as.Unknown) != 0 || as.Problems[0] != problems[0] { t.Errorf("VerdictAsRefused = %+v", as) } no := seat.VerdictsByDigestValue{Module: "nats", State: VerdictRefused, Problems: []string{"a", "b"}, Judge: "j"} if got := VerdictWhy(no); got != "the indexer's judge (j) refused the manifest of nats: a; b" { t.Errorf("VerdictWhy(refused) = %q", got) } if !SameVerdict(no, seat.VerdictsByDigestValue{Module: "nats", State: VerdictRefused, Problems: []string{"a", "b"}, Judge: "k"}) { t.Error("another judge is another verdict") } fromModule := ModuleVerdict("nats", seat.ModulesByModuleValue{Verdict: VerdictRefused, Unknown: []string{"series"}, Problems: []string{"x"}}) if fromModule == nil || fromModule.State != VerdictManifestWaits || fromModule.Unknown[0] != "series" { t.Errorf("a refusal naming unknown fields is the wait it was written as: %+v", fromModule) } if ModuleVerdict("nats", seat.ModulesByModuleValue{}) != nil { t.Error("a module with no verdict carries one") } for _, bad := range []seat.VerdictsByDigestValue{{Module: "nats", State: "accepted", Problems: []string{"p"}, Judge: "j"}, {Module: "nats", State: "refused", Judge: "j"}, {Module: "nats", State: "accepted"}, {Module: "nats", State: "odd", Judge: "j"}} { if _, err := EncodeVerdict(bad); err == nil { t.Errorf("EncodeVerdict(%+v) is accepted", bad) } } raw, err := EncodeVerdict(no) if err != nil { t.Fatal(err) } if v, err := DecodeVerdict(raw); err != nil || !SameVerdict(v, no) { t.Errorf("DecodeVerdict = %+v, %v", v, err) } if k, err := VerdictsKey(hex64("c")); err != nil || k != hex64("c") { t.Errorf("VerdictsKey = %q %v", k, err) } if _, err := ModulesKey("a.b"); err == nil { t.Error("ModulesKey accepts a dotted name") } } func TestCandidatesKeyEscapesTheGroupAndReadsBack(t *testing.T) { key, err := CandidatesKey("fix/1.2") if err != nil { t.Fatal(err) } if key == "fix/1.2" { t.Errorf("the group is not escaped: %q", key) } if group, err := ParseCandidatesKey(key); err != nil || group != "fix/1.2" { t.Errorf("read back = %q, %v", group, err) } if _, err := CandidatesKey(""); err == nil { t.Error("an empty group keys") } }