Skip to content

Commit bb64a7a

Browse files
Add tests
1 parent 3b060ec commit bb64a7a

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

packages/snaps-rpc-methods/jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
1010
],
1111
coverageThreshold: {
1212
global: {
13-
branches: 95.37,
13+
branches: 95.7,
1414
functions: 98.75,
15-
lines: 98.92,
16-
statements: 98.62,
15+
lines: 98.99,
16+
statements: 98.69,
1717
},
1818
},
1919
});

packages/snaps-rpc-methods/src/restricted/manageState.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,44 @@ describe('snap_manageState', () => {
393393
true,
394394
);
395395
});
396+
397+
it('throws an error on update if the new state is too large', async () => {
398+
const clearSnapState = jest.fn().mockReturnValueOnce(true);
399+
const getSnapState = jest.fn().mockReturnValueOnce(true);
400+
const updateSnapState = jest.fn().mockReturnValueOnce(true);
401+
const getSnap = jest.fn().mockReturnValue({ preinstalled: false });
402+
403+
const manageStateImplementation = getManageStateImplementation({
404+
clearSnapState,
405+
getSnapState,
406+
updateSnapState,
407+
getUnlockPromise: jest.fn(),
408+
getSnap,
409+
});
410+
411+
const newState = {
412+
foo: 'foo'.repeat(21_500_000),
413+
};
414+
415+
await expect(
416+
manageStateImplementation({
417+
context: { origin: 'snap-origin' },
418+
method: 'snap_manageState',
419+
params: {
420+
operation: ManageStateOperation.UpdateState,
421+
newState,
422+
},
423+
}),
424+
).rejects.toThrow(
425+
'Invalid snap_manageState "newState" parameter: The new state must not exceed 64 MB in size.',
426+
);
427+
428+
expect(updateSnapState).not.toHaveBeenCalledWith(
429+
'snap-origin',
430+
newState,
431+
true,
432+
);
433+
});
396434
});
397435

398436
describe('getValidatedParams', () => {

packages/snaps-rpc-methods/src/restricted/manageState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export function getManageStateImplementation({
234234
return null;
235235
}
236236

237+
/* istanbul ignore next */
237238
default:
238239
throw rpcErrors.invalidParams(
239240
`Invalid ${method} operation: "${

packages/snaps-utils/src/json.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ describe('getJsonSizeUnsafe', () => {
1313
const input = { foo: 'bar' };
1414
expect(getJsonSizeUnsafe(input)).toBe(13);
1515
});
16+
17+
it('calculates the size of the JSON input in bytes', () => {
18+
const input = { foo: 'bar€' };
19+
expect(getJsonSizeUnsafe(input, true)).toBe(16);
20+
});
1621
});

0 commit comments

Comments
 (0)