diff --git a/__tests__/pseudoplain-gmail-gmail.test.ts b/__tests__/pseudoplain-gmail-gmail.test.ts index 38e5a30eeeb08b788e8af72f7dadc780f5d2b3a2..0e00189ad7a30b9dcfde615d80b7bd94101cc5fc 100644 --- a/__tests__/pseudoplain-gmail-gmail.test.ts +++ b/__tests__/pseudoplain-gmail-gmail.test.ts @@ -1,3 +1,5 @@ +import { EMAIL_VENDORS } from "../src"; + const path = require("path"); import { describe } from "@jest/globals"; import { createDescribePseudoPlainTestCases } from "./utils"; @@ -6,7 +8,10 @@ const TESTS_GLOBAL_PATH = "/files/gmail-gmail"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Pseudo PLAIN] GMail-GMail", () => { - const describeFunction = createDescribePseudoPlainTestCases(testsPath); + const describeFunction = createDescribePseudoPlainTestCases( + testsPath, + EMAIL_VENDORS.GMAIL + ); describe("One", describeFunction("one")); }); diff --git a/__tests__/pseudoplain-gmail-outlook.test.ts b/__tests__/pseudoplain-gmail-outlook.test.ts index b2be78441ac8fcbbf876db06a74010cf0d689bad..46b3ce90a57fe8a463bb8aec7e7e1ee6fba306f3 100644 --- a/__tests__/pseudoplain-gmail-outlook.test.ts +++ b/__tests__/pseudoplain-gmail-outlook.test.ts @@ -1,5 +1,6 @@ import { describe } from "@jest/globals"; import { createDescribePseudoPlainTestCases } from "./utils"; +import { EMAIL_VENDORS } from "../src"; const path = require("path"); const TESTS_GLOBAL_PATH = "/files/gmail-outlook"; @@ -7,7 +8,10 @@ const TESTS_GLOBAL_PATH = "/files/gmail-outlook"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Pseudo PLAIN] Gmail-Outlook normalization", () => { - const describeFunction = createDescribePseudoPlainTestCases(testsPath); + const describeFunction = createDescribePseudoPlainTestCases( + testsPath, + EMAIL_VENDORS.GMAIL + ); describe( "One", describeFunction("one", [ diff --git a/__tests__/pseudoplain-outlook-outlook.test.ts b/__tests__/pseudoplain-outlook-outlook.test.ts index fa37a577fe7460cf06a1841d6b5793c8bd6ab3b5..509abd0fe3a1e108d3829534fe3c77f40cef0c90 100644 --- a/__tests__/pseudoplain-outlook-outlook.test.ts +++ b/__tests__/pseudoplain-outlook-outlook.test.ts @@ -1,5 +1,6 @@ import { describe } from "@jest/globals"; import { createDescribePseudoPlainTestCases } from "./utils"; +import { EMAIL_VENDORS } from "../src"; const path = require("path"); @@ -7,7 +8,10 @@ const TESTS_GLOBAL_PATH = "/files/outlook-outlook"; const testsPath = path.resolve(__dirname, `.${TESTS_GLOBAL_PATH}`); describe("[Pseudo PLAIN] Outlook-Outlook normalization", () => { - const describeFunction = createDescribePseudoPlainTestCases(testsPath); + const describeFunction = createDescribePseudoPlainTestCases( + testsPath, + EMAIL_VENDORS.OUTLOOK + ); // ["01"] - is a filter. Pass here names of directories with test cases you want to check describe("Emails Chrome", describeFunction("chrome", null, [])); diff --git a/__tests__/utils.ts b/__tests__/utils.ts index f87eb0c0e3adcaae1b1e23b917275343d9320961..ef875f21f5fc9472fed58d6451b6a0d19f10988b 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -5,9 +5,10 @@ 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, EMAIL_VENDORS } from "../src"; +import { PlainNormalizer, HTMLNormalizer } from "../src"; import { expect, test } from "@jest/globals"; import { DOM } from "@vereign/dom"; +//import { diffStringsUnified } from "jest-diff"; export const getNormalizedPlain = ( testCasePath: string @@ -111,12 +112,21 @@ export const createDescribeHtmlTestCases = ( export const createDescribePlainTestCases = (testsPath: string) => ( casesName: string, - failingCases: Array<string> = [] + failingCases: Array<string> = [], + casesToCheckOnly?: Array<string> ) => (): void => { const testsCasesPath = testsPath + "/" + casesName; - const testCasesDirs = getTestCasesDirs(testsCasesPath).filter( - (dir) => !failingCases.includes(dir) - ); + 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; @@ -127,10 +137,15 @@ export const createDescribePlainTestCases = (testsPath: string) => ( // expect(sentPlain.length).toBeGreaterThan(0); // expect(receivedPlain.length).toBeGreaterThan(0); expect(receivedPlain).toContain(sentPlain); + // const diff = diffStringsUnified(sentPlain, receivedPlain); + // console.log(diff); }); }; -export const createDescribePseudoPlainTestCases = (testsPath: string) => +export const createDescribePseudoPlainTestCases = ( + testsPath: string, + vendor: string +) => /** * @param casesGroupName - name of the folder with cases * @param failingCases - a list of cases that are failing and ignored. Pending to be fixed @@ -162,14 +177,8 @@ export const createDescribePseudoPlainTestCases = (testsPath: string) => testCasePath ); - HTMLNormalizer.normalizeVendorHtml( - receivedHtmlDocument, - EMAIL_VENDORS.OUTLOOK - ); - HTMLNormalizer.normalizeVendorHtml( - sentHtmlDocument, - EMAIL_VENDORS.OUTLOOK - ); + HTMLNormalizer.normalizeVendorHtml(receivedHtmlDocument, vendor); + HTMLNormalizer.normalizeVendorHtml(sentHtmlDocument, vendor); const normalizedReceivedPseudoPlainText = HTMLNormalizer.extractPseudoPlainPart( receivedHtmlDocument @@ -182,6 +191,12 @@ export const createDescribePseudoPlainTestCases = (testsPath: string) => expect(normalizedReceivedPseudoPlainText).toEqual( normalizedSentPseudoPlainText ); + + // const diff = diffStringsUnified( + // normalizedReceivedPseudoPlainText, + // normalizedSentPseudoPlainText + // ); + // console.log(diff); }); }; diff --git a/src/HTMLNormalizer/HTMLNormalizer.ts b/src/HTMLNormalizer/HTMLNormalizer.ts index fca69272f23ce45bf8c1100aca270cf1e0d4fe41..4fbe7c7e3b5af040c15f619eac2b252ca5b17840 100644 --- a/src/HTMLNormalizer/HTMLNormalizer.ts +++ b/src/HTMLNormalizer/HTMLNormalizer.ts @@ -84,7 +84,23 @@ export const extractPseudoPlainPart = ( document: HTMLDocument /*vendor: string*/ ): string => { - return PlainNormalizer.normalizePlain(document.body.textContent); + const textContent = PlainNormalizer.normalizePlain(document.body.textContent); + + // const anchors = document.getElementsByTagName("a"); + // const images = document.getElementsByTagName("img"); + // let meaningfulAttributes = []; + // + // Array.from(anchors).forEach((a) => { + // meaningfulAttributes.push(a.getAttribute("href")); + // }); + // Array.from(images).forEach((img) => { + // meaningfulAttributes.push(img.getAttribute("src")); + // meaningfulAttributes.push(img.getAttribute("alt")); + // }); + // + // meaningfulAttributes = meaningfulAttributes.filter((attr) => !!attr).sort(); + // console.log(meaningfulAttributes); + return textContent; }; export const printHtmlChildren = ( diff --git a/src/utils.ts b/src/utils.ts index c25376fc137c9374ef76d2115f5638d03f2865a7..c8bb6bb80bbf0e10a5b9c8bbbe95b69b179bdfcf 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,5 @@ export const removeSpacesAndLinebreaks = (s: string): string => { - const regexNewlines = new RegExp(/[\r\n\v]+/g); - const regexSpaces = new RegExp(/\s+|\u200B/g); + const removeSymbols = new RegExp(/[\r\n\v\s\u200B]+/g); - return s.replace(regexNewlines, "").replace(regexSpaces, ""); + return s.replace(removeSymbols, "").trim(); };