Something went wrong. Try again.
small gleam coding and (not yet) persistent agent daemon with a detachable cli forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839// The meter's numbers are the terminal footer's: a drifted share or fade step would make the two disagree.import assert from 'node:assert/strict';import { readFile } from 'node:fs/promises';import { stripTypeScriptTypes } from 'node:module';import test from 'node:test';
const source = stripTypeScriptTypes(await readFile(new URL('../src/lib/context.ts', import.meta.url), 'utf8'));const { cachedNow, contextTokens, nextFade, share, shortCount, stale } = await import(`data:text/javascript;base64,${Buffer.from(source).toString('base64')}`);const usage = (fields = {}) => ({ model: 'm', prompt_tokens: 100, cached_prompt_tokens: 80, completion_tokens: 10, total_tokens: null, cache_fade: [], ...fields });
test('the context is the total, else the prompt and completion', () => { assert.equal(contextTokens(usage({ total_tokens: 150 })), 150); assert.equal(contextTokens(usage()), 110); assert.equal(contextTokens(usage({ completion_tokens: null })), 100); assert.equal(contextTokens(usage({ prompt_tokens: null })), null);});
test('cached tokens hold until a fade step is reached, then follow each step', () => { const fade = [{ at: '2026-01-01T00:05:00Z', cached_tokens: 40 }, { at: '2026-01-01T01:00:00Z', cached_tokens: null }]; const at = (iso) => Date.parse(iso); assert.equal(cachedNow(usage({ cache_fade: fade }), at('2026-01-01T00:04:59Z')), 80); assert.equal(cachedNow(usage({ cache_fade: fade }), at('2026-01-01T00:05:00Z')), 40); assert.equal(cachedNow(usage({ cache_fade: fade }), at('2026-01-01T02:00:00Z')), null); assert.equal(nextFade(usage({ cache_fade: fade }), at('2026-01-01T00:05:00Z')), at('2026-01-01T01:00:00Z')); assert.equal(nextFade(usage({ cache_fade: fade }), at('2026-01-01T02:00:00Z')), null);});
test('share rounds, and usage from another model is stale', () => { assert.equal(share(76_000, 200_000), 38); assert.equal(share(396_500, 400_000), 99); assert.equal(stale(usage(), 'other'), true); assert.equal(stale(usage(), 'm'), false); assert.equal(stale(usage({ model: null }), 'other'), false);});
test('short counts keep about three digits', () => { assert.deepEqual([812, 4200, 4000, 392_000, 999_400, 1_200_000].map(shortCount), ['812', '4.2k', '4k', '392k', '999k', '1.2m']);});