Files
magpie/internal/provider/claude_models_test.go
T
yetone 6da19bf181 provider: Factory's built-in is retired; its plugin serves it
"factory" joins retiredBuiltins: every Factory account is served by
@magpie-community/opencode-factory-auth, and the hourly MoveRetiring
moves the ones still on the built-in.

Removed: the built-in's sign-in (factory_signin.go, WorkOS's device
flow), keeping the sign-in (renew, lapse, org reconcile and mend,
headers, factoryGet), its provider and account (factoryProvider,
factoryAccount, factoryAlsoOn), its login list, switch, on/off and
forget, its usage card (factoryLoginQuota), droid's prompt and body
rewriting (factory_client.go), and its rows in signin.go, logins.go,
logins_on.go, login_usage.go, subscription_usage.go and account.go.
zcodeDecode/Encode, kept in zcode.go only for factory_client.go, go
with it. An API key always goes to the plugin now (factoryAddKey), and
`magpie accounts import factory` keeps saying to add it with accounts
add factory; its dead import branches are gone.

Kept: the mover and what it reads (factoryLogins, factorySaved,
factoryCreds, factoryMu); ImportFactoryKeys, factory_key.go and
factoryWhoami, for "Use an API key instead"; droid's model table with
factoryAPIs and factoryCore, since the plugin's provider is named
"factory" (account.go APIs, FactoryGemini and servedByFactory still
answer for it); factoryAPI for the moved provider's host
(builtinHost); factory_usage.go's limits reader behind
FactoryAllowanceForTest, which the gateway's pool tests use; the
gateway's "factory" lists (estimatedMoved, the account check), as the
Zed commit kept zed's; the parity tests.

Tests:
- git rm'd, built-in only: factory_test.go (sign-in, refresh, accounts,
  orgs), factory_client_test.go, gateway factory_agents_test.go
  (droid's line on every wire, the generate route to chat) and
  factory_think_off_test.go (TestPluginThinkingOff covers the plugin);
  TestFactoryAPIKey, TestFactoryKeyAgainReadsUsageAfresh,
  TestFactoryServesModelsAddedSinceItsFetch, TestUserAgentAccountUntouched
  (TestUserAgentFor still covers an account's User-Agent).
- Changed meaning: TestGeminiEmptyReplyIsAnError,
  TestGeminiEmptyRepliesAskedAgain and TestGeminiImageModelReturnsItsImage
  used Factory's generate route as a fake-able Gemini upstream; they now
  use a Gemini API of the user's own (saveGem), the same decoder, and the
  image test checks :streamGenerateContent instead of /api/llm/g/v1/generate.
  TestMovedShowsBuiltinHost's first check is now a plain plugin
  (fakeco), and a retired Factory with no migration record shows
  api.factory.ai. TestFactoryAPIKeyMoved checks factoryLogins() is empty
  instead of the removed factoryLookup. factorySite/factoryJSON moved
  into factory_key_test.go.

Run in this worktree:
  gofmt -l internal: nothing
  go vet -tags nogui ./internal/provider ./internal/gateway ./internal/cli
    ./internal/gui, GOOS=darwin, linux, windows: clean
  staticcheck -checks U1000 -tests=false (darwin, windows) on
    internal/provider: nothing new
  go test -tags nogui -count=1 -run 'Factory|Gemini|PaymentRequired|
    ResetsInWords|UserAgent|UsedWindow|Vertex|PayloadTooLarge|UsageCache|
    AnthropicImage|ThinkingOff|Moved|Retired|Mover|Migrat|Move|
    SayTheirPlugin|Allowance|Discovery|ClaudeModels|GrokPrice|
    LoopbackSignIns|AsksOwnBackend|SideLogin|Import|Accounts|ZCode|MiMo'
    on the four packages: ok (all four); TestFactoryAPIKeyMoved ran with
    Bun (PASS), not skipped.
Not run: whole packages, the full suite, the GUI suite (machine load
~300). Not tried with a real Factory account. docs/subsystems not
changed, as for Zed.
2026-10-17 23:11:00 +08:00

172 lines
7.8 KiB
Go

package provider
import (
"context"
"os"
"path/filepath"
"slices"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// The models.dev catalog's anthropic rows, as models.dev served them on
// 2026-10-10: Opus 5.5, and Haiku 5.5, which it listed from 2026-10-07.
const (
mdOpus55 = `"claude-opus-5-5":{"id":"claude-opus-5-5","name":"Claude Opus 5.5","family":"claude-opus","attachment":true,"reasoning":true,"reasoning_options":[{"type":"effort","values":["low","medium","high","xhigh","max"]}],"tool_call":true,"structured_output":true,"temperature":false,"knowledge":"2026-06","release_date":"2026-09-22","last_updated":"2026-09-22","modalities":{"input":["text","image","pdf"],"output":["text"]},"open_weights":false,"limit":{"context":1000000,"output":128000},"cost":{"input":4,"output":20,"cache_read":0.2,"cache_write":5}}`
mdHaiku55 = `"claude-haiku-5-5":{"id":"claude-haiku-5-5","name":"Claude Haiku 5.5","family":"claude-haiku","attachment":true,"reasoning":true,"reasoning_options":[{"type":"toggle"},{"type":"effort","values":["low","medium","high","xhigh","max"]}],"tool_call":true,"structured_output":true,"temperature":false,"knowledge":"2026-06","release_date":"2026-10-07","last_updated":"2026-10-07","modalities":{"input":["text","image","pdf"],"output":["text"]},"open_weights":false,"limit":{"context":1000000,"output":128000},"cost":{"input":0.1,"output":0.5,"cache_read":0.01,"cache_write":0.125,"tiers":[{"input":0.5,"output":2.5,"cache_read":0.05,"cache_write":0.625,"tier":{"type":"context","size":100000}}]}}`
)
func writeAnthropicCatalog(t *testing.T, rows ...string) {
t.Helper()
body := `{"anthropic":{"id":"anthropic","name":"Anthropic","models":{`
for i, r := range rows {
if i > 0 {
body += ","
}
body += r
}
body += `}}}`
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
}
// A Claude model models.dev lists after the Claude account's list was
// last fetched is served at once, as the catalog has it: the copy a fetch
// keeps doesn't hold it out until the next Refresh (wakaka on Discord:
// Haiku 5.5 in Claude Code on the same account, not in magpie).
func TestClaudeAccountServesModelsListedSinceItsFetch(t *testing.T) {
home := claudeHome(t)
claudeSignIn(t, home, time.Now().Add(time.Hour))
t.Cleanup(catalog.Reset)
writeAnthropicCatalog(t, mdOpus55)
p, ok := find(All(), "claude")
if !ok || p.Account == nil {
t.Fatalf("claude: %+v %v", p, ok)
}
if _, err := p.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
// models.dev lists Haiku 5.5 now; the hourly sync wrote it
writeAnthropicCatalog(t, mdOpus55, mdHaiku55)
p, _ = find(All(), "claude")
if _, ok := p.Fetched(); !ok {
t.Fatal("the fetch left no list")
}
i := slices.IndexFunc(p.Available(), func(m catalog.Model) bool { return m.ID == "claude-haiku-5-5" })
if i < 0 {
t.Fatalf("Haiku 5.5 not served on the Claude account: %v", modelIDs(p.Available()))
}
h := p.Available()[i]
if h.Name != "Claude Haiku 5.5" || h.Context != 1_000_000 || h.Output != 128_000 || !h.Reasoning ||
!slices.Equal(h.Efforts, []string{"low", "medium", "high", "xhigh", "max"}) {
t.Fatalf("Haiku 5.5: %+v", h)
}
if h.Price == nil || h.Price.Input != 0.1 || h.Price.Output != 0.5 {
t.Fatalf("Haiku 5.5's price: %+v", h.Price)
}
var listed []string
for _, e := range Catalog() {
listed = append(listed, e.ID)
}
if !slices.Contains(listed, "claude/claude-haiku-5-5") {
t.Fatalf("not among the models agents are offered: %v", listed)
}
if rp, model, ok := Resolve("claude/claude-haiku-5-5"); !ok || rp.ID != "claude" || model != "claude-haiku-5-5" {
t.Fatalf("resolve: %+v %q %v", rp, model, ok)
}
// and a model models.dev no longer lists leaves the account's list too
writeAnthropicCatalog(t, mdHaiku55)
p, _ = find(All(), "claude")
if got := modelIDs(p.Available()); !slices.Equal(got, []string{"claude-haiku-5-5"}) {
t.Fatalf("after Opus 5.5 left the catalog: %v", got)
}
}
// The picks wakaka's and the owner's Claude accounts had saved: every
// model listed then, newest first, with no PickedFrom kept beside them, as
// providers.json held them on 2026-10-10. Haiku 5.5, listed since, was
// never served: agents are given the picks alone.
var claudeAllPicked = []string{"claude-sonnet-5-5", "claude-opus-5-5", "claude-fable-5-1", "claude-opus-5", "claude-sonnet-5", "claude-fable-5", "claude-opus-4-8", "claude-opus-4-7", "claude-sonnet-4-6", "claude-opus-4-6", "claude-opus-4-5", "claude-opus-4-5-20251101", "claude-haiku-4-5", "claude-haiku-4-5-20251001", "claude-sonnet-4-5", "claude-sonnet-4-5-20250929"}
// mdOpus41 is an older row, for a model listed before the picks were
// saved and left unpicked.
const mdOpus41 = `"claude-opus-4-1":{"id":"claude-opus-4-1","name":"Claude Opus 4.1","family":"claude-opus","reasoning":true,"tool_call":true,"release_date":"2025-08-05","last_updated":"2025-08-05","limit":{"context":200000,"output":32000}}`
func claudeExposed(t *testing.T) []string {
t.Helper()
p, ok := find(All(), "claude")
if !ok || p.Account == nil {
t.Fatalf("claude: %+v %v", p, ok)
}
return modelIDs(p.Exposed())
}
// A model models.dev lists after the user picked every model the Claude
// account listed is served beside those picks (wakaka on Discord: Haiku
// 5.5 still missing after 9b51428e, whose list had it, as the picks held
// it out); one listed then and left unpicked stays out.
func TestClaudePicksOfEveryModelTakeOnesListedSince(t *testing.T) {
home := claudeHome(t)
claudeSignIn(t, home, time.Now().Add(time.Hour))
t.Cleanup(catalog.Reset)
writeAnthropicCatalog(t, mdOpus55, mdHaiku55)
// picks saved before PickedFrom was kept, as the reporter's are
if err := store(file{Providers: []Provider{{ID: "claude", Models: claudeAllPicked}}}); err != nil {
t.Fatal(err)
}
if got := claudeExposed(t); !slices.Contains(got, "claude-haiku-5-5") || !slices.Contains(got, "claude-sonnet-5-5") {
t.Fatalf("all picked before Haiku 5.5 was listed: %v", got)
}
if p, _ := find(All(), "claude"); !slices.Contains(p.Picks(), "claude-haiku-5-5") {
t.Fatalf("the editor's picks: %v", p.Picks())
}
// an older model the user left unpicked: the picks aren't all of them
writeAnthropicCatalog(t, mdOpus55, mdHaiku55, mdOpus41)
if got := claudeExposed(t); slices.Contains(got, "claude-haiku-5-5") || slices.Contains(got, "claude-opus-4-1") {
t.Fatalf("picks that left one out took more: %v", got)
}
// picks saved now keep what was listed: Haiku 5.5 unticked stays out
writeAnthropicCatalog(t, mdOpus55, mdHaiku55)
if err := SetModels("claude", []string{"claude-opus-5-5"}); err != nil {
t.Fatal(err)
}
if got := claudeExposed(t); !slices.Equal(got, []string{"claude-opus-5-5"}) {
t.Fatalf("Haiku 5.5 left unpicked: %v", got)
}
// and every model picked, before Haiku 5.5 was listed, takes it then
writeAnthropicCatalog(t, mdOpus55)
if err := SetModels("claude", []string{"claude-opus-5-5", "claude-sonnet-5-5"}); err != nil {
t.Fatal(err)
}
if p, _ := find(All(), "claude"); !slices.Equal(p.PickedFrom, []string{"claude-opus-5-5"}) {
t.Fatalf("listed with the picks: %v", p.PickedFrom)
}
writeAnthropicCatalog(t, mdOpus55, mdHaiku55)
if got := claudeExposed(t); !slices.Equal(got, []string{"claude-haiku-5-5", "claude-opus-5-5", "claude-sonnet-5-5"}) {
t.Fatalf("listed since every model was picked: %v", got)
}
// a Save of the same picks keeps the list they were picked from
if err := SetModels("claude", []string{"claude-opus-5-5", "claude-sonnet-5-5", " claude-opus-5-5"}); err != nil {
t.Fatal(err)
}
p, _ := find(All(), "claude")
if err := Save(p); err != nil {
t.Fatal(err)
}
if p, _ := find(All(), "claude"); !slices.Equal(p.PickedFrom, []string{"claude-opus-5-5"}) {
t.Fatalf("listed after a Save of the same picks: %v", p.PickedFrom)
}
}