-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
414 lines (367 loc) · 14.7 KB
/
Copy pathmain_test.go
File metadata and controls
414 lines (367 loc) · 14.7 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package main
import (
"context"
"encoding/base64"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
type fakeModelCacheDecryptor struct {
calls int
input string
uid string
data []byte
err error
}
func (f *fakeModelCacheDecryptor) Decrypt(_ context.Context, input, uid string) ([]byte, error) {
f.calls++
f.input = input
f.uid = uid
return f.data, f.err
}
func modelByKey(catalog []*modelConfig, key string) *modelConfig {
for _, model := range catalog {
if model.Key == key {
return model
}
}
return nil
}
func TestLoadCatalogExplicitFileBypassesDecryptor(t *testing.T) {
path := filepath.Join(t.TempDir(), "catalog.json")
if err := os.WriteFile(path, []byte(`{"chat":[{"key":"explicit","display_name":"Explicit"}]}`), 0o600); err != nil {
t.Fatal(err)
}
decryptor := &fakeModelCacheDecryptor{err: errors.New("must not be called")}
catalog := loadCatalog(context.Background(), decryptor, filepath.Join(t.TempDir(), "auth", "user"), "synthetic-user", path, t.Logf)
if decryptor.calls != 0 {
t.Fatalf("Decrypt() calls = %d, want 0", decryptor.calls)
}
if model := modelByKey(catalog, "explicit"); model == nil || !model.Enable || model.Format != "openai" || model.Source != "system" {
t.Fatalf("explicit model = %#v, want merged defaults", model)
}
}
func TestLoadCatalogUsesModelCacheDecryptor(t *testing.T) {
root := t.TempDir()
authFile := filepath.Join(root, ".auth", "user")
uid := "synthetic-user"
cachePath := filepath.Join(root, ".models", uid, "catalog-v6")
if err := os.MkdirAll(filepath.Dir(cachePath), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cachePath, []byte(" encrypted-catalog\n"), 0o600); err != nil {
t.Fatal(err)
}
decryptor := &fakeModelCacheDecryptor{data: []byte(`{"chat":[{"key":"cached","display_name":"Cached","context_config":{"default":{"token_count":321000}}}]}`)}
catalog := loadCatalog(context.Background(), decryptor, authFile, uid, "", t.Logf)
if decryptor.calls != 1 || decryptor.input != "encrypted-catalog" || decryptor.uid != uid {
t.Fatalf("Decrypt() = calls %d, input %q, uid %q", decryptor.calls, decryptor.input, decryptor.uid)
}
if model := modelByKey(catalog, "cached"); model == nil || model.MaxInputTokens != 321000 || !model.Enable {
t.Fatalf("cached model = %#v, want merged cache model", model)
}
if modelByKey(catalog, "auto") == nil {
t.Fatal("merged catalog lost builtin auto model")
}
}
func TestLoadCatalogFallsBackAfterDecryptError(t *testing.T) {
root := t.TempDir()
authFile := filepath.Join(root, ".auth", "user")
uid := "synthetic-user"
cachePath := filepath.Join(root, ".models", uid, "catalog-v6")
if err := os.MkdirAll(filepath.Dir(cachePath), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cachePath, []byte("encrypted"), 0o600); err != nil {
t.Fatal(err)
}
decryptor := &fakeModelCacheDecryptor{err: errors.New("synthetic decrypt failure")}
catalog := loadCatalog(context.Background(), decryptor, authFile, uid, "", t.Logf)
if decryptor.calls != 1 {
t.Fatalf("Decrypt() calls = %d, want 1", decryptor.calls)
}
if modelByKey(catalog, "auto") == nil {
t.Fatal("fallback catalog is missing builtin auto model")
}
}
func TestLoadCatalogNilDecryptorFallsBack(t *testing.T) {
root := t.TempDir()
authFile := filepath.Join(root, ".auth", "user")
cachePath := filepath.Join(root, ".models", "synthetic-user", "catalog-v6")
if err := os.MkdirAll(filepath.Dir(cachePath), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cachePath, []byte("encrypted"), 0o600); err != nil {
t.Fatal(err)
}
catalog := loadCatalog(context.Background(), nil, authFile, "synthetic-user", "", t.Logf)
if modelByKey(catalog, "auto") == nil {
t.Fatal("fallback catalog is missing builtin auto model")
}
}
func TestLoadCatalogMissingCacheDoesNotDecrypt(t *testing.T) {
decryptor := &fakeModelCacheDecryptor{}
catalog := loadCatalog(context.Background(), decryptor, filepath.Join(t.TempDir(), ".auth", "user"), "synthetic-user", "", t.Logf)
if decryptor.calls != 0 {
t.Fatalf("Decrypt() calls = %d, want 0", decryptor.calls)
}
if modelByKey(catalog, "auto") == nil {
t.Fatal("catalog is missing builtin auto model")
}
}
func TestLoadCatalogMergesNativeQMCV1Cache(t *testing.T) {
root := t.TempDir()
authFile := filepath.Join(root, ".auth", "user")
uid := "synthetic-native-catalog-user"
plain := []byte(`{"chat":[{"key":"native-cached","display_name":"Native Cached","context_config":{"default":{"token_count":654321}}}]}`)
blob, err := encryptQMCV1ForTest(plain, uid, []byte("nonce-12byte"))
if err != nil {
t.Fatalf("catalog plaintext length %d: encrypt returned kind %q", len(plain), protocolErrorKindOf(err))
}
writeDynamicCatalogCache(t, authFile, uid, blob)
catalog := loadCatalog(context.Background(), nativeModelCacheDecryptor{}, authFile, uid, "", func(string, ...any) {})
model := modelByKey(catalog, "native-cached")
if model == nil || !model.Enable || model.Format != "openai" || model.Source != "system" || model.MaxInputTokens != 654321 {
t.Fatal("native QMC catalog model was not merged with approved defaults")
}
if modelByKey(catalog, "auto") == nil {
t.Fatal("native QMC merge lost builtin auto model")
}
}
func TestLoadCatalogNativeQMCV1FailuresRetainBuiltinCatalog(t *testing.T) {
const uid = "synthetic-fallback-catalog-user"
validPlain := []byte(`{"chat":[{"key":"must-not-merge","display_name":"Must Not Merge"}]}`)
valid, err := encryptQMCV1ForTest(validPlain, uid, []byte("nonce-12byte"))
if err != nil {
t.Fatalf("valid fallback setup length %d: encrypt returned kind %q", len(validPlain), protocolErrorKindOf(err))
}
unknownVersion := mutateCatalogQMCBlob(t, valid, len(qmcV1Prefix)-1)
badTag := mutateCatalogQMCBlob(t, valid, -1)
wrongUIDBlob, err := encryptQMCV1ForTest(validPlain, "synthetic-other-catalog-user", []byte("nonce-12byte"))
if err != nil {
t.Fatalf("wrong-UID setup length %d: encrypt returned kind %q", len(validPlain), protocolErrorKindOf(err))
}
invalidJSON, err := encryptQMCV1ForTest([]byte("synthetic invalid JSON"), uid, []byte("nonce-12byte"))
if err != nil {
t.Fatalf("invalid-JSON setup length %d: encrypt returned kind %q", len("synthetic invalid JSON"), protocolErrorKindOf(err))
}
tests := []struct {
name string
blob string
writeFile bool
decryptor modelCacheDecryptor
}{
{name: "unknown version", blob: unknownVersion, writeFile: true, decryptor: nativeModelCacheDecryptor{}},
{name: "bad tag", blob: badTag, writeFile: true, decryptor: nativeModelCacheDecryptor{}},
{name: "wrong UID", blob: wrongUIDBlob, writeFile: true, decryptor: nativeModelCacheDecryptor{}},
{name: "invalid JSON", blob: invalidJSON, writeFile: true, decryptor: nativeModelCacheDecryptor{}},
{name: "missing file", decryptor: nativeModelCacheDecryptor{}},
{name: "nil decryptor", blob: valid, writeFile: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
root := t.TempDir()
authFile := filepath.Join(root, ".auth", "user")
if tt.writeFile {
writeDynamicCatalogCache(t, authFile, uid, tt.blob)
}
catalog := loadCatalog(context.Background(), tt.decryptor, authFile, uid, "", func(string, ...any) {})
if modelByKey(catalog, "auto") == nil {
t.Fatalf("case %s: fallback catalog lacks builtin auto model", tt.name)
}
if modelByKey(catalog, "must-not-merge") != nil {
t.Fatalf("case %s: invalid dynamic catalog was merged", tt.name)
}
})
}
}
func TestCatalogReadErrorDetailOmitsPath(t *testing.T) {
const path = "/SENTINEL-DYNAMIC-CATALOG-UID/catalog-v6"
err := &os.PathError{Op: "open", Path: path, Err: os.ErrPermission}
detail := catalogReadErrorDetail(err)
if !strings.Contains(detail, "open") || !strings.Contains(detail, os.ErrPermission.Error()) {
t.Fatalf("detail = %q, want operation and errno", detail)
}
if strings.Contains(detail, path) || strings.Contains(detail, "SENTINEL-DYNAMIC-CATALOG-UID") {
t.Fatalf("detail leaked path: %q", detail)
}
}
func TestCatalogDecryptErrorDetailUsesTypedProtocolCauseButHidesArbitraryText(t *testing.T) {
typed := newProtocolError(
protocolBackendIncompatible,
"model cache data is incompatible",
errors.New("model cache envelope authentication failed"),
)
typedDetail := catalogDecryptErrorDetail(typed)
if !strings.Contains(typedDetail, string(protocolBackendIncompatible)) ||
!strings.Contains(typedDetail, "authentication failed") {
t.Fatalf("typed detail = %q", typedDetail)
}
arbitrary := errors.New("SENTINEL-DYNAMIC-CATALOG-ERROR")
arbitraryDetail := catalogDecryptErrorDetail(arbitrary)
if strings.Contains(arbitraryDetail, arbitrary.Error()) || arbitraryDetail == "" {
t.Fatalf("arbitrary detail = %q", arbitraryDetail)
}
wrappedCancellation := fmt.Errorf("SENTINEL-CATALOG-CANCELLATION: %w", context.Canceled)
cancellationDetail := catalogDecryptErrorDetail(wrappedCancellation)
if cancellationDetail != context.Canceled.Error() {
t.Fatalf("wrapped cancellation detail = %q, want %q", cancellationDetail, context.Canceled)
}
}
func TestLoadCatalogDynamicCacheLoggingIsValueFree(t *testing.T) {
const uid = "SENTINEL-DYNAMIC-CATALOG-UID"
const blob = "SENTINEL-DYNAMIC-CATALOG-BLOB"
const plain = "SENTINEL-DYNAMIC-CATALOG-PLAINTEXT"
t.Run("decrypt failure", func(t *testing.T) {
root := t.TempDir()
authFile := filepath.Join(root, ".auth", "user")
cachePath := writeDynamicCatalogCache(t, authFile, uid, blob)
decryptErr := errors.New("SENTINEL-DYNAMIC-CATALOG-ERROR " + uid + " " + blob + " " + plain + " " + cachePath)
decryptor := &fakeModelCacheDecryptor{err: decryptErr}
logs, logf := captureCatalogLogs()
catalog := loadCatalog(context.Background(), decryptor, authFile, uid, "", logf)
if modelByKey(catalog, "auto") == nil {
t.Fatal("decrypt failure fallback lacks builtin auto model")
}
output := logs.String()
if !strings.Contains(output, "catalog decrypt failed") {
t.Fatal("decrypt failure log lacks safe category")
}
assertCatalogLogOmits(t, output, uid, blob, plain, cachePath, "SENTINEL-DYNAMIC-CATALOG-ERROR")
})
t.Run("typed decrypt failure includes safe detail", func(t *testing.T) {
root := t.TempDir()
authFile := filepath.Join(root, ".auth", "user")
cachePath := writeDynamicCatalogCache(t, authFile, uid, blob)
decryptor := &fakeModelCacheDecryptor{err: newProtocolError(
protocolBackendIncompatible,
"model cache data is incompatible",
errors.New("model cache envelope authentication failed"),
)}
logs, logf := captureCatalogLogs()
catalog := loadCatalog(context.Background(), decryptor, authFile, uid, "", logf)
if modelByKey(catalog, "auto") == nil {
t.Fatal("typed decrypt failure fallback lacks builtin auto model")
}
output := logs.String()
if !strings.Contains(output, "kind=backend-incompatible") || !strings.Contains(output, "authentication failed") {
t.Fatalf("typed decrypt failure log lacks safe detail: %q", output)
}
assertCatalogLogOmits(t, output, uid, blob, cachePath)
})
t.Run("missing cache is silent", func(t *testing.T) {
root := t.TempDir()
authFile := filepath.Join(root, ".auth", "user")
logs, logf := captureCatalogLogs()
catalog := loadCatalog(context.Background(), nativeModelCacheDecryptor{}, authFile, uid, "", logf)
if modelByKey(catalog, "auto") == nil {
t.Fatal("missing cache fallback lacks builtin auto model")
}
if output := logs.String(); output != "" {
t.Fatalf("missing cache log = %q, want silence", output)
}
})
t.Run("nil decryptor", func(t *testing.T) {
root := t.TempDir()
authFile := filepath.Join(root, ".auth", "user")
cachePath := writeDynamicCatalogCache(t, authFile, uid, blob)
logs, logf := captureCatalogLogs()
catalog := loadCatalog(context.Background(), nil, authFile, uid, "", logf)
if modelByKey(catalog, "auto") == nil {
t.Fatal("nil decryptor fallback lacks builtin auto model")
}
output := logs.String()
if !strings.Contains(output, "catalog decrypt failed") {
t.Fatal("nil decryptor log lacks safe category")
}
assertCatalogLogOmits(t, output, uid, blob, cachePath)
})
}
func writeDynamicCatalogCache(t *testing.T, authFile, uid, blob string) string {
t.Helper()
cachePath := filepath.Clean(filepath.Join(filepath.Dir(authFile), "..", ".models", uid, "catalog-v6"))
if err := os.MkdirAll(filepath.Dir(cachePath), 0o755); err != nil {
t.Fatal("create dynamic catalog directory failed")
}
if err := os.WriteFile(cachePath, []byte(blob), 0o600); err != nil {
t.Fatalf("write dynamic catalog blob length %d failed", len(blob))
}
return cachePath
}
func mutateCatalogQMCBlob(t *testing.T, blob string, index int) string {
t.Helper()
envelope, err := decodeStrictStdBase64(blob)
if err != nil {
t.Fatalf("decode mutation setup blob length %d: strict decode failed", len(blob))
}
if index < 0 {
index = len(envelope) - 1
}
changed := append([]byte(nil), envelope...)
changed[index] ^= 1
return base64.StdEncoding.EncodeToString(changed)
}
func captureCatalogLogs() (*strings.Builder, func(string, ...any)) {
var logs strings.Builder
return &logs, func(format string, args ...any) {
_, _ = fmt.Fprintf(&logs, format, args...)
logs.WriteByte('\n')
}
}
func assertCatalogLogOmits(t *testing.T, output string, forbidden ...string) {
t.Helper()
for _, value := range forbidden {
if value != "" && strings.Contains(output, value) {
t.Fatal("dynamic catalog log contains a forbidden content marker")
}
}
}
func TestQoderProtocolVersion(t *testing.T) {
if qoderProtocolVersion != "1.1.34" {
t.Fatalf("qoderProtocolVersion = %q, want %q", qoderProtocolVersion, "1.1.34")
}
if qoderUserAgent() != "qoder/1.1.34" {
t.Fatalf("qoderUserAgent() = %q, want %q", qoderUserAgent(), "qoder/1.1.34")
}
}
func TestBusinessInfoUsesQoderProtocolVersion(t *testing.T) {
got := businessInfo([]upMessage{{Role: "user", Content: "synthetic"}})
if got["version"] != qoderProtocolVersion {
t.Fatalf("businessInfo version = %v, want %q", got["version"], qoderProtocolVersion)
}
}
func TestBuiltinCatalogContainsApprovedStableModels(t *testing.T) {
models := make(map[string]*modelConfig)
for _, model := range builtinCatalog() {
models[model.Key] = model
}
cmodel, ok := models["cmodel"]
if !ok {
t.Fatal("builtinCatalog is missing cmodel")
}
if cmodel.DisplayName != "Cantus" {
t.Fatalf("cmodel DisplayName = %q, want Cantus", cmodel.DisplayName)
}
gmodel, ok := models["gmodel"]
if !ok {
t.Fatal("builtinCatalog is missing gmodel")
}
if gmodel.DisplayName != "GLM-5.3" {
t.Fatalf("gmodel DisplayName = %q, want GLM-5.3", gmodel.DisplayName)
}
if !gmodel.IsReasoning {
t.Fatal("gmodel IsReasoning = false, want true")
}
if !gmodel.IsVL {
t.Fatal("gmodel IsVL = false, want true")
}
if gmodel.MaxInputTokens != 1_000_000 {
t.Fatalf("gmodel MaxInputTokens = %d, want 1000000", gmodel.MaxInputTokens)
}
if _, ok := models["qwen3.8-v116-dogfood-crit"]; ok {
t.Fatal("builtinCatalog contains qwen3.8-v116-dogfood-crit")
}
}