83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
|
import { expect, tap } from '@push.rocks/tapbundle';
|
||
|
import { CodeFeed } from '../ts/index.js';
|
||
|
|
||
|
// A subclass to mock fetchFunction for controlled pagination tests
|
||
|
class MockCodeFeed extends CodeFeed {
|
||
|
private data: Record<string, any>;
|
||
|
constructor() {
|
||
|
super('https://mock', undefined, '2024-01-01T00:00:00.000Z', {
|
||
|
enableCache: false,
|
||
|
enableNpmCheck: false,
|
||
|
taggedOnly: false,
|
||
|
verbose: false,
|
||
|
});
|
||
|
|
||
|
// Prepare mock datasets
|
||
|
const commit = (sha: string, date: string, message = 'chore: update') => ({
|
||
|
sha,
|
||
|
commit: { author: { date }, message },
|
||
|
});
|
||
|
|
||
|
const commitsPage1 = Array.from({ length: 50 }).map((_, i) =>
|
||
|
commit(`sha-${i}`, `2024-01-0${(i % 9) + 1}T00:00:00.000Z`)
|
||
|
);
|
||
|
const commitsPage2 = [commit('sha-50', '2024-01-10T00:00:00.000Z'), commit('sha-tagged', '2024-01-11T00:00:00.000Z')];
|
||
|
|
||
|
const tagsPage1 = [
|
||
|
{ name: 'v1.2.3', commit: { sha: 'sha-tagged' } },
|
||
|
];
|
||
|
|
||
|
const changelogContent = Buffer.from(
|
||
|
[
|
||
|
'# Changelog',
|
||
|
'',
|
||
|
'## 2024-01-11 - 1.2.3 - Release',
|
||
|
'* example change',
|
||
|
'',
|
||
|
].join('\n'),
|
||
|
'utf8'
|
||
|
).toString('base64');
|
||
|
|
||
|
this.data = {
|
||
|
'/api/v1/orgs?limit=50&page=1': [{ username: 'org1' }],
|
||
|
'/api/v1/orgs?limit=50&page=2': [],
|
||
|
'/api/v1/orgs/org1/repos?limit=50&page=1': [{ name: 'repo1' }],
|
||
|
'/api/v1/orgs/org1/repos?limit=50&page=2': [],
|
||
|
'/api/v1/repos/org1/repo1/commits?limit=1': [commit('probe', '2024-01-12T00:00:00.000Z')],
|
||
|
'/api/v1/repos/org1/repo1/commits?since=2024-01-01T00%3A00%3A00.000Z&limit=50&page=1': commitsPage1,
|
||
|
'/api/v1/repos/org1/repo1/commits?since=2024-01-01T00%3A00%3A00.000Z&limit=50&page=2': commitsPage2,
|
||
|
'/api/v1/repos/org1/repo1/commits?since=2024-01-01T00%3A00%3A00.000Z&limit=50&page=3': [],
|
||
|
'/api/v1/repos/org1/repo1/tags?limit=50&page=1': tagsPage1,
|
||
|
'/api/v1/repos/org1/repo1/tags?limit=50&page=2': [],
|
||
|
'/api/v1/repos/org1/repo1/contents/CHANGELOG.md': { content: changelogContent },
|
||
|
};
|
||
|
}
|
||
|
|
||
|
public async fetchFunction(urlArg: string, _optionsArg: RequestInit = {}): Promise<Response> {
|
||
|
const payload = this.data[urlArg];
|
||
|
if (payload === undefined) {
|
||
|
return new Response('Not found', { status: 404, statusText: 'Not Found' });
|
||
|
}
|
||
|
return new Response(JSON.stringify(payload), { status: 200, headers: { 'content-type': 'application/json' } });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let mockFeed: MockCodeFeed;
|
||
|
|
||
|
tap.test('mock: pagination and tag mapping', async () => {
|
||
|
mockFeed = new MockCodeFeed();
|
||
|
const results = await mockFeed.fetchAllCommitsFromInstance();
|
||
|
// ensure we received > 50 commits from two pages
|
||
|
expect(results).toBeArray();
|
||
|
expect(results.length).toBeGreaterThan(50);
|
||
|
// ensure tagged commit is present and has changelog attached when found
|
||
|
const tagged = results.find((r) => r.hash === 'sha-tagged');
|
||
|
expect(tagged).toBeTruthy();
|
||
|
expect(tagged!.tagged).toBeTrue();
|
||
|
// changelog is present for that version (via tag name)
|
||
|
expect(tagged!.changelog).toBeTypeofString();
|
||
|
});
|
||
|
|
||
|
tap.start();
|
||
|
|