import { describe, expect, test } from 'bun:test'; import type { ProxmoxAppliance } from '../../api-clients/proxmox'; import { buildUbuntuOptions, pickInitialTemplate } from './proxmox-template-selection'; // Snapshot of `pveam available --section system` filtered to Ubuntu, taken // 2026-05-08. The point of these tests is to lock in the catalog-driven // behavior that replaced the hardcoded `-1` revision URL. const catalog: ProxmoxAppliance[] = [ { template: 'ubuntu-22.04-standard_22.04-1_amd64.tar.zst', package: 'ubuntu-22.04-standard', version: '22.04-1', section: 'system', os: 'ubuntu', type: 'lxc', }, { template: 'ubuntu-24.04-standard_24.04-2_amd64.tar.zst', package: 'ubuntu-24.04-standard', version: '24.04-2', section: 'system', os: 'ubuntu', type: 'lxc', }, { template: 'ubuntu-24.10-standard_24.10-1_amd64.tar.zst', package: 'ubuntu-24.10-standard', version: '24.10-1', section: 'system', os: 'ubuntu', type: 'lxc', }, { template: 'ubuntu-25.04-standard_25.04-1.1_amd64.tar.zst', package: 'ubuntu-25.04-standard', version: '25.04-1.1', section: 'system', os: 'ubuntu', type: 'lxc', }, { template: 'ubuntu-26.04-standard_26.04-1_amd64.tar.zst', package: 'ubuntu-26.04-standard', version: '26.04-1', section: 'system', os: 'ubuntu', type: 'lxc', }, ]; describe('buildUbuntuOptions', () => { test('returns Ubuntu options sorted newest-first', () => { const options = buildUbuntuOptions(catalog); expect(options.map((o) => o.appliance.package)).toEqual([ 'ubuntu-26.04-standard', 'ubuntu-25.04-standard', 'ubuntu-24.10-standard', 'ubuntu-24.04-standard', 'ubuntu-22.04-standard', ]); }); test('flags LTS releases correctly', () => { const options = buildUbuntuOptions(catalog); const ltsByPackage = Object.fromEntries(options.map((o) => [o.appliance.package, o.isLts])); expect(ltsByPackage).toEqual({ 'ubuntu-22.04-standard': true, 'ubuntu-24.04-standard': true, 'ubuntu-24.10-standard': false, 'ubuntu-25.04-standard': false, 'ubuntu-26.04-standard': true, }); }); test('drops non-Ubuntu and non-system entries', () => { const mixed: ProxmoxAppliance[] = [ ...catalog, { template: 'debian-12-standard_12.7-1_amd64.tar.zst', package: 'debian-12-standard', version: '12.7-1', section: 'system', os: 'debian', }, { template: 'ubuntu-24.04-special_24.04-1_amd64.tar.zst', package: 'ubuntu-24.04-standard', version: '24.04-1', section: 'turnkeylinux', os: 'ubuntu', }, ]; const options = buildUbuntuOptions(mixed); expect(options).toHaveLength(catalog.length); expect(options.every((o) => (o.appliance.section ?? 'system') === 'system')).toBe(true); }); test('returns empty array when nothing matches', () => { expect(buildUbuntuOptions([])).toEqual([]); }); }); describe('pickInitialTemplate', () => { const options = buildUbuntuOptions(catalog); test('defaults to newest LTS when no current template given', () => { expect(pickInitialTemplate(options)).toBe('ubuntu-26.04-standard_26.04-1_amd64.tar.zst'); }); test('exact match wins when revision still on mirror', () => { expect(pickInitialTemplate(options, 'ubuntu-22.04-standard_22.04-1_amd64.tar.zst')).toBe( 'ubuntu-22.04-standard_22.04-1_amd64.tar.zst', ); }); test('falls back to family match when revision has been refreshed', () => { // The case that motivated the refactor: user has stale -1 cached, mirror has -2. expect(pickInitialTemplate(options, 'ubuntu-24.04-standard_24.04-1_amd64.tar.zst')).toBe( 'ubuntu-24.04-standard_24.04-2_amd64.tar.zst', ); }); test('falls through to newest LTS when current does not match any family', () => { expect(pickInitialTemplate(options, 'ubuntu-18.04-standard_18.04-1_amd64.tar.zst')).toBe( 'ubuntu-26.04-standard_26.04-1_amd64.tar.zst', ); }); test('returns undefined for empty options', () => { expect(pickInitialTemplate([])).toBeUndefined(); }); test('falls back to newest entry when no LTS in catalog', () => { const noLts = buildUbuntuOptions([ { template: 'ubuntu-25.04-standard_25.04-1.1_amd64.tar.zst', package: 'ubuntu-25.04-standard', version: '25.04-1.1', section: 'system', }, { template: 'ubuntu-24.10-standard_24.10-1_amd64.tar.zst', package: 'ubuntu-24.10-standard', version: '24.10-1', section: 'system', }, ]); expect(pickInitialTemplate(noLts)).toBe('ubuntu-25.04-standard_25.04-1.1_amd64.tar.zst'); }); });