Files
magpie/internal/gateway/main_test.go
T
yetone e13f151302 gateway tests: TestConcurrentRedactedCountTokens' children run the stand-in CLIs their parent already ran, and stop at the parent's deadline rather than a fixed 20s
The test re-executes its own binary once per mode so the redaction key
cache is cold. Each child went through TestMain again, which wrote all
eight stand-in CLIs (security, claude, codex, cursor-agent, devin, ...)
into a new temp home. On macOS a newly written program waits for the
system's first-run check before it runs. The child's fixture,
provider.Save, calls Accounts(), which runs those stand-ins (cursor and
devin identity asks, each waited for up to 3s, and `security` for the
Claude credential). So the child spent its budget on account discovery
it doesn't test. The masked count_tokens work under test takes ~6ms.

Evidence (stacks from the child's own timeout panic): every timed-out
child sat in provider.Save -> Accounts -> cliIdentity.get /
askDevinStatus / askCursorStatus / readClaudeCredential, waiting on a
stand-in's exec. None was in redact or count_tokens. Timing provider.Save
in the child under scratchpad load.sh (CPU burners plus new-script exec
loops), 6 interleaved runs each: 5.7-9.4s before, 3.9-5.1s after (one
8.1s). Idle: 4-6s before, 1.7-2.2s after. Under `taskpolicy -c
background` the old children timed out at 20s in 2 of 4 runs and in 2 of
6 earlier ones.

What remains after that is CPU starvation. Under background QoS at load
average ~150, children still went past 20s. Their stacks were runnable in
json encoding, or in `security` exec'd from a stand-in that had already
run, with nothing blocked. So the child's -test.timeout is now 90% of
the parent's remaining deadline instead of a fixed 20s. A child that
really hangs still prints its stacks before the parent's run ends.

Children now take the parent's stand-in folder from
MAGPIE_GATEWAY_TEST_STAND_INS. A child still gets its own temp HOME and
XDG folders.

Verified: -race -count=10 of the test; go test -race ./internal/gateway
(ok, 739s on a loaded machine; TestLargeRequestEndpoints alone took 272s
under -race, so the default 10m -timeout is too short there); go vet
./...; full go test -tags nogui ./... has gateway ok (298s).
2026-10-07 06:10:06 +08:00

85 lines
2.7 KiB
Go

package gateway
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/agentenv"
"github.com/yetone/magpie/internal/testenv"
)
// standInsEnv names the folder of a test binary's stand-in CLIs, for a
// test binary it starts again.
const standInsEnv = "MAGPIE_GATEWAY_TEST_STAND_INS"
func TestMain(m *testing.M) {
code, err := isolatedTests(m)
if err != nil {
fmt.Fprintln(os.Stderr, err)
code = 1
}
os.Exit(code)
}
// Isolate every process, including the cold-start redaction tests. HOME
// alone does not isolate the macOS Keychain, and account caches eventually
// expire: discovery must keep finding only the test's sign-ins and CLIs.
func isolatedTests(m *testing.M) (int, error) {
home, err := os.MkdirTemp("", "magpie-gateway-test-")
if err != nil {
return 1, err
}
defer os.RemoveAll(home)
for name, value := range map[string]string{
"HOME": home, "USERPROFILE": home,
"XDG_CONFIG_HOME": filepath.Join(home, ".config"),
"XDG_CACHE_HOME": filepath.Join(home, ".cache"),
"XDG_DATA_HOME": filepath.Join(home, ".local", "share"),
"APPDATA": filepath.Join(home, "AppData", "Roaming"),
"LOCALAPPDATA": filepath.Join(home, "AppData", "Local"),
} {
if err := os.Setenv(name, value); err != nil {
return 1, err
}
}
for _, name := range agentenv.Vars {
if err := os.Unsetenv(name); err != nil {
return 1, err
}
}
// Finding an inert CLI also prevents discovery from falling back to
// one installed at an absolute system path. Tests can prepend their
// own fakes, as fakeClaude does, and still use ordinary shell tools.
// A test binary this one starts again (the cold-start redaction tests)
// runs the stand-ins its parent made: on macOS a newly written program
// waits for the system's first-run check, and under a full suite's load
// a child's account discovery spent its whole 20s on new stand-ins.
bin := os.Getenv(standInsEnv)
if bin == "" {
bin = filepath.Join(home, "bin")
if err := os.Mkdir(bin, 0o755); err != nil {
return 1, err
}
if err := testenv.StandIns(bin, []string{"security", "secret-tool", "claude", "codex", "cursor-agent", "devin", "grok", "kiro-cli"}); err != nil {
return 1, err
}
defer testenv.RemovePrograms()
if err := os.Setenv(standInsEnv, bin); err != nil {
return 1, err
}
}
if err := os.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")); err != nil {
return 1, err
}
// no route is written to disk behind a test's back; the history's own
// tests call saveRoute themselves
keepRoutes = false
// no test asks a vendor: a provider with a real base URL and a made-up
// key would, in the background (a plan key's windows, #1016), and a
// plugin host's first start fetched models.dev
testenv.Offline()
return m.Run(), nil
}