mirror of
https://github.com/jarrodwatts/claude-hud.git
synced 2026-05-21 15:52:37 +00:00
feat(render): add output speed display (#110)
This commit is contained in:
57
tests/speed-tracker.test.js
Normal file
57
tests/speed-tracker.test.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, rm } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { getOutputSpeed } from '../dist/speed-tracker.js';
|
||||
|
||||
async function createTempHome() {
|
||||
return await mkdtemp(path.join(tmpdir(), 'claude-hud-speed-'));
|
||||
}
|
||||
|
||||
test('getOutputSpeed returns null when output tokens are missing', () => {
|
||||
const speed = getOutputSpeed({ context_window: { current_usage: { input_tokens: 10 } } });
|
||||
assert.equal(speed, null);
|
||||
});
|
||||
|
||||
test('getOutputSpeed computes tokens per second within window', async () => {
|
||||
const tempHome = await createTempHome();
|
||||
|
||||
try {
|
||||
const base = { homeDir: () => tempHome };
|
||||
const first = getOutputSpeed(
|
||||
{ context_window: { current_usage: { output_tokens: 10 } } },
|
||||
{ ...base, now: () => 1000 }
|
||||
);
|
||||
assert.equal(first, null);
|
||||
|
||||
const second = getOutputSpeed(
|
||||
{ context_window: { current_usage: { output_tokens: 20 } } },
|
||||
{ ...base, now: () => 1500 }
|
||||
);
|
||||
assert.ok(second !== null);
|
||||
assert.ok(Math.abs(second - 20) < 0.01);
|
||||
} finally {
|
||||
await rm(tempHome, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('getOutputSpeed ignores stale windows', async () => {
|
||||
const tempHome = await createTempHome();
|
||||
|
||||
try {
|
||||
const base = { homeDir: () => tempHome };
|
||||
getOutputSpeed(
|
||||
{ context_window: { current_usage: { output_tokens: 10 } } },
|
||||
{ ...base, now: () => 1000 }
|
||||
);
|
||||
|
||||
const speed = getOutputSpeed(
|
||||
{ context_window: { current_usage: { output_tokens: 30 } } },
|
||||
{ ...base, now: () => 8000 }
|
||||
);
|
||||
assert.equal(speed, null);
|
||||
} finally {
|
||||
await rm(tempHome, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user