@@ -81,6 +81,7 @@ import {
8181} from '@kilocode/db/schema' ;
8282import { db , auto_deleted_at } from '@/lib/drizzle' ;
8383import { insertTestUser } from '@/tests/helpers/user.helper' ;
84+ import { softDeleteUser } from '@/lib/user' ;
8485import { createTestPaymentMethod } from '@/tests/helpers/payment-method.helper' ;
8586import { eq , and , count } from 'drizzle-orm' ;
8687import type Stripe from 'stripe' ;
@@ -882,6 +883,43 @@ describe('processStripePaymentEventHook', () => {
882883 retrieveSpy . mockRestore ( ) ;
883884 } ) ;
884885
886+ test ( 'radar.early_fraud_warning.created does not link a new case to a soft-deleted user' , async ( ) => {
887+ await cleanupDbForTest ( ) ;
888+ testUser = await insertTestUser ( ) ;
889+ await softDeleteUser ( testUser . id ) ;
890+ const { client } = await import ( '@/lib/stripe-client' ) ;
891+ const retrieveSpy = jest . spyOn ( client . charges , 'retrieve' ) . mockResolvedValue (
892+ sampleStripeChargeResponse (
893+ sampleStripeCharge ( {
894+ id : 'ch_deleted_customer' ,
895+ customer : testUser . stripe_customer_id ,
896+ } )
897+ )
898+ ) ;
899+
900+ await processStripePaymentEventHook (
901+ sampleEarlyFraudWarningEvent ( {
902+ eventId : 'evt_deleted_customer' ,
903+ warningId : 'issfr_deleted_customer' ,
904+ charge : 'ch_deleted_customer' ,
905+ } )
906+ ) ;
907+
908+ const [ fraudCase ] = await db . select ( ) . from ( stripe_early_fraud_warning_cases ) ;
909+ expect ( fraudCase ) . toEqual (
910+ expect . objectContaining ( {
911+ stripe_customer_id : testUser . stripe_customer_id ,
912+ owner_classification : 'unmatched' ,
913+ kilo_user_id : null ,
914+ status : 'review_required' ,
915+ reason : 'No canonical customer owner matched; manual review required' ,
916+ } )
917+ ) ;
918+ expect ( await db . select ( ) . from ( stripe_early_fraud_warning_actions ) ) . toHaveLength ( 0 ) ;
919+
920+ retrieveSpy . mockRestore ( ) ;
921+ } ) ;
922+
885923 test ( 'radar.early_fraud_warning.created deduplicates repeated delivery without creating actions' , async ( ) => {
886924 await cleanupDbForTest ( ) ;
887925 testUser = await insertTestUser ( ) ;
0 commit comments