Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions apps/web/app/api/google/webhook/process-history-item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { ColdEmailSetting } from "@prisma/client";
import type { gmail_v1 } from "@googleapis/gmail";
import { isAssistantEmail } from "@/utils/assistant/is-assistant-email";
import { runColdEmailBlocker } from "@/utils/cold-email/is-cold-email";
import { runColdEmailBlockerWithProvider } from "@/utils/cold-email/is-cold-email";
import { blockUnsubscribedEmails } from "@/app/api/google/webhook/block-unsubscribed-emails";

import { markMessageAsProcessing } from "@/utils/redis/message-processing";
Expand Down Expand Up @@ -50,6 +50,9 @@ vi.mock("@/utils/cold-email/is-cold-email", () => ({
runColdEmailBlocker: vi
.fn()
.mockResolvedValue({ isColdEmail: false, reason: "hasPreviousEmail" }),
runColdEmailBlockerWithProvider: vi
.fn()
.mockResolvedValue({ isColdEmail: false, reason: "hasPreviousEmail" }),
}));
vi.mock("@/app/api/google/webhook/block-unsubscribed-emails", () => ({
blockUnsubscribedEmails: vi.fn().mockResolvedValue(false),
Expand Down Expand Up @@ -102,6 +105,7 @@ describe("processHistoryItem", () => {

const defaultOptions = {
gmail: {} as any,
provider: {} as any,
email: "user@test.com",
accessToken: "fake-token",
hasAutomationRules: false,
Expand Down Expand Up @@ -141,7 +145,7 @@ describe("processHistoryItem", () => {
await processHistoryItem(createHistoryItem(), options);

expect(blockUnsubscribedEmails).not.toHaveBeenCalled();
expect(runColdEmailBlocker).not.toHaveBeenCalled();
expect(runColdEmailBlockerWithProvider).not.toHaveBeenCalled();
expect(processAssistantEmail).toHaveBeenCalledWith({
message: expect.objectContaining({
headers: expect.objectContaining({
Expand Down Expand Up @@ -184,7 +188,7 @@ describe("processHistoryItem", () => {
await processHistoryItem(createHistoryItem(), options);

expect(blockUnsubscribedEmails).not.toHaveBeenCalled();
expect(runColdEmailBlocker).not.toHaveBeenCalled();
expect(runColdEmailBlockerWithProvider).not.toHaveBeenCalled();
});

it("should skip if email is unsubscribed", async () => {
Expand All @@ -196,7 +200,7 @@ describe("processHistoryItem", () => {
};
await processHistoryItem(createHistoryItem(), options);

expect(runColdEmailBlocker).not.toHaveBeenCalled();
expect(runColdEmailBlockerWithProvider).not.toHaveBeenCalled();
});

it("should run cold email blocker when enabled", async () => {
Expand All @@ -211,7 +215,7 @@ describe("processHistoryItem", () => {

await processHistoryItem(createHistoryItem(), options);

expect(runColdEmailBlocker).toHaveBeenCalledWith({
expect(runColdEmailBlockerWithProvider).toHaveBeenCalledWith({
email: expect.objectContaining({
from: "sender@example.com",
subject: "Test Email",
Expand All @@ -220,13 +224,13 @@ describe("processHistoryItem", () => {
threadId: "thread-123",
date: expect.any(Date),
}),
gmail: options.gmail,
provider: expect.any(Object),
emailAccount: options.emailAccount,
});
});

it("should skip further processing if cold email is detected", async () => {
vi.mocked(runColdEmailBlocker).mockResolvedValueOnce({
vi.mocked(runColdEmailBlockerWithProvider).mockResolvedValueOnce({
isColdEmail: true,
reason: "ai",
aiReason: "This appears to be a cold email",
Expand Down Expand Up @@ -254,7 +258,7 @@ describe("processHistoryItem", () => {
});

it("should add cold email to digest when coldEmailDigest is true and cold email is detected", async () => {
vi.mocked(runColdEmailBlocker).mockResolvedValueOnce({
vi.mocked(runColdEmailBlockerWithProvider).mockResolvedValueOnce({
isColdEmail: true,
reason: "ai",
aiReason: "This appears to be a cold email",
Expand All @@ -275,7 +279,7 @@ describe("processHistoryItem", () => {

await processHistoryItem(createHistoryItem(), options);

expect(runColdEmailBlocker).toHaveBeenCalledWith({
expect(runColdEmailBlockerWithProvider).toHaveBeenCalledWith({
email: expect.objectContaining({
from: "sender@example.com",
subject: "Test Email",
Expand All @@ -284,7 +288,7 @@ describe("processHistoryItem", () => {
threadId: "thread-123",
date: expect.any(Date),
}),
gmail: options.gmail,
provider: expect.any(Object),
emailAccount: options.emailAccount,
});

Expand Down Expand Up @@ -322,13 +326,13 @@ describe("processHistoryItem", () => {

await processHistoryItem(createHistoryItem(), options);

expect(runColdEmailBlocker).not.toHaveBeenCalled();
expect(runColdEmailBlockerWithProvider).not.toHaveBeenCalled();
expect(categorizeSender).toHaveBeenCalled();
expect(runRules).toHaveBeenCalled();
});

it("should process normally when cold email is not detected with coldEmailDigest enabled", async () => {
vi.mocked(runColdEmailBlocker).mockResolvedValueOnce({
vi.mocked(runColdEmailBlockerWithProvider).mockResolvedValueOnce({
isColdEmail: false,
reason: "hasPreviousEmail",
});
Expand All @@ -347,14 +351,14 @@ describe("processHistoryItem", () => {

await processHistoryItem(createHistoryItem(), options);

expect(runColdEmailBlocker).toHaveBeenCalled();
expect(runColdEmailBlockerWithProvider).toHaveBeenCalled();
expect(categorizeSender).toHaveBeenCalled();
expect(runRules).toHaveBeenCalled();
});

it("should add second email from known cold emailer to digest when coldEmailDigest is enabled", async () => {
// Mock the response for a known cold emailer (already in database)
vi.mocked(runColdEmailBlocker).mockResolvedValueOnce({
vi.mocked(runColdEmailBlockerWithProvider).mockResolvedValueOnce({
isColdEmail: true,
reason: "ai-already-labeled",
coldEmailId: "existing-cold-email-456", // Existing cold email entry ID
Expand All @@ -374,7 +378,7 @@ describe("processHistoryItem", () => {

await processHistoryItem(createHistoryItem("456", "thread-456"), options);

expect(runColdEmailBlocker).toHaveBeenCalledWith({
expect(runColdEmailBlockerWithProvider).toHaveBeenCalledWith({
email: expect.objectContaining({
from: "sender@example.com",
subject: "Test Email",
Expand All @@ -383,7 +387,7 @@ describe("processHistoryItem", () => {
threadId: "thread-456",
date: expect.any(Date),
}),
gmail: options.gmail,
provider: expect.any(Object),
emailAccount: options.emailAccount,
});

Expand Down
6 changes: 3 additions & 3 deletions apps/web/app/api/google/webhook/process-history-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { gmail_v1 } from "@googleapis/gmail";
import prisma from "@/utils/prisma";
import { emailToContent } from "@/utils/mail";
import { GmailLabel } from "@/utils/gmail/label";
import { runColdEmailBlocker } from "@/utils/cold-email/is-cold-email";
import { runColdEmailBlockerWithProvider } from "@/utils/cold-email/is-cold-email";
import { runRules } from "@/utils/ai/choose-rule/run-rules";
import { blockUnsubscribedEmails } from "@/app/api/google/webhook/block-unsubscribed-emails";
import { categorizeSender } from "@/utils/categorize/senders/categorize";
Expand Down Expand Up @@ -152,7 +152,7 @@ export async function processHistoryItem(

const content = emailToContent(parsedMessage);

const response = await runColdEmailBlocker({
const response = await runColdEmailBlockerWithProvider({
email: {
from: parsedMessage.headers.from,
to: "",
Expand All @@ -162,7 +162,7 @@ export async function processHistoryItem(
threadId,
date: internalDateToDate(parsedMessage.internalDate),
},
gmail,
provider,
emailAccount,
});

Expand Down
13 changes: 11 additions & 2 deletions apps/web/utils/actions/cold-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { actionClient } from "@/utils/actions/safe-action";
import { getGmailClientForEmail } from "@/utils/account";
import { SafeError } from "@/utils/error";
import { createEmailProvider } from "@/utils/email/provider";

export const updateColdEmailSettingsAction = actionClient
.metadata({ name: "updateColdEmailSettings" })
Expand Down Expand Up @@ -119,12 +120,20 @@ export const testColdEmailAction = actionClient
where: { id: emailAccountId },
include: {
user: { select: { aiProvider: true, aiModel: true, aiApiKey: true } },
account: {
select: {
provider: true,
},
},
},
});

if (!emailAccount) throw new SafeError("Email account not found");

const gmail = await getGmailClientForEmail({ emailAccountId });
const emailProvider = await createEmailProvider({
emailAccountId,
provider: emailAccount.account?.provider,
});

const content = emailToContent({
textHtml: textHtml || undefined,
Expand All @@ -143,7 +152,7 @@ export const testColdEmailAction = actionClient
id: messageId || "",
},
emailAccount,
gmail,
provider: emailProvider,
});

return response;
Expand Down
Loading
Loading