2022-03-05 18:44:10 +01:00
|
|
|
import { setGlobalDispatcher } from 'undici';
|
|
|
|
|
2020-03-03 23:59:19 +01:00
|
|
|
import { Config } from '@verdaccio/config';
|
2021-10-29 17:33:05 +02:00
|
|
|
import { searchUtils } from '@verdaccio/core';
|
2020-03-03 23:59:19 +01:00
|
|
|
import { setup } from '@verdaccio/logger';
|
2021-10-29 17:33:05 +02:00
|
|
|
import { configExample } from '@verdaccio/mock';
|
2020-03-03 23:59:19 +01:00
|
|
|
|
2021-09-12 16:21:19 +02:00
|
|
|
import { Storage, removeDuplicates } from '../src';
|
2020-03-03 23:59:19 +01:00
|
|
|
|
|
|
|
setup([]);
|
|
|
|
|
2021-09-12 16:21:19 +02:00
|
|
|
describe('search', () => {
|
|
|
|
describe('search manager utils', () => {
|
|
|
|
test('remove duplicates', () => {
|
|
|
|
const item: searchUtils.SearchPackageItem = {
|
|
|
|
// @ts-expect-error
|
|
|
|
package: {
|
|
|
|
name: 'foo',
|
|
|
|
},
|
2021-12-12 18:00:19 +01:00
|
|
|
['dist-tags']: {
|
|
|
|
latest: '1.0.0',
|
|
|
|
},
|
2021-09-12 16:21:19 +02:00
|
|
|
// @ts-expect-error
|
|
|
|
score: {},
|
|
|
|
searchScore: 0.4,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(removeDuplicates([item, item])).toEqual([item]);
|
2020-03-03 23:59:19 +01:00
|
|
|
});
|
|
|
|
|
2021-09-12 16:21:19 +02:00
|
|
|
test('search items', async () => {
|
|
|
|
const { MockAgent } = require('undici');
|
|
|
|
// FIXME: fetch is already part of undici
|
|
|
|
const domain = 'http://localhost:4873';
|
|
|
|
const url = '/-/v1/search?maintenance=1&popularity=1&quality=1&size=10&text=verdaccio';
|
|
|
|
const response = require('./fixtures/search.json');
|
|
|
|
const options = {
|
|
|
|
path: url,
|
|
|
|
method: 'GET',
|
|
|
|
};
|
|
|
|
const mockAgent = new MockAgent({ connections: 1 });
|
|
|
|
mockAgent.disableNetConnect();
|
|
|
|
setGlobalDispatcher(mockAgent);
|
|
|
|
const mockClient = mockAgent.get(domain);
|
|
|
|
mockClient.intercept(options).reply(200, JSON.stringify(response));
|
|
|
|
const config = new Config(configExample());
|
|
|
|
const storage = new Storage(config);
|
|
|
|
await storage.init(config);
|
|
|
|
|
|
|
|
// @ts-expect-error
|
|
|
|
const results = await storage.searchManager.search({ url, query: { text: 'foo' } });
|
|
|
|
expect(results).toHaveLength(4);
|
|
|
|
});
|
2020-03-03 23:59:19 +01:00
|
|
|
});
|
|
|
|
});
|