返回 DeepSeek-Reasonix
meterconfig_test.go
根目录 / cmd / e2ebench / meterconfig_test.go
1 package main
2
3 import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "github.com/BurntSushi/toml"
10 )
11
12 const twoProviderConfig = `
13 [[providers]]
14 name = "deepseek"
15 kind = "openai"
16 base_url = "https://api.deepseek.com"
17 models = ["deepseek-v4-flash", "deepseek-v4-pro"]
18 api_key_env = "DEEPSEEK_API_KEY"
19
20 [[providers]]
21 name = "kimi"
22 kind = "openai"
23 base_url = "https://api.moonshot.cn/v1"
24 models = ["kimi-k2"]
25 api_key_env = "MOONSHOT_API_KEY"
26 `
27
28 func writeConfig(t *testing.T, body string) string {
29 t.Helper()
30 path := filepath.Join(t.TempDir(), "config.toml")
31 if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
32 t.Fatal(err)
33 }
34 return path
35 }
36
37 func readProviders(t *testing.T, dir string) []map[string]any {
38 t.Helper()
39 raw, err := os.ReadFile(filepath.Join(dir, "config.toml"))
40 if err != nil {
41 t.Fatalf("read metered config: %v", err)
42 }
43 var doc map[string]any
44 if err := toml.Unmarshal(raw, &doc); err != nil {
45 t.Fatalf("parse metered config: %v", err)
46 }
47 var out []map[string]any
48 switch list := doc["providers"].(type) {
49 case []map[string]any:
50 out = list
51 case []any:
52 for _, entry := range list {
53 p, _ := entry.(map[string]any)
54 out = append(out, p)
55 }
56 }
57 return out
58 }
59
60 func TestMeterUpstreamFindsTheProviderServingTheModel(t *testing.T) {
61 path := writeConfig(t, twoProviderConfig)
62 got, err := meterUpstream(path, "kimi-k2")
63 if err != nil {
64 t.Fatalf("meterUpstream: %v", err)
65 }
66 if got != "https://api.moonshot.cn/v1" {
67 t.Fatalf("upstream = %q, want the provider that serves the model", got)
68 }
69 }
70
71 func TestMeterUpstreamAcceptsAVendorQualifiedModel(t *testing.T) {
72 path := writeConfig(t, twoProviderConfig)
73 got, err := meterUpstream(path, "Kimi/kimi-k2")
74 if err != nil {
75 t.Fatalf("meterUpstream: %v", err)
76 }
77 if got != "https://api.moonshot.cn/v1" {
78 t.Fatalf("upstream = %q, want the vendor prefix stripped before matching", got)
79 }
80 }
81
82 func TestMeterUpstreamFallsBackToTheFirstProvider(t *testing.T) {
83 path := writeConfig(t, twoProviderConfig)
84 got, err := meterUpstream(path, "")
85 if err != nil {
86 t.Fatalf("meterUpstream: %v", err)
87 }
88 if got != "https://api.deepseek.com" {
89 t.Fatalf("upstream = %q, want the first provider", got)
90 }
91 }
92
93 // Rewriting every endpoint would send one vendor's traffic to another's host.
94 func TestWriteMeteredConfigRedirectsOnlyTheBenchmarkedProvider(t *testing.T) {
95 path := writeConfig(t, twoProviderConfig)
96 dir := t.TempDir()
97 if err := writeMeteredConfig(path, dir, "kimi-k2", "http://127.0.0.1:9999"); err != nil {
98 t.Fatalf("writeMeteredConfig: %v", err)
99 }
100 providers := readProviders(t, dir)
101 if len(providers) != 2 {
102 t.Fatalf("providers = %d, want both preserved", len(providers))
103 }
104 byName := map[string]map[string]any{}
105 for _, p := range providers {
106 name, _ := p["name"].(string)
107 byName[name] = p
108 }
109 if got := byName["kimi"]["base_url"]; got != "http://127.0.0.1:9999" {
110 t.Fatalf("kimi base_url = %v, want the meter", got)
111 }
112 if got := byName["deepseek"]["base_url"]; got != "https://api.deepseek.com" {
113 t.Fatalf("deepseek base_url = %v, want it left alone", got)
114 }
115 if got := byName["kimi"]["no_proxy"]; got != true {
116 t.Fatalf("no_proxy = %v, want true: the meter is loopback plaintext", got)
117 }
118 }
119
120 // The key lives in an env var the child inherits, so metering must never need
121 // to read, copy, or rewrite a credential.
122 func TestWriteMeteredConfigKeepsCredentialsUntouched(t *testing.T) {
123 path := writeConfig(t, twoProviderConfig)
124 dir := t.TempDir()
125 if err := writeMeteredConfig(path, dir, "deepseek-v4-flash", "http://127.0.0.1:1"); err != nil {
126 t.Fatalf("writeMeteredConfig: %v", err)
127 }
128 raw, err := os.ReadFile(filepath.Join(dir, "config.toml"))
129 if err != nil {
130 t.Fatal(err)
131 }
132 if !strings.Contains(string(raw), "DEEPSEEK_API_KEY") {
133 t.Fatalf("api_key_env was dropped:\n%s", raw)
134 }
135 if strings.Contains(string(raw), "api_key =") {
136 t.Fatalf("a literal key appeared in the metered config:\n%s", raw)
137 }
138 }
139
140 func TestMeterUpstreamRejectsAConfigWithNoProviders(t *testing.T) {
141 path := writeConfig(t, "model = \"x\"\n")
142 if _, err := meterUpstream(path, ""); err == nil {
143 t.Fatal("a config with no providers must fail loudly, not meter nothing")
144 }
145 }
146
146 lines GO