test(cli): spawn source CLI for a session spanning two days

The regression spawned dist/cli.js, which is absent in a clean npm-test
checkout. Drive the same end-to-end check through src/cli.ts and keep the
fixture on owned temporary paths isolated from inherited provider dirs.
This commit is contained in:
Aditya Vikram Singh 2026-09-07 22:28:37 +05:30
parent c28cf06867
commit 309fcef51b
2 changed files with 97 additions and 22 deletions

View file

@ -0,0 +1,97 @@
import { spawnSync } from 'node:child_process'
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { CLEARED, REDIRECTED } from './setup/env-isolation-vars.js'
function runCli(args: string[], home: string) {
const env = { ...process.env }
delete env.FORCE_COLOR
env.NO_COLOR = '1'
for (const key of CLEARED) delete env[key]
for (const key of REDIRECTED) env[key] = home
env.CLAUDE_CONFIG_DIR = join(home, '.claude')
env.CODEBURN_CACHE_DIR = join(home, '.cache', 'codeburn')
env.CODEBURN_DESKTOP_SESSIONS_DIR = join(home, 'desktop-sessions')
env.TZ = 'UTC'
env.CODEBURN_PRICING_SNAPSHOT_ONLY = '1'
env.CODEBURN_FX_NO_FETCH = '1'
return spawnSync(process.execPath, ['--import', 'tsx', 'src/cli.ts', ...args], {
cwd: process.cwd(),
env,
encoding: 'utf-8',
timeout: 60_000,
})
}
describe('codeburn status counts a session that spans days', () => {
it('one real session spanning two days is one period project session', async () => {
const home = await mkdtemp(join(tmpdir(), 'codeburn-session-across-days-'))
try {
const cwd = '/tmp/same-session-project'
const sessionId = 'same-actual-session'
const projectDir = join(home, '.claude', 'projects', 'project-a')
await mkdir(projectDir, { recursive: true })
const now = new Date(Date.now() - 60_000)
const older = new Date(now.getTime() - 5 * 86_400_000)
const rows = [older, now].flatMap((date, index) => [
{
type: 'user',
sessionId,
timestamp: date.toISOString(),
cwd,
message: { role: 'user', content: `task${index}` },
},
{
type: 'assistant',
sessionId,
timestamp: date.toISOString(),
cwd,
message: {
id: `response${index}`,
role: 'assistant',
model: 'claude-sonnet-4-5',
content: [{ type: 'text', text: 'done' }],
usage: {
input_tokens: 1000,
output_tokens: 200,
cache_creation_input_tokens: 0,
cache_read_input_tokens: 0,
},
},
},
])
await writeFile(join(projectDir, `${sessionId}.jsonl`), `${rows.map(row => JSON.stringify(row)).join('\n')}\n`)
// Warm the daily cache the same way a real CLI user would, then read the
// menubar payload. Occupancy on two days must still be one session.
const models = runCli(['models', '--format', 'json', '--period', 'week'], home)
expect(models.status, models.stderr).toBe(0)
JSON.parse(models.stdout)
const result = runCli([
'status',
'--format', 'menubar-json',
'--period', 'week',
'--provider', 'claude',
'--no-timeline',
], home)
expect(result.status, result.stderr).toBe(0)
const json = JSON.parse(result.stdout) as {
current: {
topProjects: Array<{ id?: string; sessions: number; sessionDetails: unknown[] }>
}
}
const row = json.current.topProjects.find(project => project.id === cwd)
expect(row.sessionDetails).toHaveLength(1)
expect(row.sessions).toBe(1)
} finally {
await rm(home, { recursive: true, force: true })
}
}, 120_000)
})

View file

@ -1,22 +0,0 @@
import {it,expect} from 'vitest'
import {mkdtempSync,mkdirSync,writeFileSync} from 'node:fs'
import {homedir} from 'node:os'
import {join} from 'node:path'
import {spawnSync} from 'node:child_process'
it('one real session spanning two days is one period project session',()=>{
const dir=mkdtempSync(join(homedir(),'root-session-days-'));const claude=join(dir,'claude');const projects=join(claude,'projects','project-a');mkdirSync(projects,{recursive:true})
const now=new Date(Date.now()-60000);const older=new Date(now.getTime()-5*86400000);const sessionId='same-actual-session';const cwd='/tmp/same-session-project'
const rows=[older,now].flatMap((date,i)=>[
{type:'user',sessionId,timestamp:date.toISOString(),cwd,message:{role:'user',content:'task'+i}},
{type:'assistant',sessionId,timestamp:date.toISOString(),cwd,message:{id:'response'+i,role:'assistant',model:'claude-sonnet-4-5',content:[{type:'text',text:'done'}],usage:{input_tokens:1000,output_tokens:200,cache_creation_input_tokens:0,cache_read_input_tokens:0}}}
])
writeFileSync(join(projects,sessionId+'.jsonl'),rows.map(x=>JSON.stringify(x)).join('\n')+'\n')
const env={...process.env,CLAUDE_CONFIG_DIR:claude,CODEBURN_CACHE_DIR:join(dir,'cache'),CODEBURN_DESKTOP_SESSIONS_DIR:join(dir,'desktop')}
const cli=(args:string[])=>{const r=spawnSync(process.execPath,['dist/cli.js',...args],{cwd:process.cwd(),env,encoding:'utf8',timeout:60000});expect(r.status,r.stderr).toBe(0);return JSON.parse(r.stdout)}
cli(['models','--format','json','--period','week'])
const json=cli(['status','--format','menubar-json','--period','week','--provider','claude','--no-timeline'])
const row=json.current.topProjects.find((r:any)=>r.id===cwd)
console.log('ROOT_REAL_SESSION_DAYS',JSON.stringify({headline:json.current.sessions,row}))
expect(row.sessionDetails).toHaveLength(1)
expect(row.sessions).toBe(1)
},120000)