diff --git a/internal/gateway/main_test.go b/internal/gateway/main_test.go index e6a3896ab..cb6e2ab24 100644 --- a/internal/gateway/main_test.go +++ b/internal/gateway/main_test.go @@ -10,6 +10,10 @@ import ( "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 { @@ -45,17 +49,27 @@ func isolatedTests(m *testing.M) (int, error) { return 1, err } } - bin := filepath.Join(home, "bin") - if err := os.Mkdir(bin, 0o755); 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. - if err := testenv.StandIns(bin, []string{"security", "secret-tool", "claude", "codex", "cursor-agent", "devin", "grok", "kiro-cli"}); err != nil { - return 1, err + // 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 + } } - defer testenv.RemovePrograms() if err := os.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")); err != nil { return 1, err } diff --git a/internal/gateway/redact_test.go b/internal/gateway/redact_test.go index 922524cf3..0369cdf59 100644 --- a/internal/gateway/redact_test.go +++ b/internal/gateway/redact_test.go @@ -12,6 +12,7 @@ import ( "strings" "sync" "testing" + "time" "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/redact" @@ -28,10 +29,17 @@ func TestConcurrentRedactedCountTokens(t *testing.T) { if err != nil { t.Fatal(err) } + // a child that hangs prints its stacks before this run's own deadline + // does, rather than at a fixed 20s a loaded machine can spend on + // starting it + timeout := "0" + if d, ok := t.Deadline(); ok { + timeout = (time.Until(d) * 9 / 10).Round(time.Second).String() + } for _, mode := range []string{"shared", "new_per_request"} { t.Run(mode, func(t *testing.T) { t.Parallel() // independent processes, each with a cold key cache - cmd := exec.Command(exe, "-test.run=^TestConcurrentRedactedCountTokens$", "-test.timeout=20s") + cmd := exec.Command(exe, "-test.run=^TestConcurrentRedactedCountTokens$", "-test.timeout="+timeout) cmd.Env = append(os.Environ(), modeEnv+"="+mode) if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("concurrent first requests: %v\n%s", err, out)