import test from 'node:test'; import assert from 'node:assert/strict'; import { createReleaseDraft, prepareAssets, previousRelease, main } from '../../scripts/release-draft.mjs'; const commit = 'a'.repeat(40); const zip = Buffer.from('504b0304000102030405', 'hex'); const options = { repository: 'owner/repo', tag: 'v1.2.0', version: '1.2.0', commit, token: 'test-only', zip }; const release = (tag, extra = {}) => ({ tag_name: tag, draft: false, prerelease: false, ...extra }); function fixture({ releases = [], annotated = false, moveAt = Infinity, uploadConflict = false, comparisons = {} } = {}) { const calls = []; const assets = []; let refs = 0; let created; let failedUpload = false; const predecessorShas = new Map(); const fetchImpl = async (input, init) => { const url = new URL(input); const body = init.headers['Content-Type'] === 'application/json' ? JSON.parse(init.body) : init.body; calls.push({ url, ...init, body }); const response = (value, status = 200) => ({ ok: status < 400, status, json: async () => value }); if (url.pathname.endsWith('/git/ref/tags/v1.2.0')) { refs++; return response({ ref: 'refs/tags/v1.2.0', object: { type: annotated ? 'tag' : 'commit', sha: refs >= moveAt ? 'b'.repeat(40) : commit } }); } if (url.pathname.includes('/git/ref/tags/')) { const tag = decodeURIComponent(url.pathname.split('/git/ref/tags/')[1]); if (comparisons[tag] === 'missing') return response({}, 404); const sha = String(predecessorShas.size + 1).padStart(40, '0'); predecessorShas.set(sha, tag); return response({ ref: `refs/tags/${tag}`, object: { type: 'commit', sha } }); } if (url.pathname.includes('/git/tags/')) return response({ object: { type: 'commit', sha: commit } }); if (url.pathname.endsWith('/commits')) return response([{ sha: commit, commit: { message: 'Initial implementation' } }]); if (url.pathname.includes('/compare/')) { const sha = url.pathname.split('/compare/')[1].split('...')[0]; assert.match(sha, /^[a-f0-9]{40}$/); const tag = predecessorShas.get(sha); assert.ok(tag, 'comparison must use an explicitly resolved predecessor tag SHA'); const status = comparisons[tag] ?? 'ahead'; return status === 'missing' ? response({}, 404) : response({ status }); } if (url.pathname.endsWith('/releases/generate-notes')) return response({ body: 'Generated notes' }); if (url.pathname.endsWith('/releases') && init.method === 'GET') return response(releases); if (url.pathname.endsWith('/releases') && init.method === 'POST') { created = { id: 123, ...body }; releases.push(created); return response(created, 201); } if (url.pathname.endsWith('/releases/123')) return response(created ?? releases.find((r) => r.id === 123)); if (url.pathname.endsWith('/releases/123/assets') && init.method === 'GET') return response(assets); if (url.hostname === 'uploads.github.com') { const name = url.searchParams.get('name'); const expected = prepareAssets(zip).find((a) => a.name === name); const uploaded = { name, state: 'uploaded', size: body.length, digest: `sha256:${expected.sha256}` }; assets.push(uploaded); if (uploadConflict && !failedUpload) { failedUpload = true; return response({}, 422); } return response(uploaded, 201); } throw new Error(`Unexpected mock request: ${init.method} ${input}`); }; return { fetchImpl, calls, assets, releases, get created() { return created; } }; } test('stable notes select highest lower published stable SemVer, never drafts or prereleases', () => { const releases = [release('v1.1.0'), release('v1.1.9-rc.1', { prerelease: true }), release('v1.2.0'), release('v1.1.5', { draft: true }), release('v1.1.3'), release('bad'), release('v2.0.0')]; assert.equal(previousRelease(releases, 'v1.2.0').tag_name, 'v1.1.3'); }); test('prerelease notes can use lower published prereleases with numeric SemVer ordering', () => { const releases = ['v1.2.0-rc.2', 'v1.2.0-rc.10', 'v1.1.0', 'v1.2.0'].map((tag) => release(tag)); assert.equal(previousRelease(releases, 'v1.2.0-rc.11').tag_name, 'v1.2.0-rc.10'); assert.equal(previousRelease([], 'v1.0.0'), undefined); }); test('creates draft only with generated notes, provenance, ZIP and checksum', async () => { const f = fixture({ releases: [release('v1.1.0')] }); const result = await createReleaseDraft({ ...options, fetchImpl: f.fetchImpl }); assert.equal(result.draft, true); assert.equal(f.created.draft, true); assert.equal(f.created.prerelease, false); assert.equal(f.created.make_latest, 'false'); assert.equal(f.created.target_commitish, commit); assert.match(f.created.body, /Generated notes\n\n