import { beforeEach, describe, expect, it, vi } from "vitest"; import { EMAIL_GMAIL_FOLDER, cleanupEmailWebhookEventsForTenant, drainEmailWebhookEventsForTenant, pollStaleEmailMailboxesForTenant, renewEmailWebhooksForTenant, runEmailInitialSyncForTenant, syncMailboxIncremental, upsertNeutralMessage, type EmailWorkerClient, } from "./email-worker-tasks.server.js"; import type { MappedEmailMessage } from "./email-message-mapping.js"; // VA-P23 — sync-engine tests (Prisma + provider fetch mocked): delta drain, // resumable initial sync, renewal thresholds for BOTH providers, dedupe. const mocks = vi.hoisted(() => { class MockGmailError extends Error { readonly code: string; readonly fullResyncRequired: boolean; constructor(code: string) { super(code); this.name = "GmailError"; this.code = code; this.fullResyncRequired = code === "history_expired"; } } class MockGraphMailError extends Error { readonly code: string; readonly fullResyncRequired: boolean; constructor(code: string) { super(code); this.name = "GraphMailError"; this.code = code; this.fullResyncRequired = code === "delta_token_invalid"; } } return { MockGmailError, MockGraphMailError, listHistory: vi.fn(), listMessageIds: vi.fn(), getGmailMessage: vi.fn(), watchMailbox: vi.fn(), refreshGmailAccessToken: vi.fn(), fetchDeltaPage: vi.fn(), renewMailSubscription: vi.fn(), createMailSubscription: vi.fn(), refreshMicrosoftAccessToken: vi.fn(), }; }); vi.mock("./google/gmailApi.js", () => ({ GmailError: mocks.MockGmailError, getMessage: mocks.getGmailMessage, listHistory: mocks.listHistory, listMessageIds: mocks.listMessageIds, refreshGmailAccessToken: mocks.refreshGmailAccessToken, resolveGmailOAuthConfig: vi.fn(() => ({ clientId: "id", clientSecret: "s", redirectUri: "https://x", pubsubTopic: "projects/p/topics/t" })), watchMailbox: mocks.watchMailbox, })); vi.mock("./microsoft/graphMailApi.js", () => ({ GraphMailError: mocks.MockGraphMailError, buildInitialDeltaUrl: vi.fn((folderId: string) => `https://graph/delta/${folderId}`), createMailSubscription: mocks.createMailSubscription, fetchDeltaPage: mocks.fetchDeltaPage, refreshMicrosoftAccessToken: mocks.refreshMicrosoftAccessToken, renewMailSubscription: mocks.renewMailSubscription, resolveMicrosoftMailOAuthConfig: vi.fn(() => ({ clientId: "id", clientSecret: "s", redirectUri: "https://x", tenant: "common" })), })); vi.mock("@hivegrid/db/crypto/tenant-envelope.server", () => ({ decryptTenantField: vi.fn(async (_tenantId: string, ciphertext: string) => (ciphertext.includes("refresh") ? "refresh-token" : "access-token")), encryptTenantField: vi.fn(async (_tenantId: string, value: string) => `sealed:${value}`), })); const NOW = new Date("2026-07-02T10:00:00.000Z"); function mappedMessage(overrides: Partial = {}): MappedEmailMessage { return { providerMessageId: "pm1", providerThreadId: "th1", internetMessageId: "rfc-1@x.de", direction: "inbound", from: { address: "max@kunde.de" }, to: [{ address: "info@handwerk.de" }], cc: [], bcc: [], subject: "Anfrage", snippet: "Anfrage", bodyHtml: "

Hi

", bodyText: "Hi", sentAt: NOW, receivedAt: NOW, isRead: false, hasAttachments: false, isNewsletter: false, providerLabels: ["INBOX"], attachments: [], ...overrides, }; } type MailboxOverrides = Partial<{ id: string; provider: "gmail" | "microsoft"; status: string; webhookSubscriptionId: string | null; webhookSubscriptionExpiresAt: Date | null; webhookClientState: string | null; lastSyncAt: Date | null; }>; function mailboxRow(overrides: MailboxOverrides = {}) { return { id: "mb1", tenantId: "t1", ownerUserId: "u1", provider: "gmail" as const, emailAddress: "info@handwerk.de", oauthRefreshTokenCiphertext: "sealed-refresh", oauthAccessTokenCiphertext: "sealed-access", oauthAccessTokenExpiresAt: new Date("2026-07-02T11:00:00.000Z"), webhookSubscriptionId: "sub1", webhookSubscriptionExpiresAt: new Date("2026-07-05T10:00:00.000Z"), webhookClientState: "cs1", status: "active" as const, lastSyncAt: new Date("2026-07-02T09:00:00.000Z"), ...overrides, }; } function createClient(opts: { mailboxes?: ReturnType[]; cursors?: Array>; existingMessage?: { id: string; providerMessageId: string } | null; existingByRfc?: { id: string; providerMessageId: string } | null; webhookEvents?: Array<{ id: string; tenantId: string; mailboxId: string }>; }) { const mailboxes = opts.mailboxes ?? [mailboxRow()]; const cursors = opts.cursors ?? []; const client = { emailIntegrationMailbox: { findMany: vi.fn<(args: Record) => Promise>(async () => mailboxes), findFirst: vi.fn(async (args: { where?: { id?: string } }) => mailboxes.find((m) => !args?.where?.id || m.id === args.where.id) ?? null), updateMany: vi.fn<(args: Record) => Promise<{ count: number }>>(async () => ({ count: 1 })), }, emailIntegrationMessage: { findFirst: vi.fn(async (args: { where?: { internetMessageId?: string } }) => args?.where?.internetMessageId ? (opts.existingByRfc ?? null) : (opts.existingMessage ?? null) ), create: vi.fn<(args: Record) => Promise<{ id: string; providerMessageId: string }>>(async () => ({ id: "msg-new", providerMessageId: "pm1", })), updateMany: vi.fn<(args: Record) => Promise<{ count: number }>>(async () => ({ count: 1 })), }, emailIntegrationSyncCursor: { findMany: vi.fn<(args: Record) => Promise>(async () => cursors), findFirst: vi.fn( async (args: { where?: { externalFolderId?: string } }) => (cursors.find((c) => !args?.where?.externalFolderId || c.externalFolderId === args.where.externalFolderId) as never) ?? null ), updateMany: vi.fn<(args: Record) => Promise<{ count: number }>>(async () => ({ count: 1 })), upsert: vi.fn<(args: Record) => Promise>>(async () => ({})), }, emailIntegrationWebhookEvent: { findMany: vi.fn<(args: Record) => Promise>>( async () => opts.webhookEvents ?? [] ), update: vi.fn<(args: Record) => Promise>>(async () => ({})), updateMany: vi.fn<(args: Record) => Promise<{ count: number }>>(async () => ({ count: 1 })), deleteMany: vi.fn<(args: Record) => Promise<{ count: number }>>(async () => ({ count: 2 })), }, }; return client as unknown as EmailWorkerClient & typeof client; } beforeEach(() => { vi.clearAllMocks(); }); describe("upsertNeutralMessage", () => { it("creates a new row when nothing matches", async () => { const client = createClient({ existingMessage: null, existingByRfc: null }); const result = await upsertNeutralMessage(client, { tenantId: "t1", mailboxId: "mb1" }, mappedMessage()); expect(result).toEqual({ id: "msg-new", created: true }); expect(client.emailIntegrationMessage.create).toHaveBeenCalledOnce(); }); it("adopts the provider id onto an existing row matched by internetMessageId (Graph 202 mirror)", async () => { const client = createClient({ existingByRfc: { id: "local-mirror", providerMessageId: "local-tmp" } }); const result = await upsertNeutralMessage(client, { tenantId: "t1", mailboxId: "mb1" }, mappedMessage({ providerMessageId: "graph-real" })); expect(result).toEqual({ id: "local-mirror", created: false }); const updateArgs = client.emailIntegrationMessage.updateMany.mock.calls[0][0] as unknown as { data: { providerMessageId: string } }; expect(updateArgs.data.providerMessageId).toBe("graph-real"); }); it("updates an existing row matched by providerMessageId", async () => { const client = createClient({ existingMessage: { id: "row1", providerMessageId: "pm1" } }); const result = await upsertNeutralMessage(client, { tenantId: "t1", mailboxId: "mb1" }, mappedMessage({ internetMessageId: null })); expect(result).toEqual({ id: "row1", created: false }); expect(client.emailIntegrationMessage.create).not.toHaveBeenCalled(); }); }); describe("syncMailboxIncremental (gmail)", () => { it("drains history pages and advances the cursor", async () => { const client = createClient({ cursors: [ { id: "c1", mailboxId: "mb1", externalFolderId: EMAIL_GMAIL_FOLDER, cursorToken: "100", initialSyncStatus: "completed", initialSyncProgress: null }, ], }); mocks.listHistory .mockResolvedValueOnce({ addedMessageIds: ["g1"], nextPageToken: "p2", historyId: "150" }) .mockResolvedValueOnce({ addedMessageIds: ["g2"], historyId: "200" }); mocks.getGmailMessage.mockResolvedValue({ id: "g1", threadId: "t", labelIds: ["INBOX"], payload: { headers: [{ name: "From", value: "a@b.de" }] }, internalDate: String(NOW.getTime()), }); const result = await syncMailboxIncremental(client, "t1", "mb1", { now: NOW }); expect(result.upserted).toBe(2); expect(mocks.listHistory).toHaveBeenCalledTimes(2); const cursorUpdate = client.emailIntegrationSyncCursor.updateMany.mock.calls.at(-1)?.[0] as unknown as { data: { cursorToken: string } }; expect(cursorUpdate.data.cursorToken).toBe("200"); }); it("resets the cursor for a bounded resync when the historyId expired", async () => { const client = createClient({ cursors: [ { id: "c1", mailboxId: "mb1", externalFolderId: EMAIL_GMAIL_FOLDER, cursorToken: "1", initialSyncStatus: "completed", initialSyncProgress: null }, ], }); mocks.listHistory.mockRejectedValueOnce(new mocks.MockGmailError("history_expired")); const result = await syncMailboxIncremental(client, "t1", "mb1", { now: NOW }); expect(result.resyncScheduled).toBe(true); const resetCall = client.emailIntegrationSyncCursor.updateMany.mock.calls[0][0] as unknown as { data: { cursorToken: null; initialSyncStatus: string } }; expect(resetCall.data.cursorToken).toBeNull(); expect(resetCall.data.initialSyncStatus).toBe("pending"); }); it("fetches message details with bounded concurrency", async () => { const client = createClient({ cursors: [ { id: "c1", mailboxId: "mb1", externalFolderId: EMAIL_GMAIL_FOLDER, cursorToken: "100", initialSyncStatus: "completed", initialSyncProgress: null }, ], }); const ids = Array.from({ length: 12 }, (_, index) => `g${index + 1}`); mocks.listHistory.mockResolvedValueOnce({ addedMessageIds: ids, historyId: "200" }); let active = 0; let peak = 0; mocks.getGmailMessage.mockImplementation(async (_token: string, id: string) => { active += 1; peak = Math.max(peak, active); await new Promise((resolve) => setTimeout(resolve, 2)); active -= 1; return { id, labelIds: ["INBOX"], payload: { headers: [{ name: "From", value: "a@b.de" }] }, internalDate: String(NOW.getTime()) }; }); const result = await syncMailboxIncremental(client, "t1", "mb1", { now: NOW }); expect(result.upserted).toBe(12); expect(peak).toBe(5); }); }); describe("syncMailboxIncremental (microsoft)", () => { it("drains per-folder deltas and stores the new deltaLink", async () => { const client = createClient({ mailboxes: [mailboxRow({ provider: "microsoft" })], cursors: [ { id: "c1", mailboxId: "mb1", externalFolderId: "inbox", cursorToken: "https://graph/delta-inbox", initialSyncStatus: "completed", initialSyncProgress: null, }, { id: "c2", mailboxId: "mb1", externalFolderId: "sentitems", cursorToken: null, initialSyncStatus: "pending", initialSyncProgress: null }, ], }); mocks.fetchDeltaPage.mockResolvedValueOnce({ messages: [{ id: "m1", conversationId: "c", sentDateTime: NOW.toISOString(), from: { emailAddress: { address: "a@b.de" } } }], deltaLink: "https://graph/delta-inbox-next", }); const result = await syncMailboxIncremental(client, "t1", "mb1", { now: NOW }); expect(result.upserted).toBe(1); // Only the inbox folder had a cursor — sentitems waits for the initial sync. expect(mocks.fetchDeltaPage).toHaveBeenCalledTimes(1); const cursorUpdate = client.emailIntegrationSyncCursor.updateMany.mock.calls.at(-1)?.[0] as unknown as { data: { cursorToken: string } }; expect(cursorUpdate.data.cursorToken).toBe("https://graph/delta-inbox-next"); }); }); describe("runEmailInitialSyncForTenant", () => { it("persists resumable progress when more pages remain", async () => { const client = createClient({ cursors: [ { id: "c1", mailboxId: "mb1", externalFolderId: EMAIL_GMAIL_FOLDER, cursorToken: null, initialSyncStatus: "pending", initialSyncProgress: null }, ], }); // Always another page → the run hits the per-run page cap and persists pageToken. mocks.listMessageIds.mockResolvedValue({ messageIds: ["g1"], nextPageToken: "next-page" }); mocks.getGmailMessage.mockResolvedValue({ id: "g1", labelIds: ["INBOX"], payload: { headers: [{ name: "From", value: "a@b.de" }] }, internalDate: String(NOW.getTime()), }); const result = await runEmailInitialSyncForTenant(client, "t1", { now: NOW }); expect(result.completed).toBe(0); expect(result.fetched).toBe(5); // 5 pages per run × 1 message const progressUpdate = client.emailIntegrationSyncCursor.updateMany.mock.calls.at(-1)?.[0] as unknown as { data: { initialSyncStatus: string; initialSyncProgress: { pageToken: string; fetchedCount: number } }; }; expect(progressUpdate.data.initialSyncStatus).toBe("running"); expect(progressUpdate.data.initialSyncProgress.pageToken).toBe("next-page"); expect(progressUpdate.data.initialSyncProgress.fetchedCount).toBe(5); }); it("resumes from the persisted pageToken and completes when pages end", async () => { const client = createClient({ cursors: [ { id: "c1", mailboxId: "mb1", externalFolderId: EMAIL_GMAIL_FOLDER, cursorToken: null, initialSyncStatus: "running", initialSyncProgress: { pageToken: "resume-here", fetchedCount: 40 }, }, ], }); mocks.listMessageIds.mockResolvedValueOnce({ messageIds: ["g9"], nextPageToken: undefined }); mocks.getGmailMessage.mockResolvedValue({ id: "g9", labelIds: ["INBOX"], payload: { headers: [{ name: "From", value: "a@b.de" }] }, internalDate: String(NOW.getTime()), }); const result = await runEmailInitialSyncForTenant(client, "t1", { now: NOW }); expect(mocks.listMessageIds.mock.calls[0][1]).toMatchObject({ pageToken: "resume-here" }); expect(result.completed).toBe(1); const completedUpdate = client.emailIntegrationSyncCursor.updateMany.mock.calls.at(-1)?.[0] as unknown as { data: { initialSyncStatus: string; initialSyncProgress: { fetchedCount: number; percent: number } }; }; expect(completedUpdate.data.initialSyncStatus).toBe("completed"); expect(completedUpdate.data.initialSyncProgress.fetchedCount).toBe(41); expect(completedUpdate.data.initialSyncProgress.percent).toBe(100); }); it("marks the folder failed with a redacted Klartext error on provider failure", async () => { const client = createClient({ cursors: [ { id: "c1", mailboxId: "mb1", externalFolderId: EMAIL_GMAIL_FOLDER, cursorToken: null, initialSyncStatus: "pending", initialSyncProgress: null }, ], }); mocks.listMessageIds.mockRejectedValueOnce(new mocks.MockGmailError("provider_outage")); const result = await runEmailInitialSyncForTenant(client, "t1", { now: NOW }); expect(result.failed).toBe(1); const failedUpdate = client.emailIntegrationSyncCursor.updateMany.mock.calls.at(-1)?.[0] as unknown as { data: { initialSyncStatus: string } }; expect(failedUpdate.data.initialSyncStatus).toBe("failed"); }); it("completes a Graph folder and promotes the deltaLink to the incremental cursor", async () => { const client = createClient({ mailboxes: [mailboxRow({ provider: "microsoft" })], cursors: [{ id: "c1", mailboxId: "mb1", externalFolderId: "inbox", cursorToken: null, initialSyncStatus: "pending", initialSyncProgress: null }], }); mocks.fetchDeltaPage.mockResolvedValueOnce({ messages: [], deltaLink: "https://graph/final-delta" }); const result = await runEmailInitialSyncForTenant(client, "t1", { now: NOW }); expect(result.completed).toBe(1); const cursorPromotion = client.emailIntegrationSyncCursor.updateMany.mock.calls[0][0] as unknown as { data: { cursorToken: string } }; expect(cursorPromotion.data.cursorToken).toBe("https://graph/final-delta"); }); }); describe("renewEmailWebhooksForTenant", () => { it("re-issues the Gmail watch when the expiry is inside the threshold", async () => { const client = createClient({ mailboxes: [mailboxRow({ webhookSubscriptionExpiresAt: new Date("2026-07-02T20:00:00.000Z") })] }); mocks.watchMailbox.mockResolvedValueOnce({ historyId: "999", expiresAt: new Date("2026-07-09T10:00:00.000Z") }); const result = await renewEmailWebhooksForTenant(client, "t1", { now: NOW }); expect(result.renewed).toBe(1); expect(mocks.watchMailbox).toHaveBeenCalledOnce(); }); it("skips mailboxes whose subscription is still comfortably valid", async () => { const client = createClient({ mailboxes: [mailboxRow({ webhookSubscriptionExpiresAt: new Date("2026-07-08T10:00:00.000Z") })] }); const result = await renewEmailWebhooksForTenant(client, "t1", { now: NOW }); expect(result.renewed).toBe(0); expect(result.skipped).toBe(1); }); it("renews a Graph subscription and re-creates it when Microsoft already dropped it", async () => { const client = createClient({ mailboxes: [mailboxRow({ provider: "microsoft", webhookSubscriptionExpiresAt: new Date("2026-07-02T20:00:00.000Z") })] }); mocks.renewMailSubscription.mockRejectedValueOnce(new mocks.MockGraphMailError("subscription_failed")); mocks.createMailSubscription.mockResolvedValueOnce({ id: "sub-new", expiresAt: new Date("2026-07-05T10:00:00.000Z") }); const result = await renewEmailWebhooksForTenant(client, "t1", { now: NOW, graphWebhookUrl: "https://app/webhooks/email/microsoft" }); expect(result.renewed).toBe(1); expect(mocks.createMailSubscription).toHaveBeenCalledWith("access-token", "https://app/webhooks/email/microsoft", "cs1"); }); }); describe("drain + stale poll + cleanup", () => { it("collapses multiple pushes for one mailbox into a single incremental sync", async () => { const client = createClient({ cursors: [ { id: "c1", mailboxId: "mb1", externalFolderId: EMAIL_GMAIL_FOLDER, cursorToken: "100", initialSyncStatus: "completed", initialSyncProgress: null }, ], webhookEvents: [ { id: "e1", tenantId: "t1", mailboxId: "mb1" }, { id: "e2", tenantId: "t1", mailboxId: "mb1" }, ], }); mocks.listHistory.mockResolvedValue({ addedMessageIds: [], historyId: "101" }); const result = await drainEmailWebhookEventsForTenant(client, "t1", { now: NOW }); expect(result.claimed).toBe(2); expect(result.completed).toBe(2); // history.list ran once, not per event. expect(mocks.listHistory).toHaveBeenCalledTimes(1); }); it("polls only mailboxes silent for the stale window", async () => { const client = createClient({ mailboxes: [mailboxRow({ lastSyncAt: new Date("2026-07-02T04:00:00.000Z") })], cursors: [ { id: "c1", mailboxId: "mb1", externalFolderId: EMAIL_GMAIL_FOLDER, cursorToken: "100", initialSyncStatus: "completed", initialSyncProgress: null }, ], }); mocks.listHistory.mockResolvedValue({ addedMessageIds: [], historyId: "101" }); const result = await pollStaleEmailMailboxesForTenant(client, "t1", { now: NOW }); expect(result.polled).toBe(1); const where = client.emailIntegrationMailbox.findMany.mock.calls[0][0] as unknown as { where: { lastSyncAt: { lte: Date } } }; expect(where.where.lastSyncAt.lte).toEqual(new Date("2026-07-02T06:00:00.000Z")); }); it("cleans up done events after 30d and failed after 90d", async () => { const client = createClient({}); const result = await cleanupEmailWebhookEventsForTenant(client, "t1", { now: NOW }); expect(result.deleted).toBe(4); const doneCall = client.emailIntegrationWebhookEvent.deleteMany.mock.calls[0][0] as unknown as { where: { receivedAt: { lte: Date } } }; const failedCall = client.emailIntegrationWebhookEvent.deleteMany.mock.calls[1][0] as unknown as { where: { receivedAt: { lte: Date } } }; expect(doneCall.where.receivedAt.lte).toEqual(new Date("2026-06-02T10:00:00.000Z")); expect(failedCall.where.receivedAt.lte).toEqual(new Date("2026-04-03T10:00:00.000Z")); }); });