Files
Reims-Go/internal/plugin/risks_test.go
T
NwflowerandClaude Opus 5.5 e259833b5f feat(plugins): rework the plugin market and first-run language packs
Market page
- Card grid that fills the width; cards show only icon, name,
  publisher (trust mark by color and icon), version and the action.
- Paints from cache via the new PeekPluginMarket binding, then revalidates;
  cards reveal 24 at a time as the feed scrolls.
- Category chips come from the index (MarketBrowseResult.kinds) instead of
  a fixed list; the alpha toggle and source badge are gone.
- Installed/Market is a segmented control; refresh now shows feedback.
- WordmarkLoader writes the Reims-Go wordmark stroke by stroke while the
  market loads (static pulse under reduced motion).

Install and enable
- reims:plugin-install events drive a progress bar on the card.
- After install, a dialog offers to enable the plugin and lists what it can
  do in plain words (PluginInfo.risks / MarketCatalogItem.risks, derived
  from tier, permissions and contributions in risks.go).
- Enabling a plugin that provides UI languages switches to the first one
  unless the current language is among them (PluginInfo.locales).

Trust
- A package installed from the project's own index, fetched over HTTPS in
  this session, inherits the index's trust level when its SHA-256 matches.
  Third-party indexes (REIMS_GO_PLUGIN_MARKET_URL) cannot vouch and show
  every entry as community.
- Trust no longer waives review: official packages get the same scan,
  high-risk approval and L3 hash check as any other; only builtin and
  bundled plugins are pre-approved.

First launch
- When the OS language is not covered by zh-CN/en-US, the folder screen
  looks up a matching pack once (SuggestLanguagePack) and offers to install
  it; Continue installs and enables it so the UI follows the OS language.
  Before setup the market keeps its index in memory only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 05:17:45 -07:00

115 lines
4.3 KiB
Go

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)
}
}