Files
magpie/internal/cli/plugin_middleware_cli_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

73 lines
3.0 KiB
Go

package cli
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/middleware"
"github.com/yetone/magpie/internal/plugin"
)
// magpie plugin says which plugins are gateway middleware and their hooks,
// or why one didn't load; a middleware plugin has no provider to sign in
// to, so the list had nothing under it.
func TestPluginListSaysMiddleware(t *testing.T) {
groupsHome(t)
t.Cleanup(plugin.Settle) // what adding the plugins told ends with the test
dir := t.TempDir()
good := filepath.Join(dir, "alias.middleware.js")
bad := filepath.Join(dir, "bad.middleware.js")
os.WriteFile(good, []byte("export const events = [\"message_start\"]\nexport function onRequest(b, ctx) {}\nexport function onEvent(e, ctx) {}\n"), 0o644)
os.WriteFile(bad, []byte("export function onRequest( {\n"), 0o644)
for _, spec := range []string{good, bad} {
if _, err := plugin.Add(context.Background(), spec); err != nil {
t.Fatal(err)
}
}
middleware.Reload()
out, err := stdoutOf(t, func() error { return listPlugins(context.Background(), false) })
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "gateway middleware onRequest, onEvent") {
t.Errorf("the middleware's hooks aren't listed:\n%s", out)
}
if !strings.Contains(out, "didn't load: ") || !strings.Contains(out, "SyntaxError") {
t.Errorf("the broken middleware doesn't say why:\n%s", out)
}
js, _ := stdoutOf(t, func() error { return listPlugins(context.Background(), true) })
if !strings.Contains(js, `"middleware"`) || !strings.Contains(js, `"message_start"`) {
t.Errorf("--json has no middleware:\n%s", js)
}
}
// magpie plugin off, on and rm take a plugin's short name, as options
// does: `magpie plugin off think-tags` said there was no such plugin.
func TestPluginOnOffRmShortName(t *testing.T) {
groupsHome(t)
t.Cleanup(plugin.Settle)
dir := filepath.Join(t.TempDir(), "middleware-zz-tags")
os.MkdirAll(dir, 0o755)
os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"name": "@magpie-community/middleware-zz-tags", "magpie": {"middleware": "./zz.middleware.js"}}`), 0o644)
os.WriteFile(filepath.Join(dir, "zz.middleware.js"), []byte("export function onRequest(b) {}\n"), 0o644)
if _, err := plugin.Add(context.Background(), dir); err != nil {
t.Fatal(err)
}
off := func() bool { return plugin.Load().Plugins[0].Off }
if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "off", "zz-tags"}) }); err != nil || !off() {
t.Fatalf("off by short name: err %v, off %v", err, off())
}
if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "on", "zz-tags"}) }); err != nil || off() {
t.Fatalf("on by short name: err %v, off %v", err, off())
}
if _, err := stdoutOf(t, func() error { return pluginCmd([]string{"plugin", "rm", "zz-tags"}) }); err != nil || len(plugin.Load().Plugins) != 0 {
t.Fatalf("rm by short name: err %v, left %v", err, plugin.Load().Plugins)
}
if err := pluginCmd([]string{"plugin", "off", "nothing-here"}); err == nil {
t.Error("a name no plugin has was taken")
}
}