-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathcodex_executor_refresh_test.go
More file actions
97 lines (88 loc) · 3.14 KB
/
Copy pathcodex_executor_refresh_test.go
File metadata and controls
97 lines (88 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package executor
import (
"encoding/base64"
"encoding/json"
"testing"
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
)
func TestApplyCodexCapacityClaimsUpdatesPlanType(t *testing.T) {
auth := &cliproxyauth.Auth{
ID: "codex-auth",
Provider: "codex",
Attributes: map[string]string{
"plan_type": "free",
},
Metadata: map[string]any{
"chatgpt_plan_type": "free",
},
}
applyCodexCapacityClaims(auth, codexCapacityTestJWT(t, map[string]any{
"email": "user@example.com",
"https://api.openai.com/auth": map[string]any{
"chatgpt_account_id": "acct-123",
"chatgpt_plan_type": "plus",
"chatgpt_subscription_active_start": "2026-06-12T00:00:00Z",
"chatgpt_subscription_active_until": "2026-07-12T00:00:00Z",
"chatgpt_subscription_last_checked": "2026-06-12T00:01:00Z",
},
}))
if got := auth.Attributes["plan_type"]; got != "plus" {
t.Fatalf("plan_type attribute = %q, want plus", got)
}
if got := auth.Metadata["chatgpt_plan_type"]; got != "plus" {
t.Fatalf("chatgpt_plan_type metadata = %q, want plus", got)
}
if got := auth.Metadata["chatgpt_account_id"]; got != "acct-123" {
t.Fatalf("chatgpt_account_id metadata = %q, want acct-123", got)
}
if got := auth.Metadata["chatgpt_subscription_active_until"]; got != "2026-07-12T00:00:00Z" {
t.Fatalf("chatgpt_subscription_active_until metadata = %q, want 2026-07-12T00:00:00Z", got)
}
}
func TestApplyCodexCapacityClaimsPreservesMissingPlanAndClearsMissingValues(t *testing.T) {
auth := &cliproxyauth.Auth{
ID: "codex-auth",
Provider: "codex",
Attributes: map[string]string{
"plan_type": "plus",
},
Metadata: map[string]any{
"chatgpt_account_id": "acct-123",
"chatgpt_plan_type": "plus",
"chatgpt_subscription_active_start": "2026-06-12T00:00:00Z",
"chatgpt_subscription_active_until": "2026-07-12T00:00:00Z",
},
}
applyCodexCapacityClaims(auth, codexCapacityTestJWT(t, map[string]any{
"email": "user@example.com",
"https://api.openai.com/auth": map[string]any{},
}))
if got := auth.Attributes["plan_type"]; got != "plus" {
t.Fatalf("expected missing plan claim to preserve plan_type attribute, got %q", got)
}
if got := auth.Metadata["chatgpt_plan_type"]; got != "plus" {
t.Fatalf("expected missing plan claim to preserve chatgpt_plan_type metadata, got %q", got)
}
for _, key := range []string{
"chatgpt_account_id",
"chatgpt_subscription_active_start",
"chatgpt_subscription_active_until",
} {
if _, ok := auth.Metadata[key]; ok {
t.Fatalf("expected missing claim to clear metadata %q", key)
}
}
}
func codexCapacityTestJWT(t *testing.T, claims map[string]any) string {
t.Helper()
header, errMarshalHeader := json.Marshal(map[string]string{"alg": "none", "typ": "JWT"})
if errMarshalHeader != nil {
t.Fatalf("marshal jwt header: %v", errMarshalHeader)
}
payload, errMarshalPayload := json.Marshal(claims)
if errMarshalPayload != nil {
t.Fatalf("marshal jwt claims: %v", errMarshalPayload)
}
return base64.RawURLEncoding.EncodeToString(header) + "." +
base64.RawURLEncoding.EncodeToString(payload) + "."
}