|
| 1 | +import type { PullRequest } from './types' |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { |
| 4 | + CNBRequestError, |
| 5 | + deleteBranch, |
| 6 | + encodePath, |
| 7 | + fetchCNB, |
| 8 | + getPullBranchName, |
| 9 | + getPullRepoPath, |
| 10 | + listPulls, |
| 11 | + patchPull, |
| 12 | +} from './index' |
| 13 | + |
| 14 | +vi.mock('@actions/core', () => ({ |
| 15 | + endGroup: vi.fn(), |
| 16 | + getInput: vi.fn(), |
| 17 | + info: vi.fn(), |
| 18 | + setFailed: vi.fn(), |
| 19 | + startGroup: vi.fn(), |
| 20 | +})) |
| 21 | + |
| 22 | +function mockFetchOnce(status: number, body = '', statusText = ''): void { |
| 23 | + vi.mocked(fetch).mockResolvedValueOnce(new Response(body, { status, statusText })) |
| 24 | +} |
| 25 | + |
| 26 | +function createPullRequest(number: string, ref = 'refs/heads/feature', repo = 'tdesign/test'): PullRequest { |
| 27 | + return { |
| 28 | + assignees: [], |
| 29 | + author: {} as PullRequest['author'], |
| 30 | + base: null, |
| 31 | + blocked_on: '', |
| 32 | + body: '', |
| 33 | + comment_count: 0, |
| 34 | + created_at: '', |
| 35 | + head: { |
| 36 | + ref, |
| 37 | + repo: { |
| 38 | + id: repo, |
| 39 | + name: repo.split('/').at(-1) || repo, |
| 40 | + path: repo, |
| 41 | + web_url: '', |
| 42 | + }, |
| 43 | + sha: '', |
| 44 | + }, |
| 45 | + is_wip: false, |
| 46 | + labels: [], |
| 47 | + last_acted_at: '', |
| 48 | + mergeable_state: '', |
| 49 | + merged_by: {} as PullRequest['merged_by'], |
| 50 | + number, |
| 51 | + repo: {} as PullRequest['repo'], |
| 52 | + review_count: 0, |
| 53 | + state: 'open', |
| 54 | + title: `PR ${number}`, |
| 55 | + updated_at: '', |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +describe('cnb-delete-branch', () => { |
| 60 | + beforeEach(() => { |
| 61 | + vi.stubGlobal('fetch', vi.fn()) |
| 62 | + }) |
| 63 | + |
| 64 | + it('encodes repo paths but preserves path separators', () => { |
| 65 | + expect(encodePath('tdesign/workflows')).toBe('tdesign/workflows') |
| 66 | + expect(encodePath('tdesign/branch with space')).toBe('tdesign/branch%20with%20space') |
| 67 | + }) |
| 68 | + |
| 69 | + it('sends CNB JSON headers and parses JSON responses', async () => { |
| 70 | + mockFetchOnce(200, JSON.stringify({ ok: true })) |
| 71 | + |
| 72 | + await expect(fetchCNB('token', '/tdesign/test/-/pulls')).resolves.toEqual({ |
| 73 | + status: 200, |
| 74 | + data: { ok: true }, |
| 75 | + }) |
| 76 | + |
| 77 | + expect(fetch).toHaveBeenCalledWith('https://api.cnb.cool/tdesign/test/-/pulls', { |
| 78 | + headers: { |
| 79 | + 'Accept': 'application/json', |
| 80 | + 'Authorization': 'Bearer token', |
| 81 | + 'Content-Type': 'application/json', |
| 82 | + }, |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + it('throws CNBRequestError for failed API responses', async () => { |
| 87 | + mockFetchOnce(403, JSON.stringify({ errmsg: 'forbidden' }), 'Forbidden') |
| 88 | + |
| 89 | + await expect(fetchCNB('token', '/tdesign/test/-/pulls')).rejects.toMatchObject({ |
| 90 | + message: '[CNB] GET /tdesign/test/-/pulls failed: forbidden', |
| 91 | + status: 403, |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + it('lists pull requests across pages', async () => { |
| 96 | + const firstPage = Array.from({ length: 100 }, (_, index) => createPullRequest(String(index + 1))) |
| 97 | + const secondPage = [createPullRequest('101')] |
| 98 | + mockFetchOnce(200, JSON.stringify(firstPage)) |
| 99 | + mockFetchOnce(200, JSON.stringify(secondPage)) |
| 100 | + |
| 101 | + const pulls = await listPulls('token', 'tdesign/test', 'open') |
| 102 | + |
| 103 | + expect(pulls).toHaveLength(101) |
| 104 | + expect(fetch).toHaveBeenNthCalledWith( |
| 105 | + 1, |
| 106 | + 'https://api.cnb.cool/tdesign/test/-/pulls?state=open&page=1&page_size=100', |
| 107 | + expect.any(Object), |
| 108 | + ) |
| 109 | + expect(fetch).toHaveBeenNthCalledWith( |
| 110 | + 2, |
| 111 | + 'https://api.cnb.cool/tdesign/test/-/pulls?state=open&page=2&page_size=100', |
| 112 | + expect.any(Object), |
| 113 | + ) |
| 114 | + }) |
| 115 | + |
| 116 | + it('patches pull request state through the CNB pull endpoint', async () => { |
| 117 | + mockFetchOnce(200, '{}') |
| 118 | + |
| 119 | + await expect(patchPull('token', 'tdesign/test', '12', { state: 'closed' })).resolves.toBe(true) |
| 120 | + |
| 121 | + expect(fetch).toHaveBeenCalledWith('https://api.cnb.cool/tdesign/test/-/pulls/12', expect.objectContaining({ |
| 122 | + body: JSON.stringify({ state: 'closed' }), |
| 123 | + method: 'PATCH', |
| 124 | + })) |
| 125 | + }) |
| 126 | + |
| 127 | + it('treats missing branch as a successful no-op but rethrows other delete failures', async () => { |
| 128 | + mockFetchOnce(404, JSON.stringify({ errmsg: 'not found' }), 'Not Found') |
| 129 | + await expect(deleteBranch('token', 'tdesign/test', 'feature')).resolves.toBe(false) |
| 130 | + |
| 131 | + mockFetchOnce(403, JSON.stringify({ errmsg: 'forbidden' }), 'Forbidden') |
| 132 | + await expect(deleteBranch('token', 'tdesign/test', 'feature')).rejects.toBeInstanceOf(CNBRequestError) |
| 133 | + }) |
| 134 | + |
| 135 | + it('reads pull branch and repo safely when head data is missing', () => { |
| 136 | + expect(getPullBranchName(createPullRequest('1', 'refs/heads/feature/test'))).toBe('feature/test') |
| 137 | + expect(getPullRepoPath(createPullRequest('1'))).toBe('tdesign/test') |
| 138 | + expect(getPullBranchName({ head: null } as PullRequest)).toBeUndefined() |
| 139 | + expect(getPullRepoPath({ head: null } as PullRequest)).toBeUndefined() |
| 140 | + }) |
| 141 | +}) |
0 commit comments