@bitblit/epsilon
Version:
Tiny adapter to simplify building API gateway Lambda APIS
107 lines • 5.63 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const background_handler_1 = require("../background/background-handler");
const jest_1 = require("@bitblit/ratchet/jest");
const cron_epsilon_lambda_event_handler_1 = require("./cron-epsilon-lambda-event-handler");
const single_thread_local_background_manager_1 = require("../background/manager/single-thread-local-background-manager");
const globals_1 = require("@jest/globals");
const luxon_1 = require("luxon");
// jest.mock('@bitblit/background');
describe('#cronEpsilonLambdaEventHandler', function () {
let mockSqs;
let mockSns;
beforeEach(() => {
mockSqs = jest_1.JestRatchet.mock(globals_1.jest.fn);
mockSns = jest_1.JestRatchet.mock(globals_1.jest.fn);
});
// CAW 2021-03-10 : Disabling for now since jest mock not working when run in batch from command line...unclear why
xit('should verify that cron data functions get executed', () => __awaiter(this, void 0, void 0, function* () {
// Logger.setLevel(LoggerLevelName.silly);
const evt = {
id: '1',
version: '1',
account: 'test',
time: 'test',
region: '',
resources: ['test'],
source: null,
detail: {},
'detail-type': null,
};
const cronConfig = {
timezone: 'America/Los_Angeles',
context: 'Test',
entries: [
{
backgroundTaskType: 'test',
fireImmediate: true,
data: () => {
return { curDate: new Date().toISOString(), fixed: 'abc' };
},
},
],
};
const smConfig = {
processors: [],
httpSubmissionPath: '/background/',
implyTypeFromPathSuffix: true,
httpMetaEndpoint: '/background-meta',
};
const background = new background_handler_1.BackgroundHandler(null, null);
background.getConfig = globals_1.jest.fn(() => smConfig);
const backgroundManager = new single_thread_local_background_manager_1.SingleThreadLocalBackgroundManager();
const res = yield cron_epsilon_lambda_event_handler_1.CronEpsilonLambdaEventHandler.processCronEvent(evt, cronConfig, backgroundManager, background);
expect(res).toBeTruthy();
}), 500);
});
describe('cronEpsilonLambdaEventHandler.getCronTimeToUse', () => {
const currentTimestampEpochMS = new Date().getTime();
const sampleEvent = {
version: '0',
id: '403a3159-cf4d-4cbe-315d-ed688c429988',
'detail-type': 'Scheduled Event',
source: 'aws.events',
account: '000011112222',
time: '2024-08-30T13:16:24Z',
region: 'us-east-1',
resources: ['arn:aws:events:us-east-1:000011112222:rule/MyRule1'],
detail: {},
};
// @ts-expect-error private method
const getCronTimeToUse = cron_epsilon_lambda_event_handler_1.CronEpsilonLambdaEventHandler.getCronTimeToUse;
it('should return current timestamp when evt is undefined', () => {
const result = getCronTimeToUse(undefined, currentTimestampEpochMS);
expect(result).toBe(currentTimestampEpochMS);
});
it('should return current timestamp when evt.time is undefined', () => {
const result = getCronTimeToUse({}, currentTimestampEpochMS);
expect(result).toBe(currentTimestampEpochMS);
});
it('should return event time in milliseconds when evt.time is a valid ISO string', () => {
const recentISOTime = luxon_1.DateTime.fromMillis(currentTimestampEpochMS).toUTC().plus({ seconds: 30 }).toISO();
const eventTimeInMillis = luxon_1.DateTime.fromISO(recentISOTime).toMillis();
const result = getCronTimeToUse(Object.assign(Object.assign({}, sampleEvent), { time: recentISOTime }), currentTimestampEpochMS);
expect(result).toBe(eventTimeInMillis);
});
it('should return current timestamp when evt.time is an invalid ISO string', () => {
const invalidISOTime = 'invalid-time';
const result = getCronTimeToUse(Object.assign(Object.assign({}, sampleEvent), { time: invalidISOTime }), currentTimestampEpochMS);
expect(result).toBe(currentTimestampEpochMS);
});
it('should return current timestamp when time difference exceeds threshold', () => {
const oldISOTime = '2023-10-10T10:00:00.000Z';
const largeTimeDifference = currentTimestampEpochMS + (cron_epsilon_lambda_event_handler_1.CronEpsilonLambdaEventHandler.CRON_EVENT_TIMESTAMP_MISMATCH_MAX_THRESHOLD_MINUTES + 1) * 60 * 1000;
const result = getCronTimeToUse(Object.assign(Object.assign({}, sampleEvent), { time: oldISOTime }), largeTimeDifference);
expect(result).toBe(largeTimeDifference);
});
});
//# sourceMappingURL=cron-epsilon-lambda-event-handler.spec.js.map