package thin import ( "os" "path/filepath" "testing" "amadan.net/oficina/equipo/internal/wire" ) // fakeCodexLogin is the pinned app-server as a Codex sign-in from the // app meets it: the device code (the account that approves it is in // /.signs-in), then, in every later start of the signed-in home, // the recorded startup notices and a rate-limit notice before // account/read answers. /.home-account, when there, is the account // the sign-in leaves in the home instead of the one that approved it. const fakeCodexLogin = `#!/bin/sh case "$*" in '--version') echo 'codex-cli 0.165.0'; exit 0;; 'login --device-auth --help'|'logout') exit 0;; 'app-server --strict-config --listen stdio://') ;; *) exit 2;; esac root=$(dirname "$CODEX_HOME") email=$(cat "$CODEX_HOME/auth.json" 2>/dev/null) while IFS= read -r line; do id=$(printf '%s' "$line" | sed -n 's/.*"id":\("[^"]*"\).*/\1/p') case "$line" in *'"method":"initialize"'*) printf '{"id":%s,"result":{"userAgent":"equipo/0.165.0 (fake; linux)","codexHome":"%s","platformFamily":"unix","platformOs":"linux"}}\n' "$id" "$CODEX_HOME";; *'"method":"initialized"'*) if [ -n "$email" ]; then printf '%s\n' '{"method":"remoteControl/status/changed","params":{"status":"disabled","serverName":"fake","installationId":"fake-installation","environmentId":null}}' '{"method":"account/updated","params":{"authMode":"chatgpt","planType":"pro"}}' '{"method":"account/rateLimits/updated","params":{"rateLimits":{}}}' fi;; *'"method":"account/login/start"'*) printf '{"id":%s,"result":{"type":"chatgptDeviceCode","loginId":"login-1","verificationUrl":"https://auth.openai.com/codex/device","userCode":"FAKE-1234"}}\n' "$id" email=$(cat "$root/.signs-in") if [ -f "$root/.home-account" ]; then cat "$root/.home-account" > "$CODEX_HOME/auth.json"; else printf '%s' "$email" > "$CODEX_HOME/auth.json"; fi printf '%s\n' '{"method":"account/login/completed","params":{"loginId":"login-1","success":true,"error":null}}' '{"method":"account/updated","params":{"authMode":"chatgpt","planType":"pro"}}';; *'"method":"account/read"'*) printf '{"id":%s,"result":{"account":{"type":"chatgpt","email":"%s","planType":"pro"},"requiresOpenaiAuth":true}}\n' "$id" "$email";; *'"method":"account/rateLimits/read"'*) printf '{"id":%s,"result":{"rateLimits":{"primary":{"usedPercent":10,"windowDurationMins":300,"resetsAt":1762941600},"secondary":{"usedPercent":5,"windowDurationMins":10080,"resetsAt":1763287200},"planType":"pro","rateLimitReachedType":null}}}\n' "$id";; esac done ` // codexRig is a connector whose Codex is fakeCodexLogin and whose person // signs in as email. func codexRig(t *testing.T, email string) (*tokenRig, *Connector) { r := newTokenRig(t) r.cfg.CodexPath = filepath.Join(r.dir, "bin", "codex") if err := os.WriteFile(r.cfg.CodexPath, []byte(fakeCodexLogin), 0755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(r.root, ".signs-in"), []byte(email), 0600); err != nil { t.Fatal(err) } return r, r.connector() } // James's report: a Codex sign-in that went through at OpenAI ended "The // sign-in did not match an account." The home is read once after the // sign-in, past the app-server's notices that are not about the account, // and the connection is the account the person signed in. func TestConnectorCodexSignIn(t *testing.T) { r, c := codexRig(t, "a@example.com") if err := c.Accept(r.ctx, "f-codex-1", wire.ConnectCommand{Connection: "cn_codex_1", Person: connPerson, Provider: wire.ProviderCodex, Kind: wire.KindSubscription}); err != nil { t.Fatal(err) } end := r.ended("cn_codex_1") if end.State != wire.ConnHealthy || end.LocalID != "codex-1" || end.Label != "a@example.com" { t.Fatalf("end %+v\n%s", end, r.logs.String()) } b, ok := r.binding("codex-1") if !ok || b.Provider != wire.ProviderCodex || b.AccountID != "a@example.com" || b.PersonID != connPerson || b.ProviderHome != filepath.Join(r.root, "codex-1") { t.Fatalf("binding %+v %v", b, ok) } } // A home that holds another account than the one the person signed in // is refused, and nothing of it is kept. func TestConnectorCodexSignInMismatch(t *testing.T) { r, c := codexRig(t, "a@example.com") if err := os.WriteFile(filepath.Join(r.root, ".home-account"), []byte("b@example.com"), 0600); err != nil { t.Fatal(err) } if err := c.Accept(r.ctx, "f-codex-2", wire.ConnectCommand{Connection: "cn_codex_2", Person: connPerson, Provider: wire.ProviderCodex, Kind: wire.KindSubscription}); err != nil { t.Fatal(err) } end := r.ended("cn_codex_2") if end.State != wire.ConnError || end.Reason != "identity_mismatch" { t.Fatalf("end %+v", end) } if b, ok := r.binding("codex-1"); ok { t.Fatalf("the mismatched sign-in was bound: %+v", b) } if _, err := os.Stat(filepath.Join(r.root, "codex-1")); !os.IsNotExist(err) { t.Fatalf("the home stayed: %v", err) } }