-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
Feat/add app metadata controller #5325
Draft
frankvonhoven
wants to merge
5
commits into
main
Choose a base branch
from
feat/add-app-metadata-controller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b34b4ff
Init app metadata controller & tests
frankvonhoven 24a23c1
Add package json
frankvonhoven fc71cbb
Update the module and tests passing
frankvonhoven f835b90
Merge branch 'main' into feat/add-app-metadata-controller
frankvonhoven c68377f
Merge branch 'main' into feat/add-app-metadata-controller
frankvonhoven File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 MetaMask | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# `@metamask/app-metadata-controller` | ||
|
||
Manages the Metadata for the App | ||
|
||
## Installation | ||
|
||
`yarn add @metamask/app-metadata-controller` | ||
|
||
or | ||
|
||
`npm install @metamask/app-metadata-controller` | ||
|
||
## Contributing | ||
|
||
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* For a detailed explanation regarding each configuration property and type check, visit: | ||
* https://jestjs.io/docs/configuration | ||
*/ | ||
|
||
const merge = require('deepmerge'); | ||
const path = require('path'); | ||
|
||
const baseConfig = require('../../jest.config.packages'); | ||
|
||
const displayName = path.basename(__dirname); | ||
|
||
module.exports = merge(baseConfig, { | ||
// The display name when running multiple projects | ||
displayName, | ||
|
||
// An object that configures minimum threshold enforcement for coverage results | ||
coverageThreshold: { | ||
global: { | ||
branches: 100, | ||
functions: 100, | ||
lines: 100, | ||
statements: 100, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,73 @@ | ||||||||||||
{ | ||||||||||||
"name": "@metamask/app-metadata-controller", | ||||||||||||
"version": "1.0.0", | ||||||||||||
"description": "Manages requests that for app metadata", | ||||||||||||
"keywords": [ | ||||||||||||
"MetaMask", | ||||||||||||
"Ethereum" | ||||||||||||
], | ||||||||||||
"homepage": "https://github.com/MetaMask/core/tree/main/packages/app-metadata-controller#readme", | ||||||||||||
"bugs": { | ||||||||||||
"url": "https://github.com/MetaMask/core/issues" | ||||||||||||
}, | ||||||||||||
"repository": { | ||||||||||||
"type": "git", | ||||||||||||
"url": "https://github.com/MetaMask/core.git" | ||||||||||||
}, | ||||||||||||
"license": "MIT", | ||||||||||||
"sideEffects": false, | ||||||||||||
"exports": { | ||||||||||||
".": { | ||||||||||||
"import": { | ||||||||||||
"types": "./dist/index.d.mts", | ||||||||||||
"default": "./dist/index.mjs" | ||||||||||||
}, | ||||||||||||
"require": { | ||||||||||||
"types": "./dist/index.d.cts", | ||||||||||||
"default": "./dist/index.cjs" | ||||||||||||
} | ||||||||||||
}, | ||||||||||||
"./package.json": "./package.json" | ||||||||||||
}, | ||||||||||||
"main": "./dist/index.cjs", | ||||||||||||
"types": "./dist/index.d.cts", | ||||||||||||
"files": [ | ||||||||||||
"dist/" | ||||||||||||
], | ||||||||||||
"scripts": { | ||||||||||||
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references", | ||||||||||||
"build:docs": "typedoc", | ||||||||||||
"changelog:update": "../../scripts/update-changelog.sh @metamask/app-metadata-controller", | ||||||||||||
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/app-metadata-controller", | ||||||||||||
"publish:preview": "yarn npm publish --tag preview", | ||||||||||||
"since-latest-release": "../../scripts/since-latest-release.sh", | ||||||||||||
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", | ||||||||||||
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache", | ||||||||||||
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose", | ||||||||||||
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch" | ||||||||||||
}, | ||||||||||||
"dependencies": { | ||||||||||||
"@metamask/base-controller": "^8.0.0", | ||||||||||||
"@metamask/rpc-errors": "^7.0.2", | ||||||||||||
"@metamask/utils": "^11.1.0", | ||||||||||||
"nanoid": "^3.3.8" | ||||||||||||
Comment on lines
+50
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these dependencies used?
Suggested change
|
||||||||||||
}, | ||||||||||||
"devDependencies": { | ||||||||||||
"@metamask/auto-changelog": "^3.4.4", | ||||||||||||
"@types/jest": "^27.4.1", | ||||||||||||
"deepmerge": "^4.2.2", | ||||||||||||
"jest": "^27.5.1", | ||||||||||||
"sinon": "^9.2.4", | ||||||||||||
"ts-jest": "^27.1.4", | ||||||||||||
"typedoc": "^0.24.8", | ||||||||||||
"typedoc-plugin-missing-exports": "^2.0.0", | ||||||||||||
"typescript": "~5.2.2" | ||||||||||||
}, | ||||||||||||
"engines": { | ||||||||||||
"node": "^18.18 || >=20" | ||||||||||||
}, | ||||||||||||
"publishConfig": { | ||||||||||||
"access": "public", | ||||||||||||
"registry": "https://registry.npmjs.org/" | ||||||||||||
} | ||||||||||||
} |
154 changes: 154 additions & 0 deletions
154
packages/app-metadata-controller/src/AppMetadataController.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { Messenger } from '@metamask/base-controller'; | ||
import AppMetadataController, { | ||
getDefaultAppMetadataControllerState, | ||
type AppMetadataControllerOptions, | ||
} from './AppMetadataController'; | ||
|
||
describe('AppMetadataController', () => { | ||
describe('constructor', () => { | ||
it('accepts initial state and does not modify it if currentMigrationVersion and platform.getVersion() match respective values in state', async () => { | ||
const initState = { | ||
currentAppVersion: '1', | ||
previousAppVersion: '1', | ||
previousMigrationVersion: 1, | ||
currentMigrationVersion: 1, | ||
}; | ||
withController( | ||
{ | ||
state: initState, | ||
currentMigrationVersion: 1, | ||
currentAppVersion: '1', | ||
}, | ||
({ controller }) => { | ||
expect(controller.state).toStrictEqual(initState); | ||
}, | ||
); | ||
}); | ||
|
||
it('sets default state and does not modify it', () => { | ||
withController({ state: {} }, ({ controller }) => { | ||
expect(controller.state).toStrictEqual( | ||
getDefaultAppMetadataControllerState(), | ||
); | ||
}); | ||
}); | ||
|
||
it('sets default state and does not modify it if options version parameters match respective default values', () => { | ||
withController( | ||
{ | ||
state: {}, | ||
currentMigrationVersion: 0, | ||
currentAppVersion: '', | ||
}, | ||
({ controller }) => { | ||
expect(controller.state).toStrictEqual( | ||
getDefaultAppMetadataControllerState(), | ||
); | ||
}, | ||
); | ||
}); | ||
|
||
it('updates the currentAppVersion state property if options.currentAppVersion does not match the default value', () => { | ||
withController( | ||
{ | ||
state: {}, | ||
currentMigrationVersion: 0, | ||
currentAppVersion: '1', | ||
}, | ||
({ controller }) => { | ||
expect(controller.state).toStrictEqual({ | ||
...getDefaultAppMetadataControllerState(), | ||
currentAppVersion: '1', | ||
}); | ||
}, | ||
); | ||
}); | ||
|
||
it('updates the currentAppVersion and previousAppVersion state properties if options.currentAppVersion, currentAppVersion and previousAppVersion are all different', () => { | ||
withController( | ||
{ | ||
state: { | ||
currentAppVersion: '2', | ||
previousAppVersion: '1', | ||
}, | ||
currentAppVersion: '3', | ||
currentMigrationVersion: 0, | ||
}, | ||
({ controller }) => { | ||
expect(controller.state).toStrictEqual({ | ||
...getDefaultAppMetadataControllerState(), | ||
currentAppVersion: '3', | ||
previousAppVersion: '2', | ||
}); | ||
}, | ||
); | ||
}); | ||
|
||
it('updates the currentMigrationVersion state property if the currentMigrationVersion param does not match the default value', () => { | ||
withController( | ||
{ | ||
state: {}, | ||
currentMigrationVersion: 1, | ||
}, | ||
({ controller }) => { | ||
expect(controller.state).toStrictEqual({ | ||
...getDefaultAppMetadataControllerState(), | ||
currentMigrationVersion: 1, | ||
}); | ||
}, | ||
); | ||
}); | ||
|
||
it('updates the currentMigrationVersion and previousMigrationVersion state properties if the currentMigrationVersion param, the currentMigrationVersion state property and the previousMigrationVersion state property are all different', () => { | ||
withController( | ||
{ | ||
state: { | ||
currentMigrationVersion: 2, | ||
previousMigrationVersion: 1, | ||
}, | ||
currentMigrationVersion: 3, | ||
}, | ||
({ controller }) => { | ||
expect(controller.state).toStrictEqual({ | ||
...getDefaultAppMetadataControllerState(), | ||
currentMigrationVersion: 3, | ||
previousMigrationVersion: 2, | ||
}); | ||
}, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
type WithControllerOptions = Partial<AppMetadataControllerOptions>; | ||
|
||
type WithControllerCallback<ReturnValue> = ({ | ||
controller, | ||
}: { | ||
controller: AppMetadataController; | ||
}) => ReturnValue; | ||
|
||
type WithControllerArgs<ReturnValue> = | ||
| [WithControllerCallback<ReturnValue>] | ||
| [WithControllerOptions, WithControllerCallback<ReturnValue>]; | ||
|
||
function withController<ReturnValue>( | ||
...args: WithControllerArgs<ReturnValue> | ||
): ReturnValue { | ||
const [options = {}, fn] = args.length === 2 ? args : [{}, args[0]]; | ||
|
||
const messenger = new Messenger<never, never>(); | ||
|
||
const appMetadataControllerMessenger = messenger.getRestricted({ | ||
name: 'AppMetadataController', | ||
allowedActions: [], | ||
allowedEvents: [], | ||
}); | ||
|
||
return fn({ | ||
controller: new AppMetadataController({ | ||
messenger: appMetadataControllerMessenger, | ||
...options, | ||
}), | ||
}); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we set the version to
0.0.0
until the first release?