@@ -7,10 +7,13 @@ declare global {
77 }
88}
99
10- const CUSTOMER_EMAIL = process . env . TEST_USER_EMAIL || "customer@example.com" ;
10+ const CUSTOMER_EMAIL =
11+ process . env . TEST_USER_EMAIL || `portfolio-e2e-${ Date . now ( ) } @example.com` ;
1112const CUSTOMER_PASSWORD =
12- process . env . TEST_USER_PASSWORD || "E2ETestDemoCustomerPasswordForBookStore123!" ;
13+ process . env . TEST_USER_PASSWORD ||
14+ "E2ETestDemoCustomerPasswordForBookStore123!" ;
1315const API_URL = process . env . API_URL || "http://localhost:3001/api" ;
16+ let customerAccountSeeded = false ;
1417
1518function escapeRegExp ( value : string ) {
1619 return value . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ;
@@ -49,6 +52,32 @@ async function installBrowserActionSpies(page: Page) {
4952 } ) ;
5053}
5154
55+ async function ensureCustomerAccount ( page : Page ) {
56+ if ( customerAccountSeeded ) {
57+ return ;
58+ }
59+
60+ const response = await page
61+ . context ( )
62+ . request . post ( `${ API_URL } /auth/register` , {
63+ data : {
64+ email : CUSTOMER_EMAIL ,
65+ password : CUSTOMER_PASSWORD ,
66+ firstName : "Portfolio" ,
67+ lastName : "Customer" ,
68+ phoneNumber : "0901231234" ,
69+ } ,
70+ } ) ;
71+
72+ if ( ! response . ok ( ) && ! [ 400 , 409 ] . includes ( response . status ( ) ) ) {
73+ throw new Error (
74+ `Unable to prepare the portfolio customer account. Status: ${ response . status ( ) } ` ,
75+ ) ;
76+ }
77+
78+ customerAccountSeeded = true ;
79+ }
80+
5281async function expectImageLoaded ( image : Locator ) {
5382 await image . scrollIntoViewIfNeeded ( ) ;
5483 await expect ( image ) . toBeVisible ( { timeout : 15000 } ) ;
@@ -93,6 +122,7 @@ async function getFlashSaleCard(page: Page, index = 0) {
93122}
94123
95124async function login ( page : Page ) {
125+ await ensureCustomerAccount ( page ) ;
96126 await page . waitForLoadState ( "networkidle" ) ;
97127
98128 let loginSucceeded = false ;
@@ -108,7 +138,7 @@ async function login(page: Page) {
108138 ) ;
109139 } ) ;
110140
111- await page . locator ( 'button[type=" submit"]' ) . click ( ) ;
141+ await page . getByTestId ( "login- submit") . click ( ) ;
112142
113143 const loginResponse = await loginResponsePromise ;
114144 if ( loginResponse . ok ( ) ) {
@@ -381,20 +411,17 @@ test.describe("Portfolio CTA smoke", () => {
381411 expect ( openCalls [ 0 ] ?. [ 0 ] ) . toContain ( "mailto:contact@bookstore.com" ) ;
382412
383413 await page . getByTestId ( "chatbot-launcher" ) . click ( ) ;
384- await expect ( page . getByTestId ( "chatbot-login-cta" ) ) . toBeVisible ( ) ;
385414 await expect ( page . getByTestId ( "chatbot-status-badge" ) ) . toBeVisible ( ) ;
415+ await expect ( page . getByText ( / d e m o | p o r t f o l i o / i) ) . toBeVisible ( ) ;
386416
387- await Promise . all ( [
388- page . waitForURL ( / \/ l o g i n \? r e d i r e c t = % 2 F $ / ) ,
389- page . getByTestId ( "chatbot-login-cta" ) . click ( ) ,
390- ] ) ;
391-
392- await login ( page ) ;
393- await expect ( page ) . toHaveURL ( / \/ $ / ) ;
394-
395- await page . getByTestId ( "chatbot-launcher" ) . click ( ) ;
396- await expect ( page . getByTestId ( "chatbot-status-badge" ) ) . toContainText (
397- / g r o k | d ự p h ò n g | f a l l b a c k | d e g r a d e d / i,
398- ) ;
417+ await page
418+ . getByTestId ( "chatbot-message-input" )
419+ . fill ( "Có coupon nào đang dùng được không?" ) ;
420+ await page . getByTestId ( "chatbot-send-message" ) . click ( ) ;
421+ await expect (
422+ page
423+ . getByTestId ( "chatbot-assistant-message" )
424+ . filter ( { hasText : / K h u y ế n m ã i | P r o m o t i o n s / i } ) ,
425+ ) . toBeVisible ( { timeout : 10000 } ) ;
399426 } ) ;
400427} ) ;
0 commit comments