| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // fakeCodexLogin is the pinned app-server as a Codex sign-in from the |
| 12 | // app meets it: the device code (the account that approves it is in |
| 13 | // <root>/.signs-in), then, in every later start of the signed-in home, |
| 14 | // the recorded startup notices and a rate-limit notice before |
| 15 | // account/read answers. <root>/.home-account, when there, is the account |
| 16 | // the sign-in leaves in the home instead of the one that approved it. |
| 17 | const fakeCodexLogin = `#!/bin/sh |
| 18 | case "$*" in |
| 19 | '--version') echo 'codex-cli 0.165.0'; exit 0;; |
| 20 | 'login --device-auth --help'|'logout') exit 0;; |
| 21 | 'app-server --strict-config --listen stdio://') ;; |
| 22 | *) exit 2;; |
| 23 | esac |
| 24 | root=$(dirname "$CODEX_HOME") |
| 25 | email=$(cat "$CODEX_HOME/auth.json" 2>/dev/null) |
| 26 | while IFS= read -r line; do |
| 27 | id=$(printf '%s' "$line" | sed -n 's/.*"id":\("[^"]*"\).*/\1/p') |
| 28 | case "$line" in |
| 29 | *'"method":"initialize"'*) |
| 30 | printf '{"id":%s,"result":{"userAgent":"equipo/0.165.0 (fake; linux)","codexHome":"%s","platformFamily":"unix","platformOs":"linux"}}\n' "$id" "$CODEX_HOME";; |
| 31 | *'"method":"initialized"'*) |
| 32 | if [ -n "$email" ]; then |
| 33 | 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":{}}}' |
| 34 | fi;; |
| 35 | *'"method":"account/login/start"'*) |
| 36 | printf '{"id":%s,"result":{"type":"chatgptDeviceCode","loginId":"login-1","verificationUrl":"https://auth.openai.com/codex/device","userCode":"FAKE-1234"}}\n' "$id" |
| 37 | email=$(cat "$root/.signs-in") |
| 38 | if [ -f "$root/.home-account" ]; then cat "$root/.home-account" > "$CODEX_HOME/auth.json"; else printf '%s' "$email" > "$CODEX_HOME/auth.json"; fi |
| 39 | printf '%s\n' '{"method":"account/login/completed","params":{"loginId":"login-1","success":true,"error":null}}' '{"method":"account/updated","params":{"authMode":"chatgpt","planType":"pro"}}';; |
| 40 | *'"method":"account/read"'*) |
| 41 | printf '{"id":%s,"result":{"account":{"type":"chatgpt","email":"%s","planType":"pro"},"requiresOpenaiAuth":true}}\n' "$id" "$email";; |
| 42 | *'"method":"account/rateLimits/read"'*) |
| 43 | 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";; |
| 44 | esac |
| 45 | done |
| 46 | ` |
| 47 | |
| 48 | // codexRig is a connector whose Codex is fakeCodexLogin and whose person |
| 49 | // signs in as email. |
| 50 | func codexRig(t *testing.T, email string) (*tokenRig, *Connector) { |
| 51 | r := newTokenRig(t) |
| 52 | r.cfg.CodexPath = filepath.Join(r.dir, "bin", "codex") |
| 53 | if err := os.WriteFile(r.cfg.CodexPath, []byte(fakeCodexLogin), 0755); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := os.WriteFile(filepath.Join(r.root, ".signs-in"), []byte(email), 0600); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | return r, r.connector() |
| 60 | } |
| 61 | |
| 62 | // James's report: a Codex sign-in that went through at OpenAI ended "The |
| 63 | // sign-in did not match an account." The home is read once after the |
| 64 | // sign-in, past the app-server's notices that are not about the account, |
| 65 | // and the connection is the account the person signed in. |
| 66 | func TestConnectorCodexSignIn(t *testing.T) { |
| 67 | r, c := codexRig(t, "a@example.com") |
| 68 | 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 { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | end := r.ended("cn_codex_1") |
| 72 | if end.State != wire.ConnHealthy || end.LocalID != "codex-1" || end.Label != "a@example.com" { |
| 73 | t.Fatalf("end %+v\n%s", end, r.logs.String()) |
| 74 | } |
| 75 | b, ok := r.binding("codex-1") |
| 76 | if !ok || b.Provider != wire.ProviderCodex || b.AccountID != "a@example.com" || b.PersonID != connPerson || b.ProviderHome != filepath.Join(r.root, "codex-1") { |
| 77 | t.Fatalf("binding %+v %v", b, ok) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // A home that holds another account than the one the person signed in |
| 82 | // is refused, and nothing of it is kept. |
| 83 | func TestConnectorCodexSignInMismatch(t *testing.T) { |
| 84 | r, c := codexRig(t, "a@example.com") |
| 85 | if err := os.WriteFile(filepath.Join(r.root, ".home-account"), []byte("b@example.com"), 0600); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | 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 { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | end := r.ended("cn_codex_2") |
| 92 | if end.State != wire.ConnError || end.Reason != "identity_mismatch" { |
| 93 | t.Fatalf("end %+v", end) |
| 94 | } |
| 95 | if b, ok := r.binding("codex-1"); ok { |
| 96 | t.Fatalf("the mismatched sign-in was bound: %+v", b) |
| 97 | } |
| 98 | if _, err := os.Stat(filepath.Join(r.root, "codex-1")); !os.IsNotExist(err) { |
| 99 | t.Fatalf("the home stayed: %v", err) |
| 100 | } |
| 101 | } |
| 102 | |