import { describe, expect, test } from 'bun:test'; import type { DeployedSystem } from '@celilo/capabilities'; import { formatPlacementLine, resolveOnePlacement } from './placement-reconcile'; function sys( overrides: Partial & { infrastructure: DeployedSystem['infrastructure'] }, ): DeployedSystem { return { name: 'main', hostname: 'caddy', ipv4_address: '10.0.10.10', zone: 'dmz', ...overrides, } as DeployedSystem; } const SVC = 'svc-1'; const cs = (vmid?: number) => sys({ infrastructure: { type: 'container_service', serviceId: SVC, ...(vmid != null ? { vmid } : {}), }, }); describe('resolveOnePlacement (ISS-0060 — node reconciled from Proxmox, not a cached DB value)', () => { test('machine-pool system → machine (no Proxmox node)', () => { const s = sys({ infrastructure: { type: 'machine', machineId: 'm1' } }); expect(resolveOnePlacement(s, new Map(), new Set(), new Set())).toEqual({ kind: 'machine' }); }); test('non-Proxmox container service → other', () => { expect(resolveOnePlacement(cs(200), new Map(), new Set(), new Set([SVC]))).toEqual({ kind: 'other', }); }); test('container_service without a vmid → uncreated', () => { expect(resolveOnePlacement(cs(undefined), new Map(), new Set(), new Set())).toEqual({ kind: 'uncreated', }); }); test('Proxmox unreachable for the service → unreachable (never a stale fallback)', () => { expect(resolveOnePlacement(cs(200), new Map(), new Set([SVC]), new Set())).toEqual({ kind: 'unreachable', }); }); test('vmid present in the cluster → node (the reconciled reality)', () => { expect(resolveOnePlacement(cs(200), new Map([[200, 'node2']]), new Set(), new Set())).toEqual({ kind: 'node', node: 'node2', }); }); test('vmid absent from the cluster → absent', () => { expect(resolveOnePlacement(cs(200), new Map([[999, 'node2']]), new Set(), new Set())).toEqual({ kind: 'absent', }); }); }); describe('formatPlacementLine', () => { const s = cs(200); test('node resolution shows the real node + vmid', () => { expect(formatPlacementLine(s, { kind: 'node', node: 'node2' })).toBe( 'caddy (vmid 200) → node2 (zone dmz)', ); }); test('unreachable is explicit — no silent stale value', () => { expect(formatPlacementLine(s, { kind: 'unreachable' })).toContain('Proxmox unreachable'); }); test('absent flags a vmid not in the cluster', () => { expect(formatPlacementLine(s, { kind: 'absent' })).toContain('not found in Proxmox'); }); test('machine line omits vmid/node', () => { const m = sys({ hostname: 'iot', zone: 'internal', infrastructure: { type: 'machine' } }); expect(formatPlacementLine(m, { kind: 'machine' })).toBe('iot — machine (zone internal)'); }); });