Files
Rusty ShacklefordandWillie caa107b880 fix(plugin): refresh stale OMP catalog and report blocked OMP updates (#853)
* fix(plugin): recover OMP stale catalog installations

Refresh owned marketplace catalogs before reuse and validate the native cache identity when removing an installation. Preserve that identity through interrupted cleanup and report blocked batch updates as failures so retries remain safe and visible.

* fix(plugin): preserve reviewed versions during OMP recovery

Keep native removal identity separate from reviewed receipts so interrupted cleanup can retry safely. Expose isolated failures in previews and dry-run exit status while allowing healthy targets to proceed.

* fix(plugin): resume interrupted OMP update cleanup

Retry matching pending removals before reinstalling after registry-first interruption, and clarify isolated failure reporting. Strengthen recovery fixtures and restore accurate installation guidance.

* test(plugin): model OMP cached catalog installs

Use the cached marketplace entry for native install version and source so the stale catalog regression exercises the real version-to-cache relationship.

* test(plugin): align OMP batch expectations with safety checks

Require catalog refresh before reinstalling from an owned marketplace and assert isolated failures when native OMP ownership cannot be verified, while healthy targets update.

* fix(plugin): skip native upgrade checks for OMP imports

Imported plugins have no reviewed source to upgrade, so requiring native upgrade capability makes a safe batch skip fail unnecessarily. Cover that case and OMP dry-run failures, and align translations and native verification runbook with the failure and removal behavior.

---------

Co-authored-by: Willie <egg358358@gmail.com>
2026-11-12 13:45:55 +08:00

129 lines
4.3 KiB
Go

package plugin
import (
"context"
"path/filepath"
"strconv"
"strings"
"testing"
)
func TestPluginHostStateDetachedAccountRetainsReceipt(t *testing.T) {
var commands []string
s := fakePiNpm(t, &commands)
s.Accounts = map[string]Account{"work": {Agent: "pi", Dir: t.TempDir()}}
applyPluginRequest(t, s, Request{Action: "add", Source: hostStatePiSource(t), Targets: []string{"work"}})
d := readState(t, s)
pack := d.intent["demo"]
delete(pack.Bindings, "work")
d.intent["demo"] = pack
if err := s.save(d); err != nil {
t.Fatal(err)
}
s.Accounts = nil
inv, err := s.Packages()
if err != nil {
t.Fatal(err)
}
b := inv.Packages["demo"].Bindings["work"]
if !b.Cleanup || !b.ContextChanged || b.ID == "" {
t.Fatalf("detached account lost cleanup evidence: %+v", b)
}
p, err := s.Preview(context.Background(), Request{Action: "sync"})
if err != nil {
t.Fatal(err)
}
if !p.Blocked {
t.Fatal("detached account could execute native cleanup")
}
request := Request{Action: "update", All: true}
p, err = s.Preview(context.Background(), request)
if err != nil || p.Blocked || len(p.Changes) != 1 || p.Changes[0].Action != "skip" || !p.Changes[0].Failed {
t.Fatalf("--all did not isolate the detached account: %+v %v", p, err)
}
same := unchanged(t, s.ConfigPath, s.statePath())
if _, err := s.Apply(context.Background(), request, p.Revision); err == nil {
t.Fatal("--all did not report the detached account failure")
}
same()
}
func TestPluginHostStateRebindChangedAccountBlocked(t *testing.T) {
for _, tc := range []struct{ kind, change string }{
{"source", "directory"}, {"source", "existing-id"}, {"source", "cli"},
{"npm", "directory"}, {"npm", "cli"}, {"npm", "filtered"},
{"import", "directory"}, {"import", "cli"}, {"import", "filtered"},
} {
t.Run(tc.kind+"/"+tc.change, func(t *testing.T) {
var commands []string
s := fakePiNpm(t, &commands)
originalAccount := Account{Agent: "pi", Dir: t.TempDir()}
s.Accounts = map[string]Account{"work": originalAccount}
r := Request{Action: "add", Source: "npm:demo", Targets: []string{"work"}}
if tc.kind == "source" || tc.kind == "npm" && tc.change == "filtered" {
r.Source = hostStatePiSource(t)
}
if tc.kind == "import" {
file, _ := s.piSettingsPath("work")
writePluginFile(t, filepath.Dir(file), filepath.Base(file), `{"packages":["npm:demo"]}`)
r = Request{Action: "import", From: "work", Plugin: "npm:demo", Name: "demo"}
}
applyPluginRequest(t, s, r)
if tc.kind == "npm" && tc.change == "filtered" {
d := readState(t, s)
pack := d.intent["demo"]
pack.Bindings["work"] = declaration{Source: "npm:demo"}
d.intent["demo"] = pack
if err := s.save(d); err != nil {
t.Fatal(err)
}
r.Source = "npm:demo"
}
changed := originalAccount
if tc.change == "cli" {
changed.CLI = "pi-fork"
} else {
changed.Dir = t.TempDir()
}
s.Accounts["work"] = changed
file, _ := s.piSettingsPath("work")
if tc.kind == "import" || tc.change == "existing-id" {
b := hostPackages(t, s)["demo"].Bindings["work"]
writePluginFile(t, filepath.Dir(file), filepath.Base(file), `{"packages":[`+strconv.Quote(b.ID)+`]}`)
}
if tc.change == "filtered" {
writePluginFile(t, filepath.Dir(file), filepath.Base(file), `{"packages":[{"source":"npm:demo","extensions":["-private.ts"]}]}`)
}
same := unchanged(t, s.ConfigPath, s.statePath())
before := len(commands)
p, err := s.Preview(context.Background(), r)
if err != nil {
t.Fatal(err)
}
if !p.Blocked {
// On dacbba6e this Apply proves the original applied store is overwritten.
if _, err := s.Apply(context.Background(), r, p.Revision); err != nil {
t.Fatal(err)
}
t.Fatal("changed account add/import was not blocked")
}
if !strings.Contains(p.Changes[0].Message, "Native account or scope changed") {
t.Fatalf("context guard ran after registration preparation: %+v", p.Changes)
}
if _, err := s.Apply(context.Background(), r, p.Revision); err == nil {
t.Fatal("applied blocked account change")
}
same()
if len(commands) != before {
t.Fatal("blocked account ran native command")
}
s.Accounts["work"] = originalAccount
if tc.change == "filtered" && tc.kind == "npm" {
applyPluginRequest(t, s, Request{Action: "sync", Name: "demo"})
} else {
applyPluginRequest(t, s, r)
}
})
}
}