<commit_before>// Caseholder for easy reuse in mail tests.
"use strict";

var fluid = require("infusion");
var gpii  = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.test.mail.caseholder");

var jqUnit = require("node-jqunit");
var fs     = require("fs");

gpii.test.mail.caseholder.verifyMailInfo = function (that, info, expected) {
    jqUnit.assertTrue("The message should have been accepted...", info.accepted.length > 0);
    jqUnit.assertFalse("The message should not been rejected...", info.rejected.length > 0);

    jqUnit.assertTrue("There should be a message ID...", info.messageId);

    jqUnit.assertEquals("The sender should be correct", expected.from, info.envelope.from);
    jqUnit.assertEquals("The recipient should be correct", expected.to, info.envelope.to[0]);
};

gpii.test.mail.caseholder.verifyMailBody = function (testEnvironment, expected) {
    var messageFile = testEnvironment.smtpServer.mailServer.currentMessageFile;
    var messageBody = fs.readFileSync(messageFile, "utf8");

    var testFields = ["subject", "html", "text"];
    fluid.each(testFields, function (field) {
        var expectedValue = expected[field];
        if (expectedValue) {
            jqUnit.assertTrue("The message should contain data that matches the expected '" + field + "' field...", messageBody.indexOf(expectedValue) !== -1);
        }
    });
};

// Copied from a similar utility grade in `gpii.express`.
gpii.test.mail.caseholder.addRequiredSequences = function (sequenceStart, rawTests) {
    var completeTests = fluid.copy(rawTests);

    for (var a = 0; a < completeTests.length; a++) {
        var testSuite = completeTests[a];
        for (var b = 0; b < testSuite.tests.length; b++) {
            var tests = testSuite.tests[b];
            var modifiedSequence = sequenceStart.concat(tests.sequence);
            tests.sequence = modifiedSequence;
        }
    }

    return completeTests;
};

fluid.defaults("gpii.test.mail.caseholder", {
    gradeNames: ["fluid.test.testCaseHolder"],
    mergePolicy: {
        rawModules:    "noexpand",
        sequenceStart: "noexpand"
    },
    sequenceStart: [
        { // This sequence point is required because of a QUnit bug - it defers the start of sequence by 13ms "to avoid any current callbacks" in its words
            func: "{testEnvironment}.events.constructServer.fire"
        },
        {
            listener: "fluid.identity",
            event: "{testEnvironment}.events.onReady"
        }
    ],
    moduleSource: {
        funcName: "gpii.test.mail.caseholder.addRequiredSequences",
        args:     ["{that}.options.sequenceStart", "{that}.options.rawModules"]
    }
});<commit_msg>GPII-1707:  Converted tests to use new `loadTestingSupport` function and removed previous code duplicating work done in `gpii-express`.<commit_after>// Caseholder for easy reuse in mail tests.
"use strict";

var fluid = require("infusion");
var gpii  = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.test.mail.caseholder");

var jqUnit = require("node-jqunit");
var fs     = require("fs");

// We use the "sequence wiring" infrastructure from `gpii-express`.
require("gpii-express");
gpii.express.loadTestingSupport();

gpii.test.mail.caseholder.verifyMailInfo = function (that, info, expected) {
    jqUnit.assertTrue("The message should have been accepted...", info.accepted.length > 0);
    jqUnit.assertFalse("The message should not been rejected...", info.rejected.length > 0);

    jqUnit.assertTrue("There should be a message ID...", info.messageId);

    jqUnit.assertEquals("The sender should be correct", expected.from, info.envelope.from);
    jqUnit.assertEquals("The recipient should be correct", expected.to, info.envelope.to[0]);
};

gpii.test.mail.caseholder.verifyMailBody = function (testEnvironment, expected) {
    var messageFile = testEnvironment.smtpServer.mailServer.currentMessageFile;
    var messageBody = fs.readFileSync(messageFile, "utf8");

    var testFields = ["subject", "html", "text"];
    fluid.each(testFields, function (field) {
        var expectedValue = expected[field];
        if (expectedValue) {
            jqUnit.assertTrue("The message should contain data that matches the expected '" + field + "' field...", messageBody.indexOf(expectedValue) !== -1);
        }
    });
};

fluid.defaults("gpii.test.mail.caseholder", {
    gradeNames: ["gpii.express.tests.caseHolder.base"],
    // Although our initial sequences are currently the same as those used in the `gpii.express` test fixtures, we have
    // our own copy to avoid unexpected side effects as `gpii-express` changes.
    sequenceStart: [
        {
            func: "{testEnvironment}.events.constructServer.fire"
        },
        {
            listener: "fluid.identity",
            event: "{testEnvironment}.events.onReady"
        }
    ]
});
{'fraction_non_alphanumeric': 0.12988884314532728, 'fraction_numerical': 0.0037052284890901604, 'mean_word_length': 3.3893405600722675, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2324689', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1374, 'n_words': 358}