@@ -9,17 +9,18 @@ import CLILoginClient, {
99
1010describe ( 'buildCallbackUrl' , ( ) => {
1111 it ( 'should build a valid localhost callback URL' , ( ) => {
12- const url = buildCallbackUrl ( '8080' , 'my-token' , 'my-state' )
12+ const url = buildCallbackUrl ( '8080' , 'my-token' , 'my-state' , 'Alice' )
1313 expect ( url . hostname ) . toBe ( 'localhost' )
1414 expect ( url . port ) . toBe ( '8080' )
1515 expect ( url . pathname ) . toBe ( '/callback' )
1616 expect ( url . searchParams . get ( 'token' ) ) . toBe ( 'my-token' )
1717 expect ( url . searchParams . get ( 'state' ) ) . toBe ( 'my-state' )
18+ expect ( url . searchParams . get ( 'name' ) ) . toBe ( 'Alice' )
1819 } )
1920
2021 it ( 'should reject non-localhost hosts' , ( ) => {
2122 // buildCallbackUrl hardcodes localhost, so this tests the safety check
22- expect ( ( ) => buildCallbackUrl ( '8080' , 't' , 's' ) ) . not . toThrow ( )
23+ expect ( ( ) => buildCallbackUrl ( '8080' , 't' , 's' , 'n' ) ) . not . toThrow ( )
2324 } )
2425} )
2526
@@ -35,30 +36,34 @@ describe('CLILoginClient', () => {
3536 vi . restoreAllMocks ( )
3637 } )
3738
38- it ( 'should show loading state initially' , ( ) => {
39- fetchMock . mockReturnValue ( new Promise ( ( ) => { } ) ) // never resolves
39+ it ( 'should show confirm screen initially' , ( ) => {
4040 render ( < CLILoginClient userName = "Test User" userEmail = "test@example.com" /> )
41- expect ( screen . getByText ( / g e n e r a t i n g t o k e n / i) ) . toBeInTheDocument ( )
41+ expect ( screen . getByText ( / r e q u e s t i n g a c c e s s / i) ) . toBeInTheDocument ( )
42+ expect (
43+ screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) ,
44+ ) . toBeInTheDocument ( )
4245 expect (
4346 screen . getByText ( / T e s t U s e r \( t e s t @ e x a m p l e \. c o m \) / ) ,
4447 ) . toBeInTheDocument ( )
48+ expect ( fetchMock ) . not . toHaveBeenCalled ( )
4549 } )
4650
47- it ( 'should display token for manual copy when no port is provided ' , async ( ) => {
51+ it ( 'should display token for manual copy after authorizing ' , async ( ) => {
4852 fetchMock . mockResolvedValue ( {
4953 ok : true ,
5054 json : ( ) => Promise . resolve ( { token : 'jwt-token-value' } ) ,
5155 } )
5256
5357 render ( < CLILoginClient userName = "Test User" userEmail = "test@example.com" /> )
58+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) )
5459
5560 await waitFor ( ( ) => {
5661 expect ( screen . getByText ( 'jwt-token-value' ) ) . toBeInTheDocument ( )
5762 } )
5863 expect ( screen . getByText ( / c o p y t h i s t o k e n / i) ) . toBeInTheDocument ( )
5964 } )
6065
61- it ( 'should redirect to localhost callback when port and state are provided ' , async ( ) => {
66+ it ( 'should redirect to localhost callback after authorizing ' , async ( ) => {
6267 fetchMock . mockResolvedValue ( {
6368 ok : true ,
6469 json : ( ) => Promise . resolve ( { token : 'jwt-token-value' } ) ,
@@ -90,6 +95,8 @@ describe('CLILoginClient', () => {
9095 /> ,
9196 )
9297
98+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) )
99+
93100 await waitFor ( ( ) => {
94101 expect ( hrefSetter ) . toHaveBeenCalledWith (
95102 expect . stringContaining ( 'http://localhost:9876/callback' ) ,
@@ -99,6 +106,7 @@ describe('CLILoginClient', () => {
99106 const redirectUrl = new URL ( hrefSetter . mock . calls [ 0 ] [ 0 ] )
100107 expect ( redirectUrl . searchParams . get ( 'token' ) ) . toBe ( 'jwt-token-value' )
101108 expect ( redirectUrl . searchParams . get ( 'state' ) ) . toBe ( 'random-state' )
109+ expect ( redirectUrl . searchParams . get ( 'name' ) ) . toBe ( 'Test User' )
102110
103111 locationHref . mockRestore ( )
104112 } )
@@ -113,6 +121,8 @@ describe('CLILoginClient', () => {
113121 /> ,
114122 )
115123
124+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) )
125+
116126 await waitFor ( ( ) => {
117127 expect ( screen . getByText ( / i n v a l i d p o r t / i) ) . toBeInTheDocument ( )
118128 } )
@@ -129,6 +139,8 @@ describe('CLILoginClient', () => {
129139 /> ,
130140 )
131141
142+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) )
143+
132144 await waitFor ( ( ) => {
133145 expect ( screen . getByText ( / i n v a l i d p o r t / i) ) . toBeInTheDocument ( )
134146 } )
@@ -144,6 +156,8 @@ describe('CLILoginClient', () => {
144156 /> ,
145157 )
146158
159+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) )
160+
147161 await waitFor ( ( ) => {
148162 expect ( screen . getByText ( / m i s s i n g s t a t e / i) ) . toBeInTheDocument ( )
149163 } )
@@ -158,6 +172,7 @@ describe('CLILoginClient', () => {
158172 } )
159173
160174 render ( < CLILoginClient userName = "Test User" userEmail = "test@example.com" /> )
175+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) )
161176
162177 await waitFor ( ( ) => {
163178 expect ( screen . getByText ( 'Unauthorized' ) ) . toBeInTheDocument ( )
@@ -168,6 +183,7 @@ describe('CLILoginClient', () => {
168183 fetchMock . mockRejectedValue ( new Error ( 'Network error' ) )
169184
170185 render ( < CLILoginClient userName = "Test User" userEmail = "test@example.com" /> )
186+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) )
171187
172188 await waitFor ( ( ) => {
173189 expect ( screen . getByText ( / f a i l e d t o c o n n e c t / i) ) . toBeInTheDocument ( )
@@ -184,6 +200,7 @@ describe('CLILoginClient', () => {
184200 Object . assign ( navigator , { clipboard : { writeText } } )
185201
186202 render ( < CLILoginClient userName = "Test User" userEmail = "test@example.com" /> )
203+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) )
187204
188205 await waitFor ( ( ) => {
189206 expect ( screen . getByText ( 'copy-me' ) ) . toBeInTheDocument ( )
@@ -210,6 +227,7 @@ describe('CLILoginClient', () => {
210227 } )
211228
212229 render ( < CLILoginClient userName = "Test User" userEmail = "test@example.com" /> )
230+ fireEvent . click ( screen . getByRole ( 'button' , { name : / a u t h o r i z e c l i / i } ) )
213231
214232 await waitFor ( ( ) => {
215233 expect ( screen . getByText ( 'Server error' ) ) . toBeInTheDocument ( )
0 commit comments