diff --git a/__tests__/pseudoplain-outlook-outlook.test.ts b/__tests__/pseudoplain-outlook-outlook.test.ts index 700201ff9b7196947b50d28887dcf51f4f9e9a75..fa37a577fe7460cf06a1841d6b5793c8bd6ab3b5 100644 --- a/__tests__/pseudoplain-outlook-outlook.test.ts +++ b/__tests__/pseudoplain-outlook-outlook.test.ts @@ -1,75 +1,39 @@ -import { diffStringsUnified } from "jest-diff"; -import { describe, test } from "@jest/globals"; -import { getDOMDocuments, getTestCasesDirs } from "./utils"; -import { PlainNormalizer } from "../src"; -import { amendOutlookNodes } from "../src/HTMLNormalizer/strategies/outlook"; +import { describe } from "@jest/globals"; +import { createDescribePseudoPlainTestCases } from "./utils"; const path = require("path"); const TESTS_GLOBAL_PATH = "/files/outlook-outlook"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); -const createDescribePseudoPlainTestCases = (testsPath: string) => - /** - * @param casesGroupName - name of the folder with cases - * @param failingCases - a list of cases that are failing and ignored. Pending to be fixed - * @param casesToCheckOnly - a filter to use if you want to check specific cases - */ - ( - casesGroupName: string, - failingCases?: Array<string>, - casesToCheckOnly?: Array<string> - ) => (): void => { - const testsCasesPath = testsPath + "/" + casesGroupName; - let testCasesDirs = getTestCasesDirs(testsCasesPath); - - if (casesToCheckOnly && casesToCheckOnly.length) { - testCasesDirs = testCasesDirs.filter((dir) => - casesToCheckOnly.includes(dir) - ); - } - - if (failingCases && failingCases.length) { - testCasesDirs = testCasesDirs.filter( - (dir) => !failingCases.includes(dir) - ); - } - - test.each(testCasesDirs)("Case %s", (dirName: string) => { - const testCasePath = testsCasesPath + "/" + dirName; - const { sentHtmlDocument, receivedHtmlDocument } = getDOMDocuments( - testCasePath - ); - - amendOutlookNodes(receivedHtmlDocument); - amendOutlookNodes(sentHtmlDocument); - - //console.log(sentHtmlDocument.body.textContent); - - var normalizedReceivedPseudoPlainText = PlainNormalizer.normalizePlain(receivedHtmlDocument.body.textContent); - var normalizedSentPseudoPlainText = PlainNormalizer.normalizePlain(sentHtmlDocument.body.textContent); - - const difference = diffStringsUnified( - normalizedReceivedPseudoPlainText, - normalizedSentPseudoPlainText - ); - - console.log(difference); - }); - }; - describe("[Pseudo PLAIN] Outlook-Outlook normalization", () => { const describeFunction = createDescribePseudoPlainTestCases(testsPath); // ["01"] - is a filter. Pass here names of directories with test cases you want to check - //describe("Emails Chrome", describeFunction("chrome", null, [])); + describe("Emails Chrome", describeFunction("chrome", null, [])); - //describe("Emails Edge", describeFunction("edge", [])); - - //describe("Emails Safari", describeFunction("safari", ["08"])); + describe( + "Emails Edge", + describeFunction("edge", [ + "08", // Files are missing for test case + "10", // Files are missing for test case + ]) + ); - //Does not work at all - describe("Emails MacOS", describeFunction("macos", [])); + describe("Emails Safari", describeFunction("safari", ["08"])); - //describe("Emails Windows", describeFunction("windows", [])); + //Does not work at all + describe( + "Emails MacOS", + describeFunction("macos", [ + "23", // has special character + ]) + ); + + describe( + "Emails Windows", + describeFunction("windows", [ + "10", // missing files + ]) + ); }); diff --git a/__tests__/utils.ts b/__tests__/utils.ts index 33f9d3d4b465de8dadb07885df2d5f245d4fa238..57396440e0d77044e12e782be5f7bef84c2dab82 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -5,7 +5,7 @@ const SENT_HTML_NAME = "s_htmlContent.html"; const RECEIVED_HTML_NAME = "r_htmlContent.html"; const SENT_PLAIN_NAME = "s_plainContent.data"; const RECEIVED_PLAIN_NAME = "r_plainContent.data"; -import { PlainNormalizer, HTMLNormalizer } from "../src"; +import { PlainNormalizer, HTMLNormalizer, EMAIL_VENDORS } from "../src"; import { expect, test } from "@jest/globals"; import { DOM } from "@vereign/dom"; @@ -130,6 +130,72 @@ export const createDescribePlainTestCases = (testsPath: string) => ( }); }; +export const createDescribePseudoPlainTestCases = (testsPath: string) => + /** + * @param casesGroupName - name of the folder with cases + * @param failingCases - a list of cases that are failing and ignored. Pending to be fixed + * @param casesToCheckOnly - a filter to use if you want to check specific cases + */ + ( + casesGroupName: string, + failingCases?: Array<string>, + casesToCheckOnly?: Array<string> + ) => (): void => { + const testsCasesPath = testsPath + "/" + casesGroupName; + let testCasesDirs = getTestCasesDirs(testsCasesPath); + + if (casesToCheckOnly && casesToCheckOnly.length) { + testCasesDirs = testCasesDirs.filter((dir) => + casesToCheckOnly.includes(dir) + ); + } + + if (failingCases && failingCases.length) { + testCasesDirs = testCasesDirs.filter( + (dir) => !failingCases.includes(dir) + ); + } + + test.each(testCasesDirs)("Case %s", (dirName: string) => { + const testCasePath = testsCasesPath + "/" + dirName; + const { sentHtmlDocument, receivedHtmlDocument } = getDOMDocuments( + testCasePath + ); + + // amendOutlookNodes(receivedHtmlDocument); + // amendOutlookNodes(sentHtmlDocument); + HTMLNormalizer.normalizeVendorHtml( + receivedHtmlDocument, + EMAIL_VENDORS.OUTLOOK + ); + HTMLNormalizer.normalizeVendorHtml( + sentHtmlDocument, + EMAIL_VENDORS.OUTLOOK + ); + + const normalizedReceivedPseudoPlainText = HTMLNormalizer.extractPseudoPlainPart( + receivedHtmlDocument, + EMAIL_VENDORS.OUTLOOK + ); + + const normalizedSentPseudoPlainText = HTMLNormalizer.extractPseudoPlainPart( + sentHtmlDocument, + EMAIL_VENDORS.OUTLOOK + ); + + // const difference = diffStringsUnified( + // normalizedReceivedPseudoPlainText, + // normalizedSentPseudoPlainText + // ); + + expect(normalizedReceivedPseudoPlainText).toContain( + normalizedSentPseudoPlainText + ); + // console.log(difference); + // expect() + }); + }; + export const getDOMDocuments = ( testCasePath: string ): { diff --git a/src/HTMLNormalizer/HTMLNormalizer.ts b/src/HTMLNormalizer/HTMLNormalizer.ts index 3eb2bc8c077fb86929f8b7ae405d5b47cfa2e8a2..fca69272f23ce45bf8c1100aca270cf1e0d4fe41 100644 --- a/src/HTMLNormalizer/HTMLNormalizer.ts +++ b/src/HTMLNormalizer/HTMLNormalizer.ts @@ -18,6 +18,7 @@ import { cleanupGMailElementAttributes, pruneGmailElement, } from "./strategies/gmail"; +import { PlainNormalizer } from "../index"; const nodesAmendingFunctions = { [EMAIL_VENDORS.GMAIL]: amendGmailNodes, @@ -79,6 +80,13 @@ export const normalizeVendorHtml = ( return printHtmlChildren(mimeBody, vendorPrintFunction, 0); }; +export const extractPseudoPlainPart = ( + document: HTMLDocument + /*vendor: string*/ +): string => { + return PlainNormalizer.normalizePlain(document.body.textContent); +}; + export const printHtmlChildren = ( node: Node, printFunction: (node: Node) => string, diff --git a/src/HTMLNormalizer/index.ts b/src/HTMLNormalizer/index.ts index 5a404bb97ec11626d161e4eb38928b5572c2c1f9..63caf0692ceae3bb8615175c74eed612c64ad924 100644 --- a/src/HTMLNormalizer/index.ts +++ b/src/HTMLNormalizer/index.ts @@ -1,5 +1,6 @@ -import { normalizeVendorHtml } from "./HTMLNormalizer"; +import { normalizeVendorHtml, extractPseudoPlainPart } from "./HTMLNormalizer"; export default { normalizeVendorHtml, + extractPseudoPlainPart, }; diff --git a/src/PlainNormalizer/PlainNormalizer.ts b/src/PlainNormalizer/PlainNormalizer.ts index 9154652af2070ba6b9c893e74f2bc794f02a3363..16a59c9c4a4e9c51e90c521158c8ce468ad40628 100644 --- a/src/PlainNormalizer/PlainNormalizer.ts +++ b/src/PlainNormalizer/PlainNormalizer.ts @@ -13,9 +13,5 @@ const removeQRCodes = (s: string): string => { }; const removeListBullets = (s: string): string => { - return s - .replace("\no\n/g", "") - .replace("\n§\n/g", "") - .replace("\no /g", "") - .replace("\n§ /g", ""); + return s.replace("\n[o§]\n+/g", ""); };