fix: harden store writes, strict schemas, null cfg
This commit is contained in:
@@ -69,4 +69,18 @@ describe('readConfigFile', () => {
|
||||
expect(readConfigFile(path)).toEqual([])
|
||||
expect(console.error).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('warns on null config', () => {
|
||||
const path = join(dir, 'config.json')
|
||||
writeFileSync(path, 'null')
|
||||
expect(readConfigFile(path)).toEqual([])
|
||||
expect(console.error).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('warns on primitive config', () => {
|
||||
const path = join(dir, 'config.json')
|
||||
writeFileSync(path, '"just a string"')
|
||||
expect(readConfigFile(path)).toEqual([])
|
||||
expect(console.error).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -78,6 +78,13 @@ describe('store', () => {
|
||||
writeStore(path, [connection])
|
||||
expect(readFileSync(path, 'utf8')).toContain('\n "name": "lab"')
|
||||
})
|
||||
|
||||
it('cleans up the tmp file when rename fails', () => {
|
||||
mkdirSync(path, { recursive: true })
|
||||
expect(() => writeStore(path, [connection])).toThrow()
|
||||
const leftovers = readdirSync(join(dir, 'nested')).filter((f) => f.includes('.tmp'))
|
||||
expect(leftovers).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('defaultStorePath', () => {
|
||||
|
||||
@@ -40,6 +40,19 @@ describe('connectionConfigSchema', () => {
|
||||
it('rejects empty host', () => {
|
||||
expect(() => connectionConfigSchema.parse({ ...minimal, host: '' })).toThrow()
|
||||
})
|
||||
|
||||
it('rejects unknown top-level keys', () => {
|
||||
expect(() => connectionConfigSchema.parse({ ...minimal, readOnly: true })).toThrow()
|
||||
})
|
||||
|
||||
it('rejects unknown ssh keys', () => {
|
||||
expect(() =>
|
||||
connectionConfigSchema.parse({
|
||||
...minimal,
|
||||
ssh: { host: 'b', user: 'u', password: 'p', prot: 22 }
|
||||
})
|
||||
).toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe('defaultPort', () => {
|
||||
|
||||
@@ -74,6 +74,17 @@ describe('formatDbError', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('joins detail and hint', () => {
|
||||
const error = Object.assign(new Error('boom'), {
|
||||
code: '23505',
|
||||
detail: 'Key (id)=(1) already exists.',
|
||||
hint: 'use upsert'
|
||||
})
|
||||
expect(formatDbError('postgres', error)).toBe(
|
||||
'[postgres 23505] boom (Key (id)=(1) already exists. | use upsert)'
|
||||
)
|
||||
})
|
||||
|
||||
it('handles non-Error values', () => {
|
||||
expect(formatDbError('mysql', 'boom')).toBe('[mysql] boom')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user