import { afterAll, describe, expect, it } from 'bun:test'; import { mkdtempSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { handleSubscribersInstallDaemon } from './subscribers-install-daemon'; // resolveCeliloPath existence-checks explicit overrides, so the render // needs a celilo path that exists — a fake binary in a temp dir. const FAKE_CELOLILO = join(mkdtempSync(join(tmpdir(), 'celilo-fake-bin-')), 'celilo'); writeFileSync(FAKE_CELOLILO, '#!/bin/sh\n', { mode: 0o755 }); // --print renders without writing, so these tests never touch unit paths. const PRINT_FLAGS = { print: true, 'celilo-path': FAKE_CELOLILO }; const SAVED_ENV = process.env.CELILO_BUS_SECRET; afterAll(() => { if (SAVED_ENV === undefined) delete process.env.CELILO_BUS_SECRET; else process.env.CELILO_BUS_SECRET = SAVED_ENV; }); describe('handleSubscribersInstallDaemon secret resolution', () => { it('falls back to CELILO_BUS_SECRET when --secret is absent — the doctor remediation and the rendered unit both lean on that env var', async () => { process.env.CELILO_BUS_SECRET = 'env-secret'; const result = await handleSubscribersInstallDaemon([], { ...PRINT_FLAGS }); if (!result.success) throw new Error(`expected success: ${result.error}`); }); it('prefers the --secret flag over the environment', async () => { process.env.CELILO_BUS_SECRET = 'env-secret'; const result = await handleSubscribersInstallDaemon([], { ...PRINT_FLAGS, secret: 'flag-secret', }); if (!result.success) throw new Error(`expected success: ${result.error}`); expect((result.data as { unitContent: string }).unitContent).toContain('flag-secret'); }); it('refuses to render when neither source has a secret', async () => { delete process.env.CELILO_BUS_SECRET; const result = await handleSubscribersInstallDaemon([], { ...PRINT_FLAGS }); if (result.success) throw new Error('expected failure'); expect(result.error).toContain('secret is required'); }); });