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

[composable-controller] Fix incorrect behavior and improve type-level safeguards #4467

Merged
merged 20 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a6f6de4
Bump `@metamask/utils` from `^9.0.0` to `^9.1.0`
MajorLift Jul 26, 2024
53fe157
Add `@metamask/utils` `^9.1.0` as new dependency
MajorLift Jul 26, 2024
1a65b29
Add jsdoc for types and `ComposableController` class
MajorLift Jul 17, 2024
2021093
Define `ConfigConstraintV1`, `StateConstraintV1` for compatibility wi…
MajorLift Jul 17, 2024
ffc57d5
Redefine `BaseControllerV1Instance` using `PublicInterface` type for …
MajorLift Jul 17, 2024
e044c1a
Add `metadata` property to `BaseControllerInstance` type
MajorLift Jul 17, 2024
e38712e
Add `ControllerName` generic parameter to `RestrictedControllerMessen…
MajorLift Jul 17, 2024
0d60d66
Add property checks to `isBaseController`, `isBaseControllerV1` type …
MajorLift Jul 17, 2024
ab5ab77
Apply `StateConstraintV1` type
MajorLift Jul 17, 2024
b106da3
Make `ChildControllers` generic parameter of `ComposableController` n…
MajorLift Jul 17, 2024
6c13606
Refactor `#updateChildController` method to fix edge case of error br…
MajorLift Jul 17, 2024
a288fca
Linter fixes
MajorLift Jul 17, 2024
3de0d4d
Adapt tests to new `ComposableController` API
MajorLift Jul 17, 2024
8f89b9d
Dedupe lockfile
MajorLift Jul 26, 2024
461355a
Redefine `BaseControllerInstance` using `PublicInterface`
MajorLift Jul 29, 2024
d2d9048
Log controller name when throwing invalid controller error
MajorLift Jul 29, 2024
f59de5e
Attempt to have `messagingSystem` subscribe to `stateChange` event fo…
MajorLift Jul 31, 2024
36f9cf4
Fix failing base-controller validator functions
MajorLift Aug 1, 2024
2ca9038
Implement better state metadata constraint types
MajorLift Aug 1, 2024
9d7efea
Merge branch 'main' into 240620-fix-ComposableController-validator-types
MajorLift Aug 20, 2024
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/composable-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
"@metamask/json-rpc-engine": "^9.0.2",
"@metamask/utils": "^9.1.0",
"@types/jest": "^27.4.1",
"deepmerge": "^4.2.2",
"immer": "^9.0.6",
Expand Down
117 changes: 92 additions & 25 deletions packages/composable-controller/src/ComposableController.test.ts
Cal-L marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import type { Patch } from 'immer';
import * as sinon from 'sinon';

import type { ComposableControllerEvents } from './ComposableController';
import { ComposableController } from './ComposableController';
import type {
ChildControllerStateChangeEvents,
ComposableControllerEvents,
} from './ComposableController';
import {
ComposableController,
INVALID_CONTROLLER_ERROR,
} from './ComposableController';

// Mock BaseController classes

Expand Down Expand Up @@ -130,6 +136,18 @@ class BarController extends BaseControllerV1<never, BarControllerState> {
type BazControllerState = BaseState & {
baz: string;
};
type BazControllerEvent = {
type: `BazController:stateChange`;
payload: [BazControllerState, Patch[]];
};

type BazMessenger = RestrictedControllerMessenger<
'BazController',
never,
BazControllerEvent,
never,
never
>;

class BazController extends BaseControllerV1<never, BazControllerState> {
defaultState = {
Expand All @@ -138,12 +156,30 @@ class BazController extends BaseControllerV1<never, BazControllerState> {

override name = 'BazController' as const;

constructor() {
protected messagingSystem: BazMessenger;

constructor({ messenger }: { messenger: BazMessenger }) {
super();
this.initialize();
this.messagingSystem = messenger;
}
}

type ControllersMap = {
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
FooController: FooController;
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
QuzController: QuzController;
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
BarController: BarController;
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
BazController: BazController;
};

describe('ComposableController', () => {
afterEach(() => {
sinon.restore();
Expand All @@ -159,16 +195,30 @@ describe('ComposableController', () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
BazController: BazControllerState;
};

const composableMessenger = new ControllerMessenger<
never,
ComposableControllerEvents<ComposableControllerState>
| ComposableControllerEvents<ComposableControllerState>
| ChildControllerStateChangeEvents<ComposableControllerState>
>().getRestricted({
name: 'ComposableController',
allowedActions: [],
allowedEvents: [],
allowedEvents: ['BazController:stateChange'],
});
const controller = new ComposableController({
controllers: [new BarController(), new BazController()],
const controller = new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [
new BarController(),
new BazController({
messenger: new ControllerMessenger<never, never>().getRestricted({
name: 'BazController',
allowedActions: [],
allowedEvents: [],
}),
}),
],
messenger: composableMessenger,
});

Expand All @@ -194,7 +244,10 @@ describe('ComposableController', () => {
allowedEvents: [],
});
const barController = new BarController();
new ComposableController({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [barController],
messenger: composableMessenger,
});
Expand Down Expand Up @@ -255,11 +308,13 @@ describe('ComposableController', () => {
'QuzController:stateChange',
],
});
const composableController =
new ComposableController<ComposableControllerState>({
controllers: [fooController, quzController],
messenger: composableControllerMessenger,
});
const composableController = new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [fooController, quzController],
messenger: composableControllerMessenger,
});
expect(composableController.state).toStrictEqual({
FooController: { foo: 'foo' },
QuzController: { quz: 'quz' },
Expand Down Expand Up @@ -288,7 +343,10 @@ describe('ComposableController', () => {
allowedActions: [],
allowedEvents: ['FooController:stateChange'],
});
new ComposableController<ComposableControllerState>({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [fooController],
messenger: composableControllerMessenger,
});
Expand Down Expand Up @@ -336,11 +394,13 @@ describe('ComposableController', () => {
allowedActions: [],
allowedEvents: ['FooController:stateChange'],
});
const composableController =
new ComposableController<ComposableControllerState>({
controllers: [barController, fooController],
messenger: composableControllerMessenger,
});
const composableController = new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [barController, fooController],
messenger: composableControllerMessenger,
});
expect(composableController.state).toStrictEqual({
BarController: { bar: 'bar' },
FooController: { foo: 'foo' },
Expand Down Expand Up @@ -373,7 +433,10 @@ describe('ComposableController', () => {
allowedActions: [],
allowedEvents: ['FooController:stateChange'],
});
new ComposableController<ComposableControllerState>({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [barController, fooController],
messenger: composableControllerMessenger,
});
Expand Down Expand Up @@ -421,7 +484,10 @@ describe('ComposableController', () => {
allowedActions: [],
allowedEvents: ['FooController:stateChange'],
});
new ComposableController<ComposableControllerState>({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [barController, fooController],
messenger: composableControllerMessenger,
});
Expand Down Expand Up @@ -490,14 +556,15 @@ describe('ComposableController', () => {
});
expect(
() =>
new ComposableController({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
// @ts-expect-error - Suppressing type error to test for runtime error handling
controllers: [notController, fooController],
messenger: composableControllerMessenger,
}),
).toThrow(
'Invalid controller: controller must extend from BaseController or BaseControllerV1',
);
).toThrow(INVALID_CONTROLLER_ERROR);
});
});
});
Loading
Loading