@@ -112,6 +112,7 @@ const BASE_BODY = {
112112 } ,
113113 exp : { f : 'enc' , e : [ ] , a : { } } ,
114114 geo : { } ,
115+ engagementCreatives : [ ] ,
115116} ;
116117
117118const LOGGED_IN_BODY = {
@@ -2236,3 +2237,124 @@ describe('boot profile completion', () => {
22362237 } ) ;
22372238 } ) ;
22382239} ) ;
2240+
2241+ describe ( 'engagement creatives' , ( ) => {
2242+ const GENERATION_ID = 'test-generation-id' ;
2243+
2244+ const skadiEngagementPayload = {
2245+ promoted_name : 'Test Brand' ,
2246+ promoted_body : 'Test body' ,
2247+ promoted_cta : 'Try now' ,
2248+ promoted_url : 'https://example.com' ,
2249+ promoted_logo_img : {
2250+ dark : 'https://example.com/logo-dark.png' ,
2251+ light : 'https://example.com/logo-light.png' ,
2252+ } ,
2253+ promoted_icon_img : {
2254+ dark : 'https://example.com/icon-dark.png' ,
2255+ light : 'https://example.com/icon-light.png' ,
2256+ } ,
2257+ promoted_gradient_start : { dark : '#FF0000' , light : '#CC0000' } ,
2258+ promoted_gradient_end : { dark : '#0000FF' , light : '#0000CC' } ,
2259+ tools : [ 'tool1' , 'tool2' ] ,
2260+ keywords : [ 'keyword1' , 'keyword2' ] ,
2261+ tags : [ 'tag1' , 'tag2' ] ,
2262+ } ;
2263+
2264+ const skadiResponse = {
2265+ generation_id : GENERATION_ID ,
2266+ value : { engagement : skadiEngagementPayload } ,
2267+ } ;
2268+
2269+ const expectedCreative = {
2270+ ...skadiEngagementPayload ,
2271+ gen_id : GENERATION_ID ,
2272+ } ;
2273+
2274+ afterEach ( ( ) => {
2275+ nock . cleanAll ( ) ;
2276+ } ) ;
2277+
2278+ it ( 'should not fetch engagement creatives when ega param is missing' , async ( ) => {
2279+ const scope = nock ( process . env . SKADI_ORIGIN )
2280+ . post ( '/private' )
2281+ . reply ( 200 , skadiResponse ) ;
2282+
2283+ const res = await request ( app . server )
2284+ . get ( BASE_PATH )
2285+ . set ( 'User-Agent' , TEST_UA )
2286+ . set ( 'Cookie' , await mockLoggedInCookie ( ) )
2287+ . expect ( 200 ) ;
2288+
2289+ expect ( res . body . engagementCreatives ) . toEqual ( [ ] ) ;
2290+ expect ( scope . isDone ( ) ) . toBe ( false ) ;
2291+ } ) ;
2292+
2293+ it ( 'should return engagement creatives when ega=1 for logged in user' , async ( ) => {
2294+ nock ( process . env . SKADI_ORIGIN )
2295+ . post ( '/private' , {
2296+ placement : 'default_engagement' ,
2297+ metadata : { USERID : '1' } ,
2298+ } )
2299+ . reply ( 200 , skadiResponse ) ;
2300+
2301+ const res = await request ( app . server )
2302+ . get ( `${ BASE_PATH } ?ega=1` )
2303+ . set ( 'User-Agent' , TEST_UA )
2304+ . set ( 'Cookie' , await mockLoggedInCookie ( ) )
2305+ . expect ( 200 ) ;
2306+
2307+ expect ( res . body . engagementCreatives ) . toEqual ( [ expectedCreative ] ) ;
2308+ } ) ;
2309+
2310+ it ( 'should return engagement creatives when ega=1 for anonymous user' , async ( ) => {
2311+ nock ( process . env . SKADI_ORIGIN ) . post ( '/private' ) . reply ( 200 , skadiResponse ) ;
2312+
2313+ const res = await request ( app . server )
2314+ . get ( `${ BASE_PATH } ?ega=1` )
2315+ . set ( 'User-Agent' , TEST_UA )
2316+ . expect ( 200 ) ;
2317+
2318+ expect ( res . body . engagementCreatives ) . toEqual ( [ expectedCreative ] ) ;
2319+ } ) ;
2320+
2321+ it ( 'should return empty array when skadi returns no generation_id' , async ( ) => {
2322+ nock ( process . env . SKADI_ORIGIN )
2323+ . post ( '/private' )
2324+ . reply ( 200 , { value : { engagement : skadiEngagementPayload } } ) ;
2325+
2326+ const res = await request ( app . server )
2327+ . get ( `${ BASE_PATH } ?ega=1` )
2328+ . set ( 'User-Agent' , TEST_UA )
2329+ . set ( 'Cookie' , await mockLoggedInCookie ( ) )
2330+ . expect ( 200 ) ;
2331+
2332+ expect ( res . body . engagementCreatives ) . toEqual ( [ ] ) ;
2333+ } ) ;
2334+
2335+ it ( 'should return empty array when skadi returns empty response' , async ( ) => {
2336+ nock ( process . env . SKADI_ORIGIN ) . post ( '/private' ) . reply ( 200 , { } ) ;
2337+
2338+ const res = await request ( app . server )
2339+ . get ( `${ BASE_PATH } ?ega=1` )
2340+ . set ( 'User-Agent' , TEST_UA )
2341+ . set ( 'Cookie' , await mockLoggedInCookie ( ) )
2342+ . expect ( 200 ) ;
2343+
2344+ expect ( res . body . engagementCreatives ) . toEqual ( [ ] ) ;
2345+ } ) ;
2346+
2347+ it ( 'should return empty array when skadi returns error' , async ( ) => {
2348+ nock ( process . env . SKADI_ORIGIN )
2349+ . post ( '/private' )
2350+ . reply ( 500 , 'Internal Server Error' ) ;
2351+
2352+ const res = await request ( app . server )
2353+ . get ( `${ BASE_PATH } ?ega=1` )
2354+ . set ( 'User-Agent' , TEST_UA )
2355+ . set ( 'Cookie' , await mockLoggedInCookie ( ) )
2356+ . expect ( 200 ) ;
2357+
2358+ expect ( res . body . engagementCreatives ) . toEqual ( [ ] ) ;
2359+ } ) ;
2360+ } ) ;
0 commit comments