Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Dec 11, 2024
1 parent bceaa05 commit c22ce07
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 108 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
"@ts-bridge/cli": "^0.6.1",
"@types/jest": "^27.5.1",
"@types/lodash": "^4",
"@types/luxon": "^3",
"@types/node": "18.14.2",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^6.21.0",
Expand All @@ -106,7 +105,6 @@
"jest-silent-reporter": "^0.6.0",
"lint-staged": "^12.4.1",
"lodash": "^4.17.21",
"luxon": "^3.5.0",
"minimatch": "^7.4.1",
"prettier": "^2.8.8",
"prettier-plugin-packagejson": "^2.5.2",
Expand Down
201 changes: 200 additions & 1 deletion packages/snaps-controllers/src/cronjob/CronjobController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ describe('CronjobController', () => {
[id]: { id, scheduledAt: expect.any(String), ...backgroundEvent },
});

cronjobController.cancelBackgroundEvent(id);
cronjobController.cancelBackgroundEvent(id, MOCK_SNAP_ID);

jest.advanceTimersByTime(inMilliseconds(1, Duration.Day));

Expand All @@ -334,6 +334,37 @@ describe('CronjobController', () => {
cronjobController.destroy();
});

it('fails to cancel a background event if the caller is not the scheduler', () => {
const rootMessenger = getRootCronjobControllerMessenger();
const controllerMessenger =
getRestrictedCronjobControllerMessenger(rootMessenger);

const cronjobController = new CronjobController({
messenger: controllerMessenger,
});

const backgroundEvent = {
snapId: MOCK_SNAP_ID,
date: '2022-01-01T01:00',
request: {
method: 'handleEvent',
params: ['p1'],
},
};

const id = cronjobController.scheduleBackgroundEvent(backgroundEvent);

expect(cronjobController.state.events).toStrictEqual({
[id]: { id, scheduledAt: expect.any(String), ...backgroundEvent },
});

expect(() => cronjobController.cancelBackgroundEvent(id, 'foo')).toThrow(
'Only the origin that scheduled this event can cancel it',
);

cronjobController.destroy();
});

it("returns a list of a Snap's background events", () => {
const rootMessenger = getRootCronjobControllerMessenger();
const controllerMessenger =
Expand Down Expand Up @@ -808,4 +839,172 @@ describe('CronjobController', () => {
},
);
});

describe('CronjobController actions', () => {
describe('CronjobController:scheduleBackgroundEvent', () => {
it('schedules a background event', () => {
const rootMessenger = getRootCronjobControllerMessenger();
const controllerMessenger =
getRestrictedCronjobControllerMessenger(rootMessenger);

const cronjobController = new CronjobController({
messenger: controllerMessenger,
});

cronjobController.register(MOCK_SNAP_ID);

const id = rootMessenger.call(
'CronjobController:scheduleBackgroundEvent',
{
snapId: MOCK_SNAP_ID,
date: '2022-01-01T01:00',
request: {
method: 'handleExport',
params: ['p1'],
},
},
);

expect(cronjobController.state.events).toStrictEqual({
[id]: {
id,
snapId: MOCK_SNAP_ID,
scheduledAt: expect.any(String),
date: '2022-01-01T01:00',
request: {
method: 'handleExport',
params: ['p1'],
},
},
});

jest.advanceTimersByTime(inMilliseconds(1, Duration.Day));

expect(rootMessenger.call).toHaveBeenCalledWith(
'SnapController:handleRequest',
{
snapId: MOCK_SNAP_ID,
origin: '',
handler: HandlerType.OnCronjob,
request: {
method: 'handleExport',
params: ['p1'],
},
},
);

expect(cronjobController.state.events).toStrictEqual({});

cronjobController.destroy();
});
});

describe('CronjobController:cancelBackgroundEvent', () => {
it('cancels a background event', () => {
const rootMessenger = getRootCronjobControllerMessenger();
const controllerMessenger =
getRestrictedCronjobControllerMessenger(rootMessenger);

const cronjobController = new CronjobController({
messenger: controllerMessenger,
});

cronjobController.register(MOCK_SNAP_ID);

const id = rootMessenger.call(
'CronjobController:scheduleBackgroundEvent',
{
snapId: MOCK_SNAP_ID,
date: '2022-01-01T01:00',
request: {
method: 'handleExport',
params: ['p1'],
},
},
);

expect(cronjobController.state.events).toStrictEqual({
[id]: {
id,
snapId: MOCK_SNAP_ID,
scheduledAt: expect.any(String),
date: '2022-01-01T01:00',
request: {
method: 'handleExport',
params: ['p1'],
},
},
});

rootMessenger.call(
'CronjobController:cancelBackgroundEvent',
id,
MOCK_SNAP_ID,
);

expect(cronjobController.state.events).toStrictEqual({});

cronjobController.destroy();
});
});

describe('CronjobController:getBackgroundEvents', () => {
it("gets a list of a Snap's background events", () => {
const rootMessenger = getRootCronjobControllerMessenger();
const controllerMessenger =
getRestrictedCronjobControllerMessenger(rootMessenger);

const cronjobController = new CronjobController({
messenger: controllerMessenger,
});

cronjobController.register(MOCK_SNAP_ID);

const id = rootMessenger.call(
'CronjobController:scheduleBackgroundEvent',
{
snapId: MOCK_SNAP_ID,
date: '2022-01-01T01:00',
request: {
method: 'handleExport',
params: ['p1'],
},
},
);

expect(cronjobController.state.events).toStrictEqual({
[id]: {
id,
snapId: MOCK_SNAP_ID,
scheduledAt: expect.any(String),
date: '2022-01-01T01:00',
request: {
method: 'handleExport',
params: ['p1'],
},
},
});

const events = rootMessenger.call(
'CronjobController:getBackgroundEvents',
MOCK_SNAP_ID,
);

expect(events).toStrictEqual([
{
id,
snapId: MOCK_SNAP_ID,
scheduledAt: expect.any(String),
date: '2022-01-01T01:00',
request: {
method: 'handleExport',
params: ['p1'],
},
},
]);

cronjobController.destroy();
});
});
});
});
Loading

0 comments on commit c22ce07

Please sign in to comment.