11import { describe , it , expect , vi , beforeEach } from "vitest" ;
22import { validateCcReplyExpected } from "../validateCcReplyExpected" ;
33import type { ResendEmailData } from "@/lib/emails/validateInboundEmailEvent" ;
4+ import { INBOUND_EMAIL_DOMAIN } from "@/lib/const" ;
45
56const mockGenerate = vi . fn ( ) ;
67
@@ -28,47 +29,122 @@ describe("validateCcReplyExpected", () => {
2829 vi . clearAllMocks ( ) ;
2930 } ) ;
3031
31- it ( "always calls agent.generate regardless of TO/CC" , async ( ) => {
32- mockGenerate . mockResolvedValue ( { output : { shouldReply : true } } ) ;
32+ describe ( "when recoup email is only in TO (not CC)" , ( ) => {
33+ it ( "skips agent call and returns null (always reply)" , async ( ) => {
34+ const emailData : ResendEmailData = {
35+ ...baseEmailData ,
36+ to : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
37+ cc : [ ] ,
38+ } ;
3339
34- const emailData : ResendEmailData = {
35- ...baseEmailData ,
36- to : [ "hi@mail.recoupable.com" ] ,
37- cc : [ ] ,
38- } ;
40+ const result = await validateCcReplyExpected ( emailData , "Hello" ) ;
3941
40- await validateCcReplyExpected ( emailData , "Hello" ) ;
42+ expect ( mockGenerate ) . not . toHaveBeenCalled ( ) ;
43+ expect ( result ) . toBeNull ( ) ;
44+ } ) ;
4145
42- expect ( mockGenerate ) . toHaveBeenCalledTimes ( 1 ) ;
46+ it ( "handles multiple TO addresses with recoup email" , async ( ) => {
47+ const emailData : ResendEmailData = {
48+ ...baseEmailData ,
49+ to : [ "other@example.com" , `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
50+ cc : [ ] ,
51+ } ;
52+
53+ const result = await validateCcReplyExpected ( emailData , "Hello" ) ;
54+
55+ expect ( mockGenerate ) . not . toHaveBeenCalled ( ) ;
56+ expect ( result ) . toBeNull ( ) ;
57+ } ) ;
4358 } ) ;
4459
45- it ( "returns null when agent returns shouldReply: true" , async ( ) => {
46- mockGenerate . mockResolvedValue ( { output : { shouldReply : true } } ) ;
60+ describe ( "when recoup email is only in CC" , ( ) => {
61+ it ( "calls agent to determine if reply is expected" , async ( ) => {
62+ mockGenerate . mockResolvedValue ( { output : { shouldReply : true } } ) ;
4763
48- const emailData : ResendEmailData = {
49- ...baseEmailData ,
50- to : [ "hi@mail.recoupable.com" ] ,
51- cc : [ ] ,
52- } ;
64+ const emailData : ResendEmailData = {
65+ ...baseEmailData ,
66+ to : [ "someone@example.com" ] ,
67+ cc : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
68+ } ;
69+
70+ await validateCcReplyExpected ( emailData , "FYI" ) ;
71+
72+ expect ( mockGenerate ) . toHaveBeenCalledTimes ( 1 ) ;
73+ } ) ;
74+
75+ it ( "returns null when agent returns shouldReply: true" , async ( ) => {
76+ mockGenerate . mockResolvedValue ( { output : { shouldReply : true } } ) ;
77+
78+ const emailData : ResendEmailData = {
79+ ...baseEmailData ,
80+ to : [ "someone@example.com" ] ,
81+ cc : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
82+ } ;
83+
84+ const result = await validateCcReplyExpected ( emailData , "Please review" ) ;
85+
86+ expect ( result ) . toBeNull ( ) ;
87+ } ) ;
5388
54- const result = await validateCcReplyExpected ( emailData , "Hello" ) ;
89+ it ( "returns response when agent returns shouldReply: false" , async ( ) => {
90+ mockGenerate . mockResolvedValue ( { output : { shouldReply : false } } ) ;
5591
56- expect ( result ) . toBeNull ( ) ;
92+ const emailData : ResendEmailData = {
93+ ...baseEmailData ,
94+ to : [ "someone@example.com" ] ,
95+ cc : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
96+ } ;
97+
98+ const result = await validateCcReplyExpected ( emailData , "FYI" ) ;
99+
100+ expect ( result ) . not . toBeNull ( ) ;
101+ expect ( result ?. response ) . toBeDefined ( ) ;
102+ } ) ;
57103 } ) ;
58104
59- it ( "returns response when agent returns shouldReply: false" , async ( ) => {
60- mockGenerate . mockResolvedValue ( { output : { shouldReply : false } } ) ;
105+ describe ( "when recoup email is in both TO and CC" , ( ) => {
106+ it ( "treats as CC and calls agent" , async ( ) => {
107+ mockGenerate . mockResolvedValue ( { output : { shouldReply : true } } ) ;
61108
62- const emailData : ResendEmailData = {
63- ...baseEmailData ,
64- to : [ "someone@example.com" ] ,
65- cc : [ "hi@mail.recoupable.com" ] ,
66- } ;
109+ const emailData : ResendEmailData = {
110+ ...baseEmailData ,
111+ to : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
112+ cc : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
113+ } ;
114+
115+ await validateCcReplyExpected ( emailData , "Hello" ) ;
116+
117+ expect ( mockGenerate ) . toHaveBeenCalledTimes ( 1 ) ;
118+ } ) ;
67119
68- const result = await validateCcReplyExpected ( emailData , "FYI" ) ;
120+ it ( "returns null when agent returns shouldReply: true" , async ( ) => {
121+ mockGenerate . mockResolvedValue ( { output : { shouldReply : true } } ) ;
69122
70- expect ( result ) . not . toBeNull ( ) ;
71- expect ( result ?. response ) . toBeDefined ( ) ;
123+ const emailData : ResendEmailData = {
124+ ...baseEmailData ,
125+ to : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
126+ cc : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
127+ } ;
128+
129+ const result = await validateCcReplyExpected ( emailData , "Hello" ) ;
130+
131+ expect ( result ) . toBeNull ( ) ;
132+ } ) ;
133+
134+ it ( "returns response when agent returns shouldReply: false" , async ( ) => {
135+ mockGenerate . mockResolvedValue ( { output : { shouldReply : false } } ) ;
136+
137+ const emailData : ResendEmailData = {
138+ ...baseEmailData ,
139+ to : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
140+ cc : [ `hi${ INBOUND_EMAIL_DOMAIN } ` ] ,
141+ } ;
142+
143+ const result = await validateCcReplyExpected ( emailData , "FYI" ) ;
144+
145+ expect ( result ) . not . toBeNull ( ) ;
146+ expect ( result ?. response ) . toBeDefined ( ) ;
147+ } ) ;
72148 } ) ;
73149
74150 it ( "passes email context in prompt to agent.generate" , async ( ) => {
@@ -77,8 +153,8 @@ describe("validateCcReplyExpected", () => {
77153 const emailData : ResendEmailData = {
78154 ...baseEmailData ,
79155 from : "test@example.com" ,
80- to : [ "hi@mail.recoupable .com" ] ,
81- cc : [ "cc@example.com" ] ,
156+ to : [ "someone@example .com" ] ,
157+ cc : [ `hi ${ INBOUND_EMAIL_DOMAIN } ` , "cc@example.com" ] ,
82158 subject : "Test Subject" ,
83159 } ;
84160
0 commit comments