Skip to content
Snippets Groups Projects
Commit 2ddf7966 authored by Igor Markin's avatar Igor Markin
Browse files

Implement plain part normalisation

parent c05eaf66
No related branches found
No related tags found
1 merge request!1Initial
......@@ -4,7 +4,6 @@ import HTMLNormalizer from "../src/HTMLNormalizer";
import { EMAIL_VENDORS } from "../src/constants";
const fs = require("fs");
const path = require("path");
const util = require("util");
// Test cases from https://code.vereign.com/alexey.lunin/outlook-files-upload
......@@ -18,7 +17,7 @@ const getTestCasesDirs = (testCasesPath: string) => {
return fs.statSync(testCasesPath + "/" + file).isDirectory();
});
};
console.debug = () => {}
const getNormalizedHtml = (
testCasePath: string
): {
......@@ -73,9 +72,9 @@ describe("Outlook emails HTML normalization", () => {
});
};
describe("Emails Chrome", describeTestCases("chrome"));
describe("Emails Edge", describeTestCases("edge"));
describe("Emails Safari", describeTestCases("safari"));
//describe("Emails MacOS", describeTestCases("macos"));
// describe("Emails Windows", describeTestCases("windows"));
describe("Emails Chrome", describeTestCases("chrome"));
describe("Emails Edge", describeTestCases("edge"));
describe("Emails Safari", describeTestCases("safari"));
describe("Emails MacOS", describeTestCases("macos"));
describe("Emails Windows", describeTestCases("windows"));
});
import { describe, test, expect } from "@jest/globals";
import { PlainNormalizer } from "../src";
const fs = require("fs");
const path = require("path");
// Test cases from https://code.vereign.com/alexey.lunin/outlook-files-upload
const TESTS_GLOBAL_PATH = "/outlook-files-upload/uploads";
const SENT_HTML_NAME = "s_initialPlainContent.data";
const RECEIVED_HTML_NAME = "r_plainContent.data";
const testsPath = path.resolve(__dirname, `..${TESTS_GLOBAL_PATH}`);
const getTestCasesDirs = (testCasesPath: string) => {
return fs.readdirSync(testCasesPath).filter(function (file) {
return fs.statSync(testCasesPath + "/" + file).isDirectory();
});
};
const getNormalizedHtml = (
testCasePath: string
): {
sentPlain: string;
receivedPlain: string;
} => {
const sentPlain = fs
.readFileSync(`${testCasePath}/${SENT_HTML_NAME}`)
.toString();
const receivedPlain = fs
.readFileSync(`${testCasePath}/${RECEIVED_HTML_NAME}`)
.toString();
const sentNormalizedPlain = PlainNormalizer.normalizePlain(sentPlain);
const receivedNormalizedPlain = PlainNormalizer.normalizePlain(receivedPlain);
return {
sentPlain: sentNormalizedPlain,
receivedPlain: receivedNormalizedPlain,
};
};
describe("Outlook emails Plain normalization", () => {
const describeTestCases = (casesName: string) => () => {
const testsCasesPath = testsPath + "/" + casesName;
const testCasesDirs = getTestCasesDirs(testsCasesPath);
test.each(testCasesDirs)("Case %s", (dirName: string) => {
const testCasePath = testsCasesPath + "/" + dirName;
let normalizedHtmls;
try {
normalizedHtmls = getNormalizedHtml(testCasePath);
} catch (e) {
console.log(`Invalid test case: ${casesName}/${dirName}`);
return;
}
const { sentPlain, receivedPlain } = normalizedHtmls;
expect(sentPlain.length).toBeGreaterThan(0);
expect(receivedPlain.length).toBeGreaterThan(0);
expect(receivedPlain).toContain(sentPlain);
});
};
describe("Emails Chrome", describeTestCases("chrome"));
describe("Emails Edge", describeTestCases("edge"));
describe("Emails Safari", describeTestCases("safari"));
describe("Emails MacOS", describeTestCases("macos"));
describe("Emails Windows", describeTestCases("windows"));
});
import { removeSpacesAndLinebreaks } from "../utils";
export const normalizePlainPart = (text: string): string => {
text = removeSpacesAndLinebreaks(text);
return removeQRCodes(text);
};
const removeQRCodes = (s: string): string => {
return s
.replace(/\[qrcode.png]\s*<https:\/\/[\w./?=\-&]+>/g, "")
.replace(/<https:\/\/[\w./?=\-&]+>\s*\[qrcode.png]/g, "");
};
import { normalizePlainPart } from "./PlainNormalizer";
export default {
normalizePlain: normalizePlainPart,
};
export { default as HTMLNormalizer } from "./HTMLNormalizer";
export { default as PlainNormalizer } from "./PlainNormalizer";
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment