import { describe, it, expect } from 'vitest';
import { pickCpuTemp } from './temps';
import type { TempStat } from '$lib/api/admin';

function t(label: string, celsius: number): TempStat {
    return { label, celsius, max_celsius: celsius, critical_celsius: 100 };
}

describe('pickCpuTemp', () => {
    it('returns null when there are no sensors', () => {
        expect(pickCpuTemp([])).toBeNull();
    });

    it('prefers the Intel package sensor over individual cores', () => {
        const temps = [
            t('coretemp Core 0', 50),
            t('coretemp Core 1', 49),
            t('coretemp Package id 0', 51),
            t('acpitz temp1', 28)
        ];
        expect(pickCpuTemp(temps)?.label).toBe('coretemp Package id 0');
    });

    it('falls back to the AMD Tctl/Tdie sensor when there is no package', () => {
        const temps = [t('k10temp Tctl', 55), t('nvme Composite', 41)];
        expect(pickCpuTemp(temps)?.label).toBe('k10temp Tctl');
    });

    it('falls back to the hottest core when only per-core sensors exist', () => {
        const temps = [t('coretemp Core 0', 50), t('coretemp Core 3', 58), t('coretemp Core 1', 49)];
        expect(pickCpuTemp(temps)?.label).toBe('coretemp Core 3');
    });

    it('falls back to any cpu/coretemp-labelled sensor as a last resort', () => {
        const temps = [t('nvme Composite', 41), t('cpu_thermal', 47)];
        expect(pickCpuTemp(temps)?.label).toBe('cpu_thermal');
    });

    it('returns null when nothing looks like a CPU sensor', () => {
        const temps = [t('nvme Composite', 41), t('acpitz temp1', 28)];
        expect(pickCpuTemp(temps)).toBeNull();
    });
});
