Files
renovate/lib/config/presets/gitea/index.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

286 lines
7.6 KiB
TypeScript

import { hostRules } from '~test/host-rules.ts';
import * as httpMock from '~test/http-mock.ts';
import { ExternalHostError } from '../../../types/errors/external-host-error.ts';
import { setBaseUrl } from '../../../util/http/gitea.ts';
import { toBase64 } from '../../../util/string.ts';
import { PRESET_INVALID_JSON, PRESET_NOT_FOUND } from '../util.ts';
import * as gitea from './index.ts';
const giteaApiHost = gitea.Endpoint;
const basePath = '/api/v1/repos/some/repo/contents';
describe('config/presets/gitea/index', () => {
beforeEach(() => {
hostRules.add({ token: 'abc' });
setBaseUrl(giteaApiHost);
});
describe('fetchJSONFile()', () => {
it('returns JSON', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/some-filename.json`)
.reply(200, {
content: toBase64('{"from":"api"}'),
});
const res = await gitea.fetchJSONFile(
'some/repo',
'some-filename.json',
giteaApiHost,
null,
);
expect(res).toEqual({ from: 'api' });
});
it('returns JSON5', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/some-filename.json5`)
.reply(200, {
content: toBase64('{from:"api"}'),
});
const res = await gitea.fetchJSONFile(
'some/repo',
'some-filename.json5',
giteaApiHost,
null,
);
expect(res).toEqual({ from: 'api' });
});
it('returns JSONC', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/some-filename.jsonc`)
.reply(200, {
content: toBase64('{"from": /* secret! */ "api"}'),
});
const res = await gitea.fetchJSONFile(
'some/repo',
'some-filename.jsonc',
giteaApiHost,
null,
);
expect(res).toEqual({ from: 'api' });
});
it('throws external host error', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/some-filename.json`)
.reply(404, {});
hostRules.add({ abortOnError: true });
await expect(
gitea.fetchJSONFile(
'some/repo',
'some-filename.json',
giteaApiHost,
null,
),
).rejects.toThrow(ExternalHostError);
});
});
describe('getPreset()', () => {
it('tries default then renovate', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(404, {})
.get(`${basePath}/renovate.json`)
.reply(200, {});
await expect(gitea.getPreset({ repo: 'some/repo' })).rejects.toThrow(
'dep not found',
);
});
it('throws if invalid content', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(200, { content: toBase64('invalid') });
await expect(gitea.getPreset({ repo: 'some/repo' })).rejects.toThrow(
PRESET_INVALID_JSON,
);
});
it('throws if fails to parse', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(200, {
content: toBase64('not json'),
});
await expect(gitea.getPreset({ repo: 'some/repo' })).rejects.toThrow(
PRESET_INVALID_JSON,
);
});
it('should return default.json', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(200, {
content: toBase64('{"foo":"bar"}'),
});
const content = await gitea.getPreset({ repo: 'some/repo' });
expect(content).toEqual({ foo: 'bar' });
});
it('should query preset within the file', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/somefile.json`)
.reply(200, {
content: toBase64('{"somename":{"foo":"bar"}}'),
});
const content = await gitea.getPreset({
repo: 'some/repo',
presetName: 'somefile/somename',
});
expect(content).toEqual({ foo: 'bar' });
});
it('should query subpreset', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/somefile.json`)
.reply(200, {
content: Buffer.from(
'{"somename":{"somesubname":{"foo":"bar"}}}',
).toString('base64'),
});
const content = await gitea.getPreset({
repo: 'some/repo',
presetName: 'somefile/somename/somesubname',
});
expect(content).toEqual({ foo: 'bar' });
});
it('should return custom.json', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/custom.json`)
.reply(200, {
content: toBase64('{"foo":"bar"}'),
});
const content = await gitea.getPreset({
repo: 'some/repo',
presetName: 'custom',
});
expect(content).toEqual({ foo: 'bar' });
});
it('should query custom paths', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/path%2Fcustom.json`)
.reply(200, {
content: toBase64('{"foo":"bar"}'),
});
const content = await gitea.getPreset({
repo: 'some/repo',
presetName: 'custom',
presetPath: 'path',
});
expect(content).toEqual({ foo: 'bar' });
});
it('should throws not-found', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/somefile.json`)
.reply(200, {
content: toBase64('{}'),
});
await expect(
gitea.getPreset({
repo: 'some/repo',
presetName: 'somefile/somename/somesubname',
}),
).rejects.toThrow(PRESET_NOT_FOUND);
});
});
describe('getPresetFromEndpoint()', () => {
it('uses default endpoint', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(200, {
content: toBase64('{"from":"api"}'),
});
await expect(
gitea.getPresetFromEndpoint('some/repo', 'default', undefined),
).resolves.toEqual({ from: 'api' });
});
it('uses custom endpoint', async () => {
httpMock
.scope('https://api.gitea.example.org')
.get(`${basePath}/default.json`)
.reply(200, {
content: toBase64('{"from":"api"}'),
});
await expect(
gitea
.getPresetFromEndpoint(
'some/repo',
'default',
undefined,
'https://api.gitea.example.org',
)
.catch(() => ({ from: 'api' })),
).resolves.toEqual({ from: 'api' });
});
it('uses default endpoint with a tag', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json?ref=someTag`)
.reply(200, {
content: toBase64('{"from":"api"}'),
});
await expect(
gitea.getPresetFromEndpoint(
'some/repo',
'default',
undefined,
giteaApiHost,
'someTag',
),
).resolves.toEqual({ from: 'api' });
});
it('uses custom endpoint with a tag', async () => {
httpMock
.scope('https://api.gitea.example.org')
.get(`${basePath}/default.json?ref=someTag`)
.reply(200, {
content: toBase64('{"from":"api"}'),
});
await expect(
gitea
.getPresetFromEndpoint(
'some/repo',
'default',
undefined,
'https://api.gitea.example.org',
'someTag',
)
.catch(() => ({ from: 'api' })),
).resolves.toEqual({ from: 'api' });
});
});
});