-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
541 lines (470 loc) · 13.4 KB
/
test.js
File metadata and controls
541 lines (470 loc) · 13.4 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const { execFileSync } = require("node:child_process");
const {
countTokens,
countMessages,
getModelInfo,
fitsContextWindow,
estimateCost
} = require("./index");
test("returns 0 for empty text", () => {
assert.equal(countTokens("", "gpt-4o"), 0);
});
test("estimates tokens for OpenAI models", () => {
const result = countTokens("Hello world from OpenAI", "gpt-4o");
assert.equal(typeof result, "number");
assert.ok(result >= 1);
});
test("estimates tokens for Claude models", () => {
const result = countTokens("Hello world from Claude", "claude-3-haiku");
assert.equal(typeof result, "number");
assert.ok(result >= 1);
});
test("estimates tokens for Gemini models", () => {
const result = countTokens("Hello world from Gemini", "gemini-2.0-flash");
assert.equal(typeof result, "number");
assert.ok(result >= 1);
});
test("supports Claude family names without the claude prefix", () => {
const result = countTokens(
"Plan a release note summary with highlights and follow-up tasks.",
"sonnet-4"
);
assert.equal(typeof result, "number");
assert.ok(result >= 1);
});
test("supports Claude opus aliases with spaces", () => {
const result = countTokens(
"Review this incident summary and provide executive highlights.",
"opus 4.6"
);
assert.equal(typeof result, "number");
assert.ok(result >= 1);
});
test("returns model metadata for supported models", () => {
assert.deepEqual(getModelInfo("opus 4.6"), {
provider: "claude",
family: "claude",
normalizedModel: "opus-4-6",
contextWindow: 200000,
supportsMessages: true,
matchedBy: "opus"
});
});
test("produces different estimates for different providers", () => {
const text =
"A longer sentence with punctuation, numbers 12345, and enough content " +
"to make the provider-specific heuristics diverge after rounding.";
assert.ok(
countTokens(text, "gpt-4.1-mini") > countTokens(text, "claude-3-7-sonnet")
);
});
test("throws for unsupported model names", () => {
assert.throws(
() => countTokens("Hello", "mistral-small"),
/Unsupported model/
);
});
test("throws for invalid text input", () => {
assert.throws(() => countTokens(null, "gpt-4o"), /text must be a string/);
});
test("throws for unsupported model info lookups", () => {
assert.throws(() => getModelInfo("mistral-small"), /Unsupported model/);
});
test("counts dense mixed content higher than plain prose", () => {
const plainText = "Summarize the customer call and extract action items.";
const denseText = "Summarize call #48291 ASAP!!! Include 3 risks, 2 owners, and status ✅";
assert.ok(
countTokens(denseText, "gpt-4o") > countTokens(plainText, "gpt-4o")
);
});
test("counts chat-style message arrays", () => {
const messages = [
{
role: "system",
content: "You are a concise release notes assistant."
},
{
role: "user",
content: "Summarize the latest release and list three breaking changes."
}
];
const result = countMessages(messages, "gpt-4o");
assert.equal(typeof result, "number");
assert.ok(result > countTokens(messages[0].content, "gpt-4o"));
});
test("supports Claude models when counting messages", () => {
const messages = [
{
role: "user",
content: "Draft a customer-ready incident summary."
},
{
role: "assistant",
content: "I can help with that."
}
];
assert.ok(countMessages(messages, "sonnet-4") >= 1);
});
test("supports Gemini models when counting messages", () => {
const messages = [
{
role: "user",
content: "Summarize this product feedback into a concise update."
}
];
assert.ok(countMessages(messages, "gemini-1.5-pro") >= 1);
});
test("fitsContextWindow returns prompt budget details for text", () => {
const result = fitsContextWindow(
"Summarize this support thread.",
"gpt-4o",
500
);
assert.equal(result.provider, "openai");
assert.equal(result.contextWindow, 128000);
assert.equal(result.reservedOutputTokens, 500);
assert.ok(result.inputTokens >= 1);
assert.equal(result.fits, true);
});
test("fitsContextWindow supports chat-style messages", () => {
const result = fitsContextWindow(
[{ role: "user", content: "Summarize this outage report." }],
"gemini-1.5-pro",
1000
);
assert.equal(result.provider, "gemini");
assert.equal(result.contextWindow, 1000000);
assert.equal(result.fits, true);
});
test("fitsContextWindow returns false when reserved output exceeds capacity", () => {
const result = fitsContextWindow("hello", "sonnet-4", 500000);
assert.equal(result.availableInputTokens, 0);
assert.equal(result.fits, false);
});
test("returns 0 for an empty messages array", () => {
assert.equal(countMessages([], "gpt-4o"), 0);
});
test("throws for invalid message collections", () => {
assert.throws(() => countMessages("not-an-array", "gpt-4o"), /messages must be an array/);
assert.throws(
() => countMessages([{ role: "user", content: 42 }], "gpt-4o"),
/each message content must be a string/
);
});
test("fitsContextWindow validates its inputs", () => {
assert.throws(
() => fitsContextWindow({ text: "hello" }, "gpt-4o"),
/input must be a string or an array of messages/
);
assert.throws(
() => fitsContextWindow("hello", "gpt-4o", -1),
/maxOutputTokens must be a non-negative integer/
);
});
test("estimateCost returns totals for text input", () => {
const result = estimateCost("Summarize this issue.", "gpt-4o", {
outputTokens: 500
});
assert.equal(result.provider, "openai");
assert.equal(result.model, "gpt-4o");
assert.ok(result.inputTokens >= 1);
assert.equal(result.outputTokensReserved, 500);
assert.equal(result.totalTokensEstimated, result.inputTokens + 500);
assert.ok(result.estimatedInputCost > 0);
assert.ok(result.estimatedOutputCost > 0);
assert.ok(result.estimatedTotalCost > result.estimatedInputCost);
});
test("estimateCost supports message arrays and fallback pricing", () => {
const result = estimateCost(
[{ role: "user", content: "Summarize this issue and key risks." }],
"claude-3-5-sonnet"
);
assert.equal(result.provider, "claude");
assert.equal(result.outputTokensReserved, 0);
assert.ok(result.estimatedInputCost > 0);
});
test("estimateCost validates options", () => {
assert.throws(
() => estimateCost("hello", "gpt-4o", { outputTokens: -1 }),
/options.outputTokens must be a non-negative integer/
);
assert.throws(
() => estimateCost({ text: "hello" }, "gpt-4o"),
/input must be a string or an array of messages/
);
});
test("cli counts direct text input", () => {
const output = execFileSync(
process.execPath,
["./cli.js", "Summarize this pull request.", "--model", "gpt-4o"],
{
cwd: __dirname,
encoding: "utf8"
}
).trim();
assert.match(output, /^\d+$/);
});
test("cli counts file input", () => {
const fixturePath = path.join(__dirname, "tmp-cli-input.txt");
try {
fs.writeFileSync(
fixturePath,
"Review this release note and extract the top three changes.",
"utf8"
);
const output = execFileSync(
process.execPath,
["./cli.js", "--model", "sonnet-4", "--file", fixturePath],
{
cwd: __dirname,
encoding: "utf8"
}
).trim();
assert.match(output, /^\d+$/);
} finally {
if (fs.existsSync(fixturePath)) {
fs.unlinkSync(fixturePath);
}
}
});
test("cli cost mode prints formatted cost output", () => {
const output = execFileSync(
process.execPath,
[
"./cli.js",
"--cost",
"--model",
"gpt-4o",
"--output-tokens",
"100",
"Explain Kubernetes in 2 sentences."
],
{
cwd: __dirname,
encoding: "utf8"
}
).trim();
assert.match(output, /Tokens:\s+\d+/);
assert.match(output, /Estimated cost:\s+\$/);
assert.match(output, /Provider:\s+OpenAI/);
});
test("cli json mode returns machine-readable token output", () => {
const output = execFileSync(
process.execPath,
["./cli.js", "--json", "--model", "gpt-4o", "Summarize this pull request."],
{
cwd: __dirname,
encoding: "utf8"
}
).trim();
const result = JSON.parse(output);
assert.equal(result.mode, "tokens");
assert.equal(result.provider, "openai");
assert.equal(result.model, "gpt-4o");
assert.equal(typeof result.tokens, "number");
});
test("cli json mode returns machine-readable cost output", () => {
const output = execFileSync(
process.execPath,
[
"./cli.js",
"--json",
"--cost",
"--model",
"gpt-4o",
"--output-tokens",
"100",
"Explain Kubernetes in 2 sentences."
],
{
cwd: __dirname,
encoding: "utf8"
}
).trim();
const result = JSON.parse(output);
assert.equal(result.mode, "cost");
assert.equal(result.provider, "openai");
assert.equal(result.model, "gpt-4o");
assert.equal(result.outputTokensReserved, 100);
assert.equal(typeof result.estimatedTotalCost, "number");
});
test("cli counts tokens from --messages-file", () => {
const fixturePath = path.join(__dirname, "tmp-cli-messages.json");
try {
fs.writeFileSync(
fixturePath,
JSON.stringify(
[
{
role: "system",
content: "You are a concise assistant."
},
{
role: "user",
content: "Summarize this release note."
}
],
null,
2
),
"utf8"
);
const output = execFileSync(
process.execPath,
["./cli.js", "--model", "sonnet-4", "--messages-file", fixturePath],
{
cwd: __dirname,
encoding: "utf8"
}
).trim();
assert.match(output, /^\d+$/);
} finally {
if (fs.existsSync(fixturePath)) {
fs.unlinkSync(fixturePath);
}
}
});
test("cli cost mode supports --messages-file", () => {
const fixturePath = path.join(__dirname, "tmp-cli-messages-cost.json");
try {
fs.writeFileSync(
fixturePath,
JSON.stringify(
[
{
role: "user",
content: "Summarize this incident and list three next steps."
}
],
null,
2
),
"utf8"
);
const output = execFileSync(
process.execPath,
[
"./cli.js",
"--cost",
"--model",
"gpt-4o",
"--messages-file",
fixturePath
],
{
cwd: __dirname,
encoding: "utf8"
}
).trim();
assert.match(output, /Tokens:\s+\d+/);
assert.match(output, /Estimated cost:\s+\$/);
assert.match(output, /Provider:\s+OpenAI/);
} finally {
if (fs.existsSync(fixturePath)) {
fs.unlinkSync(fixturePath);
}
}
});
test("cli json mode supports --messages-file", () => {
const fixturePath = path.join(__dirname, "tmp-cli-messages-json.json");
try {
fs.writeFileSync(
fixturePath,
JSON.stringify(
[
{
role: "user",
content: "Review this outage summary."
}
],
null,
2
),
"utf8"
);
const output = execFileSync(
process.execPath,
[
"./cli.js",
"--json",
"--cost",
"--model",
"gemini-1.5-pro",
"--messages-file",
fixturePath
],
{
cwd: __dirname,
encoding: "utf8"
}
).trim();
const result = JSON.parse(output);
assert.equal(result.mode, "cost");
assert.equal(result.inputType, "messages");
assert.equal(result.provider, "gemini");
assert.equal(typeof result.inputTokens, "number");
} finally {
if (fs.existsSync(fixturePath)) {
fs.unlinkSync(fixturePath);
}
}
});
test("cli supports --stdin for plain text", () => {
const output = execFileSync(
process.execPath,
["./cli.js", "--stdin", "--model", "gpt-4o"],
{
cwd: __dirname,
encoding: "utf8",
input: "Summarize this deployment incident."
}
).trim();
assert.match(output, /^\d+$/);
});
test("cli supports --stdin for JSON messages in --json mode", () => {
const output = execFileSync(
process.execPath,
["./cli.js", "--stdin", "--json", "--model", "sonnet-4"],
{
cwd: __dirname,
encoding: "utf8",
input: JSON.stringify([
{
role: "system",
content: "You are a concise incident analyst."
},
{
role: "user",
content: "Summarize the top three risks from this outage."
}
])
}
).trim();
const result = JSON.parse(output);
assert.equal(result.mode, "tokens");
assert.equal(result.inputType, "messages");
assert.equal(result.provider, "claude");
assert.equal(typeof result.tokens, "number");
});
test("cli supports --stdin with --cost and --json", () => {
const output = execFileSync(
process.execPath,
["./cli.js", "--stdin", "--json", "--cost", "--model", "gpt-4o", "--output-tokens", "150"],
{
cwd: __dirname,
encoding: "utf8",
input: "Explain Kubernetes in two sentences."
}
).trim();
const result = JSON.parse(output);
assert.equal(result.mode, "cost");
assert.equal(result.inputType, "text");
assert.equal(result.provider, "openai");
assert.equal(result.outputTokensReserved, 150);
assert.equal(typeof result.estimatedTotalCost, "number");
});