Files
magpie/internal/usage/main_test.go
T
Lin Huazhong 7586df4400 test: usage's and provider's TestMains clear the agents' variables too, and a new agent's variable left out of the list now fails (yetone, reviewing #543: internal/usage/main_test.go's TestMain still clears no agent variables, so a CLAUDE_CONFIG_DIR left in the shell reaches the real ~/.claude there too; nothing fails yet when a new agent's variable is left out of Vars) — both TestMains gave their package a home of its own and cleared no agent variable, the provider one as much as the usage one that was named. A CLAUDE_CONFIG_DIR left in the shell reached the real ~/.claude through the ledger's sessions.Calls (usage/ledger.go:122): with a canary Claude folder holding three of the sessions package's fixtures, six usage tests fail — TestLedger, TestSubscriptionListPrice, TestLedgerUsesTheStatedPrice, TestLedgerConfiguredPricesAndRefresh and the two Antigravity ledger ones — and pass with the clearing; provider passes with the same canary either way, but its Accounts read what an agent keeps on the machine too. #543 missed these by how it looked for its sites: by the clearing loops a sandbox already had, which one with no loop at all has no way to answer. TestFolderVarsAreListed reads the sources of the four packages an agent's folder variable is read in — this one, sessions, provider and library, the rest of the repository reading only magpie's own variables, the process' and the desktop's — and fails on a name that is neither in agentenv.Vars nor excused here with a reason, so a new agent's variable no longer waits for a developer's shell to meet one. It reads them as files, since a test in sessions or provider cannot import this package, which imports them; it asks for one name each way its pattern matches, so a change in how a variable is read empties the scan loudly instead of passing it, and for every name excused, so no excuse outlives its variable. What it cannot see is a name the sources build at runtime, as omoDir reads its two out of a slice (omo.go:33); both are listed, and a third taken the same way would not be caught. The package doc's VarsTest is TestVarsAreRead. TestFolderVarsAreListed fails without the change when KIMI_CODE_HOME is taken out of Vars, or when a pretend NEWAGENT_HOME is read in agents.go, and names the file that reads it; the six usage tests fail without theirs. On Windows 11 the whole suite answers as it did before the change: 31 packages pass, and the same 15 tests in internal/library, internal/gateway and internal/agent fail for what they failed for before. go vet ./..., go build -tags nogui (windows, GOOS=linux, GOOS=darwin), go vet -tags nogui for linux and darwin over every package touched, and gofmt pass. (#549)
2026-10-02 18:41:27 +08:00

36 lines
1.1 KiB
Go

package usage
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/agentenv"
)
// TestMain gives the package a home of its own: a test that doesn't set one
// reads and writes there, never in the runner's agents or, on Windows, its
// APPDATA and LOCALAPPDATA.
func TestMain(m *testing.M) {
home, _ := os.MkdirTemp("", "magpie-usage-test-")
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"),
"APPDATA": filepath.Join(home, "AppData", "Roaming"),
"LOCALAPPDATA": filepath.Join(home, "AppData", "Local"),
} {
os.Setenv(k, v)
}
// The package's ledger reads what sessions finds, which asks each agent's
// own variable before its folder in the home: one left in the shell would
// point a test at the runner's real agent (#522).
for _, k := range agentenv.Vars {
os.Unsetenv(k)
}
code := m.Run()
os.RemoveAll(home)
os.Exit(code)
}