codeburn/tests/config-unparseable.test.ts
Resham Joshi 7b803b293b
fix: survive prototype-named tools and models, never wipe a config on a syntax error, validate plugin names before removal (#1897)
* fix: keep tool and model names like constructor or toString as plain names

Lookups of provider-supplied tool names and model ids into plain-object maps
returned the inherited Object.prototype member, so one session with a tool
named constructor rejected the whole parse, and a model named toString broke
the models report and the menubar payload. Route those lookups through
Object.hasOwn.

* fix: never overwrite an unparseable config.json

readConfig fell back to {} on any error, so one syntax error in a hand-edited
config.json plus any setter (currency, budget, model-alias, ...) saved {} with
one key and erased every other setting. saveConfig now moves an unparseable
file to config.json.corrupt-<timestamp>, says so on stderr and exits 1; read
paths keep the {} fallback with a one-line warning.

* fix: validate the plugin name before plugin remove, info and verify touch disk

plugin remove joined the raw name onto the plugins dir, so
`plugin remove .. --confirm` deleted the whole codeburn config dir. Check the
name against the manifest name pattern first, as the loader already requires.

* fix: keep a model or tool named __proto__ in every running total

The per-model and per-tool totals behind the models report, the menubar
payload, the JSON report and the export assigned into plain objects, so a
name like __proto__ set the prototype instead of adding a row. Build them
prototype-free, and stop the daily cache load from dropping model rows whose
name happens to be an Object.prototype member: they are written own-only.
2026-11-06 09:27:41 +01:00

50 lines
1.8 KiB
TypeScript

import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { spawnSync } from 'node:child_process'
import { afterEach, expect, it } from 'vitest'
let home = ''
afterEach(async () => {
if (home) await rm(home, { recursive: true, force: true })
})
function runCli(args: 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'), CODEBURN_CACHE_DIR: join(home, 'cache') },
encoding: 'utf-8',
timeout: 30_000,
})
}
it('a setter refuses to overwrite an unparseable config and keeps its bytes', async () => {
home = await mkdtemp(join(tmpdir(), 'codeburn-config-bad-'))
const dir = join(home, '.config', 'codeburn')
await mkdir(dir, { recursive: true })
const original = '{\n "priceOverrides": { "my-model": { "input": 1, "output": 2 } },\n}\n'
await writeFile(join(dir, 'config.json'), original)
const res = runCli(['currency', 'EUR'])
expect(res.status).toBe(1)
expect(res.stderr).toContain('is not valid JSON')
const files = await readdir(dir)
expect(files).not.toContain('config.json')
const corrupt = files.filter(f => f.startsWith('config.json.corrupt-'))
expect(corrupt).toHaveLength(1)
expect(await readFile(join(dir, corrupt[0]!), 'utf-8')).toBe(original)
})
it('a setter still creates the config when none exists', async () => {
home = await mkdtemp(join(tmpdir(), 'codeburn-config-none-'))
const res = runCli(['currency', 'EUR'])
expect(res.status).toBe(0)
expect(res.stderr).toBe('')
const saved = JSON.parse(await readFile(join(home, '.config', 'codeburn', 'config.json'), 'utf-8'))
expect(saved.currency.code).toBe('EUR')
})