From 4cf493122c74e8c3dd4dd59cc0c9960c292ddbee Mon Sep 17 00:00:00 2001
From: igor <igor.markin@vereign.com>
Date: Tue, 8 Dec 2020 19:16:38 +0300
Subject: [PATCH] Fixes

---
 __tests__/pseudoplain-gmail-gmail.test.ts     |  7 ++-
 __tests__/pseudoplain-gmail-outlook.test.ts   |  6 ++-
 __tests__/pseudoplain-outlook-outlook.test.ts |  6 ++-
 __tests__/utils.ts                            | 43 +++++++++++++------
 src/HTMLNormalizer/HTMLNormalizer.ts          | 18 +++++++-
 src/utils.ts                                  |  5 +--
 6 files changed, 64 insertions(+), 21 deletions(-)

diff --git a/__tests__/pseudoplain-gmail-gmail.test.ts b/__tests__/pseudoplain-gmail-gmail.test.ts
index 38e5a30..0e00189 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 b2be784..46b3ce9 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 fa37a57..509abd0 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 f87eb0c..ef875f2 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 fca6927..4fbe7c7 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 c25376f..c8bb6bb 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();
 };
-- 
GitLab