package resolution import ( "strings" "testing" mcat "git.novox.be/novox/mesh-sdk/go/catalogue" ) func registryShelf() map[string]mcat.Manifest { return shelf(mcat.Manifest{Module: "gitea", Version: "1", Provides: mcat.FromAnywhere("npm-package-registry"), Claims: []mcat.Claim{{Name: "npm-package-registry", Scope: mcat.ScopeMesh}}}, mcat.Manifest{Module: "verdaccio", Version: "1", Provides: mcat.FromAnywhere("npm-package-registry")}, mcat.Manifest{Module: "builder", Version: "1", Requires: []string{"npm-package-registry"}}, ) } func twoRegistries() map[string][]mcat.Provider { return map[string][]mcat.Provider{"npm-package-registry": { {Node: "anchor", At: "anchor.internal", Module: "gitea"}, {Node: "archive", At: "archive.internal", Module: "verdaccio"}, }} } func giteaHoldsTheSeat() []mcat.Held { return []mcat.Held{{Claim: "npm-package-registry", Scope: mcat.ScopeMesh, Node: "anchor", Module: "gitea"}} } func TestTheSeatsHolderAnswersWhenSeveralProvide(t *testing.T) { // The whole point: a second registry beside the holder harms nothing, and nobody pins. got, err := Resolve(registryShelf(), []string{"builder"}, reachable(), mcat.World{Offered: twoRegistries(), Held: giteaHoldsTheSeat()}) if err != nil { t.Fatal(err) } if len(got.Needs) != 1 || got.Needs[0].From != "anchor" { t.Fatalf("the seat's holder did not answer: %v", got.Needs) } } func TestAPinStillWinsOverTheSeat(t *testing.T) { // A consumer coupled to one provider's contents has said so, and the seat does not overrule it. got, err := Resolve(registryShelf(), []string{"builder"}, reachable(), mcat.World{Offered: twoRegistries(), Held: giteaHoldsTheSeat(), Pinned: map[string]mcat.Chosen{"npm-package-registry": {Node: "archive", Module: "verdaccio"}}}) if err != nil { t.Fatal(err) } if len(got.Needs) != 1 || got.Needs[0].From != "archive" { t.Fatalf("the pin was overruled by the seat: %v", got.Needs) } } func TestWithTheSeatUnheldSeveralProvidersAreStillRefused(t *testing.T) { // No seat held is no choice made, and ADR 0009's rule stands: never guessed. _, err := Resolve(registryShelf(), []string{"builder"}, reachable(), mcat.World{Offered: twoRegistries()}) if err == nil { t.Fatal("one of two registries was picked with nobody holding the seat") } if !strings.Contains(err.Error(), "pin") { t.Fatalf("the refusal does not say how to choose: %v", err) } } func TestTheHolderIsTheModuleNotTheMachine(t *testing.T) { // Two modules on one machine could provide the same thing; only the one holding the seat // answers. A holder matched by node alone would send consumers to whichever came first. providers := []mcat.Provider{ {Node: "anchor", At: "anchor.internal", Module: "verdaccio"}, {Node: "anchor", At: "anchor.internal", Module: "gitea"}, } holder, held := HolderAmong("npm-package-registry", providers, giteaHoldsTheSeat()) if !held || holder.Module != "gitea" { t.Fatalf("the holder was not told apart from a neighbour: %+v", holder) } }