Repository navigation
Expand file tree
/
Copy pathparser_test.go
More file actions
191 lines (166 loc) · 6.32 KB
/
Copy pathparser_test.go
File metadata and controls
191 lines (166 loc) · 6.32 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package codexswitch
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestNormalizeStandardOfficialAuthAddsDefaults(t *testing.T) {
authRaw := mustReadSample(t, "codex", "auth.json")
var root map[string]json.RawMessage
if err := json.Unmarshal([]byte(authRaw), &root); err != nil {
t.Fatalf("unmarshal official sample failed: %v", err)
}
delete(root, "auth_mode")
delete(root, "OPENAI_API_KEY")
trimmedRaw, err := marshalCanonicalJSON(root)
if err != nil {
t.Fatalf("marshal trimmed official auth failed: %v", err)
}
normalizedRaw, inputFormat, err := normalizeAuthJSON(trimmedRaw)
if err != nil {
t.Fatalf("normalizeAuthJSON returned error: %v", err)
}
if inputFormat != officialAuthInputFormatStandard {
t.Fatalf("expected standard format, got %s", inputFormat)
}
var normalized map[string]json.RawMessage
if err := json.Unmarshal([]byte(normalizedRaw), &normalized); err != nil {
t.Fatalf("unmarshal normalized auth failed: %v", err)
}
authMode, ok := optionalJSONStringField(normalized, "auth_mode")
if !ok || authMode != officialAuthModeTemplate {
t.Fatalf("expected auth_mode=%q, got %q", officialAuthModeTemplate, authMode)
}
if rawValue, ok := normalized["OPENAI_API_KEY"]; !ok || strings.TrimSpace(string(rawValue)) != "null" {
t.Fatalf("expected OPENAI_API_KEY=null, got %s", string(rawValue))
}
}
func TestNormalizeCLIOfficialAuthSample(t *testing.T) {
authRaw := mustReadSample(t, "codex", "cli-auth.json")
normalizedRaw, inputFormat, err := normalizeAuthJSON(authRaw)
if err != nil {
t.Fatalf("normalizeAuthJSON returned error: %v", err)
}
if inputFormat != officialAuthInputFormatCLI {
t.Fatalf("expected cli format, got %s", inputFormat)
}
if strings.Contains(normalizedRaw, "\"access_token\":") && !strings.Contains(normalizedRaw, "\"tokens\":") {
t.Fatalf("expected CLI auth to be wrapped into tokens: %s", normalizedRaw)
}
snapshot, err := buildProfileSnapshot(normalizedRaw, mustReadSample(t, "codex", "config.toml"), profileSourceImportedFileCLI, time.Date(2026, 3, 19, 0, 0, 0, 0, time.UTC))
if err != nil {
t.Fatalf("buildProfileSnapshot returned error: %v", err)
}
if snapshot.Meta.Type != ProfileTypeOfficial {
t.Fatalf("expected official profile, got %s", snapshot.Meta.Type)
}
if snapshot.Meta.Email != "cli-user@example.com" {
t.Fatalf("unexpected email: %s", snapshot.Meta.Email)
}
}
func TestNormalizeCLIOfficialAuthRejectsMissingSensitiveField(t *testing.T) {
authRaw := mustReadSample(t, "codex", "cli-auth.json")
var root map[string]json.RawMessage
if err := json.Unmarshal([]byte(authRaw), &root); err != nil {
t.Fatalf("unmarshal CLI sample failed: %v", err)
}
delete(root, "refresh_token")
trimmedRaw, err := marshalCanonicalJSON(root)
if err != nil {
t.Fatalf("marshal broken CLI auth failed: %v", err)
}
_, _, err = normalizeAuthJSON(trimmedRaw)
if err == nil {
t.Fatal("expected normalizeAuthJSON to fail when refresh_token is missing")
}
if !strings.Contains(err.Error(), "refresh_token") {
t.Fatalf("expected refresh_token error, got %v", err)
}
}
func TestBuildProfileSnapshotOfficialSample(t *testing.T) {
authRaw := mustReadSample(t, "codex", "auth.json")
configRaw := mustReadSample(t, "codex", "config.toml")
snapshot, err := buildProfileSnapshot(authRaw, configRaw, "imported_current", time.Date(2026, 3, 19, 0, 0, 0, 0, time.UTC))
if err != nil {
t.Fatalf("buildProfileSnapshot returned error: %v", err)
}
if snapshot.Meta.Type != ProfileTypeOfficial {
t.Fatalf("expected official profile, got %s", snapshot.Meta.Type)
}
if snapshot.Meta.Email != "official-team@example.com" {
t.Fatalf("unexpected email: %s", snapshot.Meta.Email)
}
if snapshot.Meta.PlanType != "team" {
t.Fatalf("unexpected plan type: %s", snapshot.Meta.PlanType)
}
if snapshot.Meta.ChatGPTAccountID == "" {
t.Fatal("expected chatgpt account id")
}
if snapshot.Meta.ClientID == "" {
t.Fatal("expected client id")
}
if snapshot.Meta.Model != "gpt-5.4" {
t.Fatalf("unexpected model: %s", snapshot.Meta.Model)
}
}
func TestBuildProfileSnapshotAPISample(t *testing.T) {
authRaw := mustReadSample(t, "api", "auth.json")
configRaw := mustReadSample(t, "api", "config.toml")
snapshot, err := buildProfileSnapshot(authRaw, configRaw, "imported_current", time.Date(2026, 3, 19, 0, 0, 0, 0, time.UTC))
if err != nil {
t.Fatalf("buildProfileSnapshot returned error: %v", err)
}
if snapshot.Meta.Type != ProfileTypeAPI {
t.Fatalf("expected api profile, got %s", snapshot.Meta.Type)
}
if snapshot.Meta.BaseURL != "https://api.example.com/v1" {
t.Fatalf("unexpected base url: %s", snapshot.Meta.BaseURL)
}
if snapshot.Meta.ModelReasoningEffort != "xhigh" {
t.Fatalf("unexpected reasoning effort: %s", snapshot.Meta.ModelReasoningEffort)
}
if !strings.Contains(snapshot.Meta.MaskedAPIKey, "*") {
t.Fatalf("expected masked api key, got %s", snapshot.Meta.MaskedAPIKey)
}
}
func TestBuildAPIProfileFromTemplate(t *testing.T) {
service := newTestService(t)
snapshot, err := service.buildAPIProfile(APIProfileInput{
BaseURL: "https://api.openai.com/v1",
Model: "gpt-5.4",
ModelReasoningEffort: "xhigh",
ModelContextWindow: "128000",
APIKey: "sk-test-1234567890",
})
if err != nil {
t.Fatalf("buildAPIProfile returned error: %v", err)
}
if snapshot.Meta.Type != ProfileTypeAPI {
t.Fatalf("expected api profile, got %s", snapshot.Meta.Type)
}
if !strings.Contains(snapshot.ConfigRaw, "base_url = \"https://api.openai.com/v1\"") {
t.Fatalf("config template not rendered correctly: %s", snapshot.ConfigRaw)
}
if !strings.Contains(snapshot.ConfigRaw, "model_context_window = 128000") {
t.Fatalf("expected model_context_window to be rendered, got %s", snapshot.ConfigRaw)
}
if !strings.Contains(snapshot.ConfigRaw, "model_auto_compact_token_limit = 115200") {
t.Fatalf("expected model_auto_compact_token_limit to follow context window, got %s", snapshot.ConfigRaw)
}
if !strings.Contains(snapshot.AuthRaw, "sk-test-1234567890") {
t.Fatalf("auth template not rendered correctly: %s", snapshot.AuthRaw)
}
}
func mustReadSample(t *testing.T, parts ...string) string {
t.Helper()
allParts := append([]string{"..", "..", "conf"}, parts...)
path := filepath.Join(allParts...)
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read sample %s failed: %v", path, err)
}
return string(data)
}