Skip to content

Commit

Permalink
test: improve unit test cov.
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 committed Jul 24, 2024
1 parent fe1c16c commit 19b348d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 42 deletions.
24 changes: 7 additions & 17 deletions app/core/service/ProxyCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,11 @@ export class ProxyCacheService extends AbstractService {
let cachedManifest;
logs.push(`[${isoNow()}] 🚧🚧🚧🚧🚧 Start update "${fullname}-${fileType}" 🚧🚧🚧🚧🚧`);
try {
if (isPkgManifest(fileType)) {
const cachedFiles = await this.proxyCacheRepository.findProxyCache(fullname, fileType);
if (!cachedFiles) throw new Error('task params error, can not found record in repo.');
cachedManifest = await this.rewriteManifestAndStore<typeof fileType>(fullname, fileType);
ProxyCache.update(cachedFiles);
await this.proxyCacheRepository.saveProxyCache(cachedFiles);
} else {
task.error = 'Unacceptable file type.';
logs.push(`[${isoNow()}] ❌ ${task.error}`);
logs.push(`[${isoNow()}] ❌❌❌❌❌ ${fullname}-${fileType} ${version ?? ''} ❌❌❌❌❌`);
await this.taskService.finishTask(task, TaskState.Fail, logs.join('\n'));
this.logger.info('[ProxyCacheService.executeTask:fail] taskId: %s, targetName: %s, %s',
task.taskId, task.targetName, task.error);
return;
}
const cachedFiles = await this.proxyCacheRepository.findProxyCache(fullname, fileType);
if (!cachedFiles) throw new Error('task params error, can not found record in repo.');
cachedManifest = await this.rewriteManifestAndStore<typeof fileType>(fullname, fileType);
ProxyCache.update(cachedFiles);
await this.proxyCacheRepository.saveProxyCache(cachedFiles);
} catch (error) {
task.error = error;
logs.push(`[${isoNow()}] ❌ ${task.error}`);
Expand Down Expand Up @@ -245,11 +235,11 @@ export class ProxyCacheService extends AbstractService {
return await this.getProxyResponse({ url, headers: { accept: ABBREVIATED_META_TYPE } }, { dataType: 'json' });
}
private async getUpstreamPackageVersionManifest(fullname: string, versionOrTag: string): Promise<HttpClientResponse> {
const url = `/${encodeURIComponent(fullname)}/${encodeURIComponent(versionOrTag)}?t=${Date.now()}&cache=0`;
const url = `/${encodeURIComponent(fullname)}/${encodeURIComponent(versionOrTag)}`;
return await this.getProxyResponse({ url }, { dataType: 'json' });
}
private async getUpstreamAbbreviatedPackageVersionManifest(fullname: string, versionOrTag: string): Promise<HttpClientResponse> {
const url = `/${encodeURIComponent(fullname)}/${encodeURIComponent(versionOrTag)}?t=${Date.now()}&cache=0`;
const url = `/${encodeURIComponent(fullname)}/${encodeURIComponent(versionOrTag)}`;
return await this.getProxyResponse({ url, headers: { accept: ABBREVIATED_META_TYPE } }, { dataType: 'json' });
}

Expand Down
4 changes: 2 additions & 2 deletions app/port/controller/package/DownloadPackageVersionTar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class DownloadPackageVersionTarController extends AbstractController {
} catch (error) {
if (this.config.cnpmcore.syncMode === SyncMode.proxy) {
// proxy mode package version not found.
const tgzStream = await this.#getTgzStream(ctx, fullname, version);
const tgzStream = await this.getTgzProxyStream(ctx, fullname, version);
this.packageManagerService.plusPackageVersionCounter(fullname, version);
return tgzStream;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ export class DownloadPackageVersionTarController extends AbstractController {
return await this.download(ctx, fullname, filenameWithVersion);
}

async #getTgzStream(ctx: EggContext, fullname: string, version: string) {
private async getTgzProxyStream(ctx: EggContext, fullname: string, version: string) {
const { res: tgzStream, headers, status } = await this.proxyCacheService.getPackageVersionTarResponse(fullname, ctx);
ctx.status = status;
ctx.set(headers as { [key: string]: string | string[] });
Expand Down
87 changes: 64 additions & 23 deletions test/core/service/ProxyCacheService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
proxyCacheRepository = await app.getEggObject(ProxyCacheRepository);
});

describe('getPackageVersionTarResponse()', () => {
it('should stop proxy when hit block list', async () => {
const name = 'cnpmcore-test-sync-blocklist';
mock(app.config.cnpmcore, 'syncPackageBlockList', [ name ]);
try {
await proxyCacheService.getPackageVersionTarResponse(name, app.mockContext());
} catch (error) {
assert(error.options.message.includes('block list'));
}
});
});

describe('getPackageManifest()', () => {
it('should invoke rewriteManifestAndStore first.', async () => {
mock(proxyCacheService, 'rewriteManifestAndStore', async () => {
Expand Down Expand Up @@ -88,13 +100,19 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
});

it('should get correct verison via tag and cache the pkg manifest', async () => {
const data = await TestUtil.readJSONFile(
TestUtil.getFixtures('registry.npmjs.org/foobar/1.0.0/package.json'),
);
mock(proxyCacheService, 'getUpstreamPackageVersionManifest', async () => {
app.mockHttpclient('https://registry.npmjs.org/foobar/latest', 'GET', {
data: await TestUtil.readFixturesFile(
'registry.npmjs.org/foobar/1.0.0/abbreviated.json',
),
persist: false,
});

mock(proxyCacheService, 'getUpstreamAbbreviatedManifests', async () => {
return {
status: 200,
data,
data: await TestUtil.readJSONFile(
TestUtil.getFixtures('registry.npmjs.org/abbreviated_foobar.json'),
),
};
});
// get manifest by http
Expand All @@ -106,11 +124,6 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
);
assert(pkgVersionManifest);
assert.equal(pkgVersionManifest.version, '1.0.0');
const pkgManifest = proxyCacheRepository.findProxyCache(
'foobar',
DIST_NAMES.ABBREVIATED_MANIFESTS,
);
assert(pkgManifest);
});
});

Expand Down Expand Up @@ -180,12 +193,16 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
'registry.npmjs.org/foobar/1.0.0/abbreviated.json',
),
);
mock(proxyCacheService, 'getUpstreamAbbreviatedPackageVersionManifest', async () => {
return {
status: 200,
data,
};
});
mock(
proxyCacheService,
'getUpstreamAbbreviatedPackageVersionManifest',
async () => {
return {
status: 200,
data,
};
},
);
const manifest = await proxyCacheService.rewriteManifestAndStore(
'foobar',
DIST_NAMES.ABBREVIATED,
Expand All @@ -198,11 +215,13 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {

describe('removeProxyCache()', () => {
it('should remove cache', async () => {
await proxyCacheRepository.saveProxyCache(ProxyCache.create({
fullname: 'foo-bar',
fileType: DIST_NAMES.ABBREVIATED,
version: '1.0.0',
}));
await proxyCacheRepository.saveProxyCache(
ProxyCache.create({
fullname: 'foo-bar',
fileType: DIST_NAMES.ABBREVIATED,
version: '1.0.0',
}),
);

await proxyCacheService.removeProxyCache(
'foobar',
Expand Down Expand Up @@ -233,6 +252,20 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
const task2 = await proxyCacheService.findExecuteTask();
assert.equal(task.id, task2?.id);
});

it('should be 500 when file type is package version manifest.', async () => {
try {
await proxyCacheService.createTask(
`foobar/${DIST_NAMES.FULL_MANIFESTS}`,
{
fullname: 'foo',
fileType: DIST_NAMES.MANIFEST,
},
);
} catch (error) {
assert.equal(error.status, 500);
}
});
});

describe('executeTask()', () => {
Expand All @@ -256,17 +289,25 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
const taskService = await app.getEggObject(TaskService);
await proxyCacheRepository.saveProxyCache(
ProxyCache.create({
fullname: 'foo',
fullname: 'foobar',
fileType: DIST_NAMES.FULL_MANIFESTS,
}),
);
const task = await proxyCacheService.createTask(
`foobar/${DIST_NAMES.FULL_MANIFESTS}`,
{
fullname: 'foo',
fullname: 'foobar',
fileType: DIST_NAMES.FULL_MANIFESTS,
},
);
mock(proxyCacheService, 'getUpstreamFullManifests', async () => {
return {
status: 200,
data: await TestUtil.readJSONFile(
TestUtil.getFixtures('registry.npmjs.org/foobar.json'),
),
};
});
await proxyCacheService.executeTask(task);
const stream = await taskService.findTaskLog(task);
assert(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ describe('test/port/controller/package/DownloadPackageVersionTarController.test.
it('should create sync specific version task when package version tgz not found in proxy mode ', async () => {
mock(app.config.cnpmcore, 'syncMode', SyncMode.proxy);
mock(app.config.cnpmcore, 'redirectNotFound', false);
app.mockHttpclient('https://registry.npmjs.org/foobar/-/foobar-1.0.0.tgz', 'GET', {
data: await TestUtil.readFixturesFile('registry.npmjs.org/foobar/-/foobar-1.0.0.tgz'),
persist: false,
repeats: 2,
});
const res = await app.httpRequest()
.get('/foobar/-/foobar-1.0.0.tgz')
.set('user-agent', publisher.ua + ' node/16.0.0')
Expand Down

0 comments on commit 19b348d

Please sign in to comment.