56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import { mkdtempSync, rmSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import type { ConnectionConfig } from '../../src/schema.js'
|
|
import { defaultStorePath, readStore, writeStore } from '../../src/store.js'
|
|
|
|
const conn = (name: string, extra: Partial<ConnectionConfig> = {}): ConnectionConfig => ({
|
|
name,
|
|
type: 'elasticsearch',
|
|
host: 'es.example.com',
|
|
scheme: 'https',
|
|
verifyTls: true,
|
|
apiKey: 'secret==',
|
|
readonly: false,
|
|
...extra
|
|
})
|
|
|
|
describe('defaultStorePath', () => {
|
|
it('honours ESMOLE_STORE when set', () => {
|
|
expect(defaultStorePath({ ESMOLE_STORE: '/custom/store.json' })).toBe('/custom/store.json')
|
|
})
|
|
|
|
it('falls back to the esmole config dir', () => {
|
|
const p = defaultStorePath({})
|
|
expect(p.endsWith(join('.config', 'esmole', 'connections.json'))).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('readStore / writeStore', () => {
|
|
let dir: string
|
|
let storePath: string
|
|
|
|
beforeEach(() => {
|
|
dir = mkdtempSync(join(tmpdir(), 'esmole-store-'))
|
|
storePath = join(dir, 'connections.json')
|
|
vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
})
|
|
|
|
afterEach(() => {
|
|
rmSync(dir, { recursive: true, force: true })
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('round-trips a connection through the core store', () => {
|
|
writeStore(storePath, [conn('es1')])
|
|
const read = readStore(storePath)
|
|
expect(read).toHaveLength(1)
|
|
expect(read[0]).toMatchObject({ name: 'es1', type: 'elasticsearch', apiKey: 'secret==' })
|
|
})
|
|
|
|
it('returns an empty list for a missing store file', () => {
|
|
expect(readStore(join(dir, 'nope.json'))).toEqual([])
|
|
})
|
|
})
|