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 { createRegistry } from '../../src/registry.js' import type { ConnectionConfig } from '../../src/schema.js' import { readStore } from '../../src/store.js' const conn = (name: string, extra: Partial = {}): ConnectionConfig => ({ name, type: 'elasticsearch', host: 'es.example.com', scheme: 'https', verifyTls: true, apiKey: 'secret==', readonly: false, ...extra }) describe('createRegistry', () => { let dir: string let storePath: string beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'esmole-registry-')) storePath = join(dir, 'connections.json') vi.spyOn(console, 'error').mockImplementation(() => {}) }) afterEach(() => { rmSync(dir, { recursive: true, force: true }) vi.restoreAllMocks() }) it('adds, lists, resolves and persists an apiKey connection', () => { const registry = createRegistry({ storePath, env: {} }) const added = registry.add(conn('primary')) expect(added.config.name).toBe('primary') expect(added.source).toBe('store') expect(registry.list().map((r) => r.config.name)).toEqual(['primary']) expect(registry.resolve('primary').config.apiKey).toBe('secret==') // store delegation: written file round-trips through readStore expect(readStore(storePath).map((c) => c.name)).toEqual(['primary']) }) it('adds a user+password connection', () => { const registry = createRegistry({ storePath, env: {} }) registry.add(conn('basic', { apiKey: undefined, user: 'elastic', password: 'pw' })) const resolved = registry.resolve('basic') expect(resolved.config.user).toBe('elastic') expect(resolved.config.password).toBe('pw') }) it('merges env connections over the store (sources delegation)', () => { const registry = createRegistry({ storePath, env: { ESMOLE_CONNECTIONS: JSON.stringify([conn('envc', { host: 'from-env' })]) } }) expect(registry.resolve('envc').config.host).toBe('from-env') expect(registry.resolve('envc').source).toBe('env') }) })