Files
magpie/internal/plugin/settle_test.go
T
wei 920d96af55 tests: provider, plugin, gateway, codexcat, gui and tui tests pass in any order and on any -count (#1146)
Squashed from wei's #1146, onto main. Each test now starts from state of
its own instead of what an earlier test (or an earlier -count run) left:

- plugin: changed() counts the OnChange hooks still running; Told waits
  for them, and Settle ends with Told and UseCached(nil), so a change one
  test made isn't told in the next and the next test's plugin list isn't
  read as changed. Only tests call Settle/Told; in production changed()
  only counts. New TestSettleWaitsForTheHooksItTold and
  TestSettleForgetsTheFoldersPlugins.
- provider: isolate forgets Copilot's state, the plans' quota cards and
  balances; the warm-up, Cursor sign-in, hold and moved-plugin tests wait
  for what they started.
- gateway: fresh() forgets refused seals, and TestRouting,
  TestSmartRouting and TestQuotaRestsUntilItsReset start from
  forgetRouting; codexSignedIn and TestForeignReasoningIsLeftOut start
  from fresh.
- codexcat: XDG_CONFIG_HOME under the test's home. tui: costAt reset.
  gui: the fake Cursor account no longer outlives its test. cli: Settle.

Left out as already on main: the cli_identity keepIdentities switch
(9c3772311 keeps the asks in a sandbox via testenv's marker) and the
omarchy reset (main's TestCurrent already resets cached/seen/asked).

Run (darwin, loaded machine):
- On main 043e0cfc7, -count=3: TestRouting, TestSmartRouting,
  TestQuotaRestsUntilItsReset, TestForeignReasoningIsLeftOut,
  TestStandaloneNativeFallback fail; codexcat and tui fail; provider
  -shuffle=1791359851244745000 fails TestCopilotAuto and
  TestCopilotSignAndModels, and -count=2 -shuffle=1791856220860256000
  -run 'Copilot|TestPlanQuotas|TestCachedCardsAskNobody' fails
  TestPlanQuotas and TestCachedCardsAskNobody. With this change, that
  provider run (-shuffle=on, -count=2), the same gateway tests,
  ./internal/codexcat, ./internal/tui and the Settle/Hold/warm-up tests
  in plugin and provider pass -count=3.
- Breaking Settle by hand: without Told(),
  TestSettleWaitsForTheHooksItTold fails; without UseCached(nil),
  TestSettleForgetsTheFoldersPlugins fails.
- gofmt -l; go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows.
Not run: the whole ./... suite at once (the machine's load times it out)
and Windows/Linux test runs.
2026-10-13 09:52:14 +08:00

83 lines
2.4 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
})
}
// 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)
}
}