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.
133 lines
5.3 KiB
Go
133 lines
5.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// claudeRefuses has the fake Claude Code fail as Claude Code does on a
|
|
// sign-in Anthropic refused, on the accounts whose token is one of toks
|
|
// (all of them with none), and answer on the others.
|
|
func claudeRefuses(t *testing.T, calls string, toks ...string) {
|
|
t.Helper()
|
|
refused := `[ -z "` + strings.Join(toks, "") + `" ]`
|
|
for _, tok := range toks {
|
|
refused += ` || [ "$tok" = "` + tok + `" ]`
|
|
}
|
|
script := `#!/bin/sh
|
|
case "$1" in auth) exit 1;; esac
|
|
creds="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/.credentials.json"
|
|
while read -r line; do
|
|
tok=$(grep -o 'tok-[a-z]*' "$creds" | head -1)
|
|
echo "$tok" >> '` + calls + `'
|
|
if ` + refused + `; then
|
|
echo '{"type":"result","is_error":true,"result":"Failed to authenticate: OAuth session expired and could not be refreshed"}'
|
|
else
|
|
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":"healthy account"}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":3}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_stop"}}'
|
|
echo '{"type":"result","is_error":false,"result":""}'
|
|
fi
|
|
done
|
|
`
|
|
binary := strings.Split(os.Getenv("PATH"), string(os.PathListSeparator))[0]
|
|
testenv.Program(t, filepath.Join(binary, "claude"), script)
|
|
}
|
|
|
|
func askClaudeModel(s *Server, text string) *httptest.ResponseRecorder {
|
|
body := `{"model":"claude/claude-sonnet-5","max_tokens":100,"messages":[{"role":"user","content":"` + text + `"}]}`
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)))
|
|
return rec
|
|
}
|
|
|
|
// An account Anthropic refused goes to the next one at once, with no rest
|
|
// to wait out and then fail again: its sign-in is not run again.
|
|
func TestClaudeAuthFailureFallsBackWithoutRetryingTheLogin(t *testing.T) {
|
|
claudeMadeFirst(t, false)
|
|
s := New()
|
|
t.Cleanup(s.subscription.abortAll)
|
|
calls := filepath.Join(t.TempDir(), "calls")
|
|
claudeRefuses(t, calls, "tok-a")
|
|
for _, text := range []string{"first", "second"} {
|
|
if rec := askClaudeModel(s, text); rec.Code != 200 || !strings.Contains(rec.Body.String(), "healthy account") {
|
|
t.Fatalf("%s: %d %s", text, rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
if b, _ := os.ReadFile(calls); strings.Count(string(b), "tok-a\n") != 1 {
|
|
t.Fatalf("refused login run again: %s", b)
|
|
}
|
|
routes := s.Trace(context.Background(), 0, 0).Routes
|
|
slices.SortFunc(routes, func(a, b Route) int { return int(a.Seq - b.Seq) })
|
|
for i, route := range routes {
|
|
if len(route.Tries) != 2 || route.Tries[0].Fail != failAuth || route.Tries[0].Rest != nil {
|
|
t.Fatalf("request %d: the refusal not told as one, or rested: %+v", i, route.Tries)
|
|
}
|
|
}
|
|
if !strings.Contains(routes[0].Tries[0].Error, "OAuth session expired") || !strings.Contains(routes[1].Tries[0].Error, "sign in again") {
|
|
t.Fatalf("errors told: %q, %q", routes[0].Tries[0].Error, routes[1].Tries[0].Error)
|
|
}
|
|
}
|
|
|
|
// With every account refused, the agent is told to sign in again, not that
|
|
// nothing is ready.
|
|
func TestClaudeAllRefusedSaysSignInAgain(t *testing.T) {
|
|
claudeMadeFirst(t, false)
|
|
loginFile := filepath.Join(filepath.Dir(provider.Path()), "logins.json")
|
|
var logins []map[string]any
|
|
raw, _ := os.ReadFile(loginFile)
|
|
if err := json.Unmarshal(raw, &logins); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, l := range logins {
|
|
l["on"] = false
|
|
}
|
|
if err := os.WriteFile(loginFile, mustJSON(logins), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claudeRefuses(t, filepath.Join(t.TempDir(), "calls"))
|
|
s := New()
|
|
t.Cleanup(s.subscription.abortAll)
|
|
askClaudeModel(s, "first")
|
|
if rec := askClaudeModel(s, "second"); rec.Code == 200 || !strings.Contains(rec.Body.String(), "sign in again") {
|
|
t.Fatalf("not told to sign in again: %d %s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// Another vendor saying a Claude sign-in's words (an OpenCode plugin on
|
|
// Anthropic's OAuth: "OAuth access token has been revoked") isn't a Claude
|
|
// account Anthropic refused: it rests as any failure does, and isn't told
|
|
// as needing a sign-in that magpie would never try again (#716 review).
|
|
func TestSignInWordsFromAnotherVendorRest(t *testing.T) {
|
|
fresh(t)
|
|
revoked := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
io.WriteString(w, `{"error":{"message":"OAuth access token has been revoked"}}`)
|
|
})
|
|
serveOn(t, "a", "ka", []string{"m"}, revoked)
|
|
serveOn(t, "b", "kb", []string{"m"}, &keyed{})
|
|
if err := provider.SaveGroup(provider.Group{Name: "Two", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
if code, body := postAs(t, s, "", `{"model":"group/two","messages":[{"role":"user","content":"hi"}]}`); code != 200 || !strings.Contains(body, "from kb") {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
if r := lastRoute(s); len(r.Tries) != 2 || r.Tries[0].Fail == failAuth || r.Tries[0].Rest == nil {
|
|
t.Fatalf("not rested as before: %+v", r.Tries)
|
|
}
|
|
}
|