"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const vitest_1 = require("vitest");
const cpuArchitecture_1 = require("../../../common/types/cpuArchitecture");
const versions_1 = require("./versions");
const ClusterImageSetApiVersion = 'hive.openshift.io/v1';
const ClusterImageSetKind = 'ClusterImageSet';
function makeClusterImageSet(overrides) {
    return Object.assign({ apiVersion: ClusterImageSetApiVersion, kind: ClusterImageSetKind }, overrides);
}
function makeOsImage(overrides) {
    const { openshiftVersion } = overrides, rest = tslib_1.__rest(overrides, ["openshiftVersion"]);
    return Object.assign({ cpuArchitecture: 'x86_64', openshiftVersion, url: 'https://example.com', version: '1' }, rest);
}
(0, vitest_1.describe)('getVersionFromClusterImageSet', () => {
    (0, vitest_1.test)('uses releaseTag when releaseImage is digest-only', () => {
        const cis = makeClusterImageSet({
            metadata: {
                name: 'img4.21.3-multi-appsub',
                labels: { visible: 'true', releaseTag: '4.21.3-multi' },
            },
            spec: {
                releaseImage: 'quay.io/openshift-release-dev/ocp-release@sha256:abc123',
            },
        });
        (0, vitest_1.expect)((0, versions_1.getVersionFromClusterImageSet)(cis)).toBe('4.21.3-multi');
    });
    (0, vitest_1.test)('falls back to releaseImage when releaseTag is absent', () => {
        const cis = makeClusterImageSet({
            metadata: { name: 'img4.15.36-multi-appsub', labels: { visible: 'true' } },
            spec: {
                releaseImage: 'quay.io/openshift-release-dev/ocp-release:4.15.36-multi',
            },
        });
        (0, vitest_1.expect)((0, versions_1.getVersionFromClusterImageSet)(cis)).toBe('4.15.36-multi');
    });
    (0, vitest_1.test)('falls back to metadata.name when tag and image do not parse', () => {
        const cis = makeClusterImageSet({
            metadata: {
                name: 'img4.18.10-multi-appsub',
                labels: { visible: 'true' },
            },
            spec: {
                releaseImage: 'quay.io/openshift-release-dev/ocp-release@sha256:abc123',
            },
        });
        (0, vitest_1.expect)((0, versions_1.getVersionFromClusterImageSet)(cis)).toBe('img4.18.10-multi-appsub');
    });
});
(0, vitest_1.describe)('getOCPVersions', () => {
    (0, vitest_1.test)('excludes image sets without visible true when extended is false', () => {
        const hidden = makeClusterImageSet({
            metadata: {
                name: '4.16.1',
                labels: { visible: 'false', releaseTag: '4.16.1' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.16.1-x86_64' },
        });
        (0, vitest_1.expect)((0, versions_1.getOCPVersions)([hidden])).toEqual([]);
    });
    (0, vitest_1.test)('includes image sets without visible true when extended is true', () => {
        var _a, _b;
        const hidden = makeClusterImageSet({
            metadata: {
                name: '4.16.2',
                labels: { releaseTag: '4.16.2' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.16.2-x86_64' },
        });
        const result = (0, versions_1.getOCPVersions)([hidden], undefined, undefined, true);
        (0, vitest_1.expect)(result).toHaveLength(1);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.version).toBe('4.16.2');
        (0, vitest_1.expect)((_b = result[0]) === null || _b === void 0 ? void 0 : _b.default).toBe(true);
    });
    (0, vitest_1.test)('sorts by label descending', () => {
        const a = makeClusterImageSet({
            metadata: {
                name: 'img-a',
                labels: { visible: 'true', releaseTag: '4.15.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.15.0-x86_64' },
        });
        const b = makeClusterImageSet({
            metadata: {
                name: 'img-b',
                labels: { visible: 'true', releaseTag: '4.21.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.21.0-x86_64' },
        });
        const result = (0, versions_1.getOCPVersions)([a, b]);
        (0, vitest_1.expect)(result.map((v) => v.version)).toEqual(['4.21.0', '4.15.0']);
    });
    (0, vitest_1.test)('marks only the first sorted option as default', () => {
        var _a, _b;
        const first = makeClusterImageSet({
            metadata: {
                name: 'first',
                labels: { visible: 'true', releaseTag: '4.14.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.14.0-x86_64' },
        });
        const second = makeClusterImageSet({
            metadata: {
                name: 'second',
                labels: { visible: 'true', releaseTag: '4.16.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.16.0-x86_64' },
        });
        const result = (0, versions_1.getOCPVersions)([first, second]);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.default).toBe(true);
        (0, vitest_1.expect)((_b = result[1]) === null || _b === void 0 ? void 0 : _b.default).toBe(false);
    });
    (0, vitest_1.test)('dedupes by version string keeping first in sorted order', () => {
        const dupA = makeClusterImageSet({
            metadata: {
                name: 'dup-a',
                labels: { visible: 'true', releaseTag: '4.17.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.17.0-x86_64' },
        });
        const dupB = makeClusterImageSet({
            metadata: {
                name: 'dup-b',
                labels: { visible: 'true', releaseTag: '4.17.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.17.0-x86_64' },
        });
        const other = makeClusterImageSet({
            metadata: {
                name: 'other',
                labels: { visible: 'true', releaseTag: '4.16.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.16.0-x86_64' },
        });
        const result = (0, versions_1.getOCPVersions)([dupA, dupB, other]);
        (0, vitest_1.expect)(result.map((v) => v.value)).toEqual(['dup-a', 'other']);
        (0, vitest_1.expect)(result.map((v) => v.version)).toEqual(['4.17.0', '4.16.0']);
    });
    (0, vitest_1.test)('when isNutanix, keeps only x86_64 or x86-64 release images or matching architecture label', () => {
        const x86 = makeClusterImageSet({
            metadata: {
                name: 'nut-x86',
                labels: { visible: 'true', releaseTag: '4.16.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.16.0-x86_64' },
        });
        const multi = makeClusterImageSet({
            metadata: {
                name: 'nut-multi',
                labels: { visible: 'true', releaseTag: '4.16.1-mutli', architecture: 'multi' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.16.1-multi' },
        });
        const result = (0, versions_1.getOCPVersions)([x86, multi], true);
        (0, vitest_1.expect)(result.map((v) => v.value)).toEqual(['nut-x86']);
    });
    (0, vitest_1.test)('when isNutanix, allows architecture label x86_64 even if release image suffix differs', () => {
        var _a;
        const cis = makeClusterImageSet({
            metadata: {
                name: 'nut-label',
                labels: { visible: 'true', releaseTag: '4.16.2-multi', architecture: 'x86_64' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.16.2-multi' },
        });
        const result = (0, versions_1.getOCPVersions)([cis], true);
        (0, vitest_1.expect)(result).toHaveLength(1);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.value).toBe('nut-label');
    });
    (0, vitest_1.test)('filters by osImages openshiftVersion major.minor', () => {
        const inRange = makeClusterImageSet({
            metadata: {
                name: 'in',
                labels: { visible: 'true', releaseTag: '4.18.55' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.18.55-x86_64' },
        });
        const outRange = makeClusterImageSet({
            metadata: {
                name: 'out',
                labels: { visible: 'true', releaseTag: '4.19.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.19.0-x86_64' },
        });
        const osImages = [makeOsImage({ openshiftVersion: '4.18' })];
        const result = (0, versions_1.getOCPVersions)([inRange, outRange], false, osImages);
        (0, vitest_1.expect)(result.map((v) => v.value)).toEqual(['in']);
    });
    (0, vitest_1.test)('cpuArchitectures from labels.architecture x86_64', () => {
        var _a;
        const cis = makeClusterImageSet({
            metadata: {
                name: 'arch-x86',
                labels: { visible: 'true', releaseTag: '4.17.0-multi', architecture: 'x86_64' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.17.0-multi' },
        });
        const result = (0, versions_1.getOCPVersions)([cis]);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.cpuArchitectures).toEqual([cpuArchitecture_1.CpuArchitecture.x86]);
    });
    (0, vitest_1.test)('cpuArchitectures from labels.architecture arm64', () => {
        var _a;
        const cis = makeClusterImageSet({
            metadata: {
                name: 'arch-arm',
                labels: { visible: 'true', releaseTag: '4.17.0-multi', architecture: 'arm64' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.17.0-multi' },
        });
        const result = (0, versions_1.getOCPVersions)([cis]);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.cpuArchitectures).toEqual([cpuArchitecture_1.CpuArchitecture.ARM]);
    });
    (0, vitest_1.test)('cpuArchitectures from labels.architecture multi', () => {
        var _a;
        const cis = makeClusterImageSet({
            metadata: {
                name: 'arch-multi',
                labels: { visible: 'true', releaseTag: '4.17.0-multi', architecture: 'multi' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.17.0-multi' },
        });
        const result = (0, versions_1.getOCPVersions)([cis]);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.cpuArchitectures).toEqual([cpuArchitecture_1.CpuArchitecture.MULTI]);
    });
    (0, vitest_1.test)('cpuArchitectures from labels.architecture s390x', () => {
        var _a;
        const cis = makeClusterImageSet({
            metadata: {
                name: 'arch-s390x',
                labels: { visible: 'true', releaseTag: '4.17.0-multi', architecture: 's390x' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.17.0-multi' },
        });
        const result = (0, versions_1.getOCPVersions)([cis]);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.cpuArchitectures).toEqual([cpuArchitecture_1.CpuArchitecture.s390x]);
    });
    (0, vitest_1.test)('cpuArchitectures normalizes x86-64 from release string when tag and image agree', () => {
        var _a;
        const cis = makeClusterImageSet({
            metadata: {
                name: 'img4.17.1-x86-64-appsub',
                labels: {
                    visible: 'true',
                    releaseTag: '4.17.1-x86-64',
                },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.17.1-x86-64' },
        });
        const result = (0, versions_1.getOCPVersions)([cis]);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.cpuArchitectures).toEqual([cpuArchitecture_1.CpuArchitecture.x86]);
    });
    (0, vitest_1.test)('labels.architecture takes precedence over releaseTag arch segment', () => {
        var _a;
        const cis = makeClusterImageSet({
            metadata: {
                name: 'prec',
                labels: {
                    visible: 'true',
                    releaseTag: '4.19.0-multi',
                    architecture: 'x86_64',
                },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.19.0-multi' },
        });
        const result = (0, versions_1.getOCPVersions)([cis]);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.cpuArchitectures).toEqual([cpuArchitecture_1.CpuArchitecture.x86]);
    });
    (0, vitest_1.test)('cpuArchitectures undefined when no arch in labels or release strings', () => {
        var _a;
        const cis = makeClusterImageSet({
            metadata: {
                name: 'ocp-release4.17.0',
                labels: { visible: 'true', releaseTag: '4.17.0' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.17.0' },
        });
        const result = (0, versions_1.getOCPVersions)([cis]);
        (0, vitest_1.expect)((_a = result[0]) === null || _a === void 0 ? void 0 : _a.cpuArchitectures).toBeUndefined();
    });
});
(0, vitest_1.describe)('getSelectedVersion', () => {
    (0, vitest_1.test)('returns parsed version when cluster image matches imageSetRef', () => {
        const cis = makeClusterImageSet({
            metadata: {
                name: 'my-imageset',
                labels: { visible: 'true', releaseTag: '4.18.10' },
            },
            spec: { releaseImage: 'quay.io/ocp/release:4.18.10-x86_64' },
        });
        const aci = {
            apiVersion: 'extensions.hive.openshift.io/v1beta1',
            kind: 'AgentClusterInstall',
            metadata: { name: 'aci', namespace: 'ns' },
            spec: { imageSetRef: { name: 'my-imageset' } },
        };
        (0, vitest_1.expect)((0, versions_1.getSelectedVersion)([cis], aci)).toBe('4.18.10');
    });
    (0, vitest_1.test)('returns imageSetRef name when no cluster image matches', () => {
        const aci = {
            apiVersion: 'extensions.hive.openshift.io/v1beta1',
            kind: 'AgentClusterInstall',
            metadata: { name: 'aci', namespace: 'ns' },
            spec: { imageSetRef: { name: 'missing' } },
        };
        (0, vitest_1.expect)((0, versions_1.getSelectedVersion)([], aci)).toBe('missing');
    });
    (0, vitest_1.test)('returns undefined when imageSetRef is missing', () => {
        const aci = {
            apiVersion: 'extensions.hive.openshift.io/v1beta1',
            kind: 'AgentClusterInstall',
            metadata: { name: 'aci', namespace: 'ns' },
            spec: {},
        };
        (0, vitest_1.expect)((0, versions_1.getSelectedVersion)([], aci)).toBeUndefined();
    });
});
(0, vitest_1.describe)('getCurrentClusterVersion', () => {
    (0, vitest_1.test)('prefers status.history[0].version', () => {
        const cv = {
            apiVersion: 'config.openshift.io/v1',
            kind: 'ClusterVersion',
            metadata: { name: 'version' },
            spec: {
                channel: 'fast-4.18',
                clusterID: 'id',
                desiredUpdate: { version: '4.18.99', image: 'img' },
            },
            status: {
                desired: { version: '4.18.0', image: 'img' },
                history: [
                    {
                        version: '4.18.5',
                        state: 'Completed',
                        startedTime: '',
                        completionTime: '',
                        image: '',
                        verified: true,
                    },
                ],
                observedGeneration: 1,
                versionHash: 'h',
                availableUpdates: [],
            },
        };
        (0, vitest_1.expect)((0, versions_1.getCurrentClusterVersion)(cv)).toBe('4.18.5');
    });
    (0, vitest_1.test)('falls back to spec.desiredUpdate.version when history is empty', () => {
        const cv = {
            apiVersion: 'config.openshift.io/v1',
            kind: 'ClusterVersion',
            metadata: { name: 'version' },
            spec: {
                channel: 'fast-4.18',
                clusterID: 'id',
                desiredUpdate: { version: '4.18.99', image: 'img' },
            },
            status: {
                desired: { version: '4.18.0', image: 'img' },
                history: [],
                observedGeneration: 1,
                versionHash: 'h',
                availableUpdates: [],
            },
        };
        (0, vitest_1.expect)((0, versions_1.getCurrentClusterVersion)(cv)).toBe('4.18.99');
    });
    (0, vitest_1.test)('returns undefined when no version is available', () => {
        const cv = {
            apiVersion: 'config.openshift.io/v1',
            kind: 'ClusterVersion',
            metadata: { name: 'version' },
            spec: {
                channel: 'fast-4.18',
                clusterID: 'id',
            },
            status: {
                desired: { version: '4.18.0', image: 'img' },
                history: [],
                observedGeneration: 1,
                versionHash: 'h',
                availableUpdates: [],
            },
        };
        (0, vitest_1.expect)((0, versions_1.getCurrentClusterVersion)(cv)).toBeUndefined();
    });
});
(0, vitest_1.describe)('getNetworkType', () => {
    (0, vitest_1.test)('returns OpenShiftSDN when version is below 4.12', () => {
        const v = {
            label: 'OpenShift 4.11.5',
            version: '4.11.5',
            value: 'x',
            default: false,
            supportLevel: 'production',
        };
        (0, vitest_1.expect)((0, versions_1.getNetworkType)(v)).toBe('OpenShiftSDN');
    });
    (0, vitest_1.test)('returns OVNKubernetes when version is 4.12 or higher', () => {
        const v = {
            label: 'OpenShift 4.12.0',
            version: '4.12.0',
            value: 'x',
            default: false,
            supportLevel: 'production',
        };
        (0, vitest_1.expect)((0, versions_1.getNetworkType)(v)).toBe('OVNKubernetes');
    });
    (0, vitest_1.test)('returns OVNKubernetes when ocpVersion is undefined', () => {
        (0, vitest_1.expect)((0, versions_1.getNetworkType)(undefined)).toBe('OVNKubernetes');
    });
    (0, vitest_1.test)('returns OVNKubernetes when version string is empty', () => {
        const v = {
            label: 'OpenShift ',
            version: '',
            value: 'x',
            default: false,
            supportLevel: 'production',
        };
        (0, vitest_1.expect)((0, versions_1.getNetworkType)(v)).toBe('OVNKubernetes');
    });
});
//# sourceMappingURL=versions.test.js.map