Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRunnableSnaps method to SnapController #3049

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 93.06,
"functions": 96.59,
"functions": 96.61,
"lines": 98.08,
"statements": 97.8
}
32 changes: 32 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1842,7 +1842,7 @@
});

// This isn't stable in CI unfortunately
it.skip('throws if the Snap is terminated while executing', async () => {

Check warning on line 1845 in packages/snaps-controllers/src/snaps/SnapController.test.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (@metamask/snaps-controllers)

Disabled test
const { manifest, sourceCode, svgIcon } =
await getMockSnapFilesWithUpdatedChecksum({
sourceCode: `
Expand Down Expand Up @@ -10022,6 +10022,38 @@
});
});

describe('SnapController:getRunnableSnaps', () => {
it('calls SnapController.getRunnableSnaps()', () => {
const messenger = getSnapControllerMessenger();
const mockSnap = getMockSnapData({
id: MOCK_SNAP_ID,
origin: MOCK_ORIGIN,
});
const mockSnap2 = getMockSnapData({
id: `${MOCK_SNAP_ID}2` as SnapId,
origin: MOCK_ORIGIN,
enabled: false,
});

const snapController = getSnapController(
getSnapControllerOptions({
messenger,
state: {
snaps: getPersistedSnapsState(
mockSnap.stateObject,
mockSnap2.stateObject,
),
},
}),
);

const result = messenger.call('SnapController:getRunnableSnaps');
expect(result).toStrictEqual([getTruncatedSnap()]);
david0xd marked this conversation as resolved.
Show resolved Hide resolved

snapController.destroy();
});
});

describe('SnapController:install', () => {
it('calls SnapController.installSnaps()', async () => {
const messenger = getSnapControllerMessenger();
Expand Down
21 changes: 21 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ import type {
} from './registry';
import { SnapsRegistryStatus } from './registry';
import { RequestQueue } from './RequestQueue';
import { getRunnableSnaps } from './selectors';
import { Timer } from './Timer';

export const controllerName = 'SnapController';
Expand Down Expand Up @@ -406,6 +407,11 @@ export type GetAllSnaps = {
handler: SnapController['getAllSnaps'];
};

export type GetRunnableSnaps = {
type: `${typeof controllerName}:getRunnableSnaps`;
handler: SnapController['getRunnableSnaps'];
};

export type StopAllSnaps = {
type: `${typeof controllerName}:stopAllSnaps`;
handler: SnapController['stopAllSnaps'];
Expand Down Expand Up @@ -465,6 +471,7 @@ export type SnapControllerActions =
| GetPermittedSnaps
| InstallSnaps
| GetAllSnaps
| GetRunnableSnaps
| IncrementActiveReferences
| DecrementActiveReferences
| GetRegistryMetadata
Expand Down Expand Up @@ -1150,6 +1157,11 @@ export class SnapController extends BaseController<
(...args) => this.getAllSnaps(...args),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:getRunnableSnaps`,
(...args) => this.getRunnableSnaps(...args),
);

this.messagingSystem.registerActionHandler(
`${controllerName}:incrementActiveReferences`,
(...args) => this.incrementActiveReferences(...args),
Expand Down Expand Up @@ -2387,6 +2399,15 @@ export class SnapController extends BaseController<
return Object.values(this.state.snaps).map(truncateSnap);
}

/**
* Gets all runnable snaps.
*
* @returns All runnable snaps.
*/
getRunnableSnaps(): TruncatedSnap[] {
return getRunnableSnaps(this.getAllSnaps());
}

/**
* Gets the serialized permitted snaps of the given origin, if any.
*
Expand Down
1 change: 1 addition & 0 deletions packages/snaps-controllers/src/test-utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ export const getSnapControllerMessenger = (
'SnapController:disable',
'SnapController:remove',
'SnapController:getAll',
'SnapController:getRunnableSnaps',
'SnapController:getPermitted',
'SnapController:install',
'SnapController:incrementActiveReferences',
Expand Down
Loading