0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/packages/store/test/versions.spec.ts
Juan Picado 99a1af1c35
vitest migration more packages (#4872)
* migrate storage package

* migrate middleware package

* migrate auth-memory plugin
2024-09-29 16:03:29 +02:00

167 lines
5.2 KiB
TypeScript

import assert from 'assert';
import { describe, expect, test } from 'vitest';
import {
getVersion,
removeLowerVersions,
sortVersionsAndFilterInvalid,
tagVersion,
} from '../src/index';
describe('versions-utils', () => {
const dist = (version) => ({
tarball: `http://registry.org/npm_test/-/npm_test-${version}.tgz`,
shasum: `sha1-${version}`,
});
describe('getVersion', () => {
const metadata = {
'1.0.0': { dist: dist('1.0.0') },
'1.0.1': { dist: dist('1.0.1') },
'0.2.1-1': { dist: dist('0.2.1-1') },
'0.2.1-alpha': { dist: dist('0.2.1-alpha') },
'0.2.1-alpha.0': { dist: dist('0.2.1-alpha.0') },
};
test('should get the right version', () => {
expect(getVersion({ ...metadata } as any, '1.0.0')).toEqual({ dist: dist('1.0.0') });
expect(getVersion({ ...metadata } as any, 'v1.0.0')).toEqual({ dist: dist('1.0.0') });
expect(getVersion({ ...metadata } as any, 'v0.2.1-1')).toEqual({ dist: dist('0.2.1-1') });
expect(getVersion({ ...metadata } as any, '0.2.1-alpha')).toEqual({
dist: dist('0.2.1-alpha'),
});
expect(getVersion({ ...metadata } as any, '0.2.1-alpha.0')).toEqual({
dist: dist('0.2.1-alpha.0'),
});
});
test('should return nothing on get non existing version', () => {
expect(getVersion({ ...metadata } as any, '0')).toBeUndefined();
expect(getVersion({ ...metadata } as any, '2.0.0')).toBeUndefined();
expect(getVersion({ ...metadata } as any, 'v2.0.0')).toBeUndefined();
});
test('should return nothing on get invalid versions', () => {
// @ts-ignore
expect(getVersion({ ...metadata } as any, undefined)).toBeUndefined();
// @ts-ignore
expect(getVersion({ ...metadata } as any, null)).toBeUndefined();
// @ts-ignore
expect(getVersion({ ...metadata } as any, 8)).toBeUndefined();
});
test('should handle no versions', () => {
// @ts-ignore
expect(getVersion(undefined, undefined)).toBeUndefined();
});
});
describe('sortVersionsAndFilterInvalid', () => {
test('should sort versions', () => {
expect(sortVersionsAndFilterInvalid(['1.0.0', '5.0.0', '2.0.0'])).toEqual([
'1.0.0',
'2.0.0',
'5.0.0',
]);
});
test('should sort versions and filter out invalid', () => {
// @ts-ignore
expect(sortVersionsAndFilterInvalid(['1.0.0', '5.0.0', '2.0.0', '', null])).toEqual([
'1.0.0',
'2.0.0',
'5.0.0',
]);
});
});
describe('tagVersion', () => {
test('add new one', () => {
let pkg = {
versions: {},
'dist-tags': {},
};
// @ts-ignore
assert(tagVersion(pkg, '1.1.1', 'foo', {}));
assert.deepEqual(pkg, {
versions: {},
'dist-tags': { foo: '1.1.1' },
});
});
test('add (compat)', () => {
const x = {
versions: {},
'dist-tags': { foo: '1.1.0' },
};
// @ts-ignore
assert(tagVersion(x, '1.1.1', 'foo'));
assert.deepEqual(x, {
versions: {},
'dist-tags': { foo: '1.1.1' },
});
});
test('add fresh tag', () => {
let x = {
versions: {},
'dist-tags': { foo: '1.1.0' },
};
// @ts-ignore
assert(tagVersion(x, '1.1.1', 'foo'));
assert.deepEqual(x, {
versions: {},
'dist-tags': { foo: '1.1.1' },
});
});
});
describe('removeLowerVersions', () => {
test('should remove lower semantic versions', () => {
const inputArray = [
{ package: { name: 'object1', version: '1.0.0' } },
{ package: { name: 'object1', version: '2.0.0' } }, // Duplicate name 'object1'
{ package: { name: 'object2', version: '2.0.0' } }, // Duplicate name 'object2'
{ package: { name: 'object2', version: '2.0.0' } },
{ package: { name: 'object3', version: '3.0.0' } },
{ package: { name: 'object4', version: '1.0.0' } },
];
const expectedOutput = [
{ package: { name: 'object1', version: '2.0.0' } },
{ package: { name: 'object2', version: '2.0.0' } },
{ package: { name: 'object3', version: '3.0.0' } },
{ package: { name: 'object4', version: '1.0.0' } },
];
// @ts-expect-error
const result = removeLowerVersions(inputArray);
expect(result).toEqual(expectedOutput);
});
test('should remove lower semantic versions 2', () => {
const inputArray = [
{ package: { name: 'object1', version: '1.0.0' } },
{ package: { name: 'object1', version: '2.0.0' } }, // Duplicate name 'object1'
{ package: { name: 'object2', version: '2.0.3' } }, // Duplicate name 'object2'
{ package: { name: 'object2', version: '2.0.0' } },
{ package: { name: 'object3', version: '3.0.0' } },
{ package: { name: 'object4', version: '1.0.0' } },
];
const expectedOutput = [
{ package: { name: 'object1', version: '2.0.0' } },
{ package: { name: 'object2', version: '2.0.3' } },
{ package: { name: 'object3', version: '3.0.0' } },
{ package: { name: 'object4', version: '1.0.0' } },
];
// @ts-expect-error
const result = removeLowerVersions(inputArray);
expect(result).toEqual(expectedOutput);
});
});
});