import { describe, expect, test } from 'bun:test'; import type { Machine } from '../../types/infrastructure'; import { machineListJson } from './machine-list'; const AT = Date.UTC(2026, 8, 8, 22, 59, 59); function machine(partial: Partial = {}): Machine { return { id: '3f6c2a44-1f0e-4a5b-9c8d-7e6f5a4b3c2d', hostname: 'forgejo', zone: 'dmz', ipAddress: '10.0.20.5', sshUser: 'peba', // A real row always carries one; the JSON must not. sshKeyEncrypted: '{"v":1,"sealed":"secret"}', hardware: { cpu_cores: 4, memory_mb: 8192, disk_gb: 128, arch: 'arm64' }, role: 'host', interfaces: [{ name: 'eth0', ipAddress: '10.0.20.5', zone: 'dmz' }], earmarkedModule: null, apiOnly: false, createdAt: new Date(AT), updatedAt: new Date(AT), ...partial, }; } function parse(machines: Machine[]): unknown[] { const result = machineListJson(machines); if (!result.success) throw new Error(result.error); return JSON.parse(result.message ?? 'null') as unknown[]; } describe('machineListJson', () => { test('output parses as a machine-readable JSON array', () => { const rows = parse([machine()]); expect(Array.isArray(rows)).toBe(true); }); test('the encrypted SSH key envelope is not in the payload', () => { // Machine-readable output gets piped into scripts and stored in // artifacts. A secret envelope in every listing is worse than a // missing field in a terminal table. const [row] = parse([machine()]) as Array>; expect(row).not.toHaveProperty('sshKeyEncrypted'); expect(row.sshUser).toBe('peba'); }); test('timestamps survive as exact instants, not strings', () => { const [row] = parse([machine()]) as Array>; expect(row.createdAt).toBe(AT); expect(row.updatedAt).toBe(AT); }); test('an empty pool is a valid answer, not an error', () => { expect(parse([])).toEqual([]); }); });