@@ -22,7 +22,7 @@ import {
22
22
weeklyImportDataStep ,
23
23
weeklyProcessAnalysisStep ,
24
24
} from '../../../src/paid-traffic-analysis/handler.js' ;
25
-
25
+ import { AWSAthenaClient } from '@adobe/spacecat-shared-athena-client' ;
26
26
use ( sinonChai ) ;
27
27
use ( chaiAsPromised ) ;
28
28
@@ -40,12 +40,20 @@ describe('Paid Traffic Analysis Handler', () => {
40
40
beforeEach ( async ( ) => {
41
41
sandbox = sinon . createSandbox ( ) ;
42
42
43
+ // Mock AWSAthenaClient.fromContext to return our mock client
44
+ const mockAthenaClientInstance = {
45
+ query : sandbox . stub ( ) . resolves ( [
46
+ { page_views : 1000 , sessions : 500 , conversion_rate : 0.05 } ,
47
+ ] ) ,
48
+ } ;
49
+ sandbox . stub ( AWSAthenaClient , 'fromContext' ) . returns ( mockAthenaClientInstance ) ;
50
+
43
51
site = {
44
52
getSiteId : sandbox . stub ( ) . returns ( siteId ) ,
45
53
getId : sandbox . stub ( ) . returns ( siteId ) ,
46
54
getDeliveryType : sandbox . stub ( ) . returns ( 'aem_edge' ) ,
47
- getBaseURL : sandbox . stub ( ) . returns ( auditUrl ) ,
48
- getPageTypes : sandbox . stub ( ) . returns ( null ) ,
55
+ getBaseURL : sandbox . stub ( ) . resolves ( auditUrl ) ,
56
+ getPageTypes : sandbox . stub ( ) . resolves ( null ) ,
49
57
} ;
50
58
51
59
mockSqs = {
@@ -64,12 +72,13 @@ describe('Paid Traffic Analysis Handler', () => {
64
72
} ;
65
73
66
74
const mockAthenaClient = {
67
- query : sandbox . stub ( ) . resolves ( [ ] ) ,
75
+ query : sandbox . stub ( ) . resolves ( [
76
+ { page_views : 1000 , sessions : 500 , conversion_rate : 0.05 } ,
77
+ ] ) ,
68
78
} ;
69
79
70
80
const mockS3Client = {
71
81
send : sandbox . stub ( ) . resolves ( {
72
- // Simulate cache files exist, so no warming needed
73
82
ContentLength : 1024 ,
74
83
LastModified : new Date ( ) ,
75
84
} ) ,
@@ -95,6 +104,9 @@ describe('Paid Traffic Analysis Handler', () => {
95
104
S3_IMPORTER_BUCKET_NAME : 'test-bucket' ,
96
105
PAID_DATA_THRESHOLD : 2000 ,
97
106
MAX_CONCURRENT_REQUESTS : 5 ,
107
+ ATHENA_TEMP : 's3://test-athena-temp/' ,
108
+ CACHE_LOCATION : 's3://test-cache-bucket/' ,
109
+ PAGE_VIEW_THRESHOLD : 100 ,
98
110
} ,
99
111
siteId,
100
112
} )
@@ -110,7 +122,7 @@ describe('Paid Traffic Analysis Handler', () => {
110
122
} ) ;
111
123
112
124
describe ( 'prepareTrafficAnalysisRequest' , ( ) => {
113
- it ( 'should prepare weekly analysis request correctly and warm cache ' , async ( ) => {
125
+ it ( 'should prepare weekly analysis request correctly' , async ( ) => {
114
126
const result = await prepareTrafficAnalysisRequest (
115
127
auditUrl ,
116
128
context ,
@@ -131,12 +143,9 @@ describe('Paid Traffic Analysis Handler', () => {
131
143
fullAuditRef : auditUrl ,
132
144
} ) ;
133
145
expect ( result . auditResult . temporalCondition ) . to . include ( 'week=2' ) ;
134
-
135
- // Verify cache warming checked S3 for existing cache files
136
- expect ( context . s3Client . send ) . to . have . been . called ;
137
146
} ) ;
138
147
139
- it ( 'should prepare monthly analysis request correctly and warm cache ' , async ( ) => {
148
+ it ( 'should prepare monthly analysis request correctly' , async ( ) => {
140
149
const result = await prepareTrafficAnalysisRequest (
141
150
auditUrl ,
142
151
context ,
@@ -151,44 +160,17 @@ describe('Paid Traffic Analysis Handler', () => {
151
160
temporalCondition : '(year=2024 AND month=12)' ,
152
161
} ;
153
162
154
- expect ( result ) . to . deep . equal ( {
163
+ expect ( result ) . to . deep . include ( {
155
164
auditResult : expectedAuditResult ,
156
165
fullAuditRef : auditUrl ,
157
166
} ) ;
158
-
159
- // Verify cache warming checked S3 for existing cache files
160
- expect ( context . s3Client . send ) . to . have . been . called ;
161
- } ) ;
162
-
163
- it ( 'should handle cache warming partial success' , async ( ) => {
164
- const result = await prepareTrafficAnalysisRequest (
165
- auditUrl ,
166
- context ,
167
- site ,
168
- 'weekly' ,
169
- ) ;
170
-
171
- expect ( result ) . to . have . property ( 'auditResult' ) ;
172
- // Verify cache warming checked S3 for existing cache files
173
- expect ( context . s3Client . send ) . to . have . been . called ;
174
- } ) ;
175
-
176
- it ( 'should warm cache with temporal parameters' , async ( ) => {
177
- const result = await prepareTrafficAnalysisRequest (
178
- auditUrl ,
179
- context ,
180
- site ,
181
- 'weekly' ,
182
- ) ;
183
-
184
- expect ( result ) . to . have . property ( 'auditResult' ) ;
185
- // Cache warming should check S3 for existing cache files
186
- expect ( context . s3Client . send ) . to . have . been . called ;
187
167
} ) ;
188
168
} ) ;
189
169
190
170
describe ( 'sendRequestToMystique' , ( ) => {
191
- it ( 'should send weekly message to Mystique correctly' , async ( ) => {
171
+ it ( 'should send weekly message to Mystique correctly' , async function ( ) {
172
+ this . timeout ( 5000 ) ; // 5 second timeout for this test
173
+
192
174
const auditData = {
193
175
id : auditId ,
194
176
auditResult : {
@@ -198,6 +180,7 @@ describe('Paid Traffic Analysis Handler', () => {
198
180
siteId,
199
181
temporalCondition : '(year=2025 AND month=1 AND week=2)' ,
200
182
} ,
183
+ period : 'weekly' ,
201
184
} ;
202
185
203
186
await sendRequestToMystique ( auditUrl , auditData , context , site ) ;
@@ -223,7 +206,9 @@ describe('Paid Traffic Analysis Handler', () => {
223
206
) ;
224
207
} ) ;
225
208
226
- it ( 'should send monthly message to Mystique correctly' , async ( ) => {
209
+ it ( 'should send monthly message to Mystique correctly' , async function ( ) {
210
+ this . timeout ( 5000 ) ; // 5 second timeout for this test
211
+
227
212
const auditData = {
228
213
id : auditId ,
229
214
auditResult : {
@@ -232,6 +217,7 @@ describe('Paid Traffic Analysis Handler', () => {
232
217
siteId,
233
218
temporalCondition : '(year=2024 AND month=12)' ,
234
219
} ,
220
+ period : 'monthly' ,
235
221
} ;
236
222
237
223
await sendRequestToMystique ( auditUrl , auditData , context , site ) ;
@@ -264,17 +250,14 @@ describe('Paid Traffic Analysis Handler', () => {
264
250
context . site = site ;
265
251
context . finalUrl = auditUrl ;
266
252
267
- // Let the function call the real prepareTrafficAnalysisRequest
268
253
const result = await weeklyImportDataStep ( context ) ;
269
254
270
- // Verify the payload structure that will be sent to import worker
271
255
expect ( result ) . to . have . property ( 'auditResult' ) ;
272
256
expect ( result ) . to . have . property ( 'fullAuditRef' , auditUrl ) ;
273
257
expect ( result ) . to . have . property ( 'type' , 'traffic-analysis' ) ;
274
258
expect ( result ) . to . have . property ( 'siteId' , siteId ) ;
275
259
expect ( result ) . to . have . property ( 'allowOverwrite' , false ) ;
276
260
277
- // Verify the auditResult has the expected temporal structure
278
261
expect ( result . auditResult ) . to . have . property ( 'year' ) ;
279
262
expect ( result . auditResult ) . to . have . property ( 'week' ) ;
280
263
expect ( result . auditResult ) . to . have . property ( 'month' ) ;
@@ -301,15 +284,14 @@ describe('Paid Traffic Analysis Handler', () => {
301
284
context . site = site ;
302
285
context . audit = mockAudit ;
303
286
304
- // Let the function call the real sendRequestToMystique
305
287
const result = await weeklyProcessAnalysisStep ( context ) ;
306
288
307
289
expect ( result ) . to . deep . equal ( {
308
290
status : 'complete' ,
309
291
findings : [ 'Traffic analysis completed and sent to Mystique' ] ,
310
292
} ) ;
311
293
312
- // Verify SQS message was sent (sendRequestToMystique calls context.sqs.sendMessage)
294
+ // Verify SQS message was sent
313
295
expect ( mockSqs . sendMessage ) . to . have . been . called ;
314
296
} ) ;
315
297
} ) ;
@@ -328,6 +310,7 @@ describe('Paid Traffic Analysis Handler', () => {
328
310
siteId,
329
311
temporalCondition : '(year=2025 AND month=1)' ,
330
312
} ,
313
+ period : 'weekly' ,
331
314
} ;
332
315
333
316
await expect (
0 commit comments