package provider import ( "context" "encoding/json" "os" "path/filepath" "strings" "testing" ) // The controller's ruling #7 (connectors spec rev 3.2 ยง6): a Codex // reading is refreshed by the app-server's account/rateLimits/read, in // the source home with the login probe's environment, after account/read // and never with a thread or a turn. The fake app-server answers only // those requests and fails on anything else; a notification it sends in // between is passed over. func TestReadCodexLimitsWithoutTurn(t *testing.T) { root := t.TempDir() binary := filepath.Join(root, "codex") script := `#!/bin/sh case "$*" in '--version') echo 'codex-cli 0.165.0'; exit 0;; 'login --device-auth --help') exit 0;; esac [ "$PWD" = "$HOME" ] || exit 20 [ "$CODEX_HOME" = "$HOME" ] || exit 22 IFS= read -r line printf '{"id":"1","result":{"userAgent":"equipo/0.165.0 fixture","codexHome":"%s"}}\n' "$CODEX_HOME" IFS= read -r line case "$line" in *'"initialized"'*) ;; *) exit 23;; esac IFS= read -r line case "$line" in *'"account/read"'*'"refreshToken":false'*) ;; *) exit 24;; esac echo '{"id":"2","result":{"account":{"type":"chatgpt","email":"fixture@example.invalid"}}}' IFS= read -r line case "$line" in *'"account/rateLimits/read"'*) ;; *) echo "$line" > "$HOME/other"; exit 25;; esac echo '{"method":"account/rateLimits/updated","params":{"rateLimits":{}}}' echo done > "$HOME/read" echo '{"id":"3","result":{"rateLimits":{"primary":{"usedPercent":61,"windowDurationMins":300,"resetsAt":1762941600},"secondary":{"usedPercent":12,"windowDurationMins":10080,"resetsAt":1763287200},"planType":"pro","rateLimitReachedType":null}}}' IFS= read -r line && echo "$line" > "$HOME/other" exit 0 ` if err := os.WriteFile(binary, []byte(script), 0700); err != nil { t.Fatal(err) } id, raw, err := ReadCodexLimits(context.Background(), Binding{Home: root, Provider: "codex"}, LoginOptions{CodexPath: binary}) if err != nil || id.AccountID != "fixture@example.invalid" { t.Fatal(id, err) } var v struct { Primary struct { UsedPercent float64 `json:"usedPercent"` } `json:"primary"` } if json.Unmarshal(raw, &v) != nil || v.Primary.UsedPercent != 61 { t.Fatalf("limits %s", raw) } if _, err := os.Stat(filepath.Join(root, "read")); err != nil { t.Fatal("the rate limits were not read", err) } if other, err := os.ReadFile(filepath.Join(root, "other")); err == nil && strings.TrimSpace(string(other)) != "" { t.Fatalf("the probe sent more: %s", other) } if _, _, err := ReadCodexLimits(context.Background(), Binding{Home: root, Provider: "claude"}, LoginOptions{CodexPath: binary}); err == nil { t.Fatal("the Codex probe accepted a Claude home") } }