import { getLatestPatch, getListOfVersionsFromManifest } from './cdn'

describe('CDN utils', () => {

    describe('Version resolution ', () => {
        it('should get the correct version from a list where all match the minor', () => {
            const version = getLatestPatch('1.1.2', ['1.1.0', '1.1.1', '1.1.3'])

            expect(version).toEqual('1.1.3')
        })

        it('should get the correct version from a list where there are multiple minor and major version', () => {
            const version = getLatestPatch('1.1.2', ['0.1.2', '1.1.0', '1.1.1', '1.1.3', '2.1.0'])

            expect(version).toEqual('1.1.3')
        })


        it('should thrown an error when there is no a compatible patch version higher than current one', () => {
            expect(() => getLatestPatch('1.1.5', ['0.1.2', '1.1.0', '1.1.1', '1.1.3'])).toThrowError()
        })


        it('should thrown an error when there is no a compatible patch version higher than current one', () => {
            expect(() => getLatestPatch('1.1.5', ['0.1.2', '1.1.0', '1.1.1', '1.1.3', '2.0.0'])).toThrowError()
        })

        it('should get the correct version from a list with beta versions', () => {
            const version = getLatestPatch('1.1.2', ['1.1.1', '1.1.3', '1.1.3-beta'])

            expect(version).toEqual('1.1.3')
        })

        it('should get the correct version for a beta', () => {
            const version = getLatestPatch('1.1.2-beta.1.2.3', ['1.1.1', '1.1.2-beta.1.2.3', '1.1.2-beta.1.2.4'])

            expect(version).toEqual('1.1.2-beta.1.2.4')
        })

        it('should get the correct version for a beta (II)', () => {
            const version = getLatestPatch('1.1.2-beta.1.2.3', ['1.1.1', '1.1.2-beta.1.2.3', '1.1.2-beta.1.2.4', '1.1.2-beta.1.3.0'])

            expect(version).toEqual('1.1.2-beta.1.3.0')
        })

        it('should throw an exception if there is not an available version', () => {
            expect(() => getLatestPatch('1.1.2', ['2.1.0', '2.1.1', '2.1.3', '3.1.0'])).toThrowError()
        })

        it('should get the correct version after releasing the stable one', () => {
            const version = getLatestPatch('1.0.0', ['1.0.0', '1.0.0-beta.1.2.3'])

            expect(version).toEqual('1.0.0')
        })
    })

    describe('Manifest parsing', () => {
        it('should get the list of available version from the filenames', () => {
            const manifest = {
                lastUpdate: 1231321321321,
                files: ['sdk-1.2.3.js', 'sdk-2.0.0.js','sdk-2.0.0-beta.1.js']
            }

            const versions = getListOfVersionsFromManifest(manifest)

            expect(versions).toEqual(['1.2.3', '2.0.0', '2.0.0-beta.1'])
        })

        it('should throw an exception if the manifest is malformed', () => {
            const manifest = {
                lastUpdate: 1231321321321,
                unknownField: ['sdk-1.2.3', 'sdk-2.0.0','sdk-2.0.0-beta.1']
            }

            expect(() => getListOfVersionsFromManifest(manifest)).toThrowError()
        })

        it('should throw an exception if the manifest is malformed (II)', () => {
            const manifest =  ['sdk-1.2.3', 'sdk-2.0.0','sdk-2.0.0-beta.1']

            expect(() => getListOfVersionsFromManifest(manifest)).toThrowError()
        })
    })
})