-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathmain_test.go
More file actions
315 lines (275 loc) · 7.31 KB
/
Copy pathmain_test.go
File metadata and controls
315 lines (275 loc) · 7.31 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/aibridge/prices/pricebook"
)
func TestCompare(t *testing.T) {
t.Parallel()
cases := []struct {
name string
old []pricebook.Row
new []pricebook.Row
wantAdded []string
wantRemoved []string
wantChanged []string
}{
{
name: "identical",
old: []pricebook.Row{row("anthropic", "claude", 1, 2)},
new: []pricebook.Row{row("anthropic", "claude", 1, 2)},
},
{
name: "added",
old: []pricebook.Row{
row("anthropic", "claude", 1, 2),
},
new: []pricebook.Row{
row("anthropic", "claude", 1, 2),
row("openai", "gpt", 3, 4),
},
wantAdded: []string{"openai/gpt"},
},
{
name: "removed",
old: []pricebook.Row{
row("anthropic", "claude", 1, 2),
row("openai", "gpt", 3, 4),
},
new: []pricebook.Row{
row("anthropic", "claude", 1, 2),
},
wantRemoved: []string{"openai/gpt"},
},
{
name: "changed",
old: []pricebook.Row{row("anthropic", "claude", 1, 2)},
new: []pricebook.Row{row("anthropic", "claude", 1, 5)},
wantChanged: []string{"anthropic/claude"},
},
{
// Every price field participates, not just input and output.
name: "cache price changed",
old: []pricebook.Row{{
Provider: "anthropic",
Model: "claude",
CacheReadPrice: int64Ptr(1),
}},
new: []pricebook.Row{{
Provider: "anthropic",
Model: "claude",
CacheReadPrice: int64Ptr(2),
}},
wantChanged: []string{"anthropic/claude"},
},
{
// A previously missing price becoming populated is a change.
name: "price set",
old: []pricebook.Row{{
Provider: "anthropic",
Model: "claude",
}},
new: []pricebook.Row{{
Provider: "anthropic",
Model: "claude",
InputPrice: int64Ptr(1),
}},
wantChanged: []string{"anthropic/claude"},
},
{
// A model whose price becomes null is a change, not a removal.
name: "price unset",
old: []pricebook.Row{
row("anthropic", "claude", 1, 2),
},
new: []pricebook.Row{{
Provider: "anthropic",
Model: "claude",
InputPrice: int64Ptr(1),
}},
wantChanged: []string{"anthropic/claude"},
},
{
// Zero is a populated price, distinct from an absent one.
name: "null becomes zero",
old: []pricebook.Row{{
Provider: "openai",
Model: "gpt",
}},
new: []pricebook.Row{{
Provider: "openai",
Model: "gpt",
InputPrice: int64Ptr(0),
}},
wantChanged: []string{"openai/gpt"},
},
{
// Zero is a real price, distinct from an absent one.
name: "zero is not null",
old: []pricebook.Row{{
Provider: "openai",
Model: "gpt",
InputPrice: int64Ptr(0),
}},
new: []pricebook.Row{{
Provider: "openai",
Model: "gpt",
}},
wantChanged: []string{"openai/gpt"},
},
{
// Same model identifier under two providers must not collide.
name: "same model different providers",
old: []pricebook.Row{
row("anthropic", "shared", 1, 2),
row("openai", "shared", 1, 2),
},
new: []pricebook.Row{
row("anthropic", "shared", 1, 2),
row("openai", "shared", 9, 2),
},
wantChanged: []string{"openai/shared"},
},
{
name: "added removed and changed together",
old: []pricebook.Row{
row("anthropic", "old-model", 1, 2),
row("openai", "gpt", 3, 4),
},
new: []pricebook.Row{
row("anthropic", "new-model", 5, 6),
row("openai", "gpt", 3, 7),
},
wantAdded: []string{"anthropic/new-model"},
wantRemoved: []string{"anthropic/old-model"},
wantChanged: []string{"openai/gpt"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := compare(tc.old, tc.new)
require.Equal(t, tc.wantAdded, names(got.added))
require.Equal(t, tc.wantRemoved, names(got.removed))
require.Equal(t, tc.wantChanged, names(got.changed))
})
}
}
func TestCompareSortsDeterministically(t *testing.T) {
t.Parallel()
newRows := []pricebook.Row{
row("openai", "b", 1, 1),
row("anthropic", "z", 1, 1),
row("anthropic", "a", 1, 1),
}
got := compare(nil, newRows)
require.Equal(t, []string{"anthropic/a", "anthropic/z", "openai/b"}, names(got.added))
}
func TestRender(t *testing.T) {
t.Parallel()
t.Run("no changes", func(t *testing.T) {
t.Parallel()
out := render(compare(
[]pricebook.Row{row("anthropic", "claude", 1, 2)},
[]pricebook.Row{row("anthropic", "claude", 1, 2)},
))
require.Equal(t, `## Price book changes
No price changes.
`, out)
})
t.Run("full summary", func(t *testing.T) {
t.Parallel()
out := render(compare(
[]pricebook.Row{
row("anthropic", "gone", 1, 2),
row("openai", "gpt", 1, 2),
},
[]pricebook.Row{
row("anthropic", "fresh", 3, 4),
row("openai", "gpt", 2, 3),
},
))
// Asserted whole rather than by substring so section order, spacing,
// and the absence of stray content are covered too.
require.Equal(t, `## Price book changes
1 model added, 1 model removed, 1 model changed.
<details>
<summary>Added</summary>
- anthropic/fresh
</details>
<details>
<summary>Removed</summary>
- anthropic/gone
</details>
<details>
<summary>Changed</summary>
- openai/gpt
</details>
`, out)
})
}
func TestRun(t *testing.T) {
t.Parallel()
t.Run("missing flags", func(t *testing.T) {
t.Parallel()
var out strings.Builder
require.ErrorContains(t, run("", "", &out), "-old and -new are both required")
})
t.Run("reads files", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
oldPath := filepath.Join(dir, "old.json")
newPath := filepath.Join(dir, "new.json")
writeFile(t, oldPath, `[{"provider":"openai","model":"gpt","input_price":1000000,"output_price":2000000,"cache_read_price":null,"cache_write_price":null}]`)
writeFile(t, newPath, `[{"provider":"openai","model":"gpt","input_price":1500000,"output_price":2000000,"cache_read_price":null,"cache_write_price":null}]`)
var out strings.Builder
require.NoError(t, run(oldPath, newPath, &out))
require.Contains(t, out.String(), "<summary>Changed</summary>\n\n- openai/gpt\n")
})
t.Run("invalid json", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "bad.json")
writeFile(t, path, "{")
var out strings.Builder
err := run(path, path, &out)
// The path is part of the message so a failed refresh names the file
// that could not be read.
require.ErrorContains(t, err, path)
require.ErrorContains(t, err, "parse:")
})
t.Run("missing file", func(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "absent.json")
var out strings.Builder
err := run(path, path, &out)
require.ErrorIs(t, err, os.ErrNotExist)
require.ErrorContains(t, err, path)
})
}
func row(provider, model string, input, output int64) pricebook.Row {
return pricebook.Row{
Provider: provider,
Model: model,
InputPrice: int64Ptr(input),
OutputPrice: int64Ptr(output),
}
}
func names(keys []pricebook.Key) []string {
if len(keys) == 0 {
return nil
}
out := make([]string, 0, len(keys))
for _, k := range keys {
out = append(out, k.String())
}
return out
}
func writeFile(t *testing.T, path, content string) {
t.Helper()
require.NoError(t, os.WriteFile(path, []byte(content), 0o600))
}
func int64Ptr(v int64) *int64 { return &v }