TestTUIFetchesSubscriptionModels/agent is red on ubuntu and macOS CI sincec80b32c9("agent: a model Codex hides but the user ticked is offered in Codex's picker"): the picker gives codex/gpt-6-luna where the test wants gpt-6-luna. The picker is right. The test passed beforec80b32c9only because nothing matched. The test signs a ChatGPT account in to Codex and writes no models_cache.json, so Codex has no list of its own here, and codexViaMagpieFor offers the models magpie serves on that account, codex/gpt-5.5 and codex/gpt-6-luna, in Codex's group. Beforec80b32c9those were dropped: ate056779a, once the list is in, the agent view's whole list is auto/gpt-5.5 and b/gpt-5.5, with no gpt-6-luna row. Codex's model field takes a value of one's own, and a picker that does hands back the typed text when no row matches, so choice() gave "gpt-6-luna", the test's want. The agent view now wants codex/gpt-6-luna, and every view's last check also needs a row to match, so typed text can't pass it again. The other five views' wants were rows already. What was run (tree bca161d7, on origin/main791e27ec, go1.26.8, linux/amd64, Linux 5.15), outside graftcp, umask 077: - TestTUIFetchesSubscriptionModels -v at791e27ec: agent red as on CI (models_test.go:96: got "codex/gpt-6-luna", want "gpt-6-luna"), the other five views pass. Here, with TestTUIAsksAgainForAMissingList, -count=3: 24 of 24 PASS. - The agent view's items before and after the list comes, printed by a throwaway test ate056779aand at791e27ec: the lists above. - Broken by hand, TestTUIFetchesSubscriptionModels -v: - codexViaMagpieFor dropping every codex/ entry, as beforec80b32c9: agent red (got "gpt-6-luna", want "codex/gpt-6-luna"), the other five pass; - the same, with the test as on main: all six pass, which is how the agent view's missing row went unnoticed; - the same, with main's bare want and the row check kept: agent red (got "gpt-6-luna", want "gpt-6-luna"), so the row check alone catches the typed text. - go test -tags nogui -count=1 -timeout 45m ./... (log kept), Bun 1.4.2: 69 packages ok and 7 red, none in what this changes, and internal/tui ok. The reds: agent TestCodexDriftReached (#3398), TestInstalledVersion, TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir and TestZedAppPath (#3141), TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout (#3641), and TestWSLColdPlaceOpensNothing (#3924); davsync TestSync (#3420); edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram (umask 077, #3640); gateway TestHandedOverGatewayFinishesItsStream (#3568) and TestPluginToldWhereTheAgentWorks (#3675); gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie (#3649); provider TestCodexAppServersOnHome (#3906), then "close of synctest channel from outside bubble" from TestUnreadLoginsWriteErrorsKeepCredentials (#3632). - gofmt -l clean. go vet -tags nogui and go build -tags nogui for linux, windows and darwin, and go vet and go build for windows without the tag: exit 0 each. go vet and go build without the tag on linux: exit 1 (this box has no GTK or WebKitGTK dev packages). Not tried: macOS, where CI is red too. Merged from JoeyWu-tech's branch, cherry-picked onto origin/mainf13aafa87together with the other of #3919/#3924. Run on mote at that tree: - go test ./internal/tui/: ok. - GOOS=linux and GOOS=windows go vet ./internal/agent/ ./internal/tui/: ok.
208 lines
7.0 KiB
Go
208 lines
7.0 KiB
Go
package tui
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// fakeCodex signs a ChatGPT account in to Codex, its models listed by a
|
|
// server of the test's own the way ChatGPT's backend answers. While down is
|
|
// there, the list fails, as with no network.
|
|
func fakeCodex(t *testing.T, h string) (down string) {
|
|
t.Helper()
|
|
down = filepath.Join(h, "codex-down")
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if _, err := os.Stat(down); err == nil || r.URL.Path != "/backend-api/codex/models" {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
var ms []map[string]any
|
|
for i, id := range []string{"gpt-5.5", "gpt-6-luna"} {
|
|
ms = append(ms, map[string]any{"slug": id, "display_name": id, "visibility": "list", "priority": i + 1})
|
|
}
|
|
b, _ := json.Marshal(map[string]any{"models": ms})
|
|
w.Write(b)
|
|
}))
|
|
old := provider.CodexBase
|
|
provider.CodexBase = srv.URL + "/backend-api/codex"
|
|
jwt := func(claims map[string]any) string {
|
|
b, _ := json.Marshal(claims)
|
|
return "e30." + base64.RawURLEncoding.EncodeToString(b) + ".sig"
|
|
}
|
|
b, _ := json.Marshal(map[string]any{"auth_mode": "chatgpt", "tokens": map[string]any{
|
|
"id_token": jwt(map[string]any{"email": "me@example.com",
|
|
"https://api.openai.com/auth": map[string]any{"chatgpt_plan_type": "pro", "chatgpt_account_id": "acct-1"}}),
|
|
"access_token": jwt(map[string]any{"exp": float64(time.Now().Add(24 * time.Hour).Unix())}),
|
|
"refresh_token": "r", "account_id": "acct-1",
|
|
}})
|
|
write(t, filepath.Join(h, ".codex", "auth.json"), string(b))
|
|
provider.ForgetAccounts()
|
|
t.Cleanup(func() {
|
|
provider.CodexBase = old
|
|
srv.Close()
|
|
provider.ForgetAccounts()
|
|
})
|
|
if p, err := provider.Find("codex"); err != nil || p.Account == nil || !p.Ready() {
|
|
t.Fatalf("the ChatGPT account isn't signed in: %v", err)
|
|
}
|
|
// a cold models.dev sync answered here: no test request leaves this
|
|
// process
|
|
client := http.DefaultClient
|
|
http.DefaultClient = &http.Client{Transport: modelsTransport{}}
|
|
t.Cleanup(func() { http.DefaultClient = client })
|
|
return down
|
|
}
|
|
|
|
// A subscription's list exists only at its vendor: the terminal asks for it
|
|
// at start and fills a model picker opened before it came, with no m on the
|
|
// Providers page (#1789, reyoung: Routing offered cursor/auto alone, from
|
|
// Cursor's built-in, since retired; Codex's ChatGPT account stands in).
|
|
func TestTUIFetchesSubscriptionModels(t *testing.T) {
|
|
for _, view := range []string{"routing", "routing with catalog sync", "routing with catalog sync last", "provider", "classifier", "agent"} {
|
|
t.Run(view, func(t *testing.T) {
|
|
fakeCodex(t, home(t))
|
|
m := model{w: 120, h: 40, syncing: strings.Contains(view, "sync")}
|
|
init := m.Init()
|
|
m, want := openModelPicker(t, m, view)
|
|
m = typeIn(m, "gpt-5.5")
|
|
// the list fetched adds codex/gpt-5.5 under this filter: the
|
|
// model on the cursor stays, not its row
|
|
// (Codex's own picker has none before its list: nothing to hold)
|
|
selected, _ := m.pk.choice()
|
|
if selected == "" && view != "provider" {
|
|
t.Fatalf("no GPT-5.5 in the picker\n%s", m.View())
|
|
}
|
|
m, _ = runModelsCmd(m, init, strings.HasSuffix(view, "last"))
|
|
if got, _ := m.pk.choice(); selected != "" && got != selected || m.pk.input.Value() != "gpt-5.5" || m.mode != modePick {
|
|
t.Fatalf("the list arriving changed the open picker: choice %q, filter %q, mode %v", got, m.pk.input.Value(), m.mode)
|
|
}
|
|
m.pk.input.SetValue("gpt-6-luna")
|
|
m.pk.refilter()
|
|
// a row of the list, not the typed text a picker that takes a
|
|
// value of one's own hands back when no row matches
|
|
if got, _ := m.pk.choice(); got != want || len(m.pk.match) == 0 {
|
|
t.Fatalf("the picker hasn't Codex's listed model: got %q, want %q\n%s", got, want, m.View())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A list that couldn't be had at start (no network yet) is asked for again
|
|
// while the terminal stays open, not only after a restart.
|
|
func TestTUIAsksAgainForAMissingList(t *testing.T) {
|
|
down := fakeCodex(t, home(t))
|
|
write(t, down, "")
|
|
old := modelsRetry
|
|
modelsRetry = 0
|
|
t.Cleanup(func() { modelsRetry = old })
|
|
|
|
m, _ := openModelPicker(t, model{w: 120, h: 40}, "routing")
|
|
m, retry := runModelsCmd(m, m.Init(), false)
|
|
if retry == nil {
|
|
t.Fatal("Codex's list failed and nothing asks for it again")
|
|
}
|
|
if err := os.Remove(down); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
provider.ForgetAccounts() // the minute FetchNew waits after a failure
|
|
m, again := runModelsCmd(m, retry, false)
|
|
m.pk.input.SetValue("gpt-6-luna")
|
|
m.pk.refilter()
|
|
if got, _ := m.pk.choice(); got != "codex/gpt-6-luna" {
|
|
t.Fatalf("asked again, still no gpt-6-luna: %q\n%s", got, m.View())
|
|
}
|
|
if again != nil {
|
|
t.Fatal("asks again with every list there")
|
|
}
|
|
}
|
|
|
|
// openModelPicker opens one of the terminal's four pickers of models, and
|
|
// says what Codex's gpt-6-luna is called in it.
|
|
func openModelPicker(t *testing.T, m model, view string) (model, string) {
|
|
t.Helper()
|
|
switch view {
|
|
case "provider":
|
|
m.page = pageProviders
|
|
m.reloadProviders()
|
|
for i, p := range m.provs {
|
|
if p.ID == "codex" {
|
|
m.prow = i
|
|
}
|
|
}
|
|
m.openProviderModels("codex")
|
|
return m, "gpt-6-luna"
|
|
case "classifier":
|
|
m.page = pageGroups
|
|
m.openClassifier(provider.Group{ID: "test", Name: "Test"})
|
|
case "agent":
|
|
a, err := agent.Find("codex")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.agents = []*agent.Agent{a}
|
|
m.reload()
|
|
m.openFieldPicker()
|
|
// Codex has no list of its own here (no models_cache.json), so the
|
|
// models magpie serves on its ChatGPT account are offered via magpie
|
|
// (codexViaMagpieFor)
|
|
return m, "codex/gpt-6-luna"
|
|
default:
|
|
m.page = pageGroups
|
|
m.openMemberPicker(pickMsg{newGroup: "Test"})
|
|
}
|
|
return m, "codex/gpt-6-luna"
|
|
}
|
|
|
|
// runModelsCmd runs what Init or a retry returned and hands its messages to
|
|
// the model, as the program would, held until the picker is open to stand
|
|
// for a vendor answering late; reverse delivers a batch's last first. It
|
|
// returns the command the models' message left, the retry, if any.
|
|
func runModelsCmd(m model, cmd tea.Cmd, reverse bool) (model, tea.Cmd) {
|
|
if cmd == nil {
|
|
return m, nil
|
|
}
|
|
msg := cmd()
|
|
if batch, ok := msg.(tea.BatchMsg); ok {
|
|
var left tea.Cmd
|
|
for i := range batch {
|
|
j := i
|
|
if reverse {
|
|
j = len(batch) - 1 - i
|
|
}
|
|
var c tea.Cmd
|
|
if m, c = runModelsCmd(m, batch[j], reverse); c != nil {
|
|
left = c
|
|
}
|
|
}
|
|
return m, left
|
|
}
|
|
next, left := m.Update(msg)
|
|
return next.(model), left
|
|
}
|
|
|
|
type modelsTransport struct{}
|
|
|
|
func (modelsTransport) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
if r.URL.Hostname() == "127.0.0.1" {
|
|
return http.DefaultTransport.RoundTrip(r) // the test's own vendor
|
|
}
|
|
if r.URL.String() != "https://models.dev/api.json" {
|
|
return nil, fmt.Errorf("unexpected request to %s", r.URL.Host)
|
|
}
|
|
return &http.Response{StatusCode: http.StatusOK, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(`{"openai":{"id":"openai","models":{}}}`)), Request: r}, nil
|
|
}
|