|
| 1 | +import { test, expect, describe, beforeEach, afterEach } from 'bun:test'; |
| 2 | +import { installShellMock, uninstallShellMock, getCallsMatching, addMockRule } from '../helpers-shell-mock.ts'; |
| 3 | +import { composeReleaseBody, parseReleaseMetadata, type ReleaseMetadata } from '../../src/core/github-release.ts'; |
| 4 | +import { reopenCommand } from '../../src/commands/reopen.ts'; |
| 5 | + |
| 6 | +/** Mock `gh release view <tag> --json ...` to return a release with the given body. */ |
| 7 | +function mockReleaseView(body: string, isDraft = true) { |
| 8 | + addMockRule({ |
| 9 | + match: /^gh release view/, |
| 10 | + response: JSON.stringify({ tagName: 'pkg-a@1.2.3', name: 'pkg-a v1.2.3', body, isDraft }), |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | +describe('reopenCommand', () => { |
| 15 | + beforeEach(() => installShellMock()); |
| 16 | + afterEach(() => uninstallShellMock()); |
| 17 | + |
| 18 | + const stagedMeta: ReleaseMetadata = { |
| 19 | + version: '1.2.3', |
| 20 | + targets: { npm: { status: 'staged', stageId: 'uuid-1', stagedAt: '2026-01-01T00:00:00Z' } }, |
| 21 | + }; |
| 22 | + |
| 23 | + test('flips a staged target to failed and edits the release body', async () => { |
| 24 | + mockReleaseView(composeReleaseBody('- A change', stagedMeta)); |
| 25 | + addMockRule({ match: /^gh release edit/, response: '' }); |
| 26 | + |
| 27 | + await reopenCommand('/tmp/x', { tag: 'pkg-a@1.2.3' }); |
| 28 | + |
| 29 | + const editCalls = getCallsMatching('gh release edit'); |
| 30 | + expect(editCalls.length).toBe(1); |
| 31 | + |
| 32 | + // The edited body's metadata should now show the target as failed (rejected). |
| 33 | + const notesIdx = editCalls[0]!.args.indexOf('--notes'); |
| 34 | + const newBody = editCalls[0]!.args[notesIdx + 1]!; |
| 35 | + const meta = parseReleaseMetadata(newBody)!; |
| 36 | + expect(meta.targets.npm!.status).toBe('failed'); |
| 37 | + expect(meta.targets.npm!.error).toContain('rejected'); |
| 38 | + // No longer staged — the stale 🟡 marker is gone. |
| 39 | + expect(newBody).not.toContain('🟡'); |
| 40 | + }); |
| 41 | + |
| 42 | + test('dry-run does not edit the release', async () => { |
| 43 | + mockReleaseView(composeReleaseBody('- A change', stagedMeta)); |
| 44 | + addMockRule({ match: /^gh release edit/, response: '' }); |
| 45 | + |
| 46 | + await reopenCommand('/tmp/x', { tag: 'pkg-a@1.2.3', dryRun: true }); |
| 47 | + |
| 48 | + expect(getCallsMatching('gh release edit')).toHaveLength(0); |
| 49 | + }); |
| 50 | + |
| 51 | + test('no-ops when the release has no staged targets', async () => { |
| 52 | + const liveMeta: ReleaseMetadata = { |
| 53 | + version: '1.2.3', |
| 54 | + targets: { npm: { status: 'success', url: 'https://npmjs.com/package/pkg-a/v/1.2.3' } }, |
| 55 | + }; |
| 56 | + mockReleaseView(composeReleaseBody('- A change', liveMeta)); |
| 57 | + addMockRule({ match: /^gh release edit/, response: '' }); |
| 58 | + |
| 59 | + await reopenCommand('/tmp/x', { tag: 'pkg-a@1.2.3' }); |
| 60 | + |
| 61 | + expect(getCallsMatching('gh release edit')).toHaveLength(0); |
| 62 | + }); |
| 63 | +}); |
0 commit comments