Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

add more test remove subject strat as it could not retrieve the orignial from anyway

ThieryMichela7719ad90ac9
diff --git a/supabase/functions/postmark/detectForwarded.spec.ts b/supabase/functions/postmark/detectForwarded.spec.ts
index 1ed13e21..cf2a0532 100644
--- a/supabase/functions/postmark/detectForwarded.spec.ts
+++ b/supabase/functions/postmark/detectForwarded.spec.ts
@@ -1,4 +1,8 @@
-import { detectForwarded } from "./detectForwarded.ts";
+import {
+ detectForwarded,
+ parseForwardedBlock,
+ parseFromLine,
+} from "./detectForwarded.ts";
import { describe, expect, it } from "vitest";
describe("detectForwarded", () => {
@@ -19,7 +23,7 @@ This is the original message body.`;
name: "John Doe",
});
- expect(result.originalBody).toContain("This is the original message body.");
+ expect(result.originalBody).toBe("This is the original message body.");
});
it("detects Apple Mail forwarded block", () => {
@@ -37,7 +41,7 @@ Here are the meeting notes.`;
name: "Jane Smith",
});
- expect(result.originalBody).toContain("Here are the meeting notes.");
+ expect(result.originalBody).toBe("Here are the meeting notes.");
});
it("detects Outlook forwarded block", () => {
@@ -55,7 +59,7 @@ Budget details attached.`;
name: "Bob Brown",
});
- expect(result.originalBody).toContain("Budget details attached.");
+ expect(result.originalBody).toBe("Budget details attached.");
});
it("detects Resent-From header", () => {
@@ -72,21 +76,6 @@ Budget details attached.`;
});
});
- it("flags subject prefix only", () => {
- const subject = "Fwd: No body separator";
- const textBody = "Just a forwarded message, no separator.";
- const result = detectForwarded(subject, textBody);
- expect(result.isForwarded).toBe(true);
- expect(result.originalFrom).toBeUndefined();
- });
-
- it("does not flag replies", () => {
- const subject = "Re: Follow up";
- const textBody = "This is a reply, not a forward.";
- const result = detectForwarded(subject, textBody);
- expect(result.isForwarded).toBe(false);
- });
-
it("handles malformed From line", () => {
const subject = "Fwd: Weird format";
const textBody = `----- Forwarded message -----
@@ -99,6 +88,62 @@ Body text.`;
const result = detectForwarded(subject, textBody);
expect(result.isForwarded).toBe(true);
expect(result.originalFrom).toBeUndefined();
- expect(result.originalBody).toContain("Body text.");
+ expect(result.originalBody).toBe("Body text.");
+ });
+
+ describe("parseFromLine", () => {
+ it("parses 'Name <email>' format", () => {
+ const result = parseFromLine("John Doe <[email protected]>");
+ expect(result).toEqual({ name: "John Doe", email: "[email protected]" });
+ });
+
+ it("parses 'email' format", () => {
+ const result = parseFromLine("[email protected]");
+ expect(result).toEqual({ name: "", email: "[email protected]" });
+ });
+
+ it("returns undefined for invalid format", () => {
+ const result = parseFromLine("not-an-email");
+ expect(result).toBeUndefined();
+ });
+ });
+
+ describe("parseForwardedBlock", () => {
+ it("parses forwarded block with From and body", () => {
+ const blockText = `From: John Doe <[email protected]>
+Date: Mon, 1 Jan 2024 10:00:00 +0000
+Subject: Hello
+
+This is the original message body.`;
+ const result = parseForwardedBlock(blockText);
+ expect(result.originalFrom).toEqual({
+ name: "John Doe",
+ email: "[email protected]",
+ });
+ expect(result.originalBody).toBe("This is the original message body.");
+ });
+
+ it("handles missing From line", () => {
+ const blockText = `Date: Mon, 1 Jan 2024 10:00:00 +0000
+Subject: Hello
+
+Body without From line.`;
+ const result = parseForwardedBlock(blockText);
+ expect(result.originalFrom).toBeUndefined();
+ expect(result.originalBody).toBe("Body without From line.");
+ });
+
+ it("handles no blank line by leaving the body untounched", () => {
+ const blockText = `From: John Doe <[email protected]>
+This is the body without a blank line separator.`;
+ const result = parseForwardedBlock(blockText);
+ expect(result.originalFrom).toEqual({
+ name: "John Doe",
+ email: "[email protected]",
+ });
+ expect(result.originalBody).toBe(
+ "From: John Doe <[email protected]>\nThis is the body without a blank line separator.",
+ );
+ });
});
});
diff --git a/supabase/functions/postmark/detectForwarded.ts b/supabase/functions/postmark/detectForwarded.ts
index 4e5cbeb8..e0ea93d8 100644
--- a/supabase/functions/postmark/detectForwarded.ts
+++ b/supabase/functions/postmark/detectForwarded.ts
@@ -41,7 +41,7 @@ const FORWARD_SEPARATOR_PATTERNS = [
* John Doe <[email protected]>
*/
-const parseFromLine = (
+export const parseFromLine = (
raw: string,
): { name: string; email: string } | undefined => {
// Format: "Name" <email> or Name <email>
@@ -64,7 +64,7 @@ const parseFromLine = (
* The header block is a sequence of lines that look like "Key: value"
* (From, Date, Subject, To, Cc, …), potentially followed by a blank line.
*/
-const parseForwardedBlock = (
+export const parseForwardedBlock = (
blockText: string,
): {
originalFrom?: { name: string; email: string };
@@ -121,14 +121,5 @@ export const detectForwarded = (
}
}
- // ── Strategy 3: Subject prefix ───────────────────────────────────────────────
- const subjectHasForwardPrefix = /^(fwd?|tr|wg|ts)\s*:/i.test(subject.trim());
-
- // still flag as forwarded
- // but we cannot parse the original sender, so return without originalFrom.
- if (subjectHasForwardPrefix) {
- return { isForwarded: true };
- }
-
return { isForwarded: false };
};
diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.ts
index b3060f42..9d97621d 100644
--- a/supabase/functions/postmark/index.ts
+++ b/supabase/functions/postmark/index.ts
@@ -55,12 +55,22 @@ Deno.serve(async (req) => {
// behavior so the email is not silently lost.
console.warn(
"Detected a forwarded email but could not extract the original sender. " +
- "Falling back to default contact resolution.",
+ "Falling back to default contact resolution.",
+ {
+ subject: Subject,
+ textBody: TextBody,
+ headers: Headers,
+ },
);
} else {
// Swap the recipient with the original sender so the note is attached to
// the right contact, and trim the note to only the original message body.
- ToFull = [{ Email: forwarded.originalFrom.email, Name: forwarded.originalFrom.name }];
+ ToFull = [
+ {
+ Email: forwarded.originalFrom.email,
+ Name: forwarded.originalFrom.name,
+ },
+ ];
if (forwarded.originalBody) {
TextBody = forwarded.originalBody;
}