// Grok Build (Cursor) cost: turns from its `stop` hook (R3). import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import fs from 'fs'; import os from 'os'; import path from 'path'; import { cursorTurnFromStop, cursorTurnsFile, parseCursorTurns, recordCursorTurn, type CursorTurn, } from '../cost-cursor'; import { cursorStop } from './fixtures/cursor-payloads'; const turn = (over: Partial): CursorTurn => ({ ts: '2026-12-02T08:00:00.000Z', sessionId: 's1', model: 'grok-4.7', workspace: '/home/dev/grok-test', input: 100, output: 10, cacheRead: 50, cacheWrite: 0, ...over, }); describe('cursorTurnFromStop', () => { it('reads the tokens, model and session of a real stop payload; no text', () => { const t = cursorTurnFromStop(cursorStop, '/home/dev/grok-test')!; expect(t).toMatchObject({ sessionId: cursorStop.session_id, model: 'grok-4.7-medium', workspace: '/home/dev/grok-test', input: 28975, output: 315, cacheRead: 20992, cacheWrite: 0, }); expect(Object.keys(t).sort()).toEqual( ['cacheRead', 'cacheWrite', 'input', 'model', 'output', 'sessionId', 'ts', 'workspace'].sort() ); }); it('prefers model_id, and a turn with no tokens is not recorded', () => { expect(cursorTurnFromStop({ ...cursorStop, model_id: 'grok-4.7' }, undefined)?.model).toBe( 'grok-4.7' ); expect( cursorTurnFromStop( { ...cursorStop, input_tokens: 0, output_tokens: 0, cache_read_tokens: 0 }, undefined ) ).toBeNull(); }); }); describe('parseCursorTurns', () => { it('sums the turns of one session, day and model into one row', () => { const rows = parseCursorTurns([ JSON.stringify(turn({})), JSON.stringify(turn({ ts: '2026-12-02T09:00:00.000Z', input: 1 })), JSON.stringify(turn({ sessionId: 's2' })), 'not json', ]); expect(rows).toHaveLength(2); const s1 = rows.find((r) => r.runId === 's1')!; expect(s1).toMatchObject({ date: '2026-12-02', inputTokens: 101, outputTokens: 20 }); expect(s1.workingDir).toBe('/home/dev/grok-test'); }); it('a model with no public price keeps its tokens at cost 0; a priced one is priced', () => { const [unknown] = parseCursorTurns([JSON.stringify(turn({ model: 'grok-4.7' }))]); expect(unknown.costUSD).toBe(0); expect(unknown.inputTokens).toBe(100); const [priced] = parseCursorTurns([JSON.stringify(turn({ model: 'claude-opus-4-5' }))]); expect(priced.costUSD).toBeGreaterThan(0); }); it('drops turns older than the cutoff', () => { const rows = parseCursorTurns( [JSON.stringify(turn({ ts: '2026-11-01T00:00:00.000Z' })), JSON.stringify(turn({}))], Date.parse('2026-12-01T00:00:00.000Z') ); expect(rows).toHaveLength(1); }); }); describe('recordCursorTurn', () => { let home: string; beforeEach(() => { home = fs.mkdtempSync(path.join(os.tmpdir(), 'n9-cursor-cost-')); }); afterEach(() => fs.rmSync(home, { recursive: true, force: true })); it('appends one private line per turn', () => { recordCursorTurn(turn({}), home); recordCursorTurn(turn({ sessionId: 's2' }), home); const file = cursorTurnsFile(home); expect(fs.readFileSync(file, 'utf8').trim().split('\n')).toHaveLength(2); if (process.platform !== 'win32') expect(fs.statSync(file).mode & 0o777).toBe(0o600); }); });