mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-17 22:36:13 +00:00
* fix(export): give the JSON export stable keys
The JSON export reused the CSV row builders, so its keys were display
headers with spaces and parentheses, and the money keys changed name
with the configured currency. A program had to reshape the file before
it could read it.
The builders now produce one camelCase row, and a single key to header
map writes the CSV, so every CSV header line stays byte-identical. The
JSON schema moves to codeburn.export.v3; the currency is stated once in
currency.code and the money values stay converted to it.
Before (v2, EUR):
{"Period":"7 Days","Date":"2026-11-02","Cost (EUR)":1.1,"Saved (EUR)":0,"API Calls":1,"Sessions":1,"Input Tokens":100000,"Output Tokens":50000,"Cache Read Tokens":200000,"Cache Write Tokens":30000,"costBasis":{"calculated":1}}
After (v3, EUR):
{"period":"7 Days","date":"2026-11-02","cost":1.1,"saved":0,"apiCalls":1,"sessions":1,"inputTokens":100000,"outputTokens":50000,"cacheReadTokens":200000,"cacheWriteTokens":30000,"costBasis":{"calculated":1}}
v2 consumers must rename keys. New keys can still appear under v3.
* docs: changelog entry for the v3 export JSON
* test: read the v3 export keys
96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { spawnSync } from 'node:child_process'
|
|
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
function runCli(args: string[], home: string) {
|
|
return spawnSync(process.execPath, ['--import', 'tsx', 'src/cli.ts', ...args], {
|
|
cwd: process.cwd(),
|
|
env: {
|
|
...process.env,
|
|
CLAUDE_CONFIG_DIR: join(home, '.claude'),
|
|
HOME: home, USERPROFILE: home,
|
|
TZ: 'UTC',
|
|
},
|
|
encoding: 'utf-8',
|
|
})
|
|
}
|
|
|
|
function userLine(sessionId: string, timestamp: string): string {
|
|
return JSON.stringify({
|
|
type: 'user',
|
|
sessionId,
|
|
timestamp,
|
|
message: { role: 'user', content: 'add feature' },
|
|
})
|
|
}
|
|
|
|
function assistantLine(sessionId: string, timestamp: string, messageId: string): string {
|
|
return JSON.stringify({
|
|
type: 'assistant',
|
|
sessionId,
|
|
timestamp,
|
|
message: {
|
|
id: messageId,
|
|
type: 'message',
|
|
role: 'assistant',
|
|
model: 'claude-sonnet-4-5',
|
|
content: [{ type: 'text', text: 'done' }],
|
|
usage: {
|
|
input_tokens: 1000,
|
|
output_tokens: 100,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
describe('codeburn export custom date range', () => {
|
|
it('exports a single custom period filtered by --from/--to', async () => {
|
|
const home = await mkdtemp(join(tmpdir(), 'codeburn-cli-export-'))
|
|
|
|
try {
|
|
const projectDir = join(home, '.claude', 'projects', 'app')
|
|
await mkdir(projectDir, { recursive: true })
|
|
await writeFile(
|
|
join(projectDir, 'in-range.jsonl'),
|
|
[
|
|
userLine('in-range', '2026-04-10T09:00:00Z'),
|
|
assistantLine('in-range', '2026-04-10T09:01:00Z', 'msg-in-range'),
|
|
].join('\n'),
|
|
)
|
|
await writeFile(
|
|
join(projectDir, 'out-of-range.jsonl'),
|
|
[
|
|
userLine('out-of-range', '2026-04-11T09:00:00Z'),
|
|
assistantLine('out-of-range', '2026-04-11T09:01:00Z', 'msg-out-of-range'),
|
|
].join('\n'),
|
|
)
|
|
|
|
const outputPath = join(home, 'custom-export.json')
|
|
const result = runCli([
|
|
'export',
|
|
'--format', 'json',
|
|
'--from', '2026-04-10',
|
|
'--to', '2026-04-10',
|
|
'--provider', 'claude',
|
|
'--output', outputPath,
|
|
], home)
|
|
|
|
expect(result.status).toBe(0)
|
|
expect(result.stdout).toContain('Exported (2026-04-10 to 2026-04-10)')
|
|
|
|
const exported = JSON.parse(await readFile(outputPath, 'utf-8')) as {
|
|
summary: Array<{ period: string; sessions: number }>
|
|
sessions: Array<{ sessionId: string }>
|
|
}
|
|
expect(exported.summary).toHaveLength(1)
|
|
expect(exported.summary[0]?.period).toBe('2026-04-10 to 2026-04-10')
|
|
expect(exported.summary[0]?.sessions).toBe(1)
|
|
expect(exported.sessions.map(s => s.sessionId)).toEqual(['in-range'])
|
|
} finally {
|
|
await rm(home, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|