dsh-usage-peak 0.0.0-stage → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,184 @@
1
+ import assert from 'node:assert/strict';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import test from 'node:test';
6
+ import zlib from 'node:zlib';
7
+
8
+ import {
9
+ aggregateTurns,
10
+ decodeSessionFile,
11
+ emptyDay,
12
+ isPeakAt,
13
+ nextSwitch,
14
+ normalizeDays,
15
+ parseSessionTurns,
16
+ priceFor,
17
+ } from '../index.js';
18
+
19
+ /** Beijing wall clock -> epoch ms. */
20
+ const bj = (y, mo, d, h, mi = 0) => Date.UTC(y, mo - 1, d, h, mi) - 8 * 3600_000;
21
+
22
+ test('priceFor picks the pro table only for pro models', () => {
23
+ assert.equal(priceFor('deepseek-flash').id, 'flash');
24
+ assert.equal(priceFor('deepseek-pro').id, 'pro');
25
+ assert.equal(priceFor('some-unknown-model').id, 'flash');
26
+ assert.equal(priceFor(undefined).id, 'flash');
27
+ });
28
+
29
+ test('isPeakAt covers the four weekday windows and nothing else', () => {
30
+ // 2026-11-09 is a Monday.
31
+ assert.equal(isPeakAt(bj(2026, 11, 9, 8, 59)), false);
32
+ assert.equal(isPeakAt(bj(2026, 11, 9, 9, 0)), true);
33
+ assert.equal(isPeakAt(bj(2026, 11, 9, 11, 59)), true);
34
+ assert.equal(isPeakAt(bj(2026, 11, 9, 12, 0)), false);
35
+ assert.equal(isPeakAt(bj(2026, 11, 9, 14, 0)), true);
36
+ assert.equal(isPeakAt(bj(2026, 11, 9, 17, 59)), true);
37
+ assert.equal(isPeakAt(bj(2026, 11, 9, 18, 0)), false);
38
+ assert.equal(isPeakAt(bj(2026, 11, 9, 23, 30)), false);
39
+ // Saturday / Sunday are always valley.
40
+ assert.equal(isPeakAt(bj(2026, 11, 7, 10, 0)), false);
41
+ assert.equal(isPeakAt(bj(2026, 11, 8, 15, 0)), false);
42
+ });
43
+
44
+ test('nextSwitch walks to the next boundary and reports the target status', () => {
45
+ const lunch = nextSwitch(bj(2026, 11, 9, 12, 30));
46
+ assert.equal(lunch.status, 'peak');
47
+ assert.equal(lunch.at, bj(2026, 11, 9, 14, 0));
48
+
49
+ const evening = nextSwitch(bj(2026, 11, 9, 18, 0));
50
+ assert.equal(evening.status, 'peak');
51
+ assert.equal(evening.at, bj(2026, 11, 10, 9, 0));
52
+
53
+ const morning = nextSwitch(bj(2026, 11, 9, 7, 15));
54
+ assert.equal(morning.status, 'peak');
55
+ assert.equal(morning.at, bj(2026, 11, 9, 9, 0));
56
+
57
+ // Friday after close: the next switch is Monday morning, not Saturday.
58
+ const weekend = nextSwitch(bj(2026, 11, 13, 19, 0));
59
+ assert.equal(weekend.status, 'peak');
60
+ assert.equal(weekend.at, bj(2026, 11, 16, 9, 0));
61
+
62
+ // Inside a peak window the next switch is back to valley.
63
+ const insidePeak = nextSwitch(bj(2026, 11, 9, 10, 0));
64
+ assert.equal(insidePeak.status, 'valley');
65
+ assert.equal(insidePeak.at, bj(2026, 11, 9, 12, 0));
66
+ });
67
+
68
+ test('decodeSessionFile stitches concatenated zstd frames', () => {
69
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'usage-peak-'));
70
+ const file = path.join(dir, 'session.v4.jsonl.zstd');
71
+ const one = Buffer.from('{"type":"assistant/message","time":1}\n');
72
+ const two = Buffer.from('{"type":"assistant/message","time":2}\n');
73
+ fs.writeFileSync(file, Buffer.concat([zlib.zstdCompressSync(one), zlib.zstdCompressSync(two)]));
74
+ try {
75
+ const text = decodeSessionFile(file);
76
+ assert.equal(text, one.toString('utf8') + two.toString('utf8'));
77
+ } finally {
78
+ fs.rmSync(dir, { recursive: true, force: true });
79
+ }
80
+ });
81
+
82
+ test('parseSessionTurns keeps only billed assistant messages', () => {
83
+ const text = [
84
+ '{"type":"user/message","time":100}',
85
+ '{"type":"assistant/message","time":200,"data":{"usage":{"inputTokens":10,"cacheReadTokens":20,"outputTokens":30},"message":{"source":{"model":"deepseek-flash"}}}}',
86
+ '{"type":"assistant/message","time":300,"data":{}}',
87
+ 'not json',
88
+ '',
89
+ '{"type":"assistant/message","time":400,"data":{"usage":{"inputTokens":1},"message":{"source":{"model":"deepseek-pro"}}}}',
90
+ ].join('\n');
91
+
92
+ assert.deepEqual(parseSessionTurns(text), [
93
+ { t: 200, model: 'deepseek-flash', input: 10, cache: 20, output: 30 },
94
+ { t: 400, model: 'deepseek-pro', input: 1, cache: 0, output: 0 },
95
+ ]);
96
+ });
97
+
98
+ test('normalizeDays accepts only the offered windows', () => {
99
+ assert.equal(normalizeDays('7'), 7);
100
+ assert.equal(normalizeDays(30), 30);
101
+ assert.equal(normalizeDays('14'), 14);
102
+ assert.equal(normalizeDays('90'), 14);
103
+ assert.equal(normalizeDays(null), 14);
104
+ assert.equal(normalizeDays(undefined), 14);
105
+ });
106
+
107
+ test('aggregateTurns zero-fills a contiguous window and prices peak at 2x', () => {
108
+ const now = bj(2026, 11, 10, 12, 0);
109
+ const turns = [
110
+ // Peak Monday: 1M miss + 1M hit + 1M out at double price -> 10.04.
111
+ { t: bj(2026, 11, 9, 10, 0), model: 'deepseek-flash', input: 1e6, cache: 1e6, output: 1e6 },
112
+ // Valley Tuesday: same tokens -> 5.02.
113
+ { t: bj(2026, 11, 10, 8, 0), model: 'deepseek-flash', input: 1e6, cache: 1e6, output: 1e6 },
114
+ ];
115
+
116
+ const agg = aggregateTurns(turns, { now, days: 7 });
117
+
118
+ assert.equal(agg.windowDays, 7);
119
+ assert.equal(agg.window.length, 7);
120
+ assert.deepEqual(
121
+ agg.window.map((day) => day.day),
122
+ ['2026-11-04', '2026-11-05', '2026-11-06', '2026-11-07', '2026-11-08', '2026-11-09', '2026-11-10'],
123
+ );
124
+ assert.equal(agg.window[0].calls, 0);
125
+ assert.equal(agg.window[0].cost, 0);
126
+
127
+ assert.equal(agg.window[5].cost, 10.04);
128
+ assert.equal(agg.window[5].peakCost, 10.04);
129
+ assert.equal(agg.window[5].savingIfValley, 5.02);
130
+ assert.equal(agg.window[6].cost, 5.02);
131
+ assert.equal(agg.window[6].valleyCost, 5.02);
132
+ assert.equal(agg.today.day, '2026-11-10');
133
+
134
+ assert.equal(agg.week.cost, 15.06);
135
+ assert.equal(agg.week.calls, 2);
136
+ assert.equal(agg.week.savingIfValley, 5.02);
137
+ assert.equal(agg.week.avgTokensPerCall, 3_000_000);
138
+ assert.equal(agg.week.avgPerDay, 2.1514);
139
+
140
+ assert.deepEqual(agg.models.map((model) => [model.id, model.calls]), [
141
+ ['flash', 2],
142
+ ['pro', 0],
143
+ ]);
144
+ assert.deepEqual(agg.warnings, []);
145
+ });
146
+
147
+ test('aggregateTurns sums per-window models and flags unknown models once', () => {
148
+ const now = bj(2026, 11, 10, 12, 0);
149
+ const turns = [
150
+ { t: bj(2026, 11, 10, 9, 0), model: 'deepseek-flash', input: 1e6, cache: 0, output: 0 },
151
+ { t: bj(2026, 11, 10, 9, 5), model: 'deepseek-pro', input: 1e6, cache: 0, output: 0 },
152
+ { t: bj(2026, 11, 10, 9, 6), model: 'mystery-model', input: 0, cache: 0, output: 1e6 },
153
+ ];
154
+
155
+ const agg = aggregateTurns(turns, { now, days: 7 });
156
+ assert.deepEqual(agg.models.map((model) => [model.id, model.calls]), [
157
+ ['flash', 2],
158
+ ['pro', 1],
159
+ ]);
160
+ assert.deepEqual(agg.warnings, [{ code: 'unknown-model', models: ['mystery-model'] }]);
161
+
162
+ // Windows that end before the turns contain only zero days.
163
+ const old = aggregateTurns(turns, { now, days: 30 });
164
+ assert.equal(old.window.length, 30);
165
+ assert.equal(old.window[0].calls, 0);
166
+ });
167
+
168
+ test('emptyDay starts with 24 empty hour buckets', () => {
169
+ const day = emptyDay('2026-11-10');
170
+ assert.equal(day.hours.length, 24);
171
+ assert.equal(day.hours[0].hour, 0);
172
+ assert.equal(day.hours[23].hour, 23);
173
+ assert.equal(day.calls, 0);
174
+ assert.equal(day.lastAt, 0);
175
+ });
176
+
177
+ test('window edges stay stable across a day boundary', () => {
178
+ const now = bj(2026, 11, 10, 0, 0) + 1; // one ms past midnight
179
+ const agg = aggregateTurns([], { now, days: 7 });
180
+ assert.equal(agg.window[6].day, '2026-11-10');
181
+ assert.equal(agg.window[0].day, '2026-11-04');
182
+ assert.equal(agg.window.length, 7);
183
+ assert.equal(now - bj(2026, 11, 10, 0, 0), 1);
184
+ });