package bootstrap import ( "encoding/json" "strings" "testing" ) // Defends novox/hq ADR 0100: a port given for the package registry at genesis reaches the one // thing that dials it by a fixed number, the builder's package binding. // The builder's package binding exactly as the catalogue's manifest carries it. const builderManifest = `{ "module": "builder", "resources": [ { "id": "package-binding", "type": "file", "path": "/var/lib/mesh/builder/package-registry.json", "mode": "0600", "content": "{\"provision\": \"package-registry\", \"from\": \"gitea\", \"at\": \"127.0.0.1\", \"as\": \"mesh-builder\", \"serves\": {\"scheme\": \"http\", \"port\": 3000, \"npm-path\": \"/api/packages/novox/npm/\"}}\n" } ] }` func bindingPort(t *testing.T, manifest []byte) float64 { t.Helper() var m struct { Resources []struct { Content string `json:"content"` } `json:"resources"` } if err := json.Unmarshal(manifest, &m); err != nil { t.Fatal(err) } var binding struct { Serves struct { Port float64 `json:"port"` } `json:"serves"` } if err := json.Unmarshal([]byte(m.Resources[0].Content), &binding); err != nil { t.Fatal(err) } return binding.Serves.Port } func TestTheBuilderFollowsThePackageRegistrysGivenPort(t *testing.T) { got, err := followPackagesPort([]byte(builderManifest), 3100) if err != nil { t.Fatal(err) } if p := bindingPort(t, got); p != 3100 { t.Errorf("the builder's binding dials %v, not the port given", p) } } func TestTheBuilderOnTheDefaultPortIsUnchanged(t *testing.T) { got, err := followPackagesPort([]byte(builderManifest), 3000) if err != nil || string(got) != builderManifest { t.Errorf("the default port changed the manifest: %v", err) } } func TestABuilderManifestThatNoLongerNamesThePortIsRefused(t *testing.T) { moved := strings.Replace(builderManifest, `\"port\": 3000`, `\"port\": 3001`, 1) if _, err := followPackagesPort([]byte(moved), 3100); err == nil || !strings.Contains(err.Error(), "--packages-port") { t.Errorf("a manifest the port cannot reach was accepted: %v", err) } }