Skip to content

Commit ff4f6ef

Browse files
committed
test(github/refs-graphql): cover fetchRefShaViaGraphQL transport + parse + auth-header edges (6 added, 71% → 86% br)
1 parent 81e6342 commit ff4f6ef

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* @file Unit tests for github/refs-graphql.ts edge cases. httpRequest is
3+
* mocked so no network is touched. Covers non-OK response, empty body,
4+
* malformed JSON, branch-OID resolution path, and the tagRef-with-token
5+
* header branch.
6+
*/
7+
8+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
9+
10+
vi.mock('../../../src/http-request/request')
11+
12+
import { fetchRefShaViaGraphQL } from '../../../src/github/refs-graphql'
13+
import { httpRequest } from '../../../src/http-request/request'
14+
15+
const JSONStringify = JSON.stringify
16+
17+
function mkResponse(body: Buffer, ok: boolean, status: number) {
18+
return {
19+
body,
20+
headers: {},
21+
ok,
22+
status,
23+
statusText: ok ? 'OK' : 'ERR',
24+
} as unknown as Awaited<ReturnType<typeof httpRequest>>
25+
}
26+
27+
describe.sequential('github/refs-graphql — fetchRefShaViaGraphQL', () => {
28+
beforeEach(() => {
29+
vi.mocked(httpRequest).mockReset()
30+
})
31+
afterEach(() => {
32+
vi.clearAllMocks()
33+
})
34+
35+
it('returns undefined on non-OK status', async () => {
36+
vi.mocked(httpRequest).mockResolvedValueOnce(
37+
mkResponse(Buffer.from(''), false, 503),
38+
)
39+
expect(
40+
await fetchRefShaViaGraphQL('o', 'r', 'v1.0.0', {}),
41+
).toBeUndefined()
42+
})
43+
44+
it('returns undefined when GraphQL returns empty body', async () => {
45+
vi.mocked(httpRequest).mockResolvedValueOnce(
46+
mkResponse(Buffer.from(''), true, 200),
47+
)
48+
expect(
49+
await fetchRefShaViaGraphQL('o', 'r', 'v1.0.0', {}),
50+
).toBeUndefined()
51+
})
52+
53+
it('returns undefined on malformed JSON body', async () => {
54+
vi.mocked(httpRequest).mockResolvedValueOnce(
55+
mkResponse(Buffer.from('<html>not json</html>'), true, 200),
56+
)
57+
expect(
58+
await fetchRefShaViaGraphQL('o', 'r', 'v1.0.0', {}),
59+
).toBeUndefined()
60+
})
61+
62+
it('returns branch OID when tagRef is null but branchRef resolves', async () => {
63+
vi.mocked(httpRequest).mockResolvedValueOnce(
64+
mkResponse(
65+
Buffer.from(
66+
JSONStringify({
67+
data: {
68+
repository: {
69+
tagRef: null,
70+
branchRef: { target: { oid: 'sha-branch' } },
71+
commit: null,
72+
},
73+
},
74+
}),
75+
),
76+
true,
77+
200,
78+
),
79+
)
80+
expect(await fetchRefShaViaGraphQL('o', 'r', 'main', {})).toBe('sha-branch')
81+
})
82+
83+
it('returns undefined when all three aliases are null', async () => {
84+
vi.mocked(httpRequest).mockResolvedValueOnce(
85+
mkResponse(
86+
Buffer.from(
87+
JSONStringify({
88+
data: {
89+
repository: {
90+
tagRef: null,
91+
branchRef: null,
92+
commit: null,
93+
},
94+
},
95+
}),
96+
),
97+
true,
98+
200,
99+
),
100+
)
101+
expect(
102+
await fetchRefShaViaGraphQL('o', 'r', 'unknown', {}),
103+
).toBeUndefined()
104+
})
105+
106+
it('sends Authorization header when token option is provided', async () => {
107+
vi.mocked(httpRequest).mockResolvedValueOnce(
108+
mkResponse(
109+
Buffer.from(
110+
JSONStringify({
111+
data: {
112+
repository: {
113+
branchRef: { target: { oid: 'sha-auth' } },
114+
},
115+
},
116+
}),
117+
),
118+
true,
119+
200,
120+
),
121+
)
122+
await fetchRefShaViaGraphQL('o', 'r', 'main', { token: 'gh-tok-xyz' })
123+
const call = vi.mocked(httpRequest).mock.calls[0]
124+
const opts = call?.[1] as { headers?: Record<string, string> }
125+
expect(opts?.headers?.['Authorization']).toBe('Bearer gh-tok-xyz')
126+
})
127+
})

0 commit comments

Comments
 (0)