|
| 1 | +import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test' |
| 2 | +import { io } from './index' |
| 3 | + |
| 4 | +describe('io.circleci.triggerPipelineForTag', () => { |
| 5 | + let originalFetch: typeof globalThis.fetch |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + originalFetch = globalThis.fetch |
| 9 | + }) |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + globalThis.fetch = originalFetch |
| 13 | + mock.restore() |
| 14 | + delete process.env.CIRCLECI_API_TOKEN |
| 15 | + }) |
| 16 | + |
| 17 | + test('throws when CIRCLECI_API_TOKEN is not set', async () => { |
| 18 | + delete process.env.CIRCLECI_API_TOKEN |
| 19 | + |
| 20 | + await expect( |
| 21 | + io.circleci.triggerPipelineForTag('gh/x/y', '9d2f5823-f2c9-4cba-918a-e7d0dc2f658a', 'some-tag'), |
| 22 | + ).rejects.toThrow(/CIRCLECI_API_TOKEN not set/) |
| 23 | + }) |
| 24 | + |
| 25 | + test('throws with actionable error when definitionId is not a valid UUID', async () => { |
| 26 | + process.env.CIRCLECI_API_TOKEN = 'fake-token' |
| 27 | + const fetchSpy = mock(() => Promise.resolve(new Response('', { status: 200 }))) |
| 28 | + globalThis.fetch = fetchSpy as unknown as typeof fetch |
| 29 | + |
| 30 | + // First segment is 10 chars instead of 8 — the exact bug that shipped to prod. |
| 31 | + await expect( |
| 32 | + io.circleci.triggerPipelineForTag('gh/x/y', '229d2f5823-f2c9-4cba-918a-e7d0dc2f658a', 'some-tag'), |
| 33 | + ).rejects.toThrow(/not a valid UUID/) |
| 34 | + |
| 35 | + // Critically: fetch must NOT have been called — we failed before hitting the API. |
| 36 | + expect(fetchSpy).not.toHaveBeenCalled() |
| 37 | + }) |
| 38 | + |
| 39 | + test('rejects obviously-not-a-uuid values', async () => { |
| 40 | + process.env.CIRCLECI_API_TOKEN = 'fake-token' |
| 41 | + const fetchSpy = mock(() => Promise.resolve(new Response('', { status: 200 }))) |
| 42 | + globalThis.fetch = fetchSpy as unknown as typeof fetch |
| 43 | + |
| 44 | + await expect(io.circleci.triggerPipelineForTag('gh/x/y', 'not-a-uuid', 'some-tag')).rejects.toThrow( |
| 45 | + /not a valid UUID/, |
| 46 | + ) |
| 47 | + await expect(io.circleci.triggerPipelineForTag('gh/x/y', '', 'some-tag')).rejects.toThrow(/not a valid UUID/) |
| 48 | + expect(fetchSpy).not.toHaveBeenCalled() |
| 49 | + }) |
| 50 | + |
| 51 | + test('posts to CircleCI API with expected body when inputs are valid', async () => { |
| 52 | + process.env.CIRCLECI_API_TOKEN = 'fake-token' |
| 53 | + const fetchSpy = mock((_url: string, _init?: RequestInit) => |
| 54 | + Promise.resolve(new Response(JSON.stringify({ id: 'abc', number: 42 }), { status: 200 })), |
| 55 | + ) |
| 56 | + globalThis.fetch = fetchSpy as unknown as typeof fetch |
| 57 | + |
| 58 | + const result = await io.circleci.triggerPipelineForTag( |
| 59 | + 'gh/agent-facets/facets', |
| 60 | + '9d2f5823-f2c9-4cba-918a-e7d0dc2f658a', |
| 61 | + '@agent-facets/core@1.0.0', |
| 62 | + ) |
| 63 | + |
| 64 | + expect(result).toEqual({ id: 'abc', number: 42 }) |
| 65 | + expect(fetchSpy).toHaveBeenCalledTimes(1) |
| 66 | + |
| 67 | + const [url, init] = fetchSpy.mock.calls[0] ?? [] |
| 68 | + expect(url).toBe('https://circleci.com/api/v2/project/gh/agent-facets/facets/pipeline/run') |
| 69 | + const headers = (init as RequestInit | undefined)?.headers as Record<string, string> |
| 70 | + expect(headers['Circle-Token']).toBe('fake-token') |
| 71 | + expect(headers['Content-Type']).toBe('application/json') |
| 72 | + |
| 73 | + const body = JSON.parse((init as RequestInit).body as string) |
| 74 | + expect(body).toEqual({ |
| 75 | + definition_id: '9d2f5823-f2c9-4cba-918a-e7d0dc2f658a', |
| 76 | + config: { tag: '@agent-facets/core@1.0.0' }, |
| 77 | + checkout: { tag: '@agent-facets/core@1.0.0' }, |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + test('throws with CircleCI error body on non-2xx response', async () => { |
| 82 | + process.env.CIRCLECI_API_TOKEN = 'fake-token' |
| 83 | + const fetchSpy = mock(() => |
| 84 | + Promise.resolve( |
| 85 | + new Response(JSON.stringify({ message: "Field 'definition_id' must be a valid uuid." }), { status: 400 }), |
| 86 | + ), |
| 87 | + ) |
| 88 | + globalThis.fetch = fetchSpy as unknown as typeof fetch |
| 89 | + |
| 90 | + await expect( |
| 91 | + io.circleci.triggerPipelineForTag( |
| 92 | + 'gh/agent-facets/facets', |
| 93 | + '9d2f5823-f2c9-4cba-918a-e7d0dc2f658a', |
| 94 | + '@agent-facets/core@1.0.0', |
| 95 | + ), |
| 96 | + ).rejects.toThrow(/CircleCI pipeline trigger failed .* 400/) |
| 97 | + }) |
| 98 | +}) |
0 commit comments