Skip to content

Commit 731e1bc

Browse files
cursoragente-fisher
andcommitted
Fix assistant auth test lint issue
Co-authored-by: Edgar Fisher <e-fisher@users.noreply.github.com>
1 parent 11007e1 commit 731e1bc

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/services/grafana/assistantAuth.test.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import http from 'node:http'
22
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3+
import type { Mock } from 'vitest'
34

45
import {
56
CallbackResult,
@@ -8,7 +9,9 @@ import {
89
} from './assistantAuth'
910

1011
describe('exchangeAssistantCode', () => {
11-
const mockFetch = vi.fn()
12+
const mockFetch: Mock<
13+
(input: URL | string, init?: RequestInit) => Promise<Partial<Response>>
14+
> = vi.fn()
1215

1316
beforeEach(() => {
1417
vi.stubGlobal('fetch', mockFetch)
@@ -53,10 +56,22 @@ describe('exchangeAssistantCode', () => {
5356
})
5457

5558
it('aborts the in-flight exchange when the caller signal aborts', async () => {
59+
let capturedRequestInput: string | URL | undefined
60+
let capturedRequestSignal: AbortSignal | undefined
61+
5662
mockFetch.mockImplementation(
57-
async (_url: URL, init?: RequestInit) =>
63+
async (input: string | URL, init?: RequestInit) =>
5864
new Promise((_, reject) => {
59-
const requestSignal = init?.signal as AbortSignal
65+
capturedRequestInput = input
66+
67+
const requestSignal = init?.signal ?? undefined
68+
69+
if (!requestSignal) {
70+
reject(new Error('Missing request signal'))
71+
return
72+
}
73+
74+
capturedRequestSignal = requestSignal
6075

6176
requestSignal.addEventListener(
6277
'abort',
@@ -78,18 +93,13 @@ describe('exchangeAssistantCode', () => {
7893
abortController.signal
7994
)
8095

81-
expect(mockFetch).toHaveBeenCalledWith(
82-
new URL('https://api.grafana.net/api/cli/v1/auth/exchange'),
83-
expect.objectContaining({
84-
signal: expect.any(AbortSignal),
85-
})
96+
expect(capturedRequestInput).toEqual(
97+
new URL('https://api.grafana.net/api/cli/v1/auth/exchange')
8698
)
87-
88-
const [, init] = mockFetch.mock.calls[0] as [URL, RequestInit]
89-
90-
expect(init.signal).not.toBe(abortController.signal)
99+
expect(capturedRequestSignal).toBeDefined()
100+
expect(capturedRequestSignal).not.toBe(abortController.signal)
91101
abortController.abort(new Error('caller aborted'))
92-
expect(init.signal?.aborted).toBe(true)
102+
expect(capturedRequestSignal?.aborted).toBe(true)
93103
await expect(exchangePromise).rejects.toThrow('caller aborted')
94104
})
95105

0 commit comments

Comments
 (0)