import { describe, it, afterEach, mock } from 'node:test'; import assert from 'node:assert/strict'; import axios from 'axios'; // gitea.js reads env vars at module load time (ESM cache), so we test // the actual values baked in at import time and verify behavior via axios mocks. afterEach(() => mock.restoreAll()); describe('gitea', async () => { const { getPRDiff, postComment } = await import('./gitea.js'); it('getPRDiff calls Gitea diff API with Authorization header', async () => { let capturedUrl, capturedOpts; mock.method(axios, 'get', async (url, opts) => { capturedUrl = url; capturedOpts = opts; return { data: 'diff content' }; }); const result = await getPRDiff(); assert.equal(result, 'diff content'); assert.ok(capturedUrl.includes('/api/v1/repos/')); assert.ok(capturedUrl.endsWith('.diff')); assert.ok(capturedOpts.headers['Authorization'].startsWith('token ')); assert.equal(capturedOpts.headers['Content-Type'], 'application/json'); }); it('postComment calls Gitea issues comments API with body', async () => { let capturedUrl, capturedBody, capturedOpts; mock.method(axios, 'post', async (url, body, opts) => { capturedUrl = url; capturedBody = body; capturedOpts = opts; return { data: { id: 1 } }; }); const result = await postComment('hello world'); assert.deepEqual(result, { id: 1 }); assert.ok(capturedUrl.includes('/api/v1/repos/')); assert.ok(capturedUrl.endsWith('/comments')); assert.equal(capturedBody.body, 'hello world'); assert.ok(capturedOpts.headers['Authorization'].startsWith('token ')); }); it('does not set httpsAgent by default (GITEA_SKIP_TLS_VERIFY not true)', async () => { let capturedOpts; mock.method(axios, 'get', async (_url, opts) => { capturedOpts = opts; return { data: '' }; }); await getPRDiff(); // httpsAgent is undefined when GITEA_SKIP_TLS_VERIFY !== 'true' assert.equal(capturedOpts.httpsAgent, undefined); }); it('getPRDiff propagates axios errors', async () => { mock.method(axios, 'get', async () => { throw new Error('network error'); }); await assert.rejects(() => getPRDiff(), /network error/); }); it('postComment propagates axios errors', async () => { mock.method(axios, 'post', async () => { throw new Error('api error'); }); await assert.rejects(() => postComment('test'), /api error/); }); });