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 test cold disk-cache Peek and post-failure // stale consistency: neither cold disk cache nor a failed refresh may ever // claim official trust or silently rebound to fresh within the cache TTL. plugin.NewMarketClient(opts).Browse(context.Background(), nil, plugin.MarketBrowseOptions{}) opts.URLs = []string{srv.URL + "/missing-index.json"} staleClient := plugin.NewMarketClient(opts) // Cold disk cache Peek (before network fetch in this session) must not vouch. if coldPeek := staleClient.Peek(nil, plugin.MarketBrowseOptions{}); len(coldPeek.Items) != 1 || coldPeek.Items[0].Entry.Trust != plugin.TrustCommunity { t.Fatalf("cold disk-cache Peek must show community trust, got items=%+v", coldPeek.Items) } browse := staleClient.Browse(context.Background(), nil, plugin.MarketBrowseOptions{}) if !browse.Stale || browse.FetchError == "" || len(browse.Items) != 1 || browse.Items[0].Entry.Trust != plugin.TrustCommunity { t.Fatalf("stale browse must show community trust, got stale=%v err=%q items=%+v", browse.Stale, browse.FetchError, browse.Items) } // Follow-up non-forced Browse within TTL (e.g. search/category filter) must stay stale + community. followUp := staleClient.Browse(context.Background(), nil, plugin.MarketBrowseOptions{Query: id}) if !followUp.Stale || followUp.FetchError == "" || len(followUp.Items) != 1 || followUp.Items[0].Entry.Trust != plugin.TrustCommunity { t.Fatalf("follow-up Browse after failure must stay stale/community, got stale=%v err=%q items=%+v", followUp.Stale, followUp.FetchError, followUp.Items) } if peek := staleClient.Peek(nil, plugin.MarketBrowseOptions{}); !peek.Stale || len(peek.Items) != 1 || peek.Items[0].Entry.Trust != plugin.TrustCommunity { t.Fatalf("peek after stale browse must keep community trust, got stale=%v items=%+v", peek.Stale, peek.Items) } info, err := staleClient.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) } // Online success followed by a failed force-refresh must also stay stale/community on subsequent Browse. var failRemote bool var switchSrv *httptest.Server _, indexFn := marketFixture(t, id, plugin.TrustOfficial, nil) switchSrv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if failRemote { http.Error(w, "down", http.StatusServiceUnavailable) return } _ = json.NewEncoder(w).Encode(indexFn()) })) t.Cleanup(switchSrv.Close) liveClient := plugin.NewMarketClient(plugin.MarketClientOptions{ Layout: mgr.Layout(), HostVersion: "0.2.10", URLs: []string{switchSrv.URL + "/index.json"}, CacheTTL: time.Minute, AllowInsecureLocalhost: true, FirstParty: true, }) if okRes := liveClient.Browse(context.Background(), nil, plugin.MarketBrowseOptions{}); okRes.Stale || okRes.Items[0].Entry.Trust != plugin.TrustOfficial { t.Fatalf("initial live browse want official/fresh, got stale=%v trust=%q", okRes.Stale, okRes.Items[0].Entry.Trust) } failRemote = true if failRes := liveClient.Browse(context.Background(), nil, plugin.MarketBrowseOptions{ForceRefresh: true}); !failRes.Stale || failRes.Items[0].Entry.Trust != plugin.TrustCommunity { t.Fatalf("failed force-refresh want stale/community, got stale=%v trust=%q", failRes.Stale, failRes.Items[0].Entry.Trust) } if afterFail := liveClient.Browse(context.Background(), nil, plugin.MarketBrowseOptions{}); !afterFail.Stale || afterFail.Items[0].Entry.Trust != plugin.TrustCommunity { t.Fatalf("non-forced Browse after failed refresh rebounded: stale=%v trust=%q", afterFail.Stale, afterFail.Items[0].Entry.Trust) } // Once remote recovers, the next non-forced Browse must retry and restore official trust immediately. failRemote = false if recovered := liveClient.Browse(context.Background(), nil, plugin.MarketBrowseOptions{}); recovered.Stale || recovered.Source != "remote" || recovered.Items[0].Entry.Trust != plugin.TrustOfficial { t.Fatalf("non-forced Browse after recovery stayed stale: source=%q stale=%v trust=%q", recovered.Source, recovered.Stale, recovered.Items[0].Entry.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) } }