import { addIssueAssignee, addIssueLabel, closeIssue, commentIssue, createIssue, reopenIssue, } from './issueActions' describe('issueActions', () => { describe('createIssue', () => { it('rejects a blank title without invoking gh', async () => { const runner = jest.fn() await expect(createIssue({ title: ' ', body: 'b' }, runner)).resolves.toEqual({ ok: false, message: 'Issue title required', }) expect(runner).not.toHaveBeenCalled() }) it('invokes `gh issue create --title --body-file` and parses the URL', async () => { const runner = jest.fn().mockResolvedValue('https://github.com/gfargo/coco/issues/42') const result = await createIssue({ title: 'Bug found', body: 'It broke.' }, runner) expect(result).toEqual({ ok: true, message: 'Created issue: https://github.com/gfargo/coco/issues/42', url: 'https://github.com/gfargo/coco/issues/42', }) expect(runner).toHaveBeenCalledTimes(1) const args = runner.mock.calls[0][0] as string[] expect(args[0]).toBe('issue') expect(args[1]).toBe('create') expect(args[2]).toBe('--title=Bug found') expect(args[3]).toMatch(/^--body-file=/) }) it('surfaces gh errors as ok: false', async () => { const runner = jest.fn((args: string[]) => args[0] === 'auth' ? Promise.resolve('Logged in to github.com') : Promise.reject(new Error('validation failed')) ) await expect(createIssue({ title: 't', body: 'b' }, runner)).resolves.toEqual({ ok: false, message: 'validation failed', }) }) }) describe('commentIssue', () => { it('rejects empty bodies without invoking gh', async () => { const runner = jest.fn() await expect(commentIssue(882, ' ', runner)).resolves.toEqual({ ok: false, message: 'Comment body required', }) expect(runner).not.toHaveBeenCalled() }) it('invokes `gh issue comment <#> --body
` and surfaces success', async () => { const runner = jest.fn().mockResolvedValue('https://github.com/gfargo/coco/issues/882#comment-1') await expect(commentIssue(882, 'lgtm', runner)).resolves.toEqual({ ok: true, message: 'https://github.com/gfargo/coco/issues/882#comment-1', }) expect(runner).toHaveBeenCalledWith(['issue', 'comment', '882', '--body=lgtm']) }) it('falls back to a synthesized message when gh output is empty', async () => { const runner = jest.fn().mockResolvedValue('') await expect(commentIssue(7, 'thanks!', runner)).resolves.toEqual({ ok: true, message: 'Commented on issue #7', }) }) it('surfaces runner errors as ok: false (compacted when gh is healthy)', async () => { // gh itself is fine (auth probe succeeds); the action failed for an // unrelated reason, so the compacted raw error is surfaced. const runner = jest.fn((args: string[]) => args[0] === 'auth' ? Promise.resolve('Logged in to github.com') : Promise.reject(new Error('rate limited')) ) await expect(commentIssue(1, 'hello', runner)).resolves.toEqual({ ok: false, message: 'rate limited', }) }) it('surfaces a de-auth as the curated recovery hint', async () => { const runner = jest .fn() .mockRejectedValue(new Error('You are not logged into any GitHub hosts.')) const result = await commentIssue(1, 'hello', runner) expect(result.ok).toBe(false) expect(result.message).toContain('gh auth login') }) }) describe('addIssueLabel', () => { it('rejects empty label names', async () => { const runner = jest.fn() await expect(addIssueLabel(1, '', runner)).resolves.toEqual({ ok: false, message: 'Label name required', }) expect(runner).not.toHaveBeenCalled() }) it('invokes `gh issue edit <#> --add-label