<commit_before>"use strict";
jest.autoMockOff();
const MockDate = require("mockdate");

const Utils = require("../../src/utils/Utils");
const RequestResults = require("../../src/constants/RequestResults");

describe("getDefaultApiResp", () => {
    const cases = [
        {
            msg: "checks success",
            status: RequestResults.SUCCESS,
            webResp: {status: 200},
            want: {success: true, timeout: false, allowed: true, code: 200}
        },
        {
            msg: "checks timeout",
            status: RequestResults.TIMEOUT,
            webResp: undefined,
            want: {success: false, timeout: true, allowed: true, code: 0}
        },
        {
            msg: "checks forbidden",
            status: RequestResults.FORBIDDEN,
            webResp: {status: 403},
            want: {success: false, timeout: false, allowed: false, code: 403}
        },
        {
            msg: "checks failure",
            status: RequestResults.FAILURE,
            webResp: {status: 400},
            want: {success: false, timeout: false, allowed: true, code: 400}
        }
    ];

    cases.forEach((c) => {
        it(c.msg, () => {
            const resp = Utils.getDefaultApiResp(c.status, c.webResp);
            expect(resp.success).toBe(c.want.success);
            expect(resp.timeout).toBe(c.want.timeout);
            expect(resp.allowed).toBe(c.want.allowed);
            expect(resp.code).toBe(c.want.code);
        });
    });
});

describe("isCallback", () => {
    const f = function() { };
    const cases = [
        {msg: "checks null is false", arg: null, res: false},
        {msg: "checks undefined is false", arg: undefined, res: false},
        {msg: "checks non-func is false", arg: "callback", res: false},
        {msg: "checks anon func is true", arg: function(){}, res: true},
        {msg: "checks func is true", arg: f, res: true}
    ]

    cases.forEach((e) => {
        it(e.msg, () => {
            expect(Utils.isCallback(e.arg)).toBe(e.res);
        });
    });
});


describe("isSet", () => {
    const cases = [
        {msg: "checks null is not set", arg: null, res: false},
        {msg: "checks undefined is not set", arg: undefined, res: false},
        {msg: "checks object is set", arg: {}, res: true},
        {msg: "checks that string is set", arg: "", res: true}
    ];

    cases.forEach((e) => {
        it(e.msg, () => {
            expect(Utils.isSet(e.arg)).toBe(e.res);
        });
    });
});


describe("isInArray", () => {
    const FIELDS = ["foo", "bar", "baz"];
    const f = (needle) => Utils.isInArray(needle, FIELDS);
    it("tests success", () => {
        expect(f("foo")).toBe(true);
        expect(f("bar")).toBe(true);
        expect(f("baz")).toBe(true);
    });

    it("tests failure", () => {
        expect(f("huh")).toBe(false);
        expect(f(null)).toBe(false);
        expect(f(undefined)).toBe(false);
    });
});


describe("isExpiredToken", () => {
    const TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6MTB9.eyJ1aWQiOjAsInBpZCI6MzIsImV4cCI6MTQ0OTIzNDY2MCwicG1zIjo3fQ==.notlegitsignature=";
    const f = () => Utils.isExpiredToken(TOKEN);
    it("tests non-expired", () => {
        expect(f()).toBe(false);
    });
    it("tests exact expired", () => {
        MockDate.set(1449234660000);
        expect(f()).toBe(true);
    });
    it("tests after is expired", () => {
        MockDate.set(1449234660001);
        expect(f()).toBe(true);
    })
});


describe("assertValidToken", () => {
    const cases = [
        {msg: "checks null is invalid", arg: null, res: false},
        {msg: "checks undefined is invalid", arg: undefined, res: false},
        {msg: "checks non-string is invalid", arg: 1, res: false},
        {msg: "checks 0-len string is invalid", arg: "", res: false},
        {msg: "checks string is valid", arg: "token", res: true}
    ];
    cases.forEach((e) => {
        it(e.msg, () => {
            try {<commit_msg>tests: fix false fail for expiration<commit_after>"use strict";
jest.autoMockOff();
const MockDate = require("mockdate");

const Utils = require("../../src/utils/Utils");
const RequestResults = require("../../src/constants/RequestResults");

describe("getDefaultApiResp", () => {
    const cases = [
        {
            msg: "checks success",
            status: RequestResults.SUCCESS,
            webResp: {status: 200},
            want: {success: true, timeout: false, allowed: true, code: 200}
        },
        {
            msg: "checks timeout",
            status: RequestResults.TIMEOUT,
            webResp: undefined,
            want: {success: false, timeout: true, allowed: true, code: 0}
        },
        {
            msg: "checks forbidden",
            status: RequestResults.FORBIDDEN,
            webResp: {status: 403},
            want: {success: false, timeout: false, allowed: false, code: 403}
        },
        {
            msg: "checks failure",
            status: RequestResults.FAILURE,
            webResp: {status: 400},
            want: {success: false, timeout: false, allowed: true, code: 400}
        }
    ];

    cases.forEach((c) => {
        it(c.msg, () => {
            const resp = Utils.getDefaultApiResp(c.status, c.webResp);
            expect(resp.success).toBe(c.want.success);
            expect(resp.timeout).toBe(c.want.timeout);
            expect(resp.allowed).toBe(c.want.allowed);
            expect(resp.code).toBe(c.want.code);
        });
    });
});

describe("isCallback", () => {
    const f = function() { };
    const cases = [
        {msg: "checks null is false", arg: null, res: false},
        {msg: "checks undefined is false", arg: undefined, res: false},
        {msg: "checks non-func is false", arg: "callback", res: false},
        {msg: "checks anon func is true", arg: function(){}, res: true},
        {msg: "checks func is true", arg: f, res: true}
    ]

    cases.forEach((e) => {
        it(e.msg, () => {
            expect(Utils.isCallback(e.arg)).toBe(e.res);
        });
    });
});


describe("isSet", () => {
    const cases = [
        {msg: "checks null is not set", arg: null, res: false},
        {msg: "checks undefined is not set", arg: undefined, res: false},
        {msg: "checks object is set", arg: {}, res: true},
        {msg: "checks that string is set", arg: "", res: true}
    ];

    cases.forEach((e) => {
        it(e.msg, () => {
            expect(Utils.isSet(e.arg)).toBe(e.res);
        });
    });
});


describe("isInArray", () => {
    const FIELDS = ["foo", "bar", "baz"];
    const f = (needle) => Utils.isInArray(needle, FIELDS);
    it("tests success", () => {
        expect(f("foo")).toBe(true);
        expect(f("bar")).toBe(true);
        expect(f("baz")).toBe(true);
    });

    it("tests failure", () => {
        expect(f("huh")).toBe(false);
        expect(f(null)).toBe(false);
        expect(f(undefined)).toBe(false);
    });
});


describe("isExpiredToken", () => {
    const TOKEN = "<KEY>fQ==.notlegitsignature=";
    const f = () => Utils.isExpiredToken(TOKEN);
    it("tests non-expired", () => {
        MockDate.set(1449234659999);
        expect(f()).toBe(false);
    });
    it("tests exact expired", () => {
        MockDate.set(1449234660000);
        expect(f()).toBe(true);
    });
    it("tests after is expired", () => {
        MockDate.set(1449234660001);
        expect(f()).toBe(true);
    })
});


describe("assertValidToken", () => {
    const cases = [
        {msg: "checks null is invalid", arg: null, res: false},
        {msg: "checks undefined is invalid", arg: undefined, res: false},
        {msg: "checks non-string is invalid", arg: 1, res: false},
        {msg: "checks 0-len string is invalid", arg: "", res: false},
        {msg: "checks string is valid", arg: "token", res: true}
    ];
    cases.forEach((e) => {
        it(e.msg, () => {
            try {
{'fraction_non_alphanumeric': 0.18232463914915167, 'fraction_numerical': 0.015193719929095973, 'mean_word_length': 2.251955537258131, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 46, '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': '5970825', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2469, 'n_words': 572}