@@ -45,6 +45,7 @@ import {
4545 impact_referral_rewards ,
4646 kilo_pass_issuances ,
4747 kilo_pass_subscriptions ,
48+ user_affiliate_attributions ,
4849} from '@kilocode/db/schema' ;
4950import {
5051 ImpactAdvocateProgramKey ,
@@ -394,7 +395,28 @@ describe('Kilo Pass Impact referral conversions', () => {
394395 ) ;
395396 } ) ;
396397
397- test ( 'missing or expired attribution suppresses affiliate SALE reporting' , async ( ) => {
398+ test ( 'historical affiliate attribution without product-scoped touch preserves affiliate SALE' , async ( ) => {
399+ const referee = await insertTestUser ( { created_at : '2026-01-02T00:00:00.000Z' } ) ;
400+ await db . insert ( user_affiliate_attributions ) . values ( {
401+ user_id : referee . id ,
402+ provider : 'impact' ,
403+ tracking_id : '' ,
404+ } ) ;
405+ const subscriptionId = await insertKiloPassSubscription ( { userId : referee . id } ) ;
406+
407+ const disposition = await processInvoice ( { refereeId : referee . id , subscriptionId } ) ;
408+
409+ expect ( disposition ) . toEqual (
410+ expect . objectContaining ( {
411+ shouldEnqueueAffiliateSale : true ,
412+ winningTouchType : ImpactReferralWinningTouchType . Affiliate ,
413+ disqualificationReason : 'referral_affiliate_won' ,
414+ } )
415+ ) ;
416+ expect ( await db . select ( ) . from ( impact_referral_rewards ) ) . toHaveLength ( 0 ) ;
417+ } ) ;
418+
419+ test ( 'missing attribution and expired product-scoped touches suppress affiliate SALE reporting' , async ( ) => {
398420 const noTouchReferee = await insertTestUser ( { created_at : '2026-01-02T00:00:00.000Z' } ) ;
399421 const noTouchSubscriptionId = await insertKiloPassSubscription ( {
400422 userId : noTouchReferee . id ,
@@ -414,22 +436,60 @@ describe('Kilo Pass Impact referral conversions', () => {
414436 ) ;
415437
416438 await cleanupDbForTest ( ) ;
417- const expiredTouchReferee = await insertTestUser ( { created_at : '2026-01-02T00:00:00.000Z' } ) ;
439+ const expiredAffiliateReferee = await insertTestUser ( {
440+ created_at : '2026-01-02T00:00:00.000Z' ,
441+ } ) ;
442+ await db . insert ( user_affiliate_attributions ) . values ( {
443+ user_id : expiredAffiliateReferee . id ,
444+ provider : 'impact' ,
445+ tracking_id : 'historical-affiliate-click' ,
446+ } ) ;
418447 await insertTouch ( {
419- userId : expiredTouchReferee . id ,
448+ userId : expiredAffiliateReferee . id ,
420449 type : 'affiliate' ,
421450 touchedAt : '2025-12-01T00:00:00.000Z' ,
422451 } ) ;
423- const expiredTouchSubscriptionId = await insertKiloPassSubscription ( {
424- userId : expiredTouchReferee . id ,
452+ const expiredAffiliateSubscriptionId = await insertKiloPassSubscription ( {
453+ userId : expiredAffiliateReferee . id ,
454+ } ) ;
455+
456+ const expiredAffiliateDisposition = await processInvoice ( {
457+ refereeId : expiredAffiliateReferee . id ,
458+ subscriptionId : expiredAffiliateSubscriptionId ,
459+ } ) ;
460+
461+ expect ( expiredAffiliateDisposition ) . toEqual (
462+ expect . objectContaining ( {
463+ shouldEnqueueAffiliateSale : false ,
464+ winningTouchType : ImpactReferralWinningTouchType . None ,
465+ disqualificationReason : 'referral_no_valid_attribution' ,
466+ } )
467+ ) ;
468+
469+ await cleanupDbForTest ( ) ;
470+ const expiredReferralReferee = await insertTestUser ( {
471+ created_at : '2026-01-02T00:00:00.000Z' ,
472+ } ) ;
473+ await db . insert ( user_affiliate_attributions ) . values ( {
474+ user_id : expiredReferralReferee . id ,
475+ provider : 'impact' ,
476+ tracking_id : 'historical-affiliate-click' ,
477+ } ) ;
478+ await insertTouch ( {
479+ userId : expiredReferralReferee . id ,
480+ type : 'referral' ,
481+ touchedAt : '2025-12-01T00:00:00.000Z' ,
482+ } ) ;
483+ const expiredReferralSubscriptionId = await insertKiloPassSubscription ( {
484+ userId : expiredReferralReferee . id ,
425485 } ) ;
426486
427- const expiredTouchDisposition = await processInvoice ( {
428- refereeId : expiredTouchReferee . id ,
429- subscriptionId : expiredTouchSubscriptionId ,
487+ const expiredReferralDisposition = await processInvoice ( {
488+ refereeId : expiredReferralReferee . id ,
489+ subscriptionId : expiredReferralSubscriptionId ,
430490 } ) ;
431491
432- expect ( expiredTouchDisposition ) . toEqual (
492+ expect ( expiredReferralDisposition ) . toEqual (
433493 expect . objectContaining ( {
434494 shouldEnqueueAffiliateSale : false ,
435495 winningTouchType : ImpactReferralWinningTouchType . None ,
0 commit comments