package catalog import "testing" func TestEmbeddedCatalogLoads(t *testing.T) { cat, err := Embedded() if err != nil { t.Fatalf("embedded catalog does not parse: %v", err) } if len(cat.Apps) == 0 { t.Fatal("embedded catalog is empty") } } // TestEveryTargetHasDetection guards the property the engine relies on to be // idempotent: without detect rules it cannot tell an app is already installed. func TestEveryTargetHasDetection(t *testing.T) { cat, err := Embedded() if err != nil { t.Fatal(err) } for _, app := range cat.Apps { for _, goos := range app.Platforms() { if app.TargetFor(goos).Detect.Empty() { t.Errorf("%s/%s has no detect rules", app.ID, goos) } } } } func TestProfilesReferenceRealApps(t *testing.T) { cat, err := Embedded() if err != nil { t.Fatal(err) } for name, members := range cat.Profiles { if len(members) == 0 { t.Errorf("profile %q is empty", name) } for _, id := range members { if _, ok := cat.Get(id); !ok { t.Errorf("profile %q references unknown app %q", name, id) } } } } func TestResolveExpandsProfilesAndDeduplicates(t *testing.T) { cat, err := Embedded() if err != nil { t.Fatal(err) } apps, unknown := cat.Resolve([]string{"@standard", "chrome", "chrome"}) if len(unknown) != 0 { t.Fatalf("unexpected unknown names: %v", unknown) } seen := map[string]int{} for _, a := range apps { seen[a.ID]++ } if seen["chrome"] != 1 { t.Errorf("chrome resolved %d times, want 1", seen["chrome"]) } if len(apps) < len(cat.Profiles["standard"]) { t.Errorf("resolved %d apps, expected at least the %d in @standard", len(apps), len(cat.Profiles["standard"])) } } func TestResolveReportsUnknownNames(t *testing.T) { cat, err := Embedded() if err != nil { t.Fatal(err) } _, unknown := cat.Resolve([]string{"chrome", "definitely-not-an-app", "@nope"}) if len(unknown) != 2 { t.Fatalf("got unknown=%v, want 2 entries", unknown) } } func TestAliasesResolve(t *testing.T) { cat, err := Embedded() if err != nil { t.Fatal(err) } app, ok := cat.Get("notepad++") if !ok { t.Fatal("alias notepad++ did not resolve") } if app.ID != "notepadplusplus" { t.Errorf("alias resolved to %q", app.ID) } } func TestOverlayReplacesAppAndAddsProfile(t *testing.T) { base, err := Embedded() if err != nil { t.Fatal(err) } overlay, err := Parse([]byte(` apps: - id: chrome name: Google Chrome windows: sources: - provider: choco id: googlechrome detect: registry: ["Google Chrome"] profiles: onboarding: [chrome] `), "test overlay") if err != nil { t.Fatal(err) } base.merge(overlay) base.reindex() app, ok := base.Get("chrome") if !ok { t.Fatal("chrome missing after merge") } sources := app.Windows.Sources if len(sources) != 1 || sources[0].Provider != "choco" { t.Errorf("overlay did not replace the shipped entry: %+v", sources) } if app.Darwin != nil { t.Error("overlay replace should be wholesale, not a deep merge") } if _, ok := base.Profiles["onboarding"]; !ok { t.Error("overlay profile was not added") } } func TestValidateRejectsBadEntries(t *testing.T) { cases := map[string]string{ "missing id": "apps:\n - name: No ID\n windows:\n sources:\n - provider: winget\n id: X\n", "no platforms": "apps:\n - id: x\n name: X\n", "no sources": "apps:\n - id: x\n name: X\n windows:\n sources: []\n", "direct wthout url": "apps:\n - id: x\n name: X\n windows:\n sources:\n - provider: direct\n kind: msi\n", "provider no id": "apps:\n - id: x\n name: X\n windows:\n sources:\n - provider: winget\n", } for name, doc := range cases { if _, err := Parse([]byte(doc), "test"); err == nil { t.Errorf("%s: expected a validation error", name) } } } func TestParseRejectsUnknownFields(t *testing.T) { // A typo in a catalog key must fail loudly rather than be ignored. _, err := Parse([]byte("apps:\n - id: x\n name: X\n windowz:\n sources: []\n"), "test") if err == nil { t.Fatal("expected an error for an unknown field") } } func TestSourceURLForArch(t *testing.T) { src := Source{ URL: "https://example.test/default.msi", URLs: map[string]string{"arm64": "https://example.test/arm.msi"}, } if got := src.URLFor("arm64"); got != "https://example.test/arm.msi" { t.Errorf("arm64 URL = %q", got) } if got := src.URLFor("amd64"); got != "https://example.test/default.msi" { t.Errorf("amd64 fallback URL = %q", got) } } func TestSourceSupportsArch(t *testing.T) { unrestricted := Source{} if !unrestricted.SupportsArch("riscv64") { t.Error("a source with no arch list should support every arch") } restricted := Source{Arch: []string{"amd64"}} if restricted.SupportsArch("arm64") { t.Error("arm64 should not match an amd64-only source") } if !restricted.SupportsArch("amd64") { t.Error("amd64 should match an amd64-only source") } }