package plugin_test import ( "context" "crypto/sha256" "encoding/hex" "encoding/json" "os" "path/filepath" "strings" "testing" "github.com/Nwflower/reims-go/internal/plugin" ) func sha256HexBytes(b []byte) string { sum := sha256.Sum256(b) return hex.EncodeToString(sum[:]) } func TestExternalThemePersistsAcrossRestartAndUpgrade(t *testing.T) { dataRoot := t.TempDir() ctx := context.Background() buildThemeZip := func(ver, css string) string { man, _ := json.Marshal(plugin.Manifest{ Schema: 1, ID: "test.theme.persist", Version: ver, Name: plugin.LocalizedText{EnUS: "Persist Theme"}, Publisher: "tester", License: "MIT", Engines: plugin.Engines{PluginAPI: "^1.0.0", UIAPI: "^2.0.0"}, Platforms: []string{"any"}, Contributes: plugin.Contributes{ Themes: []plugin.ThemeContribution{{ ID: "test.theme.persist", Name: plugin.LocalizedText{EnUS: "Persist Theme"}, Stylesheet: "theme.css", }}, }, }) return makePluginZip(t, map[string][]byte{ "plugin.json": man, "theme.css": []byte(css), }, nil, false) } openMgr := func(safe bool) *plugin.Manager { mgr, err := plugin.NewManager(plugin.ManagerOptions{ DataRoot: dataRoot, HostVersion: "0.2.11", SafeMode: safe, }) if err != nil { t.Fatalf("NewManager: %v", err) } if err := mgr.LoadExternal(); err != nil { t.Fatalf("LoadExternal: %v", err) } mgr.Registry().Reconcile(ctx) mgr.MarkBootComplete() return mgr } mgr1 := openMgr(false) v1Zip := buildThemeZip("1.0.1", ".rg-p-sidebar { background: #112233; }") if _, err := mgr1.InstallLocalArchive(ctx, v1Zip, plugin.PackageInstallOptions{}); err != nil { t.Fatalf("install v1: %v", err) } if err := mgr1.Enable(ctx, "test.theme.persist", false); err != nil { t.Fatalf("enable v1: %v", err) } if err := mgr1.SetActiveTheme("test.theme.persist", "ocean"); err != nil { t.Fatalf("SetActiveTheme: %v", err) } if got := mgr1.ActiveThemePayload(); got.ThemeID != "test.theme.persist" || got.ActiveScheme != "ocean" { t.Fatalf("active theme before restart = %+v", got) } mgr1.Close() // 1. Restart: external theme and active scheme must persist after LoadExternal + Reconcile. mgr2 := openMgr(false) defer mgr2.Close() afterRestart := mgr2.ActiveThemePayload() if afterRestart.ThemeID != "test.theme.persist" || afterRestart.ActiveScheme != "ocean" || !strings.Contains(afterRestart.CSS, "#112233") { t.Fatalf("active theme lost across restart: %+v", afterRestart) } // 2. Upgrade (no new high-risk permissions): active theme must survive and pick up v1.0.2 CSS. v2Zip := buildThemeZip("1.0.2", ".rg-p-sidebar { background: #445566; }") if _, err := mgr2.InstallLocalArchive(ctx, v2Zip, plugin.PackageInstallOptions{}); err != nil { t.Fatalf("upgrade v2: %v", err) } afterUpgrade := mgr2.ActiveThemePayload() if afterUpgrade.ThemeID != "test.theme.persist" || !strings.Contains(afterUpgrade.CSS, "#445566") { t.Fatalf("active theme lost or stale after upgrade: %+v", afterUpgrade) } } func TestInteractiveThemeUIScriptHashPinningAndUpgradeReapproval(t *testing.T) { dataRoot := t.TempDir() ctx := context.Background() buildInteractiveZip := func(ver string, script []byte) string { man, _ := json.Marshal(plugin.Manifest{ Schema: 1, ID: "test.theme.studio", Version: ver, Name: plugin.LocalizedText{EnUS: "Studio Theme"}, Publisher: "tester", License: "MIT", Engines: plugin.Engines{PluginAPI: "^1.0.0", UIAPI: "^2.0.0"}, Platforms: []string{"any"}, Permissions: []string{"ui:script"}, Contributes: plugin.Contributes{ Themes: []plugin.ThemeContribution{{ ID: "test.theme.studio", Name: plugin.LocalizedText{EnUS: "Studio Theme"}, Stylesheet: "theme.css", UIScript: "studio.js", }}, }, }) return makePluginZip(t, map[string][]byte{ "plugin.json": man, "theme.css": []byte(".rg-p-shell { color: var(--text-primary); }"), "studio.js": script, }, nil, false) } scriptV1 := []byte("sdk.on('view.changed', () => {}); // v1\n") scriptV2 := []byte("sdk.on('view.changed', () => {}); // v2 modified\n") hashV1 := sha256HexBytes(scriptV1) hashV2 := sha256HexBytes(scriptV2) mgr1, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: dataRoot, HostVersion: "0.2.11"}) if err != nil { t.Fatalf("NewManager: %v", err) } if err := mgr1.LoadExternal(); err != nil { t.Fatalf("LoadExternal: %v", err) } mgr1.Registry().Reconcile(ctx) mgr1.MarkBootComplete() info1, err := mgr1.InstallLocalArchive(ctx, buildInteractiveZip("1.0.1", scriptV1), plugin.PackageInstallOptions{}) if err != nil { t.Fatalf("install v1: %v", err) } if info1.EntryPath == "" || info1.EntryHash != hashV1 || !info1.NeedsApproval { t.Fatalf("v1 entry metadata wrong: path=%q hash=%q (want %q) needsApproval=%v", info1.EntryPath, info1.EntryHash, hashV1, info1.NeedsApproval) } if err := mgr1.Enable(ctx, "test.theme.studio", false); err == nil { t.Fatal("enabling ui:script plugin without explicit approval succeeded") } if err := mgr1.Enable(ctx, "test.theme.studio", true); err != nil { t.Fatalf("enable v1 with approval: %v", err) } if err := mgr1.SetActiveTheme("test.theme.studio", ""); err != nil { t.Fatalf("SetActiveTheme: %v", err) } p1 := mgr1.ActiveThemePayload() if p1.ThemeID != "test.theme.studio" || !strings.Contains(p1.UIScriptURL, "v=1.0.1") || !strings.Contains(p1.UIScriptURL, "h="+hashV1[:12]) { t.Fatalf("unexpected v1 active payload: %+v", p1) } mgr1.Close() // Restart with unchanged script stays approved and active. mgr2, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: dataRoot, HostVersion: "0.2.11"}) if err != nil { t.Fatalf("NewManager restart: %v", err) } defer mgr2.Close() if err := mgr2.LoadExternal(); err != nil { t.Fatalf("LoadExternal restart: %v", err) } mgr2.Registry().Reconcile(ctx) mgr2.MarkBootComplete() pRestart := mgr2.ActiveThemePayload() if pRestart.ThemeID != "test.theme.studio" || pRestart.UIScriptURL == "" { t.Fatalf("interactive theme lost on restart: %+v", pRestart) } // Upgrade with modified studio.js MUST pause for re-approval and fall back safely. info2, err := mgr2.InstallLocalArchive(ctx, buildInteractiveZip("1.0.2", scriptV2), plugin.PackageInstallOptions{}) if err != nil { t.Fatalf("upgrade v2: %v", err) } if !info2.NeedsApproval || info2.Enabled || info2.EntryHash != hashV2 { t.Fatalf("modified uiScript upgrade must require re-approval: enabled=%v needsApproval=%v hash=%q want=%q", info2.Enabled, info2.NeedsApproval, info2.EntryHash, hashV2) } if pPaused := mgr2.ActiveThemePayload(); pPaused.ThemeID != "builtin.theme.default" || pPaused.UIScriptURL != "" { t.Fatalf("paused interactive theme should fall back to default: %+v", pPaused) } // Re-approving v1.0.2 restores the active theme and busts the script URL cache. if err := mgr2.Enable(ctx, "test.theme.studio", true); err != nil { t.Fatalf("re-approve v2: %v", err) } p2 := mgr2.ActiveThemePayload() if p2.ThemeID != "test.theme.studio" || !strings.Contains(p2.UIScriptURL, "v=1.0.2") || !strings.Contains(p2.UIScriptURL, "h="+hashV2[:12]) { t.Fatalf("re-approved interactive theme not restored or URL not cache-busted: %+v", p2) } if p2.UIScriptURL == p1.UIScriptURL { t.Fatalf("expected updated UIScriptURL after script change, both were %q", p2.UIScriptURL) } } func TestDisableAndRemoveActiveThemeSemantics(t *testing.T) { dataRoot := t.TempDir() ctx := context.Background() themeMan, _ := json.Marshal(plugin.Manifest{ Schema: 1, ID: "test.theme.pack", Version: "1.0.0", Name: plugin.LocalizedText{EnUS: "Theme Pack"}, Publisher: "tester", License: "MIT", Contributes: plugin.Contributes{ Themes: []plugin.ThemeContribution{{ ID: "test.theme.pack.dark", Name: plugin.LocalizedText{EnUS: "Dark"}, Stylesheet: "theme.css", }}, }, }) themeZip := makePluginZip(t, map[string][]byte{ "plugin.json": themeMan, "theme.css": []byte(".rg-p-sidebar { background: #101010; }"), }, nil, false) otherMan, _ := json.Marshal(plugin.Manifest{ Schema: 1, ID: "test.other.noop", Version: "1.0.0", Name: plugin.LocalizedText{EnUS: "Other Plugin"}, Publisher: "tester", License: "MIT", }) otherZip := makePluginZip(t, map[string][]byte{"plugin.json": otherMan}, nil, false) openMgr := func(safe bool) *plugin.Manager { mgr, err := plugin.NewManager(plugin.ManagerOptions{ DataRoot: dataRoot, HostVersion: "0.2.11", SafeMode: safe, }) if err != nil { t.Fatalf("NewManager(safe=%v): %v", safe, err) } if err := mgr.LoadExternal(); err != nil { t.Fatalf("LoadExternal(safe=%v): %v", safe, err) } mgr.Registry().Reconcile(ctx) mgr.MarkBootComplete() return mgr } mgr := openMgr(false) if _, err := mgr.InstallLocalArchive(ctx, themeZip, plugin.PackageInstallOptions{}); err != nil { t.Fatalf("install theme: %v", err) } if _, err := mgr.InstallLocalArchive(ctx, otherZip, plugin.PackageInstallOptions{}); err != nil { t.Fatalf("install other: %v", err) } if err := mgr.Enable(ctx, "test.theme.pack", false); err != nil { t.Fatalf("enable theme: %v", err) } if err := mgr.Enable(ctx, "test.other.noop", false); err != nil { t.Fatalf("enable other: %v", err) } if err := mgr.SetActiveTheme("test.theme.pack.dark", "warm"); err != nil { t.Fatalf("SetActiveTheme: %v", err) } mgr.Close() // 1. Disabling an unrelated plugin in safeMode (as CLI does) MUST NOT wipe ActiveThemeID. safeMgr := openMgr(true) if err := safeMgr.Disable(ctx, "test.other.noop"); err != nil { t.Fatalf("safe Disable unrelated: %v", err) } safeMgr.Close() st, err := plugin.NewStateStore(plugin.NewPluginsLayout(dataRoot).Root).Load() if err != nil { t.Fatalf("load state: %v", err) } if st.ActiveThemeID != "test.theme.pack.dark" || st.ActiveSchemeID != "warm" { t.Fatalf("disabling unrelated plugin in safeMode wiped active theme: theme=%q scheme=%q", st.ActiveThemeID, st.ActiveSchemeID) } // 2. Disabling the theme plugin in safeMode clears BOTH ActiveThemeID and ActiveSchemeID. safeMgr2 := openMgr(true) if err := safeMgr2.Disable(ctx, "test.theme.pack"); err != nil { t.Fatalf("safe Disable theme: %v", err) } safeMgr2.Close() st, err = plugin.NewStateStore(plugin.NewPluginsLayout(dataRoot).Root).Load() if err != nil { t.Fatalf("load state after disable: %v", err) } if st.ActiveThemeID != "" || st.ActiveSchemeID != "" { t.Fatalf("disabling active theme plugin left stale state: theme=%q scheme=%q", st.ActiveThemeID, st.ActiveSchemeID) } // 3. Removing the active theme plugin clears BOTH ActiveThemeID and ActiveSchemeID. mgr3 := openMgr(false) defer mgr3.Close() if err := mgr3.Enable(ctx, "test.theme.pack", false); err != nil { t.Fatalf("re-enable theme: %v", err) } if err := mgr3.SetActiveTheme("test.theme.pack.dark", "warm"); err != nil { t.Fatalf("re-activate theme: %v", err) } if err := mgr3.Remove(ctx, "test.theme.pack", true); err != nil { t.Fatalf("remove theme: %v", err) } st, err = plugin.NewStateStore(plugin.NewPluginsLayout(dataRoot).Root).Load() if err != nil { t.Fatalf("load state after remove: %v", err) } if st.ActiveThemeID != "" || st.ActiveThemePluginID != "" || st.ActiveSchemeID != "" { t.Fatalf("removing active theme plugin left stale state: theme=%q plugin=%q scheme=%q", st.ActiveThemeID, st.ActiveThemePluginID, st.ActiveSchemeID) } // 4. Removing an orphaned theme plugin (whose themeID != pluginID and whose // package directory vanished on disk) must still clear active theme state. if _, err := mgr3.InstallLocalArchive(ctx, themeZip, plugin.PackageInstallOptions{}); err != nil { t.Fatalf("re-install theme: %v", err) } if err := mgr3.Enable(ctx, "test.theme.pack", false); err != nil { t.Fatalf("re-enable theme for orphan test: %v", err) } if err := mgr3.SetActiveTheme("test.theme.pack.dark", "midnight"); err != nil { t.Fatalf("activate theme for orphan test: %v", err) } mgr3.Close() // Delete package files to turn test.theme.pack into an orphaned state.json record. if err := os.RemoveAll(filepath.Join(plugin.NewPluginsLayout(dataRoot).PkgsDir(), "test.theme.pack")); err != nil { t.Fatalf("remove pkg dir: %v", err) } orphanMgr := openMgr(true) defer orphanMgr.Close() if err := orphanMgr.Remove(ctx, "test.theme.pack", true); err != nil { t.Fatalf("remove orphaned theme plugin: %v", err) } st, err = plugin.NewStateStore(plugin.NewPluginsLayout(dataRoot).Root).Load() if err != nil { t.Fatalf("load state after orphan remove: %v", err) } if st.ActiveThemeID != "" || st.ActiveThemePluginID != "" || st.ActiveSchemeID != "" { t.Fatalf("removing orphaned theme plugin left stale state: theme=%q plugin=%q scheme=%q", st.ActiveThemeID, st.ActiveThemePluginID, st.ActiveSchemeID) } // 5. Removing an orphaned legacy plugin whose ID happens to be a prefix of // another live plugin's active theme ID MUST NOT clear that live theme. prefixMan, _ := json.Marshal(plugin.Manifest{ Schema: 1, ID: "test.theme.pack.dark", Version: "1.0.0", Name: plugin.LocalizedText{EnUS: "Live Dark Theme"}, Publisher: "tester", License: "MIT", Contributes: plugin.Contributes{ Themes: []plugin.ThemeContribution{{ ID: "test.theme.pack.dark", Name: plugin.LocalizedText{EnUS: "Live Dark"}, Stylesheet: "theme.css", }}, }, }) liveDarkZip := makePluginZip(t, map[string][]byte{ "plugin.json": prefixMan, "theme.css": []byte(".rg-p-sidebar { background: #181818; }"), }, nil, false) orphanMgr.Close() mgr5 := openMgr(false) if _, err := mgr5.InstallLocalArchive(ctx, liveDarkZip, plugin.PackageInstallOptions{}); err != nil { t.Fatalf("install live dark theme: %v", err) } if err := mgr5.Enable(ctx, "test.theme.pack.dark", false); err != nil { t.Fatalf("enable live dark theme: %v", err) } if err := mgr5.SetActiveTheme("test.theme.pack.dark", "carbon"); err != nil { t.Fatalf("activate live dark theme: %v", err) } mgr5.Close() // Inject a legacy state.json (no active_theme_plugin_id) with an orphaned prefix plugin "test.theme.pack". store := plugin.NewStateStore(plugin.NewPluginsLayout(dataRoot).Root) st, err = store.Load() if err != nil { t.Fatalf("load state for prefix collision test: %v", err) } st.ActiveThemePluginID = "" st.Plugins["test.theme.pack"] = &plugin.PluginRecord{ ID: "test.theme.pack", Version: "1.0.0", Source: plugin.SourceLocal, } if err := store.Save(st); err != nil { t.Fatalf("save legacy state: %v", err) } mgr6 := openMgr(true) defer mgr6.Close() if err := mgr6.Remove(ctx, "test.theme.pack", true); err != nil { t.Fatalf("remove orphaned prefix plugin: %v", err) } st, err = store.Load() if err != nil { t.Fatalf("reload state after prefix orphan remove: %v", err) } if st.ActiveThemeID != "test.theme.pack.dark" || st.ActiveSchemeID != "carbon" { t.Fatalf("removing unrelated orphaned prefix plugin wiped live theme: theme=%q scheme=%q", st.ActiveThemeID, st.ActiveSchemeID) } }