Files
2026-09-28 16:15:50 +02:00

75 lines
2.6 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { GiteaClient, GiteaApiError } from '../src/gitea.js';
function fakeFetch(handler) {
return async (url, opts) => handler(url, opts);
}
function jsonResponse(status, body) {
return {
ok: status >= 200 && status < 300,
status,
text: async () => JSON.stringify(body),
};
}
test('getPullRequest returns the PR body on success', async () => {
const client = new GiteaClient({
baseUrl: 'https://git.example.com',
token: 't',
fetchImpl: fakeFetch(async (url) => {
assert.equal(url, 'https://git.example.com/api/v1/repos/o/r/pulls/5');
return jsonResponse(200, { head: { ref: 'feature' } });
}),
});
const pr = await client.getPullRequest('o/r', 5);
assert.equal(pr.head.ref, 'feature');
});
test('getPullRequest returns null on 404 (issue, not a PR)', async () => {
const client = new GiteaClient({
baseUrl: 'https://git.example.com',
token: 't',
fetchImpl: fakeFetch(async () => jsonResponse(404, { message: 'not found' })),
});
const pr = await client.getPullRequest('o/r', 5);
assert.equal(pr, null);
});
test('getPullRequest rethrows non-404 errors', async () => {
const client = new GiteaClient({
baseUrl: 'https://git.example.com',
token: 't',
fetchImpl: fakeFetch(async () => jsonResponse(500, { message: 'boom' })),
});
await assert.rejects(() => client.getPullRequest('o/r', 5), GiteaApiError);
});
test('createIssueComment sends the expected method, path and body', async () => {
let captured;
const client = new GiteaClient({
baseUrl: 'https://git.example.com/',
token: 'secret-token',
fetchImpl: fakeFetch(async (url, opts) => {
captured = { url, opts };
return jsonResponse(201, { id: 1 });
}),
});
await client.createIssueComment('o/r', 9, 'hello');
assert.equal(captured.url, 'https://git.example.com/api/v1/repos/o/r/issues/9/comments');
assert.equal(captured.opts.method, 'POST');
assert.equal(captured.opts.headers.Authorization, 'token secret-token');
assert.deepEqual(JSON.parse(captured.opts.body), { body: 'hello' });
});
test('authenticatedCloneUrl embeds credentials', () => {
const client = new GiteaClient({ baseUrl: 'https://git.example.com', token: 'abc' });
assert.equal(client.authenticatedCloneUrl('o/r'), 'https://oauth2:[email protected]/o/r.git');
});
test('trailing slash on baseUrl does not produce a double slash', () => {
const client = new GiteaClient({ baseUrl: 'https://git.example.com/', token: 'abc' });
assert.equal(client.baseUrl, 'https://git.example.com');
});