2e14873f2d
Track application source and tests; exclude local env, user workspaces, and runtime data via .gitignore. Co-authored-by: Cursor <cursoragent@cursor.com>
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { computeDeltaCostCents, normalizeTokenState } from './billing.mjs';
|
|
|
|
const config = {
|
|
useBackendCost: false,
|
|
usdCnyRate: 7.2,
|
|
inputCentsPer1k: 2,
|
|
outputCentsPer1k: 6,
|
|
minBillCents: 1,
|
|
};
|
|
|
|
test('normalizeTokenState reads camelCase fields', () => {
|
|
const state = normalizeTokenState({
|
|
inputTokens: 10,
|
|
outputTokens: 20,
|
|
accumulatedInputTokens: 100,
|
|
accumulatedOutputTokens: 200,
|
|
});
|
|
assert.equal(state.accumulatedInputTokens, 100);
|
|
assert.equal(state.accumulatedOutputTokens, 200);
|
|
});
|
|
|
|
test('computeDeltaCostCents bills token deltas', () => {
|
|
const previous = { lastInputTokens: 1000, lastOutputTokens: 500 };
|
|
const current = normalizeTokenState({
|
|
accumulatedInputTokens: 2000,
|
|
accumulatedOutputTokens: 1500,
|
|
});
|
|
const cost = computeDeltaCostCents(previous, current, config);
|
|
assert.equal(cost, 8);
|
|
});
|
|
|
|
test('computeDeltaCostCents returns zero when no usage delta', () => {
|
|
const previous = { lastInputTokens: 1000, lastOutputTokens: 500 };
|
|
const current = normalizeTokenState({
|
|
accumulatedInputTokens: 1000,
|
|
accumulatedOutputTokens: 500,
|
|
});
|
|
assert.equal(computeDeltaCostCents(previous, current, config), 0);
|
|
});
|