import test from 'node:test'; import assert from 'node:assert/strict'; import { checkGiteaAccess, validateEndpoint } from './renovate-preflight.mjs'; test('accepts an HTTPS Gitea API endpoint and trims its trailing slash', () => { assert.equal(validateEndpoint('https://forge.example/api/v1/'), 'https://forge.example/api/v1'); }); test('rejects non-HTTPS, malformed, and non-API endpoints', () => { for (const value of ['http://forge.example/api/v1', 'https://forge.example/', 'not a URL']) { assert.throws(() => validateEndpoint(value), /HTTPS URL ending in \/api\/v1/); } }); test('checks identity and repository reads without exposing the token', async () => { const calls = []; await checkGiteaAccess('https://forge.example/api/v1', 'secret-token', async (url, options) => { calls.push({ url: url.href, authorization: options.headers.Authorization }); return { ok: true, status: 200 }; }); assert.deepEqual(calls.map(call => new URL(call.url).pathname), [ '/api/v1/user', '/api/v1/repos/arnaud.dartois/Heimdall', ]); assert.ok(calls.every(call => call.authorization === 'token secret-token')); }); test('reports authentication status without printing response details or credentials', async () => { await assert.rejects( checkGiteaAccess('https://forge.example/api/v1', 'secret-token', async () => ({ ok: false, status: 403, })), error => error.message.includes('HTTP 403') && error.message.includes('repository read and write access') && !error.message.includes('secret-token'), ); });