From d2b0137b4aaec4d7bc6f2b6e800573c6172a16c8 Mon Sep 17 00:00:00 2001 From: Monte Lai Date: Thu, 31 Aug 2023 19:33:54 +0800 Subject: [PATCH] fix: update snap object keys to be mandatory and move name to metadata (#87) * fix: update snap object keys to be mandatory and move name to metadata * add snap metadata test --- src/internal/types.test.ts | 60 +++++++++++++++++++++++++++++++++++++- src/internal/types.ts | 7 +++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/internal/types.test.ts b/src/internal/types.test.ts index c2eba773b..e206ac58a 100644 --- a/src/internal/types.test.ts +++ b/src/internal/types.test.ts @@ -14,6 +14,7 @@ describe('InternalAccount', () => { keyring: { type: 'Test Keyring', }, + name: 'Account 1', }, }; @@ -29,6 +30,7 @@ describe('InternalAccount', () => { type: 'eip155:eoa', metadata: { keyring: {}, + name: 'Account 1', }, }; @@ -44,7 +46,9 @@ describe('InternalAccount', () => { options: {}, methods: [], type: 'eip155:eoa', - metadata: {}, + metadata: { + name: 'Account 1', + }, }; expect(() => assert(account, InternalAccountStruct)).toThrow( @@ -77,6 +81,7 @@ describe('InternalAccount', () => { keyring: { type: 'Test Keyring', }, + name: 'Account 1', extra: 'field', }, }; @@ -85,4 +90,57 @@ describe('InternalAccount', () => { 'At path: metadata.extra -- Expected a value of type `never`', ); }); + + it('should contain snap name, id and enabled if the snap metadata exists', () => { + const account = { + id: '606a7759-b0fb-48e4-9874-bab62ff8e7eb', + address: '0x000', + options: {}, + methods: [], + type: 'eip155:eoa', + metadata: { + keyring: { + type: 'Test Keyring', + }, + name: 'Account 1', + snap: { + id: 'test-snap', + enabled: true, + name: 'Test Snap', + }, + }, + }; + + expect(() => assert(account, InternalAccountStruct)).not.toThrow(); + }); + + it.each([['name', 'enabled', 'id']])( + 'should throw if snap.%i is not set', + (key: string) => { + const account = { + id: '606a7759-b0fb-48e4-9874-bab62ff8e7eb', + address: '0x000', + options: {}, + methods: [], + type: 'eip155:eoa', + metadata: { + keyring: { + type: 'Test Keyring', + }, + name: 'Account 1', + snap: { + id: 'test-snap', + enabled: true, + name: 'Test Snap', + }, + }, + }; + + delete account.metadata.snap[key as keyof typeof account.metadata.snap]; + + const regex = new RegExp(`At path: metadata.snap.${key}`, 'u'); + + expect(() => assert(account, InternalAccountStruct)).toThrow(regex); + }, + ); }); diff --git a/src/internal/types.ts b/src/internal/types.ts index 387ce0a96..3b11e87aa 100644 --- a/src/internal/types.ts +++ b/src/internal/types.ts @@ -7,11 +7,12 @@ export const InternalAccountStruct = object({ metadata: object({ snap: optional( object({ - id: optional(string()), - name: optional(string()), - enabled: optional(boolean()), + id: string(), + enabled: boolean(), + name: string(), }), ), + name: string(), keyring: object({ type: string(), }),