import { test } from 'node:test'; import assert from 'node:assert/strict'; import { giteaHosting } from './release-hosting.mjs'; test('release hosting refuses a pre-existing Git tag on another commit', async () => { const fetch = globalThis.fetch; const revision = 'a'.repeat(40); let writes = 0; globalThis.fetch = async (url, options) => { if (options.method) { writes++; return Response.json({ id: 1, draft: true }); } if (String(url).includes('/releases/tags/')) return new Response('', { status: 404 }); if (String(url).includes('/tags/installation-v')) return Response.json({ commit: { sha: 'b'.repeat(40) } }); return Response.json({ private: false, permissions: { push: true } }); }; try { const hosting = await giteaHosting({ run: async () => 'username=test\npassword=test\n', repository: 'https://example.test/owner/repo', identity: { revision, version: '1.0.0' }, body: '' }); await assert.rejects(hosting.open(), /tag.*revision/i); assert.equal(writes, 0); } finally { globalThis.fetch = fetch; } }); test('matching Git tag is accepted and verified again before publishing', async () => { const fetch = globalThis.fetch; const identity = { revision: 'a'.repeat(40), version: '1.0.0' }; let sha = identity.revision; let writes = 0; globalThis.fetch = async (url, options) => { if (options.method) { writes++; return Response.json({ id: 1, draft: false }); } if (String(url).includes('/releases/tags/')) return Response.json({ id: 1, draft: true, body: `` }); if (String(url).includes('/tags/')) return Response.json({ commit: { sha } }); if (String(url).endsWith('/assets')) return Response.json([]); return Response.json({ private: false, permissions: { push: true } }); }; try { const hosting = await giteaHosting({ run: async () => 'username=test\npassword=test\n', repository: 'https://example.test/owner/repo', identity, body: '' }); const release = await hosting.open(); await hosting.verify(release, [], false); sha = 'b'.repeat(40); await assert.rejects(hosting.verify(release, [], false), /tag.*revision/i); await assert.rejects(hosting.publish(release), /tag.*revision/i); assert.equal(writes, 0); } finally { globalThis.fetch = fetch; } });