1
+ import { test , expect , type Page , type Browser } from '@playwright/test' ;
2
+ import { waitForAuthCompletion , validateApiKey , login } from './helpers/auth' ;
3
+ import path from 'path' ;
4
+ import { promisify } from 'util' ;
5
+ import { exec } from 'child_process' ;
6
+
7
+ const execAsync = promisify ( exec ) ;
8
+
9
+ test . describe ( 'CLI Login Flow' , ( ) => {
10
+ const baseUrl = process . env . TEST_BASE_URL || 'http://localhost:3000' ;
11
+ let authPage : Page ;
12
+
13
+ test . beforeAll ( async ( { browser } : { browser : Browser } ) => {
14
+ // Create a new context and page for authentication
15
+ const context = await browser . newContext ( ) ;
16
+ authPage = await context . newPage ( ) ;
17
+ await login ( authPage ) ;
18
+ } ) ;
19
+
20
+ test . afterAll ( async ( ) => {
21
+ await authPage . close ( ) ;
22
+ } ) ;
23
+
24
+ test ( 'complete login flow with team selection' , async ( ) => {
25
+ // Find the CLI binary path relative to the test file
26
+ const cliPath = path . resolve ( __dirname , '../../../packages/htmldocs/dist/cli/index.mjs' ) ;
27
+
28
+ // Execute the CLI login command with --headless to get the auth URL
29
+ const { stdout } = await execAsync ( `node ${ cliPath } login --headless` , {
30
+ env : {
31
+ ...process . env ,
32
+ API_URL : baseUrl ,
33
+ } ,
34
+ } ) ;
35
+
36
+ // Get the authorization URL from stdout
37
+ const authUrl = stdout . trim ( ) ;
38
+ expect ( authUrl ) . toContain ( `${ baseUrl } /authorize` ) ;
39
+ expect ( authUrl ) . toContain ( 'callback=' ) ;
40
+ console . log ( 'authUrl' , authUrl ) ;
41
+
42
+ // Navigate to the auth URL using the already authenticated page
43
+ await authPage . goto ( authUrl ) ;
44
+
45
+ // Verify we're on the auth page
46
+ await expect ( authPage ) . toHaveURL ( / .* \/ a u t h o r i z e / ) ;
47
+
48
+ // Click the create token button
49
+ await authPage . getByRole ( 'button' , { name : / c r e a t e n e w a p i t o k e n / i } ) . click ( ) ;
50
+
51
+ // Wait for success message
52
+ await expect ( authPage . getByText ( 'Token Created Successfully' ) ) . toBeVisible ( ) ;
53
+ await expect ( authPage . getByText ( 'You can safely close this window now.' ) ) . toBeVisible ( ) ;
54
+
55
+ // Extract session ID from the URL
56
+ const url = new URL ( authUrl ) ;
57
+ const callback = url . searchParams . get ( 'callback' ) ;
58
+ const decodedData = JSON . parse ( Buffer . from ( callback ! , 'base64' ) . toString ( ) ) ;
59
+ const sessionId = decodedData . session_id ;
60
+
61
+ // Wait for auth completion and verify
62
+ const authResult = await waitForAuthCompletion ( sessionId ) ;
63
+ expect ( authResult . apiKey ) . toBeTruthy ( ) ;
64
+ expect ( authResult . teamId ) . toBeTruthy ( ) ;
65
+
66
+ // Validate API key format
67
+ const isValidApiKey = await validateApiKey ( authResult . apiKey ! ) ;
68
+ expect ( isValidApiKey ) . toBe ( true ) ;
69
+ } ) ;
70
+ } ) ;
0 commit comments