Files
Reims-Go/internal/plugin/trust_test.go
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

154 lines
5.3 KiB
Go

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