Files
renovate/lib/modules/platform/scm-manager/scm-manager-helper.spec.ts
Sebastian Poxhofer 05e1271376 test: use expect().resolves instead of awaiting inside expect (#45672)
Enables the vitest/prefer-expect-resolves oxlint rule and applies its
autofix across the spec suite.
2026-09-04 10:40:46 +00:00

408 lines
10 KiB
TypeScript

import * as httpMock from '~test/http-mock.ts';
import { setBaseUrl } from '../../../util/http/scm-manager.ts';
import type { PullRequest, Repo, User } from './schema.ts';
import {
createScmPr,
getAllRepoPrs,
getAllRepos,
getCurrentUser,
getDefaultBranch,
getRepo,
getRepoPr,
updateScmPr,
} from './scm-manager-helper.ts';
import type {
PullRequestCreateParams,
PullRequestUpdateParams,
} from './types.ts';
describe('modules/platform/scm-manager/scm-manager-helper', () => {
const endpoint = 'http://localhost:8080/scm/api/v2';
const token = 'apiToken';
setBaseUrl(endpoint);
const repo: Repo = {
contact: 'test@test.com',
creationDate: '2023-08-02T10:48:24.762Z',
description: 'Default Repo',
lastModified: '2023-08-10T10:48:24.762Z',
namespace: 'default',
name: 'repo',
type: 'git',
archived: false,
exporting: false,
healthCheckRunning: false,
_links: {
protocol: [
{ name: 'http', href: 'http://localhost:8080/scm/default/repo' },
],
defaultBranch: {
href: `${endpoint}/config/git/default/repo/default-branch`,
},
},
};
const pullRequest: PullRequest = {
id: '1337',
author: { displayName: 'Thomas Zerr', id: 'tzerr' },
source: 'feature/test',
target: 'develop',
title: 'The PullRequest',
description: 'Another PullRequest',
creationDate: '2023-08-02T10:48:24.762Z',
status: 'OPEN',
labels: [],
tasks: { todo: 2, done: 4 },
_links: {},
_embedded: {
defaultConfig: {
mergeStrategy: 'SQUASH',
deleteBranchOnMerge: true,
},
},
};
describe('getCurrentUser', () => {
it('should return the current user', async () => {
const expectedUser: User = {
mail: 'test@test.de',
displayName: 'Test User',
name: 'test',
};
httpMock.scope(endpoint).get('/me').reply(200, expectedUser);
await expect(getCurrentUser(token)).resolves.toEqual(expectedUser);
});
it.each`
expectedResponse
${401}
${500}
`(
'should throw expected response $expectedResponse',
async ({ expectedResponse }: { expectedResponse: number }) => {
httpMock.scope(endpoint).get('/me').reply(expectedResponse);
await expect(getCurrentUser(token)).rejects.toThrow(
'Request failed with status code',
);
},
);
});
describe('getRepo', () => {
it('should return the repo', async () => {
httpMock
.scope(endpoint)
.get(`/repositories/${repo.namespace}/${repo.name}`)
.reply(200, repo);
await expect(getRepo(`${repo.namespace}/${repo.name}`)).resolves.toEqual(
repo,
);
});
it.each`
expectedResponse
${401}
${403}
${404}
${500}
`(
'should throw expected response $expectedResponse',
async ({ expectedResponse }: { expectedResponse: number }) => {
httpMock
.scope(endpoint)
.get(`/repositories/${repo.namespace}/${repo.name}`)
.reply(expectedResponse);
await expect(getRepo(`${repo.namespace}/${repo.name}`)).rejects.toThrow(
'Request failed with status code',
);
},
);
});
describe('getAllRepos', () => {
it('should return all repos', async () => {
httpMock
.scope(endpoint)
.get('/repositories?pageSize=1000000')
.reply(200, {
page: 0,
pageTotal: 1,
_embedded: { repositories: [repo] },
});
await expect(getAllRepos()).resolves.toEqual([repo]);
});
it.each`
expectedResponse
${401}
${403}
${500}
`(
'should throw expected response $expectedResponse',
async ({ expectedResponse }: { expectedResponse: number }) => {
httpMock
.scope(endpoint)
.get('/repositories?pageSize=1000000')
.reply(expectedResponse);
await expect(getAllRepos()).rejects.toThrow(
'Request failed with status code',
);
},
);
});
describe('getDefaultBranch', () => {
it('should return the default branch', async () => {
httpMock
.scope(endpoint)
.get('/config/git/default/repo/default-branch')
.reply(200, {
defaultBranch: 'develop',
});
await expect(getDefaultBranch(repo)).resolves.toBe('develop');
});
it.each`
expectedResponse
${401}
${403}
${404}
${500}
`(
'should throw expected response $expectedResponse',
async ({ expectedResponse }: { expectedResponse: number }) => {
httpMock
.scope(endpoint)
.get('/config/git/default/repo/default-branch')
.reply(expectedResponse);
await expect(getDefaultBranch(repo)).rejects.toThrow(
'Request failed with status code',
);
},
);
});
describe('getAllRepoPrs', () => {
it('should return all repo PRs', async () => {
httpMock
.scope(endpoint)
.get(
`/pull-requests/${repo.namespace}/${repo.name}?status=ALL&pageSize=1000000`,
)
.reply(200, {
page: 0,
pageTotal: 1,
_embedded: {
pullRequests: [pullRequest],
},
});
await expect(
getAllRepoPrs(`${repo.namespace}/${repo.name}`, true),
).resolves.toEqual([pullRequest]);
});
it('should return all of my PRs', async () => {
httpMock
.scope(endpoint)
.get(
`/pull-requests/${repo.namespace}/${repo.name}?status=MINE&pageSize=1000000`,
)
.reply(200, {
page: 0,
pageTotal: 1,
_embedded: {
pullRequests: [pullRequest],
},
});
await expect(
getAllRepoPrs(`${repo.namespace}/${repo.name}`, false),
).resolves.toEqual([pullRequest]);
});
it.each`
expectedResponse
${401}
${403}
${404}
${500}
`(
'should throw expected response $expectedResponse',
async ({ expectedResponse }: { expectedResponse: number }) => {
httpMock
.scope(endpoint)
.get(
`/pull-requests/${repo.namespace}/${repo.name}?status=ALL&pageSize=1000000`,
)
.reply(expectedResponse);
await expect(
getAllRepoPrs(`${repo.namespace}/${repo.name}`, true),
).rejects.toThrow('Request failed with status code');
},
);
});
describe('getRepoPr', () => {
it('should return the repo PR', async () => {
httpMock
.scope(endpoint)
.get(`/pull-requests/${repo.namespace}/${repo.name}/${pullRequest.id}`)
.reply(200, pullRequest);
await expect(
getRepoPr(`${repo.namespace}/${repo.name}`, 1337),
).resolves.toEqual(pullRequest);
});
it.each`
expectedResponse
${401}
${403}
${404}
${500}
`(
'should throw expected response $expectedResponse',
async ({ expectedResponse }: { expectedResponse: number }) => {
httpMock
.scope(endpoint)
.get(
`/pull-requests/${repo.namespace}/${repo.name}/${pullRequest.id}`,
)
.reply(expectedResponse);
await expect(
getRepoPr(`${repo.namespace}/${repo.name}`, 1337),
).rejects.toThrow('Request failed with status code');
},
);
});
describe('createScmPr', () => {
it('should create PR for a repo', async () => {
const expectedCreateParams: PullRequestCreateParams = {
source: 'feature/test',
target: 'develop',
title: 'Test Title',
description: 'PR description',
status: 'OPEN',
};
const expectedPrId = 1337;
httpMock
.scope(endpoint)
.post(`/pull-requests/${repo.namespace}/${repo.name}`)
.reply(201, undefined, {
location: `${endpoint}/pull-requests/${repo.namespace}/${repo.name}/${expectedPrId}`,
});
httpMock
.scope(endpoint)
.get(`/pull-requests/${repo.namespace}/${repo.name}/${expectedPrId}`)
.reply(200, pullRequest);
await expect(
createScmPr(`${repo.namespace}/${repo.name}`, expectedCreateParams),
).resolves.toEqual(pullRequest);
});
it.each`
expectedResponse
${401}
${403}
${404}
${500}
`(
'should throw expected response $expectedResponse',
async ({ expectedResponse }: { expectedResponse: number }) => {
httpMock
.scope(endpoint)
.post(`/pull-requests/${repo.namespace}/${repo.name}`)
.reply(expectedResponse);
await expect(
createScmPr(`${repo.namespace}/${repo.name}`, {
source: 'feature/test',
target: 'develop',
title: 'Test Title',
description: 'PR description',
status: 'OPEN',
}),
).rejects.toThrow('Request failed with status code');
},
);
});
describe('updateScmPr', () => {
it('should update PR for a repo', async () => {
const expectedUpdateParams: PullRequestUpdateParams = {
title: 'Test Title',
description: 'PR description',
status: 'OPEN',
target: 'new/target',
};
const expectedPrId = 1337;
httpMock
.scope(endpoint)
.get(`/pull-requests/${repo.namespace}/${repo.name}/${expectedPrId}`)
.reply(200, pullRequest);
httpMock
.scope(endpoint)
.put(`/pull-requests/${repo.namespace}/${repo.name}/${expectedPrId}`)
.reply(204);
await expect(
updateScmPr(
`${repo.namespace}/${repo.name}`,
expectedPrId,
expectedUpdateParams,
),
).resolves.not.toThrow();
});
it.each`
expectedResponse
${401}
${403}
${404}
${500}
`(
'should throw expected response $expectedResponse',
async ({ expectedResponse }: { expectedResponse: number }) => {
const expectedPrId = 1337;
httpMock
.scope(endpoint)
.get(`/pull-requests/${repo.namespace}/${repo.name}/${expectedPrId}`)
.reply(200, pullRequest);
httpMock
.scope(endpoint)
.put(`/pull-requests/${repo.namespace}/${repo.name}/${expectedPrId}`)
.reply(expectedResponse);
await expect(
updateScmPr(`${repo.namespace}/${repo.name}`, expectedPrId, {
title: 'Test Title',
description: 'PR description',
status: 'OPEN',
}),
).rejects.toThrow('Request failed with status code');
},
);
});
});