"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.
83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// A moved Factory's keys are the plugin's accounts: an imported key is kept
|
|
// by the plugin as its own "Factory API key" sign-in keeps one, an api
|
|
// account with whoami's metadata, and not as a built-in account the moved
|
|
// Factory never reads. The same key again is "exists".
|
|
func TestFactoryAPIKeyMoved(t *testing.T) {
|
|
signIn(t)
|
|
bun := testenv.Bun(t)
|
|
t.Setenv("MAGPIE_BUN", bun)
|
|
t.Setenv("FAKE_ID", "factory")
|
|
t.Cleanup(plugin.Settle)
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
|
|
if _, err := plugin.Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := setMigration("factory", func(m *Migration) { m.State, m.Package = MovePlugin, abs }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const good = "fk-good_0123456789abcdef"
|
|
factorySite(t, func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/api/cli/whoami" || r.Header.Get("Authorization") != "Bearer "+good {
|
|
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
|
|
w.WriteHeader(404)
|
|
return
|
|
}
|
|
factoryJSON(w, 200, map[string]any{"userId": "user_k", "orgId": "fac_K", "email": "key@example.com"})
|
|
})
|
|
res, err := ImportFactoryKeys(ctx, []string{good})
|
|
if err != nil || len(res) != 1 || res[0].Status != "added" || res[0].User != "key@example.com" {
|
|
t.Fatalf("imported %+v, %v", res, err)
|
|
}
|
|
var kept []map[string]any
|
|
for _, a := range plugin.Auths("factory") {
|
|
kept = append(kept, a)
|
|
}
|
|
if len(kept) != 1 || kept[0]["type"] != "api" || kept[0]["key"] != good {
|
|
t.Fatalf("the plugin keeps %v", kept)
|
|
}
|
|
if md, _ := kept[0]["metadata"].(map[string]any); md["email"] != "key@example.com" || md["userId"] != "user_k" {
|
|
t.Fatalf("the key's metadata %v", kept[0]["metadata"])
|
|
}
|
|
if ls := factoryLogins(); len(ls) != 0 {
|
|
t.Fatalf("the key was kept by the built-in too: %+v", ls)
|
|
}
|
|
if res, err = ImportFactoryKeys(ctx, []string{good}); err != nil || len(res) != 1 || res[0].Status != "exists" {
|
|
t.Fatalf("again: %+v, %v", res, err)
|
|
}
|
|
if n := len(plugin.Auths("factory")); n != 1 {
|
|
t.Fatalf("again: the plugin keeps %d accounts", n)
|
|
}
|
|
}
|
|
|
|
// factorySite is a fake Factory API at the root, its EU region under /eu.
|
|
func factorySite(t *testing.T, h http.HandlerFunc) *httptest.Server {
|
|
t.Helper()
|
|
srv := httptest.NewServer(h)
|
|
t.Cleanup(srv.Close)
|
|
t.Cleanup(FactoryBaseForTest(srv.URL, srv.URL+"/eu"))
|
|
return srv
|
|
}
|
|
|
|
func factoryJSON(w http.ResponseWriter, code int, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(code)
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|