chore: small changes related to license ux (#31095)
parent
957e70c7e0
commit
cb3eef392b
@ -0,0 +1,110 @@ |
||||
import { getVersionStatus } from './getVersionStatus'; |
||||
|
||||
describe('if the server version from server and the highest version from cloud are the same', () => { |
||||
describe('the expiration date is in the future', () => { |
||||
it('should return as latest version', () => { |
||||
const status = getVersionStatus('3.0.0', [ |
||||
{ |
||||
version: '3.0.0', |
||||
expiration: new Date(new Date().setFullYear(new Date().getFullYear() + 1)), |
||||
security: false, |
||||
infoUrl: '', |
||||
}, |
||||
]); |
||||
|
||||
expect(status.label).toBe('latest'); |
||||
}); |
||||
}); |
||||
|
||||
describe('the expiration date is in the past', () => { |
||||
it('should return as outdated version', () => { |
||||
const status = getVersionStatus('3.0.0', [ |
||||
{ |
||||
version: '3.0.0', |
||||
expiration: new Date('2020-01-01'), |
||||
security: false, |
||||
infoUrl: '', |
||||
}, |
||||
]); |
||||
|
||||
expect(status.label).toBe('outdated'); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('if the server version is not in the list of supported versions', () => { |
||||
it('should return as outdated version', () => { |
||||
const status = getVersionStatus('2.0.0', [ |
||||
{ |
||||
version: '3.0.0', |
||||
expiration: new Date(), |
||||
security: false, |
||||
infoUrl: '', |
||||
}, |
||||
]); |
||||
|
||||
expect(status.label).toBe('outdated'); |
||||
}); |
||||
}); |
||||
|
||||
describe('if the server version is in the list of supported versions but is not the highest', () => { |
||||
describe('the expiration date is in the future', () => { |
||||
it('should return as available version', () => { |
||||
const status = getVersionStatus('3.0.0', [ |
||||
{ |
||||
version: '3.0.0', |
||||
expiration: new Date(new Date().setFullYear(new Date().getFullYear() + 1)), |
||||
security: false, |
||||
infoUrl: '', |
||||
}, |
||||
{ |
||||
version: '4.0.0', |
||||
expiration: new Date(), |
||||
security: false, |
||||
infoUrl: '', |
||||
}, |
||||
]); |
||||
expect(status.label).toBe('available_version'); |
||||
}); |
||||
}); |
||||
describe('the expiration date is in the past', () => { |
||||
it('should return as outdated version', () => { |
||||
const status = getVersionStatus('3.0.0', [ |
||||
{ |
||||
version: '3.0.0', |
||||
expiration: new Date('2020-01-01'), |
||||
security: false, |
||||
infoUrl: '', |
||||
}, |
||||
{ |
||||
version: '4.0.0', |
||||
expiration: new Date(), |
||||
security: false, |
||||
infoUrl: '', |
||||
}, |
||||
]); |
||||
expect(status.label).toBe('outdated'); |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
describe('if the server version is not in the list of supported versions but is the highest', () => { |
||||
it('should return as latest version', () => { |
||||
const status = getVersionStatus('4.0.0', [ |
||||
{ |
||||
version: '2.0.0', |
||||
expiration: new Date(), |
||||
security: false, |
||||
infoUrl: '', |
||||
}, |
||||
{ |
||||
version: '3.0.0', |
||||
expiration: new Date(), |
||||
security: false, |
||||
infoUrl: '', |
||||
}, |
||||
]); |
||||
|
||||
expect(status.label).toBe('outdated'); |
||||
}); |
||||
}); |
||||
@ -0,0 +1,29 @@ |
||||
import type { SupportedVersions } from '@rocket.chat/server-cloud-communication'; |
||||
import semver from 'semver'; |
||||
|
||||
import type { VersionStatus } from './components/VersionTag'; |
||||
|
||||
export const getVersionStatus = ( |
||||
serverVersion: string, |
||||
versions: SupportedVersions['versions'], |
||||
): { label: VersionStatus; expiration: Date | undefined } => { |
||||
const coercedServerVersion = String(semver.coerce(serverVersion)); |
||||
const highestVersion = versions.reduce((prev, current) => (prev.version > current.version ? prev : current)); |
||||
const currentVersionData = versions.find((v) => v.version.includes(coercedServerVersion) || v.version.includes(serverVersion)); |
||||
const currentVersionIsExpired = currentVersionData?.expiration && new Date(currentVersionData.expiration) < new Date(); |
||||
|
||||
const isSupported = |
||||
!currentVersionIsExpired && (currentVersionData?.version === coercedServerVersion || currentVersionData?.version === serverVersion); |
||||
|
||||
const versionStatus: { |
||||
label: VersionStatus; |
||||
expiration: Date | undefined; |
||||
} = { |
||||
label: 'outdated', |
||||
...(isSupported && semver.gte(coercedServerVersion, highestVersion.version) && { label: 'latest' }), |
||||
...(isSupported && semver.gt(highestVersion.version, coercedServerVersion) && { label: 'available_version' }), |
||||
expiration: currentVersionData?.expiration, |
||||
}; |
||||
|
||||
return versionStatus; |
||||
}; |
||||
@ -0,0 +1,11 @@ |
||||
import type { LicenseBehavior } from '../definition/LicenseBehavior'; |
||||
|
||||
export const logKind = (behavior: LicenseBehavior) => { |
||||
switch (behavior) { |
||||
case 'prevent_installation': |
||||
case 'invalidate_license': |
||||
return 'error'; |
||||
default: |
||||
return 'info'; |
||||
} |
||||
}; |
||||
Loading…
Reference in new issue