Files
magpie/internal/testenv/testenv.go
T
yetone 79fec406c6 agent, gui: a Claude Code provider switch (CLAUDE_CODE_USE_BEDROCK, …) steps aside while magpie is wired in and comes back after; one magpie can't take out is said (jw1dev on X)
jw1dev's ~/.claude/settings.json had CLAUDE_CODE_USE_BEDROCK=1. magpie wrote
ANTHROPIC_BASE_URL and its model ids beside it. Bedrock ignores the base URL,
so Claude Code went to AWS, Bedrock refused magpie's model, and magpie still
showed Claude Code as connected.

Claude Code 2.1.300's six switches (agentenv.ClaudeProviders: BEDROCK,
VERTEX, FOUNDRY, ANTHROPIC_AWS, ANTHROPIC_GOOGLE_CLOUD, MANTLE; on = 1, true,
yes or on, any case, trimmed):
- In a settings file magpie writes (user settings, WSL twins), a switch
  that is on is taken out on every route that writes the wiring: connect,
  model pick and Rewire/Reapply through writeTiers, and Sync. Its raw JSON
  value is stashed (claude.cloud.<var>, under the distro id for WSL). It is
  put back as written when magpie steps out (Disconnect, or the model set
  back to default), unless the user set it again since. AWS_*/GCP
  variables and a switch that is off stay.
- Where magpie doesn't write it (managed settings, ~/.claude.json's env,
  the login shell's env), Check names the variable, the cloud and the
  file. It reads them in Claude Code's own order: managed wins, then the
  user's settings, then ~/.claude.json, then the shell. Two notice
  templates, translated in zh, zh-TW, ja and de. The row's line and its
  Reconnect pill now show a drift detail through tNotice.
- proc.LoginEnv reads the switches but never sets them in magpie's env,
  and testenv unsets them.
- T3 Code: a switch is never carried onto magpie's instance. Its Check
  says when the user's ~/.claude settings have one on. That Check first
  checks the instance's ANTHROPIC_BASE_URL against the gateway (t3EnvOf),
  as every agent's Check must for TestDriftUnwiredEveryAgent,
  TestPortMovesEveryAgent and TestReverseProxyIsStillMagpie.
- Claude Desktop's Code tab runs with CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST,
  under which Claude Code ignores these switches in settings, so nothing
  changes there.

Run:
- gofmt -l: clean.
- go vet: native ./..., GOOS=linux -tags nogui, GOOS=windows: clean.
- go test -tags nogui ./...: every package ok except internal/agent, where
  t3code's first Check (cloud only) failed the three every-agent tests
  above. Fixed with the wiring check, then the whole internal/agent package
  was rerun: ok (368s).
- GUI, Chromium and WebKit, 102/102 each: new claude-cloud-drift (5
  languages, 1100 and 420px, file and shell notices), gui-de, gui-ja,
  gui-zh-tw, i18n-keys, agent-notice-lang, agent-proxied,
  agent-unreachable, cli-behind, menu-scroll, agent-connect,
  agent-disconnect.
- Sandbox HOME (scratchpad dir, live ~/.claude untouched):
  - Setup: real claude 2.1.300 with settings.json
    {CLAUDE_CODE_USE_BEDROCK:"1", AWS_REGION, AWS_PROFILE}, a magpie-cloud
    binary from this tree serving on 127.0.0.1:3487, a fake OpenAI
    upstream on 3486.
  - `magpie claude fake/fake-model` took the switch out and stashed "1".
  - `claude -p "say hi"` printed PONG-FROM-FAKE-UPSTREAM, and the fake
    logged POST /v1/chat/completions model=fake-model.
  - `magpie claude default` left settings.json byte-for-byte the same as
    before connect (cmp).
  - Control: the switch put back by hand beside the wiring (the reported
    state). Claude Code never reached magpie: the fake logged nothing.

Broken by hand, each went red:
- no dropClouds in writeTiers, then in Sync;
- putClouds returning at once, and putClouds writing over the user's own;
- no claudeCloud in Check;
- the T3 filter and the T3 Check;
- shellVars without ClaudeProviders;
- the tNotice calls taken out of the line and pill: claude-cloud-drift
  went 17 red.

Not tried:
- A project's .claude/settings.json(.local) isn't read, because magpie
  doesn't know where Claude Code runs.
- WSL and Windows were not run for real; tests only.
- Real Vertex, Foundry and AWS accounts.
- T3 Code and Claude Desktop with a switch, run for real.
- The drift shown in the real app window. The CLI has no drift output.
- The user's own ANTHROPIC_MODEL being lost across connect/disconnect is a
  separate gap, fixed in its own commit.
2026-10-15 07:41:30 +08:00

266 lines
9.1 KiB
Go

// Package testenv gives a package's tests a home of their own, on every OS:
// HOME, USERPROFILE (Windows' home), the XDG folders and Windows' APPDATA
// and LOCALAPPDATA all point into one new temporary folder, the agents' own
// variables (agentenv.Vars) are cleared, and the keychain tools and agent
// CLIs on PATH are failing stand-ins, so that no test reads or writes the
// files of the user running it. Setting HOME alone isn't that: on Windows
// Go reads the home from USERPROFILE, and a test then meets the real
// ~/.claude. A package uses it from its TestMain:
//
// func TestMain(m *testing.M) { testenv.Main(m) }
//
// and a test that wants a home of its own sets it with SetHome, never
// HOME alone. Only tests import it.
package testenv
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/agentenv"
)
const (
// prefix starts the temporary home's name; Run removes only a folder
// so named.
prefix = "magpie-test-home-"
// Marker holds the temporary home in the environment. A test binary
// started again by one of its own tests inherits it, and keeps the
// environment that test gave it (its fixtures) instead of isolating
// again; a test that hands such a child an environment of its own
// passes Marker along.
Marker = "MAGPIE_TEST_SANDBOX"
)
// folderVars are the variables Isolate points into the temporary home.
var folderVars = []string{"HOME", "USERPROFILE", "XDG_CONFIG_HOME", "XDG_CACHE_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "APPDATA", "LOCALAPPDATA"}
// proxyVars are the machine's proxy variables. net/http reads them once per
// process, at its first routed request, so a test binary that lets them
// stand freezes the machine's proxy into that read: a test that sets its
// own afterwards reads the machine's instead (search's Auto, which follows
// ALL_PROXY alone for that reason), and Offline's refused hosts are carried
// for real by the machine's proxy standing on loopback.
var proxyVars = []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"}
// ClearProxy unsets the machine's proxy variables. Isolate calls it before
// the tests run; a package that isolates itself in its own TestMain calls
// it too, before the first request any test makes.
func ClearProxy() {
for _, k := range proxyVars {
os.Unsetenv(k)
}
}
// Main runs the package's tests in a home of their own and exits with
// their code.
func Main(m *testing.M) {
os.Exit(Run(m))
}
// Run is Main for a TestMain with more to set up: it isolates, runs the
// tests and removes the temporary home, and gives the code to exit with.
// It runs no test when the home can't be made safe.
func Run(m *testing.M) int {
return RunIn(m, nil)
}
// RunIn is Run with setup called on the temporary home before the tests,
// for fixtures that live in it.
func RunIn(m *testing.M, setup func(home string)) int {
if home := os.Getenv(Marker); home != "" {
// started again by one of the tests: keep what it set up
if err := checkChild(home); err != nil {
fmt.Fprintln(os.Stderr, "testenv:", err)
return 1
}
if setup != nil {
setup(home)
}
defer RemovePrograms()
return m.Run()
}
home, err := Isolate()
if err != nil {
fmt.Fprintln(os.Stderr, "testenv:", err)
return 1
}
defer remove(home)
defer RemovePrograms()
if setup != nil {
setup(home)
}
return m.Run()
}
// Isolate points the home and the config, cache and data folders into a
// new temporary folder, clears the agents' variables, puts the stand-ins
// first on PATH and gives the folder. It fails when Go would still
// resolve any of them, or a stand-in's tool, outside that folder.
func Isolate() (string, error) {
home, err := os.MkdirTemp("", prefix)
if err != nil {
return "", err
}
for k, v := 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"),
"XDG_STATE_HOME": filepath.Join(home, ".local", "state"),
"APPDATA": filepath.Join(home, "AppData", "Roaming"),
"LOCALAPPDATA": filepath.Join(home, "AppData", "Local"),
Marker: home,
} {
if err := os.Setenv(k, v); err != nil {
return "", err
}
}
for _, k := range append(slices.Clone(agentenv.Vars), agentenv.ClaudeProviders...) {
if err := os.Unsetenv(k); err != nil {
return "", err
}
}
ClearProxy()
if err := inertTools(home); err != nil {
return "", err
}
for _, name := range tools {
if p, err := exec.LookPath(name); err != nil || !within(p, home) {
return "", fmt.Errorf("%s resolves to %q, not the stand-in in %s (%v)", name, p, home, err)
}
}
for name, dir := range map[string]func() (string, error){
"home": os.UserHomeDir, "config": os.UserConfigDir, "cache": os.UserCacheDir,
} {
p, err := dir()
if err != nil || !within(p, home) {
return "", fmt.Errorf("the %s folder %q is outside the test home %s (%v)", name, p, home, err)
}
}
return home, nil
}
// checkChild accepts the environment a test gave the binary it started
// again: the sandbox, and every folder variable and agent folder set, lie
// in the temporary folder. On Windows that folder must come from TMP or
// TEMP, since without them Go would take it from USERPROFILE, the very
// thing to check.
func checkChild(home string) error {
if runtime.GOOS == "windows" && os.Getenv("TMP") == "" && os.Getenv("TEMP") == "" {
return errors.New("a test started this binary again without TMP or TEMP: pass them along")
}
tmp := os.TempDir()
if !within(home, tmp) {
return fmt.Errorf("%s is %q, outside the temporary folder %s", Marker, home, tmp)
}
vars := append([]string{}, folderVars...)
for _, k := range agentenv.Vars {
if !agentenv.NotPaths[k] {
vars = append(vars, k)
}
}
for _, k := range vars {
if v := os.Getenv(k); v != "" && !within(v, tmp) {
return fmt.Errorf("%s is %q, outside the temporary folder %s", k, v, tmp)
}
}
return nil
}
// SetHome makes dir the home for one test, on every OS: HOME, and
// USERPROFILE, which Go reads on Windows.
func SetHome(t testing.TB, dir string) {
t.Helper()
t.Setenv("HOME", dir)
t.Setenv("USERPROFILE", dir)
}
// tools are the programs a test must never reach for real: the system
// keychains (macOS' security, Linux's secret-tool), which hold the user's
// sign-ins outside any home, and the agents' CLIs.
var tools = []string{"security", "secret-tool", "claude", "codex", "cursor-agent", "devin", "grok", "kiro-cli"}
// inertTools puts a failing stand-in for each of tools first on PATH, in
// home/bin. Tests can still put their own fakes before it.
func inertTools(home string) error {
bin := filepath.Join(home, "bin")
if err := os.Mkdir(bin, 0o755); err != nil {
return err
}
if err := StandIns(bin, tools); err != nil {
return err
}
return os.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
}
// StandIns writes a failing program for each name in bin. On macOS each
// is run once now, in the background, so that the check macOS makes of a
// new program's first run (Program) is made while the tests start rather
// than in the first test to reach it, and without holding them up.
// Windows finds a program by PATHEXT and passes over a file with no
// extension, so there each also gets a .bat, which comes before the user's
// own codex.cmd further down PATH: without it the gateway's tests, which
// make their stand-ins here, read the installed Codex's version (#1827,
// EdwinDigital: TestCodexPoolSaysTheClientsVersion saw 0.162.0).
func StandIns(bin string, names []string) error {
for _, name := range names {
p := filepath.Join(bin, name)
if err := os.WriteFile(p, []byte("#!/bin/sh\nexit 1\n"), 0o755); err != nil {
return err
}
if runtime.GOOS == "windows" {
if err := os.WriteFile(p+".bat", []byte("@exit /b 1\r\n"), 0o755); err != nil {
return err
}
}
if runtime.GOOS == "darwin" {
warming.Add(1)
go func() {
defer warming.Done()
if proc, err := os.StartProcess(p, []string{p}, &os.ProcAttr{}); err == nil {
proc.Wait()
record(p)
}
}()
}
}
return nil
}
// Zone sets time.Local for the test, and puts it back after. It first
// waits out the stand-ins' first runs: they os.Stat what ran, which reads
// time.Local, so a test that swaps it while they are under way races them.
func Zone(t testing.TB, loc *time.Location) {
warming.Wait()
old := time.Local
time.Local = loc
t.Cleanup(func() { time.Local = old })
}
// warming is the stand-ins' first runs under way.
var warming sync.WaitGroup
// within says whether path lies in dir.
func within(path, dir string) bool {
rel, err := filepath.Rel(dir, path)
return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) && !filepath.IsAbs(rel)
}
// remove deletes the temporary home Isolate made, and nothing else: a
// folder in the temporary folder, named as Isolate names them.
func remove(home string) {
if filepath.IsAbs(home) && filepath.Dir(home) == filepath.Clean(os.TempDir()) && strings.HasPrefix(filepath.Base(home), prefix) {
os.RemoveAll(home)
}
}