Files
memind/billing.test.mjs
T
John 79457230c1 Improve MindSpace visual editor UX and default RMB billing.
Add color-coded edit highlights with an in-preview legend, strip CSP meta
for srcdoc editing, and simplify fullscreen preview to immersive visual edit
only. Default billing to CNY token rates unless H5_USE_BACKEND_COST=1.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-16 00:59:28 -07:00

55 lines
1.8 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);
});
test('computeDeltaCostCents ignores backend USD cost unless explicitly enabled', () => {
const previous = { lastInputTokens: 0, lastOutputTokens: 0, lastAccumulatedCost: 0.01 };
const current = normalizeTokenState({
accumulatedInputTokens: 1000,
accumulatedOutputTokens: 500,
accumulatedCost: 0.02,
});
const rmbConfig = { ...config, useBackendCost: false };
assert.equal(computeDeltaCostCents(previous, current, rmbConfig), 5);
const usdConfig = { ...config, useBackendCost: true };
assert.equal(computeDeltaCostCents(previous, current, usdConfig), 8);
});