@@ -174,22 +174,6 @@ describe('PTA2Controller', () => {
174174 expect ( body . message ) . to . equal ( 'Month must be a valid number' ) ;
175175 } ) ;
176176
177- it ( 'returns 302 with cached result if cache exists' , async ( ) => {
178- mockS3 . send . callsFake ( ( cmd ) => {
179- if ( cmd . constructor && cmd . constructor . name === 'HeadObjectCommand' ) {
180- // Simulate cache exists
181- return Promise . resolve ( { } ) ;
182- }
183- return Promise . resolve ( { } ) ;
184- } ) ;
185-
186- const controller = PTA2Controller ( mockContext , mockLog , mockEnv ) ;
187- const res = await controller . getPTAWeeklySummary ( ) ;
188- expect ( res . status ) . to . equal ( 302 ) ;
189- expect ( res . headers . get ( 'location' ) ) . to . equal ( TEST_PRESIGNED_URL ) ;
190- expect ( mockAthenaQuery ) . not . to . have . been . called ;
191- } ) ;
192-
193177 it ( 'queries Athena and returns 200 with fresh data on cache miss' , async ( ) => {
194178 const mockAthenaResults = [
195179 {
@@ -205,7 +189,6 @@ describe('PTA2Controller', () => {
205189 const res = await controller . getPTAWeeklySummary ( ) ;
206190 expect ( res . status ) . to . equal ( 200 ) ;
207191 expect ( mockAthenaQuery ) . to . have . been . calledOnce ;
208- expect ( lastPutObject ) . to . exist ;
209192 } ) ;
210193
211194 it ( 'bypasses cache when noCache is true' , async ( ) => {
@@ -252,71 +235,7 @@ describe('PTA2Controller', () => {
252235 const gzippedBuffer = Buffer . from ( await res . arrayBuffer ( ) ) ;
253236 const decompressed = await gunzipAsync ( gzippedBuffer ) ;
254237 const body = JSON . parse ( decompressed . toString ( ) ) ;
255- expect ( body ) . to . deep . equal ( [ ] ) ;
256- } ) ;
257-
258- it ( 'returns 302 when cache is successfully verified after being created' , async ( ) => {
259- let cacheExists = false ;
260- mockS3 . send . callsFake ( ( cmd ) => {
261- if ( cmd . constructor && cmd . constructor . name === 'HeadObjectCommand' ) {
262- if ( cacheExists ) {
263- return Promise . resolve ( { } ) ;
264- }
265- const err = new Error ( 'not found' ) ;
266- err . name = 'NotFound' ;
267- return Promise . reject ( err ) ;
268- }
269- if ( cmd . constructor && cmd . constructor . name === 'PutObjectCommand' ) {
270- lastPutObject = cmd ;
271- cacheExists = true ;
272- return Promise . resolve ( { } ) ;
273- }
274- return Promise . resolve ( { } ) ;
275- } ) ;
276-
277- const mockAthenaResults = [
278- {
279- year : 2024 ,
280- week : 23 ,
281- pageviews : 1000 ,
282- } ,
283- ] ;
284- mockAthenaQuery . resolves ( mockAthenaResults ) ;
285-
286- const controller = PTA2Controller ( mockContext , mockLog , mockEnv ) ;
287- const res = await controller . getPTAWeeklySummary ( ) ;
288- expect ( res . status ) . to . equal ( 302 ) ;
289- expect ( res . headers . get ( 'location' ) ) . to . equal ( TEST_PRESIGNED_URL ) ;
290- expect ( mockLog . debug ) . to . have . been . calledWithMatch ( 'Successfully verified file existence' ) ;
291- } ) ;
292-
293- it ( 'returns response directly if cache write fails' , async ( ) => {
294- mockS3 . send . callsFake ( ( cmd ) => {
295- if ( cmd . constructor && cmd . constructor . name === 'PutObjectCommand' ) {
296- throw new Error ( 'S3 put failed' ) ;
297- }
298- if ( cmd . constructor && cmd . constructor . name === 'HeadObjectCommand' && cmd . input . Key . includes ( `${ SITE_ID } /` ) ) {
299- const err = new Error ( 'not found' ) ;
300- err . name = 'NotFound' ;
301- return Promise . reject ( err ) ;
302- }
303- return Promise . resolve ( { } ) ;
304- } ) ;
305-
306- const mockAthenaResults = [
307- {
308- year : 2024 ,
309- week : 23 ,
310- pageviews : 1000 ,
311- } ,
312- ] ;
313- mockAthenaQuery . resolves ( mockAthenaResults ) ;
314-
315- const controller = PTA2Controller ( mockContext , mockLog , mockEnv ) ;
316- const res = await controller . getPTAWeeklySummary ( ) ;
317- expect ( res . status ) . to . equal ( 200 ) ;
318- const contentEncoding = res . headers . get ( 'content-encoding' ) ;
319- expect ( contentEncoding ) . to . equal ( 'gzip' ) ;
238+ expect ( body ) . to . equal ( null ) ;
320239 } ) ;
321240
322241 it ( 'uses month parameter when week is not provided' , async ( ) => {
@@ -399,48 +318,6 @@ describe('PTA2Controller', () => {
399318 expect ( mockAthenaQuery ) . to . have . been . calledOnce ;
400319 } ) ;
401320
402- it ( 'handles cache verification failure by returning response directly' , async ( ) => {
403- let putCalled = false ;
404- mockS3 . send . callsFake ( ( cmd ) => {
405- if ( cmd . constructor && cmd . constructor . name === 'PutObjectCommand' ) {
406- putCalled = true ;
407- lastPutObject = cmd ;
408- return Promise . resolve ( { } ) ;
409- }
410- if ( cmd . constructor && cmd . constructor . name === 'HeadObjectCommand' ) {
411- if ( putCalled ) {
412- // Simulate verification failure
413- const err = new Error ( 'not found' ) ;
414- err . name = 'NotFound' ;
415- return Promise . reject ( err ) ;
416- }
417- const err = new Error ( 'not found' ) ;
418- err . name = 'NotFound' ;
419- return Promise . reject ( err ) ;
420- }
421- return Promise . resolve ( { } ) ;
422- } ) ;
423-
424- // Also stub getSignedUrl to return null
425- mockContext . s3 . getSignedUrl . resolves ( null ) ;
426-
427- const mockAthenaResults = [
428- {
429- year : 2024 ,
430- week : 23 ,
431- pageviews : 1000 ,
432- } ,
433- ] ;
434- mockAthenaQuery . resolves ( mockAthenaResults ) ;
435-
436- const controller = PTA2Controller ( mockContext , mockLog , mockEnv ) ;
437- const res = await controller . getPTAWeeklySummary ( ) ;
438- expect ( res . status ) . to . equal ( 200 ) ;
439- expect ( mockLog . warn ) . to . have . been . calledWithMatch ( 'Failed to return cache key' ) ;
440- const contentEncoding = res . headers . get ( 'content-encoding' ) ;
441- expect ( contentEncoding ) . to . equal ( 'gzip' ) ;
442- } ) ;
443-
444321 it ( 'uses correct database and table names from env' , async ( ) => {
445322 const mockAthenaResults = [
446323 {
@@ -464,22 +341,5 @@ describe('PTA2Controller', () => {
464341 expect ( description ) . to . include ( 'test-db' ) ;
465342 expect ( description ) . to . include ( SITE_ID ) ;
466343 } ) ;
467-
468- it ( 'logs appropriate messages throughout the flow' , async ( ) => {
469- const mockAthenaResults = [
470- {
471- year : 2024 ,
472- week : 23 ,
473- pageviews : 1000 ,
474- } ,
475- ] ;
476- mockAthenaQuery . resolves ( mockAthenaResults ) ;
477-
478- const controller = PTA2Controller ( mockContext , mockLog , mockEnv ) ;
479- await controller . getPTAWeeklySummary ( ) ;
480-
481- expect ( mockLog . info ) . to . have . been . calledWithMatch ( 'Cached result for file:' ) ;
482- expect ( mockLog . info ) . to . have . been . calledWithMatch ( 'Athena result JSON to S3 cache' ) ;
483- } ) ;
484344 } ) ;
485345} ) ;
0 commit comments