From 69f800ae8b41cc2e14012ff19ebf8e822a8b5268 Mon Sep 17 00:00:00 2001 From: elrrrrrrr Date: Tue, 25 Jun 2024 19:35:20 +0800 Subject: [PATCH] fix: sync pkg --- test/repository/SearchRepository.test.ts | 71 ++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/repository/SearchRepository.test.ts b/test/repository/SearchRepository.test.ts index 1275fc1f..5e84c32e 100644 --- a/test/repository/SearchRepository.test.ts +++ b/test/repository/SearchRepository.test.ts @@ -2,14 +2,19 @@ import { strict as assert } from 'node:assert'; import { app, mock } from 'egg-mock/bootstrap'; import { SearchManifestType, SearchRepository } from '../../app/repository/SearchRepository'; import { mockES } from '../../config/config.unittest'; +import { PackageManagerService } from '../../app/core/service/PackageManagerService'; +import { TestUtil } from '../TestUtil'; +import { PackageSearchService } from '../../app/core/service/PackageSearchService'; describe('test/repository/SearchRepository.test.ts', () => { let searchRepository: SearchRepository; + let packageManagerService: PackageManagerService; beforeEach(async () => { mock(app.config.cnpmcore, 'enableElasticsearch', true); mock(app.config.cnpmcore, 'elasticsearchIndex', 'cnpmcore_packages'); searchRepository = await app.getEggObject(SearchRepository); + packageManagerService = await app.getEggObject(PackageManagerService); }); afterEach(async () => { @@ -107,5 +112,71 @@ describe('test/repository/SearchRepository.test.ts', () => { const id = await searchRepository.removePackage(mockedPackageName); assert.equal(id, mockedPackageName); }); + + it('should clear blocked pkg', async () => { + + await TestUtil.createPackage({ + name: '@cnpm/example', + }); + + const _source = { + downloads: { + all: 0, + }, + package: { + name: '@cnpm/example', + description: 'example package', + }, + }; + + mockES.add({ + method: 'POST', + path: `/${app.config.cnpmcore.elasticsearchIndex}/_search`, + }, () => { + return { + hits: { + total: { value: 1, relation: 'eq' }, + hits: [{ + _source, + }], + }, + }; + }); + + + let res = await searchRepository.searchPackage({ + body: { + query: { + match: { + 'package.name': '@cnpm/example', + }, + }, + }, + }); + + assert.deepEqual(res.hits.length, 1); + + res = await searchRepository.searchPackage({ + body: { + query: { + match: { + 'package.name': '@cnpm/example', + }, + }, + }, + }); + + let called = false; + + mock(PackageSearchService.prototype, 'removePackage', async (fullname: string) => { + if (fullname === '@cnpm/example') { + called = true; + } + }); + + await packageManagerService.blockPackageByFullname('@cnpm/example', 'test'); + assert(called); + + }); }); });