|
1 | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 2 |
|
3 | | -import type { CreateLinkDraft, Uuid } from "@plotday/twister"; |
4 | | - |
5 | | -import { GmailApi, GmailApiError, type GmailHeader, type GmailMessage } from "./gmail-api"; |
6 | | -import { type GmailSyncHost, onCreateLinkFn, onNoteCreatedFn } from "./sync"; |
| 3 | +import type { CreateLinkDraft, NewLinkWithNotes, Uuid } from "@plotday/twister"; |
| 4 | + |
| 5 | +import { |
| 6 | + GmailApi, |
| 7 | + GmailApiError, |
| 8 | + type GmailHeader, |
| 9 | + type GmailMessage, |
| 10 | + type GmailMessagePart, |
| 11 | + type GmailThread, |
| 12 | +} from "./gmail-api"; |
| 13 | +import { |
| 14 | + type GmailSyncHost, |
| 15 | + onCreateLinkFn, |
| 16 | + onNoteCreatedFn, |
| 17 | + processEmailThreadsFn, |
| 18 | +} from "./sync"; |
7 | 19 |
|
8 | 20 | /** Decode the base64url raw message the Gmail send API would receive. */ |
9 | 21 | function decodeRawMessage(b64url: string): string { |
@@ -379,3 +391,93 @@ describe("onNoteCreatedFn — calendar event thread", () => { |
379 | 391 | expect(res).toBeUndefined(); |
380 | 392 | }); |
381 | 393 | }); |
| 394 | + |
| 395 | +/** Build a GmailMessagePart, encoding `data` as base64url like the real API. */ |
| 396 | +function part( |
| 397 | + mimeType: string, |
| 398 | + opts: { |
| 399 | + data?: string; |
| 400 | + parts?: GmailMessagePart[]; |
| 401 | + headers?: Array<[string, string]>; |
| 402 | + } = {} |
| 403 | +): GmailMessagePart { |
| 404 | + return { |
| 405 | + mimeType, |
| 406 | + headers: (opts.headers ?? []).map(([name, value]) => ({ name, value })), |
| 407 | + body: |
| 408 | + opts.data !== undefined |
| 409 | + ? { size: opts.data.length, data: b64url(opts.data) } |
| 410 | + : undefined, |
| 411 | + parts: opts.parts, |
| 412 | + }; |
| 413 | +} |
| 414 | + |
| 415 | +/** A single-message GmailThread carrying a `text/calendar` ICS part. */ |
| 416 | +function calendarUpdateThread(threadId: string, ics: string): GmailThread { |
| 417 | + const message: GmailMessage = { |
| 418 | + id: `${threadId}-msg-1`, |
| 419 | + threadId, |
| 420 | + labelIds: ["INBOX"], |
| 421 | + snippet: "Event updated", |
| 422 | + historyId: "1", |
| 423 | + internalDate: "1700000000000", |
| 424 | + sizeEstimate: 500, |
| 425 | + payload: part("multipart/mixed", { |
| 426 | + headers: [ |
| 427 | + ["From", "calendar-notification@google.com"], |
| 428 | + ["To", "me@example.com"], |
| 429 | + ["Subject", "Updated: Weekly sync"], |
| 430 | + ], |
| 431 | + parts: [ |
| 432 | + part("text/plain", { data: "The event has been updated." }), |
| 433 | + part("text/calendar", { data: ics }), |
| 434 | + ], |
| 435 | + }), |
| 436 | + }; |
| 437 | + return { id: threadId, historyId: "1", messages: [message] }; |
| 438 | +} |
| 439 | + |
| 440 | +function gmailThreadWithIcsUpdate(uid: string): GmailThread { |
| 441 | + const ics = `BEGIN:VCALENDAR\r\nMETHOD:REQUEST\r\nBEGIN:VEVENT\r\nUID:${uid}\r\nSEQUENCE:2\r\nEND:VEVENT\r\nEND:VCALENDAR`; |
| 442 | + return calendarUpdateThread("cal-update-thread", ics); |
| 443 | +} |
| 444 | + |
| 445 | +function gmailThreadWithIcsCancel(uid: string): GmailThread { |
| 446 | + const ics = `BEGIN:VCALENDAR\r\nMETHOD:CANCEL\r\nBEGIN:VEVENT\r\nUID:${uid}\r\nEND:VEVENT\r\nEND:VCALENDAR`; |
| 447 | + return calendarUpdateThread("cal-cancel-thread", ics); |
| 448 | +} |
| 449 | + |
| 450 | +describe("processEmailThreadsFn — calendar-thread bundling", () => { |
| 451 | + it("adds icaluid:<uid> to sources when the conversation is a calendar update", async () => { |
| 452 | + const { host } = makeHost(); |
| 453 | + const saved: NewLinkWithNotes[] = []; |
| 454 | + (host.tools.integrations.saveLink as ReturnType<typeof vi.fn>).mockImplementation( |
| 455 | + async (l: NewLinkWithNotes) => { |
| 456 | + saved.push(l); |
| 457 | + return "T"; |
| 458 | + } |
| 459 | + ); |
| 460 | + |
| 461 | + await processEmailThreadsFn( |
| 462 | + host, |
| 463 | + [gmailThreadWithIcsUpdate("uid-1")], |
| 464 | + false, |
| 465 | + "INBOX" |
| 466 | + ); |
| 467 | + |
| 468 | + expect(saved[0].sources).toContain("icaluid:uid-1"); |
| 469 | + }); |
| 470 | + |
| 471 | + it("records a cancel-email marker for a cancellation email", async () => { |
| 472 | + const { host, store } = makeHost(); |
| 473 | + |
| 474 | + await processEmailThreadsFn( |
| 475 | + host, |
| 476 | + [gmailThreadWithIcsCancel("uid-1")], |
| 477 | + false, |
| 478 | + "INBOX" |
| 479 | + ); |
| 480 | + |
| 481 | + expect(store.get("cancel-email:uid-1")).toBeTruthy(); |
| 482 | + }); |
| 483 | +}); |
0 commit comments