Files
magpie/internal/plugin/settle_test.go
T
yetone b790594074 plugin: Settle waits for the sign-in hook, so TestPluginModelsSayExpired no longer writes into its removed folder (#1935, Nxir; #1543)
The cause: a host's "signIn" event (a models hook saying an account's
sign-in expired) ran provider's OnSignIn hook with a bare go f(...). That
hook writes the account's lapse into logins.json (lastgood.Keep, then the
file, then Changed) under .config/magpie. TestPluginModelsSayExpired
returns as soon as the lapse reads back, and Settle, its cleanup, waited
for refreshes and the change hooks (Told) but not this one, so the rest
of its write could land while TempDir removed the folder: "unlinkat
.config/magpie: directory not empty", seen under load on #1543 and on
Nxir's CI run.

The fix: the sign-in hook runs through the same counted tell() as the
change hooks, so Told, and so Settle, waits for it. Settle then waits for
refreshes and restarts once more, since a hook told before the providers
were read reads them, which can start a host again.

Reproduced by injecting a writer into the hook (300 files in
.config/magpie, 1ms apart, after notePluginSaid): on the old code
TestPluginModelsSayExpired -count=3 failed with the reported "directory
not empty"; with the fix it passed 3 of 3. The injection was removed.
New TestSettleWaitsForTheSignInHook (synctest, as its sibling for change
hooks) goes red with the bare go f(...) put back.

Run: go test -tags nogui ./internal/plugin/ ./internal/provider/
-count=1 in a sandbox HOME (both ok), TestSettle* -race -count=3, gofmt -l,
GOOS=linux and windows go vet ./internal/plugin/.
2026-10-13 15:48:53 +08:00

119 lines
3.5 KiB
Go

package plugin
import (
"sync"
"sync/atomic"
"testing"
"testing/synctest"
)
// watched, while set, is called with each change told: the change hook
// watchChanges adds calls it.
var (
watched atomic.Pointer[func()]
addWatching sync.Once
)
// watchChanges has f called with each change told (OnChange) until the
// test ends.
func watchChanges(t *testing.T, f func()) {
addWatching.Do(func() {
OnChange(func() {
if f := watched.Load(); f != nil {
(*f)()
}
})
})
watched.Store(&f)
t.Cleanup(func() { watched.Store(nil) })
}
// A test ends with Settle, and Settle returns once the hooks its restart
// told have run. Told in the background, with nothing waiting for it,
// provider.Changed landed in the next test and dropped what that test
// held: its settings were read again while held.
func TestSettleWaitsForTheHooksItTold(t *testing.T) {
storeSandbox(t)
Settle() // no host of an earlier test's, nor anything it told, still running
synctest.Test(t, func(t *testing.T) {
release := make(chan struct{})
watchChanges(t, func() { <-release })
settled := make(chan struct{})
go func() {
Settle()
close(settled)
}()
synctest.Wait() // the hook waits for its release
select {
case <-settled:
t.Error("Settle returned while a hook its restart told was still running: what the hook does lands in the next test")
default:
}
close(release)
<-settled
})
}
// What a host said of an account's sign-in is written before Settle
// returns (#1935, #1543): provider's OnSignIn hook writes the account's
// lapse into logins.json, and run in the background with nothing waiting
// for it, it wrote under a test's folder as the folder was removed
// ("unlinkat .config/magpie: directory not empty").
func TestSettleWaitsForTheSignInHook(t *testing.T) {
storeSandbox(t)
Settle()
t.Cleanup(func() { OnSignIn(nil) })
synctest.Test(t, func(t *testing.T) {
release := make(chan struct{})
var said atomic.Pointer[string]
OnSignIn(func(provider, account, s string) {
<-release
said.Store(&s)
})
(&host{}).dispatch(message{Event: "signIn", Provider: "fakeco", Account: "k", Said: "expired"})
settled := make(chan struct{})
go func() {
Settle()
close(settled)
}()
synctest.Wait()
select {
case <-settled:
t.Error("Settle returned while the sign-in hook was still running: it writes into a folder going away")
default:
}
close(release)
<-settled
if p := said.Load(); p == nil || *p != "expired" {
t.Errorf("the hook wasn't told: %v", p)
}
})
}
// The next test's plugins are its own. After Settle, the plugins' list in
// the next test's folder read as changed (checkList), the list the last
// test's folder had being the one seen last: the change was told there,
// in the background, and dropped what that test held.
func TestSettleForgetsTheFoldersPlugins(t *testing.T) {
t.Setenv("MAGPIE_BUN", "") // no Bun, nor a host: nothing asks the plugins here
storeSandbox(t)
t.Cleanup(Settle)
spec := "opencode-fake-auth"
writeList(t, `{"plugins":[{"spec":"`+spec+`"}]}`)
UseCached([]Provider{{ID: "fakeco", Spec: spec, Name: "FakeCo"}})
Settle()
storeSandbox(t) // the next test's folder, the same plugin listed in it
writeList(t, `{"plugins": [{"spec": "`+spec+`"}]}`)
var told atomic.Int32
watchChanges(t, func() { told.Add(1) })
ps := Cached()
Told()
if n := told.Load(); n != 0 {
t.Errorf("the next test's plugins read as changed: %d changes told in it", n)
}
if len(ps) != 0 {
t.Errorf("the next test's plugins answered with the last test's providers: %+v", ps)
}
}