@@ -4,12 +4,15 @@ import { createAuthSlice } from './auth-slice'
44import type { AuthSlice } from './auth-slice'
55
66// Use vi.hoisted to create mocks that can be referenced in vi.mock
7- const { mockSaveApiKey, mockGetApiKey, mockClearApiKey, mockGetBugs } = vi . hoisted ( ( ) => ( {
8- mockSaveApiKey : vi . fn ( ) ,
9- mockGetApiKey : vi . fn ( ) ,
10- mockClearApiKey : vi . fn ( ) ,
11- mockGetBugs : vi . fn ( ) ,
12- } ) )
7+ const { mockSaveApiKey, mockGetApiKey, mockClearApiKey, mockGetBugs, mockWhoAmI } = vi . hoisted (
8+ ( ) => ( {
9+ mockSaveApiKey : vi . fn ( ) ,
10+ mockGetApiKey : vi . fn ( ) ,
11+ mockClearApiKey : vi . fn ( ) ,
12+ mockGetBugs : vi . fn ( ) ,
13+ mockWhoAmI : vi . fn ( ) ,
14+ } ) ,
15+ )
1316
1417// Mock ApiKeyStorage
1518vi . mock ( '@/lib/storage/api-key-storage' , ( ) => ( {
@@ -24,6 +27,7 @@ vi.mock('@/lib/storage/api-key-storage', () => ({
2427vi . mock ( '@/lib/bugzilla/client' , ( ) => ( {
2528 BugzillaClient : vi . fn ( ) . mockImplementation ( ( ) => ( {
2629 getBugs : mockGetBugs ,
30+ whoAmI : mockWhoAmI ,
2731 } ) ) ,
2832} ) )
2933
@@ -36,6 +40,7 @@ describe('AuthSlice', () => {
3640 mockSaveApiKey . mockResolvedValue ( )
3741 mockGetApiKey . mockResolvedValue ( null )
3842 mockGetBugs . mockResolvedValue ( [ ] )
43+ mockWhoAmI . mockResolvedValue ( { id : 12345 , real_name : 'Test User' , name : 'test@mozilla.com' } )
3944
4045 useStore = create < AuthSlice > ( ) ( ( ...args ) => ( {
4146 ...createAuthSlice ( ...args ) ,
@@ -64,6 +69,12 @@ describe('AuthSlice', () => {
6469
6570 expect ( validationError ) . toBeNull ( )
6671 } )
72+
73+ it ( 'should have username null initially' , ( ) => {
74+ const { username } = useStore . getState ( )
75+
76+ expect ( username ) . toBeNull ( )
77+ } )
6778 } )
6879
6980 describe ( 'setApiKey' , ( ) => {
@@ -92,6 +103,15 @@ describe('AuthSlice', () => {
92103 expect ( isValid ) . toBe ( true )
93104 } )
94105
106+ it ( 'should fetch and store username on successful validation' , async ( ) => {
107+ const { setApiKey } = useStore . getState ( )
108+ await setApiKey ( 'test-api-key-123' )
109+
110+ const { username } = useStore . getState ( )
111+ expect ( username ) . toBe ( 'Test User' )
112+ expect ( mockWhoAmI ) . toHaveBeenCalled ( )
113+ } )
114+
95115 it ( 'should set isValidating during validation' , async ( ) => {
96116 const { setApiKey } = useStore . getState ( )
97117 const promise = setApiKey ( 'test-api-key-123' )
@@ -178,6 +198,20 @@ describe('AuthSlice', () => {
178198 // Can't easily verify this without exposing storage instance
179199 // Trust that implementation calls storage.clearApiKey()
180200 } )
201+
202+ it ( 'should clear username' , async ( ) => {
203+ const { setApiKey, clearApiKey } = useStore . getState ( )
204+ await setApiKey ( 'test-api-key-123' )
205+
206+ const { username : usernameBefore } = useStore . getState ( )
207+ expect ( usernameBefore ) . toBe ( 'Test User' )
208+
209+ clearApiKey ( )
210+
211+ const { username : usernameAfter } = useStore . getState ( )
212+
213+ expect ( usernameAfter ) . toBeNull ( )
214+ } )
181215 } )
182216
183217 describe ( 'loadApiKey' , ( ) => {
0 commit comments