@squiz/optimization-utils 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,308 +0,0 @@
1
- import { Container } from 'inversify';
2
- import {
3
- CreateScheduleCommand,
4
- CreateScheduleCommandInput,
5
- DeleteScheduleCommand,
6
- DeleteScheduleCommandInput,
7
- GetScheduleCommand,
8
- GetScheduleCommandInput,
9
- ResourceNotFoundException,
10
- SchedulerClient,
11
- } from '@aws-sdk/client-scheduler';
12
- import { randomUUID } from 'crypto';
13
- import { faker } from '@faker-js/faker';
14
- import { ExceptionOptionType } from '@smithy/smithy-client/dist-types/exceptions';
15
- import { SchedulerServiceException } from '@aws-sdk/client-scheduler/dist-types/models/SchedulerServiceException';
16
- import { Logger, createLoggerMock } from '@squiz/optimization-logger';
17
- import { createSchedulerClientMock } from '../../testing/mock';
18
- import { eventToDynamoDB } from '../../event/DynamoDBEventMapper';
19
- import { Scheduler } from '../Scheduler';
20
- import {
21
- EventBridgeScheduler,
22
- EventBridgeSchedulerConfig,
23
- } from '../EventBridgeScheduler';
24
- import { DomainEvent } from '../../event/DomainEvent';
25
-
26
- jest.mock('@aws-sdk/client-scheduler', () => {
27
- return {
28
- ...jest.requireActual('@aws-sdk/client-scheduler'),
29
- CreateScheduleCommand: jest.fn(),
30
- GetScheduleCommand: jest.fn(),
31
- DeleteScheduleCommand: jest.fn(),
32
- };
33
- });
34
-
35
- class ExampleEvent implements DomainEvent {
36
- readonly detail: Record<string, unknown> = {};
37
- readonly eventId: string = randomUUID();
38
- readonly name: string = ExampleEvent.name;
39
- readonly time: Date = new Date();
40
- readonly version: string = '1';
41
- readonly entityId = 'uuid';
42
- }
43
-
44
- describe('EventBridgeScheduler', () => {
45
- let scheduler: Scheduler;
46
- let schedulerClient: SchedulerClient;
47
- let event: ExampleEvent;
48
- const config: EventBridgeSchedulerConfig = {
49
- eventTableName: 'event-table',
50
- groupName: 'experiment',
51
- region: 'eu-north-1',
52
- schedulerRole: 'example-arn',
53
- };
54
-
55
- beforeEach(() => {
56
- jest.resetAllMocks();
57
-
58
- const container = new Container({ defaultScope: 'Singleton' });
59
-
60
- container.bind(Scheduler).toDynamicValue(({ container: c }) => {
61
- return new EventBridgeScheduler(
62
- config,
63
- c.get(SchedulerClient),
64
- c.get(Logger),
65
- );
66
- });
67
- container
68
- .bind(SchedulerClient)
69
- .toConstantValue(createSchedulerClientMock());
70
- container.bind(Logger).toConstantValue(createLoggerMock());
71
-
72
- event = new ExampleEvent();
73
- schedulerClient = container.get(SchedulerClient);
74
- scheduler = container.get(Scheduler);
75
- });
76
-
77
- describe('schedule', () => {
78
- it('should call the CreateScheduleCommand with the PutItemCommandInput', async () => {
79
- await scheduler.schedule(event, {
80
- type: 'at',
81
- value: faker.date.future(),
82
- name: randomUUID(),
83
- });
84
-
85
- expect(schedulerClient.send).toHaveBeenCalledWith(
86
- expect.any(CreateScheduleCommand),
87
- );
88
- expect(CreateScheduleCommand).toHaveBeenCalledWith(
89
- expect.objectContaining({
90
- Target: {
91
- Arn: 'arn:aws:scheduler:::aws-sdk:dynamodb:putItem',
92
- Input: JSON.stringify({
93
- TableName: config.eventTableName,
94
- Item: {
95
- ...eventToDynamoDB(event),
96
- eventId: {
97
- S: '<aws.scheduler.execution-id>',
98
- },
99
- time: {
100
- S: '<aws.scheduler.scheduled-time>',
101
- },
102
- },
103
- }),
104
- RoleArn: config.schedulerRole,
105
- },
106
- } as CreateScheduleCommandInput),
107
- );
108
- });
109
-
110
- it('should schedule with the "at" schedule expression', async () => {
111
- await scheduler.schedule(event, {
112
- type: 'at',
113
- value: new Date('2023-01-01T00:00:00.000Z'),
114
- name: randomUUID(),
115
- });
116
-
117
- expect(schedulerClient.send).toHaveBeenCalledWith(
118
- expect.any(CreateScheduleCommand),
119
- );
120
- expect(CreateScheduleCommand).toHaveBeenCalledWith(
121
- expect.objectContaining({
122
- ScheduleExpression: 'at(2023-01-01T00:00:00)',
123
- ScheduleExpressionTimezone: 'UTC',
124
- } as CreateScheduleCommandInput),
125
- );
126
- });
127
-
128
- it('should schedule with the "rate" schedule expression', async () => {
129
- await scheduler.schedule(event, {
130
- type: 'rate',
131
- value: 15,
132
- unit: 'days',
133
- name: randomUUID(),
134
- });
135
-
136
- expect(schedulerClient.send).toHaveBeenCalledWith(
137
- expect.any(CreateScheduleCommand),
138
- );
139
- expect(CreateScheduleCommand).toHaveBeenCalledWith(
140
- expect.objectContaining({
141
- ScheduleExpression: 'rate(15 days)',
142
- ScheduleExpressionTimezone: 'UTC',
143
- } as CreateScheduleCommandInput),
144
- );
145
- });
146
-
147
- it('should schedule with the "cron" schedule expression', async () => {
148
- await scheduler.schedule(event, {
149
- type: 'cron',
150
- value: '* * * * ? *',
151
- name: randomUUID(),
152
- });
153
-
154
- expect(schedulerClient.send).toHaveBeenCalledWith(
155
- expect.any(CreateScheduleCommand),
156
- );
157
- expect(CreateScheduleCommand).toHaveBeenCalledWith(
158
- expect.objectContaining({
159
- ScheduleExpression: 'cron(* * * * ? *)',
160
- ScheduleExpressionTimezone: 'UTC',
161
- } as CreateScheduleCommandInput),
162
- );
163
- });
164
-
165
- it('should create a schedule name which includes the event name', async () => {
166
- await scheduler.schedule(event, {
167
- type: 'cron',
168
- value: '* * * * ? *',
169
- name: 'ExampleSchedule',
170
- });
171
-
172
- expect(schedulerClient.send).toHaveBeenCalledWith(
173
- expect.any(CreateScheduleCommand),
174
- );
175
- expect(CreateScheduleCommand).toHaveBeenCalledWith(
176
- expect.objectContaining({
177
- Name: 'ExampleSchedule',
178
- } as CreateScheduleCommandInput),
179
- );
180
- });
181
-
182
- it('should trim end if the name contains more than 64 characters', async () => {
183
- await scheduler.schedule(event, {
184
- type: 'cron',
185
- value: '* * * * ? *',
186
- name: 'pVlH%46KTS).@KFhaumnG!#>dcZ#FTh=yBwD$rwDz@s41BO5C=?>gr9J.#[)!T^4Y',
187
- });
188
-
189
- expect(schedulerClient.send).toHaveBeenCalledWith(
190
- expect.any(CreateScheduleCommand),
191
- );
192
- expect(CreateScheduleCommand).toHaveBeenCalledWith(
193
- expect.objectContaining({
194
- Name: 'pVlH%46KTS).@KFhaumnG!#>dcZ#FTh=yBwD$rwDz@s41BO5C=?>gr9J.#[)!T^4',
195
- } as CreateScheduleCommandInput),
196
- );
197
- });
198
-
199
- it('should call CreateScheduleCommand with correct params', async () => {
200
- await scheduler.schedule(event, {
201
- type: 'cron',
202
- value: '* * * * ? *',
203
- name: randomUUID(),
204
- });
205
-
206
- expect(schedulerClient.send).toHaveBeenCalledWith(
207
- expect.any(CreateScheduleCommand),
208
- );
209
- expect(CreateScheduleCommand).toHaveBeenCalledWith(
210
- expect.objectContaining({
211
- GroupName: config.groupName,
212
- State: 'ENABLED',
213
- FlexibleTimeWindow: {
214
- Mode: 'OFF',
215
- },
216
- ActionAfterCompletion: 'DELETE',
217
- ClientToken: expect.any(String),
218
- } as CreateScheduleCommandInput),
219
- );
220
- });
221
-
222
- it('should remove the existing schedule if it exists', async () => {
223
- const scheduleName = randomUUID();
224
-
225
- await scheduler.schedule(event, {
226
- type: 'at',
227
- value: faker.date.future(),
228
- name: scheduleName,
229
- });
230
-
231
- expect(GetScheduleCommand).toHaveBeenCalledWith({
232
- GroupName: config.groupName,
233
- Name: scheduleName,
234
- } as GetScheduleCommandInput);
235
- expect(DeleteScheduleCommand).toHaveBeenCalledWith({
236
- GroupName: config.groupName,
237
- Name: scheduleName,
238
- } as DeleteScheduleCommandInput);
239
- });
240
- });
241
-
242
- describe('deleteSchedule', () => {
243
- it('should delete schedule if it exists', async () => {
244
- const scheduleName = 'example-schedule';
245
-
246
- await scheduler.deleteSchedule(scheduleName);
247
-
248
- expect(GetScheduleCommand).toHaveBeenCalledWith({
249
- GroupName: config.groupName,
250
- Name: scheduleName,
251
- } as GetScheduleCommandInput);
252
- expect(DeleteScheduleCommand).toHaveBeenCalledWith({
253
- GroupName: config.groupName,
254
- Name: scheduleName,
255
- } as DeleteScheduleCommandInput);
256
- });
257
-
258
- it('should skip deleting schedule if it does not exist', async () => {
259
- (schedulerClient.send as jest.Mock).mockRejectedValueOnce(
260
- new ResourceNotFoundException(
261
- {} as ExceptionOptionType<
262
- ResourceNotFoundException,
263
- SchedulerServiceException
264
- >,
265
- ),
266
- );
267
- const scheduleName = randomUUID();
268
-
269
- await scheduler.schedule(event, {
270
- type: 'at',
271
- value: faker.date.future(),
272
- name: scheduleName,
273
- });
274
-
275
- expect(GetScheduleCommand).toHaveBeenCalledTimes(1);
276
- expect(DeleteScheduleCommand).toHaveBeenCalledTimes(0);
277
- });
278
-
279
- it('should rethrow exception if the GetScheduleCommand return exception other than ResourceNotFound', async () => {
280
- (schedulerClient.send as jest.Mock).mockRejectedValueOnce(new Error());
281
- const scheduleName = randomUUID();
282
-
283
- await expect(() =>
284
- scheduler.deleteSchedule(scheduleName),
285
- ).rejects.toThrow();
286
- });
287
-
288
- it('should trim the scheduleName up to 64 characters', async () => {
289
- const scheduleNameWith65Characters =
290
- 'pVlH%46KTS).@KFhaumnG!#>dcZ#FTh=yBwD$rwDz@s41BO5C=?>gr9J.#[)!T^4Y';
291
- const expectedScheduleName =
292
- 'pVlH%46KTS).@KFhaumnG!#>dcZ#FTh=yBwD$rwDz@s41BO5C=?>gr9J.#[)!T^4';
293
-
294
- await scheduler.deleteSchedule(scheduleNameWith65Characters);
295
-
296
- expect(GetScheduleCommand).toHaveBeenCalledWith(
297
- expect.objectContaining({
298
- Name: expectedScheduleName,
299
- }),
300
- );
301
- expect(DeleteScheduleCommand).toHaveBeenCalledWith(
302
- expect.objectContaining({
303
- Name: expectedScheduleName,
304
- }),
305
- );
306
- });
307
- });
308
- });
@@ -1,15 +0,0 @@
1
- import { SchedulerClient } from '@aws-sdk/client-scheduler';
2
- import { Scheduler } from '../scheduler/Scheduler';
3
-
4
- export const createSchedulerClientMock = (): SchedulerClient => {
5
- return {
6
- send: jest.fn(),
7
- } as unknown as SchedulerClient;
8
- };
9
-
10
- export const createSchedulerMock = (): Scheduler => {
11
- return {
12
- schedule: jest.fn(),
13
- deleteSchedule: jest.fn(),
14
- };
15
- };
@@ -1,17 +0,0 @@
1
- import { AttributeValue } from '@aws-sdk/client-dynamodb';
2
-
3
- export type MarshalledResult<T> = {
4
- [K in keyof T]: T[K] extends string
5
- ? AttributeValue.SMember
6
- : T[K] extends number
7
- ? AttributeValue.NMember
8
- : T[K] extends Record<string, unknown>
9
- ? AttributeValue.MMember
10
- : T[K] extends Array<unknown>
11
- ? AttributeValue.LMember
12
- : T[K] extends boolean
13
- ? AttributeValue.BOOLMember
14
- : T[K] extends undefined
15
- ? Partial<T[K]>
16
- : never;
17
- };
@@ -1,11 +0,0 @@
1
- type MethodKeysOrNever<T> = {
2
- [K in keyof T]: T[K] extends Function ? K : never;
3
- };
4
- export type MethodKeys<T> = MethodKeysOrNever<T>[keyof T];
5
- export type ClassFields<T> = Omit<T, Exclude<MethodKeys<T>, undefined>>;
6
- export type OverwriteValueOf<TInstance, DValueOfReturn> = Omit<
7
- TInstance,
8
- 'valueOf'
9
- > & {
10
- valueOf(): DValueOfReturn;
11
- };
package/tsconfig.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "extends": "@tsconfig/node20/tsconfig.json",
3
- "compilerOptions": {
4
- "experimentalDecorators": true,
5
- "emitDecoratorMetadata": true,
6
- "sourceMap": true,
7
- "declaration": true,
8
- "outDir": "dist",
9
- "baseUrl": "."
10
- },
11
- "include": ["src"],
12
- "exclude": ["dist", "**/*.spec.ts"]
13
- }