pi-odd-worktree 3.14.0 → 3.15.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.
- package/README.md +1 -0
- package/extensions/odd-worktree-tools.ts +172 -0
- package/lib/alert-mode.js +82 -0
- package/lib/alert-rows.js +82 -0
- package/lib/opencode-usage.js +256 -0
- package/lib/peak-labels.js +64 -0
- package/lib/peak-providers.js +63 -0
- package/lib/peak-schedule.js +242 -0
- package/package.json +1 -1
- package/tools/alert-mode.test.js +140 -0
- package/tools/alert-rows.test.js +196 -0
- package/tools/opencode-usage-alert.test.js +252 -0
- package/tools/opencode-usage-lifecycle.test.js +110 -0
- package/tools/opencode-usage.test.js +321 -0
- package/tools/peak-labels.test.js +111 -0
- package/tools/peak-providers.test.js +109 -0
- package/tools/peak-schedule.test.js +423 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// S1 (opencode-usage-alert): the OpenCode Go usage source. It reads the read-only endpoint the
|
|
3
|
+
// OpenCode console itself uses, maps its three windows into one provider-scoped snapshot, and
|
|
4
|
+
// registers itself through the usage-source contract with the peak schedule as its `plan`.
|
|
5
|
+
// Failures resolve to no snapshot and report a fixed reason code, never the key: the shell
|
|
6
|
+
// keeps its last good snapshot, and only a missing key proves the provider is unusable.
|
|
7
|
+
const { test, describe } = require('node:test');
|
|
8
|
+
const assert = require('node:assert/strict');
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
PROVIDER,
|
|
12
|
+
PENDING_NOTE,
|
|
13
|
+
REQUEST_TIMEOUT_MS,
|
|
14
|
+
USAGE_SOURCE_SCHEMA,
|
|
15
|
+
USAGE_URL,
|
|
16
|
+
createUsageSource,
|
|
17
|
+
fetchOpenCodeGoUsage,
|
|
18
|
+
parseOpenCodeGoUsage,
|
|
19
|
+
} = require('../lib/opencode-usage.js');
|
|
20
|
+
// The plan literals below are asserted against the engine that produces them, for the provider the
|
|
21
|
+
// source publishes: DeepSeek's schedule. The engine is provider-agnostic now (lib/peak-schedule.js
|
|
22
|
+
// reads the registry), so the same call takes the provider explicitly.
|
|
23
|
+
const { providerById } = require('../lib/peak-providers.js');
|
|
24
|
+
const { formatPeakState, peakStateFor } = require('../lib/peak-schedule.js');
|
|
25
|
+
|
|
26
|
+
const NOW = Date.parse('2026-09-23T12:00:00Z');
|
|
27
|
+
// 2026-09-23 is a Wednesday: 03:40 UTC is inside the first peak window, 04:30 UTC between them.
|
|
28
|
+
const PEAK_INSTANT = Date.parse('2026-09-23T03:40:00Z');
|
|
29
|
+
const OFF_PEAK_INSTANT = Date.parse('2026-09-23T04:30:00Z');
|
|
30
|
+
|
|
31
|
+
function payload() {
|
|
32
|
+
return {
|
|
33
|
+
usage: {
|
|
34
|
+
rolling: { status: 'ok', percent: 20, resetsAt: '2026-09-23T17:00:00.000Z' },
|
|
35
|
+
weekly: { status: 'ok', percent: 42.5, resetsAt: '2026-09-30T12:00:00Z' },
|
|
36
|
+
monthly: { status: 'ok', percent: 55, resetsAt: '2026-10-23T12:00:00Z' },
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function response(data = payload(), options = {}) {
|
|
42
|
+
return { ok: true, redirected: false, url: USAGE_URL, json: async () => data, ...options };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('the usage payload', () => {
|
|
46
|
+
test('maps the three Go windows into one provider-scoped snapshot', () => {
|
|
47
|
+
assert.deepEqual(parseOpenCodeGoUsage(payload(), NOW), {
|
|
48
|
+
provider: PROVIDER,
|
|
49
|
+
plan: undefined,
|
|
50
|
+
fetchedAt: NOW,
|
|
51
|
+
limits: [
|
|
52
|
+
{
|
|
53
|
+
name: 'go',
|
|
54
|
+
scope: 'provider',
|
|
55
|
+
limitReached: false,
|
|
56
|
+
windows: [
|
|
57
|
+
{ label: '5h', usedPercent: 20, windowSeconds: 18_000, resetAt: Date.parse('2026-09-23T17:00:00.000Z') },
|
|
58
|
+
{ label: 'week', usedPercent: 42.5, windowSeconds: 604_800, resetAt: Date.parse('2026-09-30T12:00:00Z') },
|
|
59
|
+
{ label: 'month', usedPercent: 55, windowSeconds: 2_592_000, resetAt: Date.parse('2026-10-23T12:00:00Z') },
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('a full window or a rate-limited status marks the limit reached', () => {
|
|
67
|
+
const full = payload();
|
|
68
|
+
full.usage.weekly.percent = 100;
|
|
69
|
+
assert.equal(parseOpenCodeGoUsage(full, NOW)?.limits[0].limitReached, true);
|
|
70
|
+
|
|
71
|
+
const throttled = payload();
|
|
72
|
+
throttled.usage.monthly.status = 'rate-limited';
|
|
73
|
+
assert.equal(parseOpenCodeGoUsage(throttled, NOW)?.limits[0].limitReached, true);
|
|
74
|
+
|
|
75
|
+
const border = payload();
|
|
76
|
+
border.usage.rolling.percent = 0;
|
|
77
|
+
assert.equal(parseOpenCodeGoUsage(border, NOW)?.limits[0].limitReached, false);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('any window that is missing or malformed rejects the whole snapshot', () => {
|
|
81
|
+
for (const key of ['rolling', 'weekly', 'monthly']) {
|
|
82
|
+
const missing = payload();
|
|
83
|
+
delete missing.usage[key];
|
|
84
|
+
assert.equal(parseOpenCodeGoUsage(missing, NOW), undefined, `missing ${key}`);
|
|
85
|
+
|
|
86
|
+
for (const broken of [
|
|
87
|
+
{ status: 'ok', percent: '20', resetsAt: '2026-09-23T17:00:00Z' },
|
|
88
|
+
{ status: 'ok', percent: 20 },
|
|
89
|
+
{ status: 'ok', percent: 20, resetsAt: 'yesterday' },
|
|
90
|
+
{ status: 'ok', percent: -1, resetsAt: '2026-09-23T17:00:00Z' },
|
|
91
|
+
{ status: 'ok', percent: 101, resetsAt: '2026-09-23T17:00:00Z' },
|
|
92
|
+
{ status: 'ok', percent: Number.NaN, resetsAt: '2026-09-23T17:00:00Z' },
|
|
93
|
+
{ status: 'active', percent: 20, resetsAt: '2026-09-23T17:00:00Z' },
|
|
94
|
+
{ percent: 20, resetsAt: '2026-09-23T17:00:00Z' },
|
|
95
|
+
null,
|
|
96
|
+
[],
|
|
97
|
+
]) {
|
|
98
|
+
const data = payload();
|
|
99
|
+
data.usage[key] = broken;
|
|
100
|
+
assert.equal(parseOpenCodeGoUsage(data, NOW), undefined, `broken ${key}: ${JSON.stringify(broken)}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
assert.equal(parseOpenCodeGoUsage({}, NOW), undefined);
|
|
104
|
+
assert.equal(parseOpenCodeGoUsage({ usage: [] }, NOW), undefined);
|
|
105
|
+
assert.equal(parseOpenCodeGoUsage({ usage: 'nope' }, NOW), undefined);
|
|
106
|
+
assert.equal(parseOpenCodeGoUsage(null, NOW), undefined);
|
|
107
|
+
assert.equal(parseOpenCodeGoUsage(payload(), Number.NaN), undefined);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('the fetch', () => {
|
|
112
|
+
test('a missing key never sends a request', async () => {
|
|
113
|
+
const fetchFn = () => {
|
|
114
|
+
throw new Error('fetch must not run');
|
|
115
|
+
};
|
|
116
|
+
assert.equal(await fetchOpenCodeGoUsage(undefined, fetchFn, NOW), undefined);
|
|
117
|
+
assert.equal(await fetchOpenCodeGoUsage('', fetchFn, NOW), undefined);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('one GET to the fixed route, with the key only in the header, and no body leak', async () => {
|
|
121
|
+
let calls = 0;
|
|
122
|
+
const usage = await fetchOpenCodeGoUsage(
|
|
123
|
+
'test-secret',
|
|
124
|
+
async (url, options) => {
|
|
125
|
+
calls++;
|
|
126
|
+
assert.equal(url, USAGE_URL);
|
|
127
|
+
const { signal, ...rest } = options;
|
|
128
|
+
assert.ok(signal instanceof AbortSignal);
|
|
129
|
+
assert.deepEqual(rest, {
|
|
130
|
+
method: 'GET',
|
|
131
|
+
headers: { Authorization: 'Bearer test-secret', Accept: 'application/json' },
|
|
132
|
+
cache: 'no-store',
|
|
133
|
+
redirect: 'error',
|
|
134
|
+
});
|
|
135
|
+
return response();
|
|
136
|
+
},
|
|
137
|
+
NOW,
|
|
138
|
+
);
|
|
139
|
+
assert.equal(calls, 1);
|
|
140
|
+
assert.equal(usage?.provider, PROVIDER);
|
|
141
|
+
assert.equal(JSON.stringify(usage).includes('test-secret'), false);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test('failures resolve to no snapshot and report a fixed code, never the key', async () => {
|
|
145
|
+
const reasons = [];
|
|
146
|
+
const report = (reason) => reasons.push(reason);
|
|
147
|
+
|
|
148
|
+
for (const [value, expected] of [
|
|
149
|
+
[response(undefined, { ok: false, status: 401 }), 'http-401'],
|
|
150
|
+
[response(undefined, { ok: false, status: 500 }), 'http-500'],
|
|
151
|
+
[response(undefined, { redirected: true }), 'redirect'],
|
|
152
|
+
[response(undefined, { url: 'https://other.example/usage' }), 'redirect'],
|
|
153
|
+
[response({ usage: {} }), 'unexpected-shape:rolling'],
|
|
154
|
+
[response({ usage: 'nope' }), 'unexpected-shape'],
|
|
155
|
+
]) {
|
|
156
|
+
const usage = await fetchOpenCodeGoUsage('secret', async () => value, NOW, { report });
|
|
157
|
+
assert.equal(usage, undefined, expected);
|
|
158
|
+
assert.equal(reasons.at(-1), expected);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
assert.equal(
|
|
162
|
+
await fetchOpenCodeGoUsage(
|
|
163
|
+
'secret',
|
|
164
|
+
async () => {
|
|
165
|
+
throw new Error('secret: network failure');
|
|
166
|
+
},
|
|
167
|
+
NOW,
|
|
168
|
+
{ report },
|
|
169
|
+
),
|
|
170
|
+
undefined,
|
|
171
|
+
);
|
|
172
|
+
assert.equal(reasons.at(-1), 'network');
|
|
173
|
+
|
|
174
|
+
assert.equal(
|
|
175
|
+
await fetchOpenCodeGoUsage(
|
|
176
|
+
'secret',
|
|
177
|
+
async () =>
|
|
178
|
+
response(undefined, {
|
|
179
|
+
json: async () => {
|
|
180
|
+
throw new Error('secret: invalid JSON');
|
|
181
|
+
},
|
|
182
|
+
}),
|
|
183
|
+
NOW,
|
|
184
|
+
{ report },
|
|
185
|
+
),
|
|
186
|
+
undefined,
|
|
187
|
+
);
|
|
188
|
+
assert.equal(reasons.at(-1), 'invalid-json');
|
|
189
|
+
|
|
190
|
+
assert.equal(await fetchOpenCodeGoUsage(undefined, async () => response(), NOW, { report }), undefined);
|
|
191
|
+
assert.equal(reasons.at(-1), 'missing-key');
|
|
192
|
+
assert.equal(reasons.join(' ').includes('secret'), false);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('a throwing diagnostics sink cannot break a successful refresh', async () => {
|
|
196
|
+
const usage = await fetchOpenCodeGoUsage('secret', async () => response(), NOW, {
|
|
197
|
+
report: () => {
|
|
198
|
+
throw new Error('sink');
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
assert.equal(usage?.provider, PROVIDER);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('the request carries its own bounded window, and a hung response is a timeout', async () => {
|
|
205
|
+
let seen;
|
|
206
|
+
await fetchOpenCodeGoUsage(
|
|
207
|
+
'secret',
|
|
208
|
+
async (_url, options) => {
|
|
209
|
+
seen = options.signal;
|
|
210
|
+
return response();
|
|
211
|
+
},
|
|
212
|
+
NOW,
|
|
213
|
+
);
|
|
214
|
+
assert.ok(seen instanceof AbortSignal);
|
|
215
|
+
assert.equal(REQUEST_TIMEOUT_MS, 10_000);
|
|
216
|
+
|
|
217
|
+
// A request that outlives its own window is reported as a timeout, not as a network error.
|
|
218
|
+
const reasons = [];
|
|
219
|
+
const hung = (_url, options) =>
|
|
220
|
+
new Promise((_resolve, reject) => {
|
|
221
|
+
options.signal.addEventListener('abort', () => reject(new Error('aborted')));
|
|
222
|
+
});
|
|
223
|
+
assert.equal(await fetchOpenCodeGoUsage('secret', hung, NOW, { timeoutMs: 5, report: (r) => reasons.push(r) }), undefined);
|
|
224
|
+
assert.deepEqual(reasons, ['timeout']);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe('the source it registers', () => {
|
|
229
|
+
const sourceOf = (onReadiness) => createUsageSource({ onReadiness });
|
|
230
|
+
|
|
231
|
+
test('the payload has exactly the four keys the usage-source contract names', () => {
|
|
232
|
+
const source = sourceOf(() => {});
|
|
233
|
+
assert.deepEqual(Object.keys(source).sort(), ['fetch', 'pendingNote', 'provider', 'schema']);
|
|
234
|
+
assert.equal(USAGE_SOURCE_SCHEMA, 'gentle-pi.usage-source/v1');
|
|
235
|
+
assert.equal(source.schema, USAGE_SOURCE_SCHEMA);
|
|
236
|
+
assert.equal(source.provider, 'opencode-go');
|
|
237
|
+
assert.equal(PROVIDER, 'opencode-go');
|
|
238
|
+
// A missing key must leave exactly this note behind, with the `·` the shell prints.
|
|
239
|
+
assert.equal(source.pendingNote, 'opencode-go · press r to fetch');
|
|
240
|
+
assert.equal(PENDING_NOTE, source.pendingNote);
|
|
241
|
+
assert.equal(typeof source.fetch, 'function');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('the plan is the peak schedule of the fetch instant, peak or off-peak', async () => {
|
|
245
|
+
const source = sourceOf(() => {});
|
|
246
|
+
for (const [now, expected] of [
|
|
247
|
+
[PEAK_INSTANT, 'peak 20m'],
|
|
248
|
+
[OFF_PEAK_INSTANT, 'off-peak 1hr 30m'],
|
|
249
|
+
]) {
|
|
250
|
+
const usage = await source.fetch('secret', async () => response(), now);
|
|
251
|
+
assert.equal(usage?.plan, expected, String(now));
|
|
252
|
+
assert.equal(usage?.plan, formatPeakState(peakStateFor(providerById('deepseek'), now), now));
|
|
253
|
+
// The window labels stay exactly as read: the schedule rides only on the provider line.
|
|
254
|
+
assert.deepEqual(
|
|
255
|
+
usage?.limits[0].windows.map((window) => window.label),
|
|
256
|
+
['5h', 'week', 'month'],
|
|
257
|
+
);
|
|
258
|
+
// The panel's own role is muted already, so the field carries no escape at all.
|
|
259
|
+
assert.equal(String(usage?.plan).includes('\x1b['), false);
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('a successful fetch reports "ready" once', async () => {
|
|
264
|
+
const verdicts = [];
|
|
265
|
+
const usage = await sourceOf((verdict) => verdicts.push(verdict)).fetch('secret', async () => response(), NOW);
|
|
266
|
+
assert.equal(usage?.provider, PROVIDER);
|
|
267
|
+
assert.deepEqual(verdicts, ['ready']);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test('only a missing key reports "missing-key"', async () => {
|
|
271
|
+
const verdicts = [];
|
|
272
|
+
const source = sourceOf((verdict) => verdicts.push(verdict));
|
|
273
|
+
const calls = () => {
|
|
274
|
+
throw new Error('fetch must not run');
|
|
275
|
+
};
|
|
276
|
+
assert.equal(await source.fetch(undefined, calls, NOW), undefined);
|
|
277
|
+
assert.equal(await source.fetch('', calls, NOW), undefined);
|
|
278
|
+
assert.deepEqual(verdicts, ['missing-key', 'missing-key']);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test('a timeout, a network error or a malformed response reports "unknown", never unusable', async () => {
|
|
282
|
+
const verdicts = [];
|
|
283
|
+
const source = sourceOf((verdict) => verdicts.push(verdict));
|
|
284
|
+
|
|
285
|
+
const hung = (_url, options) =>
|
|
286
|
+
new Promise((_resolve, reject) => {
|
|
287
|
+
options.signal.addEventListener('abort', () => reject(new Error('aborted')));
|
|
288
|
+
});
|
|
289
|
+
assert.equal(await source.fetch('secret', hung, NOW, { timeoutMs: 5 }), undefined);
|
|
290
|
+
assert.deepEqual(verdicts, ['unknown'], 'timeout');
|
|
291
|
+
|
|
292
|
+
assert.equal(
|
|
293
|
+
await source.fetch(
|
|
294
|
+
'secret',
|
|
295
|
+
async () => {
|
|
296
|
+
throw new Error('offline');
|
|
297
|
+
},
|
|
298
|
+
NOW,
|
|
299
|
+
),
|
|
300
|
+
undefined,
|
|
301
|
+
);
|
|
302
|
+
assert.equal(verdicts.at(-1), 'unknown', 'network error');
|
|
303
|
+
|
|
304
|
+
for (const bad of [response({ usage: {} }), response({ usage: 'nope' }), response({})]) {
|
|
305
|
+
assert.equal(await source.fetch('secret', async () => bad, NOW), undefined);
|
|
306
|
+
assert.equal(verdicts.at(-1), 'unknown', 'malformed response');
|
|
307
|
+
}
|
|
308
|
+
// Nothing above proves the key is missing, so the provider is never marked unusable.
|
|
309
|
+
assert.equal(verdicts.includes('missing-key'), false);
|
|
310
|
+
assert.equal(verdicts.length, 5);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test('a throwing readiness sink cannot break a refresh', async () => {
|
|
314
|
+
const source = sourceOf(() => {
|
|
315
|
+
throw new Error('ui gone');
|
|
316
|
+
});
|
|
317
|
+
const usage = await source.fetch('secret', async () => response(), NOW);
|
|
318
|
+
assert.equal(usage?.provider, PROVIDER);
|
|
319
|
+
assert.equal(await source.fetch(undefined, async () => response(), NOW), undefined);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// S1/S5/S7 (opencode-usage-alert): the two strings this package paints for a peak schedule — the
|
|
3
|
+
// panel's `plan` field and one widget line per peaking provider. Two surfaces, two different
|
|
4
|
+
// freedoms: the panel paints `plan` in its own muted role and inserts its own ` · `, so the field
|
|
5
|
+
// is plain text with no leading separator; the widget line paints itself, so only its state word
|
|
6
|
+
// is amber. The line carries the provider's own label and nothing invented: DeepSeek's published
|
|
7
|
+
// `2×` price multiplier is in its label, Claude has none because Anthropic publishes none.
|
|
8
|
+
const { test, describe } = require('node:test');
|
|
9
|
+
const assert = require('node:assert/strict');
|
|
10
|
+
|
|
11
|
+
const { PEAK_WIDGET_KEY, panelSchedule, peakWidgetLine } = require('../lib/peak-labels.js');
|
|
12
|
+
const { providerById } = require('../lib/peak-providers.js');
|
|
13
|
+
const { formatBoundaryClock, formatPeakState, peakStateFor } = require('../lib/peak-schedule.js');
|
|
14
|
+
|
|
15
|
+
const DEEPSEEK = providerById('deepseek');
|
|
16
|
+
const CLAUDE = providerById('claude');
|
|
17
|
+
|
|
18
|
+
const utc = (iso) => Date.parse(iso);
|
|
19
|
+
// 2026-09-23 is a Wednesday: 03:40 UTC is inside DeepSeek's first window, 04:30 UTC between them.
|
|
20
|
+
const DURING_PEAK = utc('2026-09-23T03:40:00Z');
|
|
21
|
+
const OFF_PEAK = utc('2026-09-23T04:30:00Z');
|
|
22
|
+
// 2026-07-15 is a Wednesday: 12:30 UTC is 05:30 PDT, inside Claude's window.
|
|
23
|
+
const CLAUDE_PEAK = utc('2026-07-15T12:30:00Z');
|
|
24
|
+
|
|
25
|
+
describe('the panel schedule', () => {
|
|
26
|
+
test('the panel plan is the schedule, with no colour and no leading separator of its own', () => {
|
|
27
|
+
assert.equal(panelSchedule(peakStateFor(DEEPSEEK, DURING_PEAK), DURING_PEAK), 'peak 20m');
|
|
28
|
+
assert.equal(panelSchedule(peakStateFor(DEEPSEEK, OFF_PEAK), OFF_PEAK), 'off-peak 1hr 30m');
|
|
29
|
+
// The panel already prints ` · ` before the field: a leading one would render as ` · · `.
|
|
30
|
+
for (const [provider, now] of [
|
|
31
|
+
[DEEPSEEK, DURING_PEAK],
|
|
32
|
+
[DEEPSEEK, OFF_PEAK],
|
|
33
|
+
[CLAUDE, CLAUDE_PEAK],
|
|
34
|
+
]) {
|
|
35
|
+
const plan = panelSchedule(peakStateFor(provider, now), now);
|
|
36
|
+
assert.equal(plan.includes('\x1b['), false, plan);
|
|
37
|
+
assert.equal(plan.startsWith(' '), false, plan);
|
|
38
|
+
assert.equal(plan.startsWith('·'), false, plan);
|
|
39
|
+
assert.equal(plan, formatPeakState(peakStateFor(provider, now), now), 'the field is the state label verbatim');
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('the schedule is the same text whatever the session model is priced at', () => {
|
|
44
|
+
// The panel is unconditional: the field follows the clock, never a model choice.
|
|
45
|
+
assert.equal(panelSchedule(peakStateFor(DEEPSEEK, OFF_PEAK), OFF_PEAK), 'off-peak 1hr 30m');
|
|
46
|
+
// Friday 10:30 UTC is off-peak until Monday 01:00 UTC; above a day the countdown is whole hours.
|
|
47
|
+
assert.equal(
|
|
48
|
+
panelSchedule(peakStateFor(DEEPSEEK, utc('2026-09-25T10:30:00Z')), utc('2026-09-25T10:30:00Z')),
|
|
49
|
+
'off-peak 2d 14hr',
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('the widget line', () => {
|
|
55
|
+
test("the key is the one this package owns in pi's widget map", () => {
|
|
56
|
+
assert.equal(PEAK_WIDGET_KEY, 'opencode-go-peak-widget');
|
|
57
|
+
assert.equal(typeof PEAK_WIDGET_KEY, 'string');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('one amber state word, the provider label, then the boundary in the machine timezone', () => {
|
|
61
|
+
const state = peakStateFor(DEEPSEEK, DURING_PEAK);
|
|
62
|
+
const boundary = formatBoundaryClock(state.until, DURING_PEAK);
|
|
63
|
+
const line = peakWidgetLine(DEEPSEEK, state, DURING_PEAK);
|
|
64
|
+
assert.equal(line, `\x1b[33mpeak\x1b[0m · 2× deepseek · until ${boundary}`);
|
|
65
|
+
// The amber closes right after the state word, so the details stay in the terminal's own
|
|
66
|
+
// foreground instead of inheriting a dimmed amber.
|
|
67
|
+
assert.equal(line.indexOf('\x1b[0m'), `\x1b[33mpeak`.length);
|
|
68
|
+
assert.equal(line.slice(`\x1b[33mpeak\x1b[0m`.length).includes('\x1b'), false, 'no further escape');
|
|
69
|
+
assert.equal(line.includes('\n'), false, 'one line');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('the Claude line carries Claude\'s own label and no invented multiplier', () => {
|
|
73
|
+
const state = peakStateFor(CLAUDE, CLAUDE_PEAK);
|
|
74
|
+
const boundary = formatBoundaryClock(state.until, CLAUDE_PEAK);
|
|
75
|
+
const line = peakWidgetLine(CLAUDE, state, CLAUDE_PEAK);
|
|
76
|
+
assert.equal(line, `\x1b[33mpeak\x1b[0m · claude · until ${boundary}`);
|
|
77
|
+
assert.equal(line.includes('2×'), false, 'Anthropic publishes no multiplier for the window');
|
|
78
|
+
assert.equal(line.indexOf('\x1b[0m'), `\x1b[33mpeak`.length);
|
|
79
|
+
assert.equal(line.slice(`\x1b[33mpeak\x1b[0m`.length).includes('\x1b'), false, 'no further escape');
|
|
80
|
+
assert.equal(line.includes('\n'), false, 'one line');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('two providers peaking at once draw two lines, in the registry order, each with its label', () => {
|
|
84
|
+
const until = utc('2026-09-23T04:00:00Z');
|
|
85
|
+
const lines = [DEEPSEEK, CLAUDE].map((provider) =>
|
|
86
|
+
peakWidgetLine(provider, { kind: 'peak', until }, DURING_PEAK),
|
|
87
|
+
);
|
|
88
|
+
assert.equal(lines.length, 2, 'one row per peaking provider, never one merged row');
|
|
89
|
+
const clock = formatBoundaryClock(until, DURING_PEAK);
|
|
90
|
+
assert.deepEqual(lines, [
|
|
91
|
+
`\x1b[33mpeak\x1b[0m · 2× deepseek · until ${clock}`,
|
|
92
|
+
`\x1b[33mpeak\x1b[0m · claude · until ${clock}`,
|
|
93
|
+
]);
|
|
94
|
+
assert.equal(
|
|
95
|
+
lines[0].endsWith(`until ${clock}`),
|
|
96
|
+
true,
|
|
97
|
+
'the rows differ only by the provider they name',
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('there is no off-peak variant of the line: the caller clears the widget instead', () => {
|
|
102
|
+
// Whatever the state object says, the widget text is the peak line; gating is the caller's.
|
|
103
|
+
for (const [provider, now] of [
|
|
104
|
+
[DEEPSEEK, OFF_PEAK],
|
|
105
|
+
[CLAUDE, utc('2026-07-15T18:00:00Z')],
|
|
106
|
+
]) {
|
|
107
|
+
const line = peakWidgetLine(provider, peakStateFor(provider, now), now);
|
|
108
|
+
assert.equal(line.startsWith('\x1b[33mpeak\x1b[0m · '), true, line);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// S5/S7 (opencode-usage-alert): the peak registry. Every provider whose window the `Alert`
|
|
3
|
+
// section can draw lives here, and this list in declaration order IS the row order — the
|
|
4
|
+
// extension walks it and draws one row per peaking provider, so a provider added to this array
|
|
5
|
+
// appears with no change to the wiring. The engine reads `zone`, `windows` and `weekdaysOnly`
|
|
6
|
+
// from the same entries, so the schedule and the rows can never disagree about a provider.
|
|
7
|
+
const { test, describe } = require('node:test');
|
|
8
|
+
const assert = require('node:assert/strict');
|
|
9
|
+
|
|
10
|
+
const { PROVIDERS, providerById, providerIds } = require('../lib/peak-providers.js');
|
|
11
|
+
|
|
12
|
+
describe('the peak registry', () => {
|
|
13
|
+
test('the registry declares DeepSeek, then Claude, in that order', () => {
|
|
14
|
+
assert.deepEqual(providerIds(), ['deepseek', 'claude']);
|
|
15
|
+
assert.deepEqual(
|
|
16
|
+
PROVIDERS.map((provider) => provider.id),
|
|
17
|
+
['deepseek', 'claude'],
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('every entry carries the whole contract the wiring and the engine read', () => {
|
|
22
|
+
assert.deepEqual(PROVIDERS[0], {
|
|
23
|
+
id: 'deepseek',
|
|
24
|
+
setting: 'Deepseek peak',
|
|
25
|
+
label: '2× deepseek',
|
|
26
|
+
zone: 'UTC',
|
|
27
|
+
windows: [
|
|
28
|
+
[1, 4],
|
|
29
|
+
[6, 10],
|
|
30
|
+
],
|
|
31
|
+
weekdaysOnly: true,
|
|
32
|
+
});
|
|
33
|
+
assert.deepEqual(PROVIDERS[1], {
|
|
34
|
+
id: 'claude',
|
|
35
|
+
setting: 'Claude peak',
|
|
36
|
+
label: 'claude',
|
|
37
|
+
zone: 'America/Los_Angeles',
|
|
38
|
+
windows: [[5, 11]],
|
|
39
|
+
weekdaysOnly: true,
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('the two schedules: DeepSeek in UTC, Claude in the zone Anthropic publishes', () => {
|
|
44
|
+
assert.equal(providerById('deepseek').zone, 'UTC');
|
|
45
|
+
assert.deepEqual(providerById('deepseek').windows, [
|
|
46
|
+
[1, 4],
|
|
47
|
+
[6, 10],
|
|
48
|
+
]);
|
|
49
|
+
// Anthropic publishes 05:00-11:00 Pacific on weekdays, so the UTC edges move with PDT/PST.
|
|
50
|
+
assert.equal(providerById('claude').zone, 'America/Los_Angeles');
|
|
51
|
+
assert.deepEqual(providerById('claude').windows, [[5, 11]]);
|
|
52
|
+
assert.equal(providerById('claude').weekdaysOnly, true);
|
|
53
|
+
assert.equal(providerById('deepseek').weekdaysOnly, true);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('Claude carries no price multiplier: Anthropic publishes none for the window', () => {
|
|
57
|
+
assert.equal(providerById('claude').label, 'claude');
|
|
58
|
+
assert.equal(providerById('claude').label.includes('2×'), false);
|
|
59
|
+
assert.equal(providerById('claude').label.includes('x'), false);
|
|
60
|
+
// DeepSeek's `2×` is its published price multiplier and stays.
|
|
61
|
+
assert.equal(providerById('deepseek').label, '2× deepseek');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('the settings are the two Alert lines, in declaration order', () => {
|
|
65
|
+
assert.deepEqual(
|
|
66
|
+
PROVIDERS.map((provider) => provider.setting),
|
|
67
|
+
['Deepseek peak', 'Claude peak'],
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('the registry is frozen and answers the declared objects themselves', () => {
|
|
72
|
+
assert.equal(Object.isFrozen(PROVIDERS), true);
|
|
73
|
+
for (const provider of PROVIDERS) {
|
|
74
|
+
assert.equal(Object.isFrozen(provider), true, provider.id);
|
|
75
|
+
assert.equal(Object.isFrozen(provider.windows), true, provider.id);
|
|
76
|
+
}
|
|
77
|
+
assert.equal(providerById('deepseek'), PROVIDERS[0]);
|
|
78
|
+
assert.equal(providerById('claude'), PROVIDERS[1]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('providerById answers a declared id only', () => {
|
|
82
|
+
for (const bad of [undefined, null, '', 'DeepSeek', 'anthropic', 'opencode-go', 0, 42, {}, ['claude']]) {
|
|
83
|
+
assert.equal(providerById(bad), undefined, String(bad));
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('providerIds is a fresh array: a caller cannot edit the registry through it', () => {
|
|
88
|
+
const ids = providerIds();
|
|
89
|
+
ids.push('third');
|
|
90
|
+
ids.reverse();
|
|
91
|
+
assert.deepEqual(providerIds(), ['deepseek', 'claude']);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('every window is a well-formed half-open hour pair and every zone is a real zone', () => {
|
|
95
|
+
for (const provider of PROVIDERS) {
|
|
96
|
+
assert.equal(typeof provider.zone, 'string', provider.id);
|
|
97
|
+
// A typo here would quietly move the whole window; Intl is the check that it resolves.
|
|
98
|
+
assert.doesNotThrow(() => new Intl.DateTimeFormat('en-US', { timeZone: provider.zone }), provider.id);
|
|
99
|
+
assert.equal(provider.windows.length > 0, true, provider.id);
|
|
100
|
+
for (const window of provider.windows) {
|
|
101
|
+
assert.equal(window.length, 2, provider.id);
|
|
102
|
+
const [start, end] = window;
|
|
103
|
+
assert.equal(Number.isInteger(start), true, provider.id);
|
|
104
|
+
assert.equal(Number.isInteger(end), true, provider.id);
|
|
105
|
+
assert.equal(start >= 0 && start < end && end <= 24, true, `${provider.id} ${start}-${end}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
});
|