Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/examples/packages/client-status/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@metamask/auto-changelog": "^5.0.2",
"@metamask/snaps-cli": "workspace:^",
"@metamask/snaps-jest": "workspace:^",
"@metamask/snaps-utils": "workspace:^",
"@swc/core": "1.11.31",
"@swc/jest": "^0.2.38",
"@types/node": "18.14.2",
Expand Down
3 changes: 3 additions & 0 deletions packages/examples/packages/client-status/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@jest/globals';
import { installSnap } from '@metamask/snaps-jest';
import { getPlatformVersion } from '@metamask/snaps-utils';

describe('onRpcRequest', () => {
it('throws an error if the requested method does not exist', async () => {
Expand Down Expand Up @@ -31,6 +32,8 @@ describe('onRpcRequest', () => {
expect(response).toRespondWith({
locked: false,
active: true,
clientVersion: '13.6.0-flask.0',
platformVersion: getPlatformVersion(),
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import type { GetClientStatusResult } from '@metamask/snaps-sdk';
import { getPlatformVersion } from '@metamask/snaps-utils';
import type { PendingJsonRpcResponse } from '@metamask/utils';

import { getClientStatusHandler } from './getClientStatus';
Expand All @@ -24,9 +25,11 @@ describe('snap_getClientStatus', () => {

const getIsLocked = jest.fn().mockReturnValue(true);
const getIsActive = jest.fn().mockReturnValue(false);
const getVersion = jest.fn().mockReturnValue('13.6.0-flask.0');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Test Expectation Mismatch for Hook Names

The test assertion for hookNames is missing the getVersion: true property that was added to the implementation. The test expects hookNames to only contain getIsLocked and getIsActive, but the implementation now includes getVersion as well (added at line 16 in getClientStatus.ts). This will cause the test to fail because the actual object has three properties while the test only expects two.

Fix in Cursor Fix in Web

const hooks = {
getIsLocked,
getIsActive,
getVersion,
};

const engine = new JsonRpcEngine();
Expand All @@ -51,7 +54,12 @@ describe('snap_getClientStatus', () => {
expect(response).toStrictEqual({
jsonrpc: '2.0',
id: 1,
result: { locked: true, active: false },
result: {
clientVersion: '13.6.0-flask.0',
platformVersion: getPlatformVersion(),
locked: true,
active: false,
},
});
});
});
Expand Down
17 changes: 15 additions & 2 deletions packages/snaps-rpc-methods/src/permitted/getClientStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';
import type { PermittedHandlerExport } from '@metamask/permission-controller';
import type { GetClientStatusResult } from '@metamask/snaps-sdk';
import { getPlatformVersion } from '@metamask/snaps-utils';
import type {
JsonRpcParams,
JsonRpcRequest,
Expand All @@ -12,6 +13,7 @@ import type { MethodHooksObject } from '../utils';
const hookNames: MethodHooksObject<GetClientStatusHooks> = {
getIsLocked: true,
getIsActive: true,
getVersion: true,
};

/**
Expand All @@ -37,6 +39,11 @@ export type GetClientStatusHooks = {
* @returns Whether the client is active or not.
*/
getIsActive: () => boolean;

/**
* @returns The version string for the client.
*/
getVersion: () => string;
};

/**
Expand All @@ -51,15 +58,21 @@ export type GetClientStatusHooks = {
* @param hooks - The RPC method hooks.
* @param hooks.getIsLocked - A function that returns whether the client is locked or not.
* @param hooks.getIsActive - A function that returns whether the client is opened or not.
* @param hooks.getVersion - A function that returns the client version.
* @returns Nothing.
*/
async function getClientStatusImplementation(
_request: JsonRpcRequest,
response: PendingJsonRpcResponse<GetClientStatusResult>,
_next: unknown,
end: JsonRpcEngineEndCallback,
{ getIsLocked, getIsActive }: GetClientStatusHooks,
{ getIsLocked, getIsActive, getVersion }: GetClientStatusHooks,
): Promise<void> {
response.result = { locked: getIsLocked(), active: getIsActive() };
response.result = {
locked: getIsLocked(),
active: getIsActive(),
clientVersion: getVersion(),
platformVersion: getPlatformVersion(),
};
return end();
}
7 changes: 6 additions & 1 deletion packages/snaps-sdk/src/types/methods/get-client-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ export type GetClientStatusParams = never;
*
* It returns an object containing useful information about the client.
*/
export type GetClientStatusResult = { locked: boolean; active: boolean };
export type GetClientStatusResult = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a good opportunity to document the parameters now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clientVersion: string;
platformVersion: string;
locked: boolean;
active: boolean;
};
23 changes: 23 additions & 0 deletions packages/snaps-simulation/src/simulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,29 @@ describe('getPermittedHooks', () => {
await close();
});

it('returns the `getVersion` hook', async () => {
const { snapId, close } = await getMockServer({
manifest: getSnapManifest(),
});

const location = detectSnapLocation(snapId, {
allowLocal: true,
});

const snapFiles = await fetchSnap(snapId, location);

const { getVersion } = getPermittedHooks(
snapId,
snapFiles,
controllerMessenger,
runSaga,
);

expect(getVersion()).toBe('13.6.0-flask.0');

await close();
});

it('returns the `getSnapFile` hook', async () => {
const value = JSON.stringify({ bar: 'baz' });
const { snapId, close } = await getMockServer({
Expand Down
8 changes: 8 additions & 0 deletions packages/snaps-simulation/src/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ export type PermittedMiddlewareHooks = {
*/
getIsActive: () => boolean;

/**
* A hook that returns the client version.
*
* @returns A string that corresponds to the client version.
*/
getVersion: () => string;

/**
* A hook that returns the Snap's auxiliary file for the given path. This hook
* is bound to the Snap ID.
Expand Down Expand Up @@ -486,6 +493,7 @@ export function getPermittedHooks(
getUnlockPromise: asyncResolve(),
getIsLocked: () => false,
getIsActive: () => true,
getVersion: () => '13.6.0-flask.0',

getSnapFile: async (path: string, encoding: AuxiliaryFileEncoding) =>
await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2971,6 +2971,7 @@ __metadata:
"@metamask/snaps-cli": "workspace:^"
"@metamask/snaps-jest": "workspace:^"
"@metamask/snaps-sdk": "workspace:^"
"@metamask/snaps-utils": "workspace:^"
"@swc/core": "npm:1.11.31"
"@swc/jest": "npm:^0.2.38"
"@types/node": "npm:18.14.2"
Expand Down
Loading