| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // The controller's ruling #7 (connectors spec rev 3.2 §6): a Codex |
| 13 | // reading is refreshed by the app-server's account/rateLimits/read, in |
| 14 | // the source home with the login probe's environment, after account/read |
| 15 | // and never with a thread or a turn. The fake app-server answers only |
| 16 | // those requests and fails on anything else; a notification it sends in |
| 17 | // between is passed over. |
| 18 | func TestReadCodexLimitsWithoutTurn(t *testing.T) { |
| 19 | root := t.TempDir() |
| 20 | binary := filepath.Join(root, "codex") |
| 21 | script := `#!/bin/sh |
| 22 | case "$*" in |
| 23 | '--version') echo 'codex-cli 0.165.0'; exit 0;; |
| 24 | 'login --device-auth --help') exit 0;; |
| 25 | esac |
| 26 | [ "$PWD" = "$HOME" ] || exit 20 |
| 27 | [ "$CODEX_HOME" = "$HOME" ] || exit 22 |
| 28 | IFS= read -r line |
| 29 | printf '{"id":"1","result":{"userAgent":"equipo/0.165.0 fixture","codexHome":"%s"}}\n' "$CODEX_HOME" |
| 30 | IFS= read -r line |
| 31 | case "$line" in *'"initialized"'*) ;; *) exit 23;; esac |
| 32 | IFS= read -r line |
| 33 | case "$line" in *'"account/read"'*'"refreshToken":false'*) ;; *) exit 24;; esac |
| 34 | echo '{"id":"2","result":{"account":{"type":"chatgpt","email":"fixture@example.invalid"}}}' |
| 35 | IFS= read -r line |
| 36 | case "$line" in *'"account/rateLimits/read"'*) ;; *) echo "$line" > "$HOME/other"; exit 25;; esac |
| 37 | echo '{"method":"account/rateLimits/updated","params":{"rateLimits":{}}}' |
| 38 | echo done > "$HOME/read" |
| 39 | echo '{"id":"3","result":{"rateLimits":{"primary":{"usedPercent":61,"windowDurationMins":300,"resetsAt":1762941600},"secondary":{"usedPercent":12,"windowDurationMins":10080,"resetsAt":1763287200},"planType":"pro","rateLimitReachedType":null}}}' |
| 40 | IFS= read -r line && echo "$line" > "$HOME/other" |
| 41 | exit 0 |
| 42 | ` |
| 43 | if err := os.WriteFile(binary, []byte(script), 0700); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | id, raw, err := ReadCodexLimits(context.Background(), Binding{Home: root, Provider: "codex"}, LoginOptions{CodexPath: binary}) |
| 47 | if err != nil || id.AccountID != "fixture@example.invalid" { |
| 48 | t.Fatal(id, err) |
| 49 | } |
| 50 | var v struct { |
| 51 | Primary struct { |
| 52 | UsedPercent float64 `json:"usedPercent"` |
| 53 | } `json:"primary"` |
| 54 | } |
| 55 | if json.Unmarshal(raw, &v) != nil || v.Primary.UsedPercent != 61 { |
| 56 | t.Fatalf("limits %s", raw) |
| 57 | } |
| 58 | if _, err := os.Stat(filepath.Join(root, "read")); err != nil { |
| 59 | t.Fatal("the rate limits were not read", err) |
| 60 | } |
| 61 | if other, err := os.ReadFile(filepath.Join(root, "other")); err == nil && strings.TrimSpace(string(other)) != "" { |
| 62 | t.Fatalf("the probe sent more: %s", other) |
| 63 | } |
| 64 | if _, _, err := ReadCodexLimits(context.Background(), Binding{Home: root, Provider: "claude"}, LoginOptions{CodexPath: binary}); err == nil { |
| 65 | t.Fatal("the Codex probe accepted a Claude home") |
| 66 | } |
| 67 | } |
| 68 | |