Files
homeclaw/tests/cost-estimate.test.ts

61 lines
3.4 KiB
TypeScript

/**
* cost-estimate — 토큰 기록 × 단가표 추산, 앤트로픽 잔액 추산.
* 표본 단가: 2026-10-17 공식 가격 페이지(Haiku 5.5 $0.10/$0.50, Sonnet 5.5 $2/$10, Flash-Lite $0.30/$2.50).
*/
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import { priceFor, costOf, summarize, kstDay } from '../src/providers/cost-estimate';
const row = (model: string, p: number, c: number, ts = '2026-10-17T05:00:00Z') => ({ ts, model, promptTokens: p, completionTokens: c });
describe('단가·비용', () => {
test('Haiku 5.5: 입력 8,000 + 출력 500 토큰', () => {
assert.ok(Math.abs(costOf(row('claude-haiku-5-5', 8000, 500))! - (8000 * 0.1 + 500 * 0.5) / 1e6) < 1e-12);
});
test('Haiku 5.5는 프롬프트 10만 토큰을 넘으면 비싼 단가', () => {
assert.deepEqual(priceFor('claude-haiku-5-5', 150_000, '2026-10-17'), { in: 0.5, out: 2.5 });
});
test('Sonnet 5.5는 Haiku의 20배 입력 단가', () => {
assert.equal(priceFor('claude-sonnet-5-5', 1, '2026-10-17')!.in / priceFor('claude-haiku-5-5', 1, '2026-10-17')!.in, 20);
});
test('Gemini Flash는 2027년부터 단가 2배', () => {
assert.equal(priceFor('gemini-3.6-flash', 1, '2026-12-31')!.in, 0.75);
assert.equal(priceFor('gemini-3.6-flash', 1, '2027-01-01')!.in, 1.5);
});
test('올라마·로컬 모델은 비용 없음(null)', () => {
assert.equal(costOf(row('gemma4:31b-cloud', 7000, 150)), null);
assert.equal(costOf(row('kimi-k2.6:cloud', 7000, 150)), null);
});
});
describe('summarize', () => {
const now = new Date('2026-10-17T10:00:00Z'); // KST 19:00
test('KST 날짜 경계: UTC 15시 이후는 다음 날로 센다', () => {
assert.equal(kstDay('2026-10-17T14:59:59Z'), '2026-10-17');
assert.equal(kstDay('2026-10-17T15:00:00Z'), '2026-10-18');
});
test('오늘·최근 7일·이번 달 합산, 단가 없는 모델은 호출 수만', () => {
const rows = [row('claude-haiku-5-5', 10_000, 1_000), row('gemma4:31b-cloud', 7000, 100), row('claude-haiku-5-5', 10_000, 1_000, '2026-10-10T05:00:00Z')];
const s = summarize(rows, {}, now);
assert.equal(s.today.calls, 2);
assert.equal(s.last7.calls, 2); // 10/10은 7일 창(10/11~10/17) 밖
assert.equal(s.month.calls, 3);
assert.ok(Math.abs(s.today.usd - (10_000 * 0.1 + 1_000 * 0.5) / 1e6) < 1e-12);
assert.equal(s.byModel['gemma4:31b-cloud'].priced, false);
});
test('잔액 추산: 기준 시점 이후 claude 지출만 뺀다', () => {
const rows = [row('claude-sonnet-5-5', 1_000_000, 0, '2026-10-17T01:00:00Z'), row('claude-sonnet-5-5', 1_000_000, 0, '2026-10-17T06:00:00Z'), row('gemma4:31b-cloud', 9_000_000, 0, '2026-10-17T06:00:00Z')];
const s = summarize(rows, { balance_usd: 5, as_of: '2026-10-17T03:00:00Z', warn_below_usd: 1 }, now);
assert.equal(s.anthropic!.spent_since, 2); // 기준 시점 이후 Sonnet 100만 입력 = $2
assert.equal(s.anthropic!.estimated_balance, 3);
assert.equal(s.anthropic!.low, false);
});
test('잔액이 경고선 이하면 low', () => {
const s = summarize([row('claude-sonnet-5-5', 2_000_000, 0, '2026-10-17T06:00:00Z')], { balance_usd: 4.5, as_of: '2026-10-17T03:00:00Z', warn_below_usd: 1 }, now);
assert.equal(s.anthropic!.low, true);
});
test('잔액 기록이 없으면 anthropic은 null', () => {
assert.equal(summarize([], {}, now).anthropic, null);
});
});