@bitblit/epsilon
Version:
Tiny adapter to simplify building API gateway Lambda APIS
134 lines • 6.48 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const cron_util_1 = require("./cron-util");
describe('#cronUtil', function () {
it('should test matching event to entry', () => __awaiter(this, void 0, void 0, function* () {
const sEvent = JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, '../../test-data/sample-json/sample-schedule-event-1.json')).toString());
const cfg = {
context: 'prod',
timezone: 'America/Los_Angeles',
entries: [], // only works since this isn't checked
};
const cron1 = {
contextMatchFilter: new RegExp('prod'),
};
const match1 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron1, cfg);
expect(match1).toBeTruthy();
const cron2 = {
contextMatchFilter: new RegExp('dev'),
};
const match2 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron2, cfg);
expect(match2).toBeFalsy();
const cron3 = {
eventFilter: new RegExp('.*MyScheduledRule.*'),
};
const match3 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron3, cfg);
expect(match3).toBeTruthy();
const cron4 = {
eventFilter: new RegExp('.*NotMyRule.*'),
};
const match4 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron4, cfg);
expect(match4).toBeFalsy();
}));
it('should match times', () => __awaiter(this, void 0, void 0, function* () {
const sEvent = JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, '../../test-data/sample-json/sample-schedule-event-1.json')).toString());
const cfg = {
context: 'prod',
timezone: 'America/Los_Angeles',
entries: [], // only works since this isn't checked
};
const entry1 = {};
const cron1 = {
contextMatchFilter: new RegExp('prod'),
};
const match1 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron1, cfg);
expect(match1).toBeTruthy();
const cron2 = {
contextMatchFilter: new RegExp('dev'),
};
const match2 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron2, cfg);
expect(match2).toBeFalsy();
const cron3 = {
eventFilter: new RegExp('.*MyScheduledRule.*'),
};
const match3 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron3, cfg);
expect(match3).toBeTruthy();
const cron4 = {
eventFilter: new RegExp('.*NotMyRule.*'),
};
const match4 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron4, cfg);
expect(match4).toBeFalsy();
}));
it('should match time with override', () => __awaiter(this, void 0, void 0, function* () {
const sEvent = JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, '../../test-data/sample-json/sample-schedule-event-1.json')).toString());
const cfg = {
context: 'prod',
timezone: 'America/Los_Angeles',
entries: [], // only works since this isn't checked
};
const entry1 = {};
const cron1 = {
hourFilter: [3],
};
const cron2 = {
overrideTimezone: 'etc/utc',
hourFilter: [3],
};
//Timestamp in milliseconds: 1652931000000
//Date and time (GMT): Thursday, May 19, 2022 3:30:00 AM
const testTimestampEpochMS = 1652931000000;
const match1 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron1, cfg, testTimestampEpochMS);
const match2 = cron_util_1.CronUtil.eventMatchesEntry(sEvent, cron2, cfg, testTimestampEpochMS);
expect(match1).toBeFalsy();
expect(match2).toBeTruthy();
}));
it('should match day of month filters', () => __awaiter(this, void 0, void 0, function* () {
const sEvent = JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, '../../test-data/sample-json/sample-schedule-event-1.json')).toString());
const cfg = {
context: 'prod',
timezone: 'etc/utc',
entries: [], // only works since this isn't checked
};
const entry1 = {};
//Timestamp in milliseconds: 1652931000000
//Date and time (GMT): Thursday, May 19, 2022 3:30:00 AM
const testTimestampEpochMS = 1652931000000;
const dayOfMonth = {
dayOfMonthFilter: [19],
};
const matchDay = cron_util_1.CronUtil.eventMatchesEntry(sEvent, dayOfMonth, cfg, testTimestampEpochMS);
expect(matchDay).toBeTruthy();
}));
it('should match month of year filter', () => __awaiter(this, void 0, void 0, function* () {
const sEvent = JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, '../../test-data/sample-json/sample-schedule-event-1.json')).toString());
const cfg = {
context: 'prod',
timezone: 'etc/utc',
entries: [], // only works since this isn't checked
};
const entry1 = {};
//Timestamp in milliseconds: 1652931000000
//Date and time (GMT): Thursday, May 19, 2022 3:30:00 AM
const testTimestampEpochMS = 1652931000000;
const monthOfYear = {
monthOfYearFilter: [5],
};
const matchMonth = cron_util_1.CronUtil.eventMatchesEntry(sEvent, monthOfYear, cfg, testTimestampEpochMS);
expect(matchMonth).toBeTruthy();
}));
});
//# sourceMappingURL=cron-util.spec.js.map