In WSL, with only magpie tui and Cursor's subscription, Claude Code said ECONNREFUSED. The terminal set agents to the gateway but never served it, and nothing listened at 127.0.0.1:3425 unless the app, magpie web or magpie serve ran. magpie tui now serves the gateway in its own process while it is open, as the app does, whenever no magpie answers at its address. One that does (the app, magpie web, magpie serve) keeps it. The terminal looks every 15s and takes the gateway up once that one is gone. The status line says "● gateway <url> · served while magpie tui is open" when it has nothing else to say, or "✗ gateway not running: <why>" when the port couldn't be had. On quitting after serving, it says the gateway stopped with it and what serves it otherwise. The usage text and docs/reference.md say so too. Tested in a sandbox HOME on port 3486 with a real DeepSeek key: magpie tui alone answered /v1/models; Claude Code (sandbox HOME) set to deepseek/deepseek-flash ran a Bash tool turn through it; the status line showed the gateway; q printed the note and freed the port. TestTUIServesTheGatewayWhenNoneDoes (serves, status, lets go at quit) and TestTUILeavesTheGatewayToAnotherAndTakesItUp (leaves another magpie's gateway alone, takes it up once it is gone) are new and pass 3x under -race; they don't build without the change. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
90 lines
2.4 KiB
Go
90 lines
2.4 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
func freeAddr(t *testing.T) string {
|
|
t.Helper()
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer l.Close()
|
|
return l.Addr().String()
|
|
}
|
|
|
|
func waitFor(t *testing.T, what string, ok func() bool) {
|
|
t.Helper()
|
|
for end := time.Now().Add(5 * time.Second); time.Now().Before(end); time.Sleep(20 * time.Millisecond) {
|
|
if ok() {
|
|
return
|
|
}
|
|
}
|
|
t.Fatal(what)
|
|
}
|
|
|
|
// With no magpie serving, the terminal serves the gateway while it is open
|
|
// and lets go as it quits (gnayiab on X: ECONNREFUSED in WSL with only
|
|
// magpie tui).
|
|
func TestTUIServesTheGatewayWhenNoneDoes(t *testing.T) {
|
|
t.Setenv("MAGPIE_ADDR", freeAddr(t))
|
|
if gateway.Running() {
|
|
t.Fatal("a gateway answers before the terminal started")
|
|
}
|
|
ctx, stop := context.WithCancel(context.Background())
|
|
wait := serveGateway(ctx)
|
|
waitFor(t, "no gateway while the terminal is open", gateway.Running)
|
|
if !gw.here.Load() {
|
|
t.Fatal("not marked as served here")
|
|
}
|
|
if s := gatewayStatus(); !strings.Contains(s, gateway.URL()) || !strings.Contains(s, "served while magpie tui is open") {
|
|
t.Fatalf("status %q", s)
|
|
}
|
|
stop()
|
|
wait()
|
|
waitFor(t, "the gateway still answers after the terminal quit", func() bool { return !gateway.Running() })
|
|
if gw.here.Load() || gatewayStatus() != "" {
|
|
t.Fatalf("still marked as served here: %q", gatewayStatus())
|
|
}
|
|
}
|
|
|
|
// A magpie that has the gateway keeps it; the terminal takes it up once
|
|
// that one is gone.
|
|
func TestTUILeavesTheGatewayToAnotherAndTakesItUp(t *testing.T) {
|
|
t.Setenv("MAGPIE_ADDR", freeAddr(t))
|
|
old := gatewayWatch
|
|
gatewayWatch = 100 * time.Millisecond
|
|
defer func() { gatewayWatch = old }()
|
|
|
|
other, endOther := context.WithCancel(context.Background())
|
|
otherDone := make(chan struct{})
|
|
go func() {
|
|
defer close(otherDone)
|
|
gateway.New().ListenAndServe(other)
|
|
}()
|
|
waitFor(t, "the other magpie's gateway never answered", gateway.Running)
|
|
|
|
ctx, stop := context.WithCancel(context.Background())
|
|
defer stop()
|
|
wait := serveGateway(ctx)
|
|
time.Sleep(300 * time.Millisecond)
|
|
if gw.here.Load() || gatewayStatus() != "" {
|
|
t.Fatalf("took the gateway another magpie serves: %q", gatewayStatus())
|
|
}
|
|
|
|
endOther()
|
|
<-otherDone
|
|
waitFor(t, "the terminal didn't take the gateway up once the other was gone", func() bool {
|
|
return gw.here.Load() && gateway.Running()
|
|
})
|
|
stop()
|
|
wait()
|
|
}
|