Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -0,0 +1,186 @@
import { describe, expect, it, vi } from "vitest";
import type { MappedEmailMessage } from "./email-message-mapping.js";
import {
advanceAdminMailboxCursor,
createAdminEmailSyncService,
decideAdminEmailMatch,
providerMessageKey,
} from "./admin-email-sync.server.js";
const message = (overrides: Partial<MappedEmailMessage> = {}): MappedEmailMessage => ({
providerMessageId: "provider-1",
providerThreadId: "thread-1",
internetMessageId: "rfc-1",
direction: "inbound",
from: { address: "buyer@example.com" },
to: [{ address: "sales@hivegrid.de" }],
cc: [],
bcc: [],
subject: "Demo",
snippet: "Hallo",
bodyHtml: "<p>Hallo</p>",
bodyText: "Hallo",
sentAt: new Date("2026-07-24T10:00:00.000Z"),
receivedAt: new Date("2026-07-24T10:00:01.000Z"),
isRead: false,
hasAttachments: false,
isNewsletter: false,
providerLabels: ["INBOX"],
attachments: [],
...overrides,
});
describe("admin email matching", () => {
it("matches one exact contact and its only open deal deterministically", () => {
expect(
decideAdminEmailMatch({
message: message(),
mailboxAddresses: ["sales@hivegrid.de"],
candidates: [
{
id: "contact-1",
kind: "contact",
openDealIds: ["deal-1"],
},
],
})
).toMatchObject({
status: "matched",
contactId: "contact-1",
dealId: "deal-1",
reason: "single_open_deal",
});
});
it("puts duplicate identities and multiple deals into the visible ambiguity path", () => {
expect(
decideAdminEmailMatch({
message: message(),
mailboxAddresses: ["sales@hivegrid.de"],
candidates: [
{ id: "contact-b", kind: "contact" },
{ id: "contact-a", kind: "contact" },
],
})
).toMatchObject({
status: "ambiguous",
reason: "ambiguous_identity",
contactId: null,
});
expect(
decideAdminEmailMatch({
message: message(),
mailboxAddresses: ["sales@hivegrid.de"],
candidates: [
{
id: "contact-1",
kind: "contact",
openDealIds: ["deal-b", "deal-a"],
},
],
})
).toMatchObject({
status: "ambiguous",
reason: "ambiguous_deal",
dealId: null,
});
});
it("does not auto-match newsletters and validates explicit deal references", () => {
expect(
decideAdminEmailMatch({
message: message({ isNewsletter: true }),
mailboxAddresses: ["sales@hivegrid.de"],
candidates: [{ id: "contact-1", kind: "contact" }],
})
).toMatchObject({ status: "unmatched", reason: "newsletter" });
expect(
decideAdminEmailMatch({
message: message({ subject: "Re: [Deal #deal-2] Angebot" }),
mailboxAddresses: ["sales@hivegrid.de"],
candidates: [
{
id: "contact-1",
kind: "contact",
openDealIds: ["deal-1"],
},
],
})
).toMatchObject({ status: "ambiguous", reason: "ambiguous_deal" });
});
});
describe("admin email sync contract", () => {
it("uses stable provider keys and recovers an expired cursor without advancing it", () => {
expect(
providerMessageKey({
mailboxId: "mailbox-1",
providerMessageId: "provider-1",
})
).toBe("mailbox-1:provider-1");
expect(
advanceAdminMailboxCursor({
current: {
cursorToken: "old",
status: "running",
recoveryGeneration: 2,
lastErrorMessageKey: null,
},
nextCursor: null,
outcome: "cursor_expired",
})
).toEqual({
cursorToken: null,
status: "pending",
recoveryGeneration: 3,
lastErrorMessageKey: "admin.sales.email.sync.cursorExpired",
});
});
it("maps a provider batch once and delegates an atomic cursor/message write", async () => {
const persistBatch = vi.fn().mockResolvedValue({
upserted: 1,
replayed: 0,
});
const service = createAdminEmailSyncService({ persistBatch });
const candidatesFor = vi.fn().mockResolvedValue([
{
id: "contact-1",
kind: "contact",
openDealIds: ["deal-1"],
},
]);
await expect(
service.persistProviderBatch({
adminUserId: "admin-1",
mailboxId: "mailbox-1",
mailboxAddresses: ["sales@hivegrid.de"],
externalFolderId: "inbox",
expectedCursor: "cursor-1",
nextCursor: "cursor-2",
messages: [message()],
candidatesFor,
})
).resolves.toEqual({ upserted: 1, replayed: 0 });
expect(candidatesFor).toHaveBeenCalledWith("buyer@example.com");
expect(persistBatch).toHaveBeenCalledWith(
expect.objectContaining({
expectedCursor: "cursor-1",
nextCursor: "cursor-2",
messages: [
expect.objectContaining({
key: "mailbox-1:provider-1",
match: expect.objectContaining({
contactId: "contact-1",
dealId: "deal-1",
}),
}),
],
})
);
});
});