codeburn/tests/cli-settings-json.test.ts
iamtoruk 62cb4f8630 test: isolate the real home on Windows and make the CLI suite pass there
os.homedir() reads USERPROFILE on Windows and never HOME, so the
isolation setup and every test that moved home were still resolving the
developer's real home directory. Fixtures came back empty, real sessions
leaked into counts, and the code under test wrote plans, receipts and a
sync ledger into the real ~/.config/codeburn during a run.

USERPROFILE now joins the redirected variables and a shared setHome()
helper moves HOME and USERPROFILE together; every spawn env carries both.
The remaining Windows-only failures were POSIX-shaped fixtures: chmod
tricks that only toggle a read-only attribute on NTFS, pid 1 as an
always-alive lock holder, splitting paths on '/', bare tar, mode-bit
expectations, and per-file timeouts too short for the cost of a tsx spawn.
Each gets a platform-aware equivalent so nothing is skipped on Windows and
the POSIX branches stay byte-for-byte what they were.

Full run on Windows: 267 files passed, 2 skipped, 3592 tests passed.
2026-09-02 13:43:57 -07:00

83 lines
3 KiB
TypeScript

import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { spawnSync } from 'node:child_process'
import { afterEach, describe, expect, it, vi } from 'vitest'
vi.setConfig({ testTimeout: 30_000 })
const homes: string[] = []
afterEach(async () => {
while (homes.length > 0) {
const home = homes.pop()
if (home) await rm(home, { recursive: true, force: true })
}
})
async function makeHome(): Promise<string> {
const home = await mkdtemp(join(tmpdir(), 'codeburn-settings-json-'))
homes.push(home)
return home
}
function runCli(args: string[], home: string) {
return spawnSync(process.execPath, ['--import', 'tsx', 'src/cli.ts', ...args], {
cwd: process.cwd(),
env: { ...process.env, HOME: home, USERPROFILE: home, CLAUDE_CONFIG_DIR: join(home, '.claude'), TZ: 'UTC' },
encoding: 'utf-8',
timeout: 30_000,
})
}
describe('settings CLI JSON list output', () => {
it('lists model aliases as rows sorted by from', async () => {
const home = await makeHome()
expect(runCli(['model-alias', 'z-model', 'gpt-4o'], home).status).toBe(0)
expect(runCli(['model-alias', 'a-model', 'claude-sonnet-4-6'], home).status).toBe(0)
const result = runCli(['model-alias', '--list', '--format', 'json'], home)
expect(result.status).toBe(0)
expect(JSON.parse(result.stdout)).toEqual([
{ from: 'a-model', to: 'claude-sonnet-4-6' },
{ from: 'z-model', to: 'gpt-4o' },
])
})
it('lists configured proxy paths as strings', async () => {
const home = await makeHome()
expect(runCli(['proxy-path', '/work/copilot-repo'], home).status).toBe(0)
const result = runCli(['proxy-path', '--list', '--format', 'json'], home)
expect(result.status).toBe(0)
expect(JSON.parse(result.stdout)).toEqual(['/work/copilot-repo'])
})
it('lists price overrides as rows sorted by model with a config path', async () => {
const home = await makeHome()
expect(runCli(['price-override', 'z-model', '--input', '0.5', '--output', '2', '--cache-read', '0.05'], home).status).toBe(0)
expect(runCli(['price-override', 'a-model', '--input', '0.27', '--output', '1.1', '--cache-creation', '0.42'], home).status).toBe(0)
const result = runCli(['price-override', '--list', '--format', 'json'], home)
expect(result.status).toBe(0)
const parsed = JSON.parse(result.stdout)
expect(parsed.overrides).toEqual([
{ model: 'a-model', inputPerM: 0.27, outputPerM: 1.1, cacheCreationPerM: 0.42 },
{ model: 'z-model', inputPerM: 0.5, outputPerM: 2, cacheReadPerM: 0.05 },
])
expect(typeof parsed.configPath).toBe('string')
expect(parsed.configPath).toContain('config.json')
})
it('emits an empty override list as JSON when none are configured', async () => {
const home = await makeHome()
const result = runCli(['price-override', '--list', '--format', 'json'], home)
expect(result.status).toBe(0)
expect(JSON.parse(result.stdout).overrides).toEqual([])
})
})