macOS checks a newly written executable before its first exec: 0.27s alone, 2-5s while other new programs start (a go test ./... starting its test binaries). The same file again takes 0.009s, and the pass is kept across a rename and an in-place rewrite.108852b4worked round this for one test with /bin/sh -c; the class was every test that writes a `#!/bin/sh` fake and times its answer. testenv.Program(t, path, script) replaces os.WriteFile(path, script, 0o755) for fakes. On macOS it renames a file that has run once from a pool to path and writes the script into it in place; at the test's cleanup the file goes back to the pool (only a path the test created; a stand-in it wrote over stays). Fakes still end with their tests: nothing about how they are started or waited on changed. Run (and gateway's TestMain) remove the pool at the end. TestMain's PATH stand-ins are run once in the background (testenv.StandIns). Other systems write the file as before. Converted every `#!/bin/sh` fake written by tests under internal/ (agent, gateway, gui, library, plugin, proc, provider, update). Windows, binary and node/bun fakes are unchanged. Under the same load (3 loops each starting 8 freshly written scripts at a time), 4 rounds each, before (108852b4/ origin/main) vs after: - provider TestSavedButSignedOutClaudeServed: 4/4 fail (8.01s) -> 4/4 pass - provider TestClaudeSignInOverStaleCredentials: 4/4 fail (5.00s) -> 4/4 pass - provider TestDevinSignedInOnlyInMagpie: 3/4 fail ("CLI's own []") -> 4/4 pass - gateway TestClaudeRunEndsWhenTheClientLeaves: 4/4 fail -> 4/4 pass (and 3/3 fail -> 3/3 pass at a third of that load) - gateway TestClientGoneRecordedAsCanceled: 4/4 fail -> 4/4 pass - gui TestProvidersAnswerWhileListsComeIn ("cursor found false" at 3.03s): 4/4 fail -> 4/4 pass. The same class, not bfe8bcaf's test order. - proc TestUserPath (login shell's PATH never came, 7.01s): 4/4 fail -> 4/4 pass Unloaded, every touched package passes; no pool directory or process is left after a run.
113 lines
4.4 KiB
Go
113 lines
4.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// claudeWorkDirs runs two Claude subscription requests through a script
|
|
// standing in for Claude Code, with a temp folder of the test's own and a
|
|
// home kept in git, and gives the folders the two runs worked in and the
|
|
// folder they should share.
|
|
func claudeWorkDirs(t *testing.T, prepare func(work string)) (dirs []string, work string) {
|
|
t.Helper()
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("a shell script stands in for Claude Code")
|
|
}
|
|
home := t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(home, ".git"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testenv.SetHome(t, home)
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("TMPDIR", t.TempDir())
|
|
work = filepath.Join(os.TempDir(), claudeWorkName())
|
|
if prepare != nil {
|
|
prepare(work)
|
|
}
|
|
dir := t.TempDir()
|
|
script := `#!/bin/sh
|
|
pwd -P >> ` + dir + `/dirs
|
|
while read -r line; do
|
|
echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":1}}}}'
|
|
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ok"}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_stop"}}'
|
|
echo '{"type":"result","subtype":"success","is_error":false,"result":""}'
|
|
done
|
|
`
|
|
testenv.Program(t, filepath.Join(dir, "claude"), script)
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
|
|
s := New()
|
|
p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "u"}}
|
|
for _, said := range []string{"one", "two"} {
|
|
body := `{"model":"claude-sonnet-5","max_tokens":100,"system":"rules","messages":[{"role":"user","content":"` + said + `"}]}`
|
|
rec := httptest.NewRecorder()
|
|
var u Usage
|
|
if code, msg := s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, p, "claude-sonnet-5", []byte(body), &u); code != 200 {
|
|
t.Fatalf("%d %s", code, msg)
|
|
}
|
|
}
|
|
b, _ := os.ReadFile(filepath.Join(dir, "dirs"))
|
|
return strings.Fields(string(b)), evalSymlinks(work)
|
|
}
|
|
|
|
// Every run works in the same folder: Claude Code puts its working
|
|
// directory in the system prompt, ahead of the conversation, so a folder of
|
|
// each run's own left nothing past Claude Code's own part of the prompt to
|
|
// be read from the cache. The folder is the user's own, made for them
|
|
// alone, in the temp folder: not under a home kept in git, whose status
|
|
// Claude Code would put in the prompt.
|
|
func TestClaudeRunsShareAWorkDir(t *testing.T) {
|
|
dirs, work := claudeWorkDirs(t, nil)
|
|
if len(dirs) != 2 || dirs[0] != work || dirs[1] != work {
|
|
t.Fatalf("working directories: %q, want %s", dirs, work)
|
|
}
|
|
if home := evalSymlinks(os.Getenv("HOME")); strings.HasPrefix(work, home+string(filepath.Separator)) || !strings.HasPrefix(work, evalSymlinks(os.TempDir())+string(filepath.Separator)) {
|
|
t.Fatalf("work folder %s: in the home %s, or not in the temp folder", work, home)
|
|
}
|
|
fi, err := os.Stat(work)
|
|
if err != nil || fi.Mode().Perm() != 0o700 {
|
|
t.Fatalf("work folder: %v %v", fi.Mode(), err)
|
|
}
|
|
}
|
|
|
|
// A work folder others can write to is not worked in, as anything put
|
|
// there (a CLAUDE.md) would be read by every run: each run works in a
|
|
// folder of its own instead.
|
|
func TestClaudeWorkDirOthersCanWriteIsAvoided(t *testing.T) {
|
|
dirs, work := claudeWorkDirs(t, func(work string) {
|
|
if err := os.MkdirAll(work, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
os.Chmod(work, 0o777)
|
|
})
|
|
if len(dirs) != 2 || dirs[0] == work || dirs[1] == work || dirs[0] == dirs[1] {
|
|
t.Fatalf("working directories: %q, shared %s", dirs, work)
|
|
}
|
|
}
|
|
|
|
// A symlink where the work folder goes, to a folder anyone could have
|
|
// filled, is not followed: each run works in a folder of its own.
|
|
func TestClaudeWorkDirSymlinkIsAvoided(t *testing.T) {
|
|
var target string
|
|
dirs, work := claudeWorkDirs(t, func(work string) {
|
|
target = t.TempDir()
|
|
if err := os.Symlink(target, work); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
target = evalSymlinks(target)
|
|
if len(dirs) != 2 || dirs[0] == target || dirs[1] == target || dirs[0] == dirs[1] {
|
|
t.Fatalf("working directories: %q, linked %s (%s)", dirs, target, work)
|
|
}
|
|
}
|