11import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest' ;
2- import fs from 'fs/promises ' ;
2+ import { storePurchaseData } from '@lib/purchase-storage ' ;
33
4- // Mock fs module
5- vi . mock ( 'fs/promises' , ( ) => ( {
6- default : {
7- mkdir : vi . fn ( ) ,
8- writeFile : vi . fn ( ) ,
9- } ,
4+ // Mock purchase storage
5+ vi . mock ( '@lib/purchase-storage' , ( ) => ( {
6+ storePurchaseData : vi . fn ( ) . mockResolvedValue ( undefined ) ,
107} ) ) ;
118
129// Mock Stripe
@@ -27,10 +24,6 @@ describe('stripe-webhook API', () => {
2724 beforeEach ( ( ) => {
2825 vi . clearAllMocks ( ) ;
2926
30- // Mock successful file operations
31- ( fs . mkdir as any ) . mockResolvedValue ( undefined ) ;
32- ( fs . writeFile as any ) . mockResolvedValue ( undefined ) ;
33-
3427 // Mock successful email sending
3528 ( global . fetch as any ) . mockResolvedValue ( {
3629 ok : true ,
@@ -83,9 +76,11 @@ describe('stripe-webhook API', () => {
8376 expect ( responseText ) . toBe ( 'Webhook handled successfully' ) ;
8477
8578 // Verify purchase data was stored
86- expect ( fs . writeFile ) . toHaveBeenCalledWith (
87- expect . stringMatching ( / E S C - 1 2 3 4 5 6 7 8 \. j s o n $ / ) ,
88- expect . stringContaining ( '"purchaseCode":"ESC-12345678"' )
79+ expect ( storePurchaseData ) . toHaveBeenCalledWith (
80+ expect . objectContaining ( {
81+ purchaseCode : 'ESC-12345678' ,
82+ sessionId : 'cs_test_session123' ,
83+ } )
8984 ) ;
9085 } ) ;
9186
@@ -186,13 +181,13 @@ describe('stripe-webhook API', () => {
186181 expect ( responseText ) . toBe ( 'Webhook handled successfully' ) ;
187182
188183 // Should not store any purchase data for unhandled events
189- expect ( fs . writeFile ) . not . toHaveBeenCalled ( ) ;
184+ expect ( storePurchaseData ) . not . toHaveBeenCalled ( ) ;
190185 } ) ;
191186
192187 it ( 'should handle file system errors gracefully' , async ( ) => {
193188 const { POST } = await import ( '../../pages/api/stripe-webhook' ) ;
194189
195- ( fs . mkdir as any ) . mockRejectedValue ( new Error ( 'Permission denied' ) ) ;
190+ ( storePurchaseData as any ) . mockRejectedValueOnce ( new Error ( 'Permission denied' ) ) ;
196191
197192 const mockEvent = {
198193 type : 'checkout.session.completed' ,
@@ -277,7 +272,7 @@ describe('stripe-webhook API', () => {
277272 expect ( response . status ) . toBe ( 200 ) ;
278273
279274 // Purchase data should still be stored
280- expect ( fs . writeFile ) . toHaveBeenCalled ( ) ;
275+ expect ( storePurchaseData ) . toHaveBeenCalled ( ) ;
281276 } ) ;
282277
283278 it ( 'should store complete purchase data with all metadata' , async ( ) => {
@@ -316,13 +311,14 @@ describe('stripe-webhook API', () => {
316311 await POST ( { request : mockRequest } as any ) ;
317312
318313 // Verify all data is included
319- expect ( fs . writeFile ) . toHaveBeenCalledWith (
320- expect . stringMatching ( / E S C - 1 2 3 4 5 6 7 8 \. j s o n $ / ) ,
321- expect . stringMatching ( / " p u r c h a s e C o d e " : " E S C - 1 2 3 4 5 6 7 8 " / )
314+ expect ( storePurchaseData ) . toHaveBeenCalledWith (
315+ expect . objectContaining ( {
316+ purchaseCode : 'ESC-12345678' ,
317+ } )
322318 ) ;
323319
324- const writeCall = ( fs . writeFile as any ) . mock . calls [ 0 ] ;
325- const savedData = JSON . parse ( writeCall [ 1 ] ) ;
320+ const storeCall = ( storePurchaseData as any ) . mock . calls [ 0 ] ;
321+ const savedData = storeCall [ 0 ] ;
326322
327323 expect ( savedData ) . toMatchObject ( {
328324 sessionId : 'cs_test_session123' ,
0 commit comments