package plugin_test import ( "context" "encoding/json" "net/http" "net/http/httptest" "os" "testing" "time" "github.com/Nwflower/reims-go/internal/fetch" "github.com/Nwflower/reims-go/internal/plugin" ) // marketFixture serves one plugin package and an index that lists it with the // given trust level and permissions. func marketFixture(t *testing.T, id string, trust plugin.TrustLevel, perms []string) (*httptest.Server, func() plugin.MarketIndex) { t.Helper() manifest, _ := json.Marshal(plugin.Manifest{ Schema: 1, ID: id, Version: "1.0.0", Name: plugin.LocalizedText{EnUS: id}, Publisher: "Reims-Go", License: "MIT", Engines: plugin.Engines{PluginAPI: "^1.0.0"}, Permissions: perms, Contributes: plugin.Contributes{Themes: []plugin.ThemeContribution{{ ID: id, Name: plugin.LocalizedText{EnUS: "T"}, Stylesheet: "theme.css", }}}, }) zipPath := makePluginZip(t, map[string][]byte{ "plugin.json": manifest, "theme.css": []byte(`.rg-p-sidebar { background: var(--surface-sidebar); }`), }, nil, false) zipBytes, _ := os.ReadFile(zipPath) sum, _ := fetch.FileSHA256(zipPath) var srv *httptest.Server index := func() plugin.MarketIndex { return plugin.MarketIndex{Schema: 1, Plugins: []plugin.MarketEntry{{ ID: id, Version: "1.0.0", Channel: "stable", Trust: trust, Kind: "theme", Name: plugin.LocalizedText{EnUS: id}, Permissions: perms, DownloadURL: srv.URL + "/pkg.zip", SHA256: sum, SizeBytes: int64(len(zipBytes)), }}} } srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/pkg.zip": _, _ = w.Write(zipBytes) case "/index.json": _ = json.NewEncoder(w).Encode(index()) default: http.NotFound(w, r) } })) t.Cleanup(srv.Close) return srv, index } func newTrustTestManager(t *testing.T) *plugin.Manager { t.Helper() mgr, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: t.TempDir(), HostVersion: "0.2.10"}) if err != nil { t.Fatal(err) } if err := mgr.LoadExternal(); err != nil { t.Fatal(err) } t.Cleanup(mgr.Close) return mgr } func TestFirstPartyIndexVouchesForInstalls(t *testing.T) { const id = "official.theme.calm" srv, _ := marketFixture(t, id, plugin.TrustOfficial, nil) install := func(firstParty bool) plugin.TrustLevel { mgr := newTrustTestManager(t) mc := plugin.NewMarketClient(plugin.MarketClientOptions{ Layout: mgr.Layout(), HostVersion: "0.2.10", URLs: []string{srv.URL + "/index.json"}, CacheTTL: time.Minute, AllowInsecureLocalhost: true, FirstParty: firstParty, }) browse := mc.Browse(context.Background(), nil, plugin.MarketBrowseOptions{}) if len(browse.Items) != 1 { t.Fatalf("browse items = %d", len(browse.Items)) } info, err := mc.InstallFromMarket(context.Background(), mgr, id, plugin.MarketInstallOptions{}) if err != nil { t.Fatalf("install: %v", err) } // The card must promise what the install delivers. if shown := browse.Items[0].Entry.Trust; shown != info.Trust { t.Fatalf("market shows %q but install got %q", shown, info.Trust) } return info.Trust } if got := install(true); got != plugin.TrustOfficial { t.Fatalf("first-party index install trust = %q, want official", got) } if got := install(false); got != plugin.TrustCommunity { t.Fatalf("third-party index install trust = %q, want community", got) } } func TestStaleIndexDoesNotVouch(t *testing.T) { const id = "official.theme.stale" srv, _ := marketFixture(t, id, plugin.TrustOfficial, nil) mgr := newTrustTestManager(t) opts := plugin.MarketClientOptions{ Layout: mgr.Layout(), HostVersion: "0.2.10", URLs: []string{srv.URL + "/index.json"}, CacheTTL: time.Minute, AllowInsecureLocalhost: true, FirstParty: true, } // Seed the disk cache, then point a fresh client at a dead index URL: the // package still downloads, but only the stale cache lists it. plugin.NewMarketClient(opts).Browse(context.Background(), nil, plugin.MarketBrowseOptions{}) opts.URLs = []string{srv.URL + "/missing-index.json"} info, err := plugin.NewMarketClient(opts).InstallFromMarket(context.Background(), mgr, id, plugin.MarketInstallOptions{}) if err != nil { t.Fatalf("install: %v", err) } if info.Trust != plugin.TrustCommunity { t.Fatalf("install from stale cache trust = %q, want community", info.Trust) } } func TestOfficialPluginsStillNeedApproval(t *testing.T) { const id = "official.theme.clip" srv, _ := marketFixture(t, id, plugin.TrustOfficial, []string{"host:clipboard"}) mgr := newTrustTestManager(t) mc := plugin.NewMarketClient(plugin.MarketClientOptions{ Layout: mgr.Layout(), HostVersion: "0.2.10", URLs: []string{srv.URL + "/index.json"}, CacheTTL: time.Minute, AllowInsecureLocalhost: true, FirstParty: true, }) info, err := mc.InstallFromMarket(context.Background(), mgr, id, plugin.MarketInstallOptions{}) if err != nil { t.Fatalf("install: %v", err) } if info.Trust != plugin.TrustOfficial || !info.NeedsApproval { t.Fatalf("official plugin with high-risk permission: trust=%q needs_approval=%v, want official/true", info.Trust, info.NeedsApproval) } if err := mgr.Enable(context.Background(), id, false); err == nil { t.Fatal("enabling without approval succeeded for an official high-risk plugin") } if err := mgr.Enable(context.Background(), id, true); err != nil { t.Fatalf("enable with approval: %v", err) } }