"use strict";

// Public module from npm
const expect = require("chai").expect;

// Modules from file
const Credentials = require("../../app/scripts/classes/credentials.js");

module.exports.suite = function suite() {
    it("Check token formatting", async function testValidToken() {
        // Token example:
        // 1604309951,0338213c00fcbd894fd9415e6ba08403
        // 1604309986,ebdb75502337699381f0f55c86353555
        // 1604310008,2d50d55808e5ec3a157ec01953da9d26

        // Fetch token (is a GET request, we don't need the credentials)
        const cred = new Credentials(null, null);
        await cred.fetchToken();

        // Parse token for assert
        const splitted = cred.token.split(",");
        const unique = splitted[0];
        const hash = splitted[1];
        expect(splitted.length).to.be.equal(2, "The token consists of two parts");

        // Check type of parts
        expect(isNumeric(unique)).to.be.true;
        expect(isNumeric(hash)).to.be.false;

        // The second part is most probably the MD5 hash of something
        expect(hash.length).to.be.equal(32, "Hash should have 32 hex chars");
    });
};

//#region Private methods
/**
 * @private
 * Check if a string is a number
 * @param {String} str 
 * @author Dan, Ben Aston
 * @see https://preview.tinyurl.com/y46jqwkt
 */
function isNumeric(str) {
    // We only process strings!  
    if (typeof str != "string") return false;

    // Use type coercion to parse the _entirety_ of the string 
    // (`parseFloat` alone does not do this) and ensure strings 
    // of whitespace fail
    return !isNaN(str) && !isNaN(parseFloat(str));
}
//#endregion