package plugin_test import ( "context" "encoding/json" "net/http" "net/http/httptest" "reflect" "strings" "sync/atomic" "testing" "time" "github.com/Nwflower/reims-go/internal/plugin" ) func TestManifestRisks(t *testing.T) { cases := []struct { name string m plugin.Manifest tier plugin.Tier want []string }{ {"builtin reports nothing", plugin.Manifest{Permissions: []string{"vm:control"}}, plugin.TierBuiltin, nil}, {"plain theme", plugin.Manifest{Contributes: plugin.Contributes{Themes: []plugin.ThemeContribution{{ID: "t"}}}}, plugin.TierL1, []string{plugin.RiskUI}}, {"locale pack via l10n dir", plugin.Manifest{L10n: "l10n"}, plugin.TierL1, []string{plugin.RiskUI}}, { "sidecar implies native, background, network and files", plugin.Manifest{Permissions: []string{"host:clipboard"}}, plugin.TierL3, []string{plugin.RiskNative, plugin.RiskBackground, plugin.RiskNetwork, plugin.RiskFiles, plugin.RiskClipboard}, }, { "permissions map to categories in severity order", plugin.Manifest{Permissions: []string{"ui:slot", "input:filter", "net:api.example.com", "fs:shared", "vm:control"}}, plugin.TierL1, []string{plugin.RiskNetwork, plugin.RiskFiles, plugin.RiskVM, plugin.RiskInput, plugin.RiskUI}, }, {"low-risk permissions add nothing", plugin.Manifest{Permissions: []string{"vm:read", "fs:plugin_data", "ui:notify"}}, plugin.TierL1, nil}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { if got := plugin.ManifestRisks(tc.m, tc.tier); !reflect.DeepEqual(got, tc.want) { t.Fatalf("ManifestRisks = %v, want %v", got, tc.want) } }) } } func TestMarketEntryRisks(t *testing.T) { got := plugin.MarketEntryRisks(plugin.MarketEntry{Kind: "locale", Tier: plugin.TierL1}) if !reflect.DeepEqual(got, []string{plugin.RiskUI}) { t.Fatalf("locale entry risks = %v", got) } got = plugin.MarketEntryRisks(plugin.MarketEntry{Kind: "tool", Tier: plugin.TierL2, Permissions: []string{"net:x.example"}}) if !reflect.DeepEqual(got, []string{plugin.RiskBackground, plugin.RiskNetwork}) { t.Fatalf("script entry risks = %v", got) } } func TestMarketKindFacetsAndPeek(t *testing.T) { var hits atomic.Int32 entry := func(id, kind string) plugin.MarketEntry { return plugin.MarketEntry{ ID: id, Version: "1.0.0", Kind: kind, Channel: "stable", Name: plugin.LocalizedText{EnUS: id}, DownloadURL: "https://example.invalid/x.zip", SHA256: strings.Repeat("a", 64), } } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hits.Add(1) _ = json.NewEncoder(w).Encode(plugin.MarketIndex{ Schema: 1, Plugins: []plugin.MarketEntry{ entry("a.theme.one", "theme"), entry("a.theme.two", "theme"), entry("a.lang.one", "locale"), entry("a.tool.one", ""), }, }) })) defer srv.Close() layout := plugin.NewPluginsLayout(t.TempDir()) opts := plugin.MarketClientOptions{ Layout: layout, URLs: []string{srv.URL + "/index.json"}, CacheTTL: time.Minute, AllowInsecureLocalhost: true, } mc := plugin.NewMarketClient(opts) // Nothing cached yet: Peek serves the embedded fallback without a request. if res := mc.Peek(nil, plugin.MarketBrowseOptions{}); res.Source != "fallback" || hits.Load() != 0 { t.Fatalf("cold Peek = source %q, %d requests", res.Source, hits.Load()) } // Facets count the whole index even while a kind filter narrows items. res := mc.Browse(context.Background(), nil, plugin.MarketBrowseOptions{Kind: "theme"}) if len(res.Items) != 2 { t.Fatalf("filtered items = %d, want 2", len(res.Items)) } wantKinds := []plugin.MarketKindFacet{{Kind: "theme", Count: 2}, {Kind: "locale", Count: 1}} if !reflect.DeepEqual(res.Kinds, wantKinds) { t.Fatalf("kinds = %+v, want %+v", res.Kinds, wantKinds) } // A new client reads the disk cache in Peek, and its next Browse still // goes to the network because nothing was fetched in this session. before := hits.Load() mc2 := plugin.NewMarketClient(opts) if res := mc2.Peek(nil, plugin.MarketBrowseOptions{}); res.Source != "cache" || len(res.Items) != 4 || hits.Load() != before { t.Fatalf("warm Peek = source %q, %d items, %d new requests", res.Source, len(res.Items), hits.Load()-before) } if res := mc2.Browse(context.Background(), nil, plugin.MarketBrowseOptions{}); res.Source != "remote" { t.Fatalf("Browse after Peek = source %q, want remote", res.Source) } }