import { describe, expect, test } from 'bun:test'; import { type ProxmoxProviderConfig, buildVmTemplateChoices, resolveReconfiguredProviderConfig, } from './service-reconfigure'; describe('buildVmTemplateChoices — VM-template reconfigure menu', () => { const templates = [ { vmid: 9000, name: 'ubuntu-2404-cloudinit' }, { vmid: 9001, name: 'ubuntu-2204-cloudinit' }, ]; test('current set + detected templates: keep first, others (current deduped), build, clear', () => { const choices = buildVmTemplateChoices('ubuntu-2404-cloudinit', templates); expect(choices.map((c) => c.value)).toEqual([ '__keep__', 'ubuntu-2204-cloudinit', // the other detected template — current is not re-listed '__build__', '__clear__', ]); expect(choices[0].label).toBe('Keep current (ubuntu-2404-cloudinit)'); }); test('current unset: no keep/clear; detected templates, build, then skip', () => { const choices = buildVmTemplateChoices(undefined, templates); expect(choices.map((c) => c.value)).toEqual([ 'ubuntu-2404-cloudinit', 'ubuntu-2204-cloudinit', '__build__', '__skip__', ]); }); test('current set, no templates detected: keep, build, clear', () => { const choices = buildVmTemplateChoices('ubuntu-2404-cloudinit', []); expect(choices.map((c) => c.value)).toEqual(['__keep__', '__build__', '__clear__']); }); test('current unset, no templates detected: build, skip', () => { const choices = buildVmTemplateChoices(undefined, []); expect(choices.map((c) => c.value)).toEqual(['__build__', '__skip__']); }); test('a detected template carries its VMID in the label', () => { const choices = buildVmTemplateChoices(undefined, [{ vmid: 9002, name: 'debian-12' }]); expect(choices[0]).toEqual({ value: 'debian-12', label: 'debian-12 (VMID 9002)' }); }); }); describe('resolveReconfiguredProviderConfig — drop-bug guard (ISS-0128)', () => { const current: ProxmoxProviderConfig = { default_target_node: 'pve', lxc_template: 'local:vztmpl/ubuntu-24.04.tar.zst', storage: 'local-lvm', vm_template: 'ubuntu-2404-cloudinit', }; test('keeping the VM template (edit echoes current) preserves it while applying other edits', () => { const result = resolveReconfiguredProviderConfig({ default_target_node: 'pve2', lxc_template: 'local:vztmpl/ubuntu-24.04-2.tar.zst', storage: 'fast-lvm', vm_template: 'ubuntu-2404-cloudinit', // VM_KEEP resolves to the current value }); expect(result).toEqual({ default_target_node: 'pve2', lxc_template: 'local:vztmpl/ubuntu-24.04-2.tar.zst', storage: 'fast-lvm', vm_template: 'ubuntu-2404-cloudinit', }); }); test('the pre-fix behavior would have dropped vm_template — guard against regressing', () => { // Reconfiguring only the LXC fields must NOT lose the VM template: the // handler threads currentConfig.vm_template through as the edit value. const result = resolveReconfiguredProviderConfig({ default_target_node: current.default_target_node, lxc_template: 'local:vztmpl/new.tar.zst', storage: current.storage, vm_template: current.vm_template, }); expect(result.vm_template).toBe('ubuntu-2404-cloudinit'); }); test('clearing (undefined edit) removes the field entirely, not sets it undefined', () => { const result = resolveReconfiguredProviderConfig({ default_target_node: 'pve', lxc_template: current.lxc_template, storage: current.storage, vm_template: undefined, }); expect('vm_template' in result).toBe(false); }); test('setting a new template overwrites the old', () => { const result = resolveReconfiguredProviderConfig({ default_target_node: 'pve', lxc_template: current.lxc_template, storage: current.storage, vm_template: 'debian-12-cloudinit', }); expect(result.vm_template).toBe('debian-12-cloudinit'); }); test('no VM template edit leaves it absent', () => { const result = resolveReconfiguredProviderConfig({ default_target_node: 'pve', lxc_template: 'local:vztmpl/x.tar.zst', storage: 'local-lvm', vm_template: undefined, }); expect('vm_template' in result).toBe(false); }); });