Skip to content

Commit 0352a2d

Browse files
Add tests
1 parent 709b86a commit 0352a2d

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

packages/cli-kit/src/private/node/session.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,82 @@ describe('getLastSeenUserIdAfterAuth', () => {
460460
})
461461
})
462462

463+
describe('setLastSeenUserIdAfterAuth', () => {
464+
beforeEach(() => {
465+
// Reset the userId before each test
466+
setLastSeenUserIdAfterAuth(undefined as any)
467+
})
468+
469+
test('sets the userId correctly', async () => {
470+
// Given
471+
const testUserId = 'test-user-id-123'
472+
473+
// When
474+
setLastSeenUserIdAfterAuth(testUserId)
475+
476+
// Then
477+
const retrievedUserId = await getLastSeenUserIdAfterAuth()
478+
expect(retrievedUserId).toBe(testUserId)
479+
})
480+
481+
test('overwrites existing userId when called multiple times', async () => {
482+
// Given
483+
const firstUserId = 'first-user-id'
484+
const secondUserId = 'second-user-id'
485+
486+
// When
487+
setLastSeenUserIdAfterAuth(firstUserId)
488+
let retrievedUserId = await getLastSeenUserIdAfterAuth()
489+
expect(retrievedUserId).toBe(firstUserId)
490+
491+
setLastSeenUserIdAfterAuth(secondUserId)
492+
retrievedUserId = await getLastSeenUserIdAfterAuth()
493+
494+
// Then
495+
expect(retrievedUserId).toBe(secondUserId)
496+
})
497+
498+
test('handles empty string userId', async () => {
499+
// Given
500+
const emptyUserId = ''
501+
502+
// When
503+
setLastSeenUserIdAfterAuth(emptyUserId)
504+
505+
// Then
506+
const retrievedUserId = await getLastSeenUserIdAfterAuth()
507+
// Empty string is falsy, so it falls back to 'unknown'
508+
expect(retrievedUserId).toBe('unknown')
509+
})
510+
511+
test('handles undefined userId', async () => {
512+
// Given & When
513+
setLastSeenUserIdAfterAuth(undefined as any)
514+
515+
// Then
516+
const retrievedUserId = await getLastSeenUserIdAfterAuth()
517+
// Undefined is falsy, so it falls back to 'unknown'
518+
expect(retrievedUserId).toBe('unknown')
519+
})
520+
521+
test('persists userId across multiple getLastSeenUserIdAfterAuth calls', async () => {
522+
// Given
523+
const testUserId = 'persistent-user-id'
524+
setLastSeenUserIdAfterAuth(testUserId)
525+
526+
// When - Call getLastSeenUserIdAfterAuth multiple times
527+
const firstCall = await getLastSeenUserIdAfterAuth()
528+
const secondCall = await getLastSeenUserIdAfterAuth()
529+
const thirdCall = await getLastSeenUserIdAfterAuth()
530+
531+
// Then - All calls should return the same userId and fetchSessions should not be called
532+
expect(firstCall).toBe(testUserId)
533+
expect(secondCall).toBe(testUserId)
534+
expect(thirdCall).toBe(testUserId)
535+
expect(fetchSessions).not.toHaveBeenCalled()
536+
})
537+
})
538+
463539
describe('getLastSeenAuthMethod', () => {
464540
beforeEach(() => {
465541
vi.mocked(getCurrentSessionId).mockReturnValue(undefined)

packages/cli-kit/src/public/node/error-handler.test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,27 @@ import {mockAndCaptureOutput} from './testing/output.js'
44
import * as error from './error.js'
55
import {hashString} from '../../public/node/crypto.js'
66
import {isLocalEnvironment} from '../../private/node/context/service.js'
7+
import {getLastSeenUserIdAfterAuth} from '../../private/node/session.js'
78
import {settings} from '@oclif/core'
89
import {beforeEach, describe, expect, test, vi} from 'vitest'
910

1011
const onNotify = vi.fn()
12+
const capturedEventHandler = vi.fn()
1113

1214
vi.mock('process')
1315
vi.mock('@bugsnag/js', () => {
1416
return {
1517
default: {
16-
notify: (reportedError: any, args: any, callback: any) => {
18+
notify: (reportedError: any, eventHandler: any, callback: any) => {
1719
onNotify(reportedError)
20+
// Create a mock event to pass to the event handler
21+
const mockEvent = {
22+
severity: '',
23+
unhandled: false,
24+
setUser: vi.fn(),
25+
}
26+
eventHandler(mockEvent)
27+
capturedEventHandler(mockEvent)
1828
callback(null)
1929
},
2030
isStarted: () => true,
@@ -25,6 +35,7 @@ vi.mock('./cli.js')
2535
vi.mock('./context/local.js')
2636
vi.mock('../../public/node/crypto.js')
2737
vi.mock('../../private/node/context/service.js')
38+
vi.mock('../../private/node/session.js')
2839
vi.mock('@oclif/core', () => ({
2940
settings: {
3041
debug: false,
@@ -39,8 +50,10 @@ beforeEach(() => {
3950
vi.mocked(hashString).mockReturnValue('hashed-macaddress')
4051
vi.mocked(isUnitTest).mockReturnValue(true)
4152
onNotify.mockClear()
53+
capturedEventHandler.mockClear()
4254
vi.mocked(settings).debug = false
4355
vi.mocked(isLocalEnvironment).mockReturnValue(false)
56+
vi.mocked(getLastSeenUserIdAfterAuth).mockResolvedValue('test-user-id-123')
4457
})
4558

4659
describe('errorHandler', async () => {
@@ -159,6 +172,8 @@ describe('skips sending errors to Bugsnag', () => {
159172
describe('sends errors to Bugsnag', () => {
160173
test('processes Error instances as unhandled', async () => {
161174
const toThrow = new Error('In test')
175+
capturedEventHandler.mockClear()
176+
162177
const res = await sendErrorToBugsnag(toThrow, 'unexpected_error')
163178
expect(res.reported).toEqual(true)
164179
expect(res.unhandled).toEqual(true)
@@ -207,4 +222,41 @@ describe('sends errors to Bugsnag', () => {
207222
expect(res.error).toEqual(toThrow)
208223
expect(mockOutput.debug()).toMatch('Error reporting to Bugsnag: Error: Bugsnag is down')
209224
})
225+
226+
test('sets user ID from getLastSeenUserIdAfterAuth when reporting to Bugsnag', async () => {
227+
// Given
228+
capturedEventHandler.mockClear()
229+
const testUserId = 'specific-test-user-id'
230+
vi.mocked(getLastSeenUserIdAfterAuth).mockResolvedValue(testUserId)
231+
const toThrow = new Error('In test')
232+
233+
// When
234+
const res = await sendErrorToBugsnag(toThrow, 'unexpected_error')
235+
236+
// Then
237+
expect(res.reported).toEqual(true)
238+
expect(capturedEventHandler).toHaveBeenCalled()
239+
240+
const mockEvent = capturedEventHandler.mock.calls[0][0]
241+
expect(mockEvent.setUser).toHaveBeenCalledWith(testUserId)
242+
expect(mockEvent.severity).toEqual('error')
243+
expect(mockEvent.unhandled).toEqual(true)
244+
})
245+
246+
test('handles missing user ID gracefully', async () => {
247+
// Given
248+
capturedEventHandler.mockClear()
249+
vi.mocked(getLastSeenUserIdAfterAuth).mockResolvedValue('unknown')
250+
const toThrow = new Error('In test')
251+
252+
// When
253+
const res = await sendErrorToBugsnag(toThrow, 'unexpected_error')
254+
255+
// Then
256+
expect(res.reported).toEqual(true)
257+
expect(capturedEventHandler).toHaveBeenCalled()
258+
259+
const mockEvent = capturedEventHandler.mock.calls[0][0]
260+
expect(mockEvent.setUser).toHaveBeenCalledWith('unknown')
261+
})
210262
})

0 commit comments

Comments
 (0)