Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vrockz747 committed Dec 14, 2024
1 parent d141845 commit 10bcf39
Show file tree
Hide file tree
Showing 22 changed files with 1,466 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/app-starknet/src/operations/getPublicKeys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export const getPublicKeys = async (
);
assert(
params.derivationPaths.reduce(
(acc, path) => acc && path.path.length > 3,
(acc, path) => acc && path.path.length > 5,
true,
),
'derivationPaths should be greater than 3',
'derivationPaths should be greater than 5',
);

await sdk.checkAppCompatibility(APP_VERSION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import {
SeedGenerationStatus,
} from '../../proto/generated/types';
import { runGetPublicKeysOnDevice } from '../runGetPublicKeys';
import {
OperationHelper,
logger as rootLogger,
getStarknetApiJs,
} from '../../utils';
import { OperationHelper, logger as rootLogger } from '../../utils';
import { GetPublicKeysEvent } from '../types';
import {
IGetUserVerifiedPublicKeyParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ export const runGetPublicKeysOnDevice = async (
forceStatusUpdate(GetPublicKeysEvent.VERIFY);

return {
publicKeys: publicKeys.map(e => '0x' + uint8ArrayToHex(e)),
publicKeys: publicKeys.map(e => `0x${uint8ArrayToHex(e)}`),
};
};
4 changes: 2 additions & 2 deletions packages/app-starknet/src/operations/signTxn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const signTxn = async (
assert(params.txn, 'txn should be defined');
assert(params.derivationPath, 'derivationPaths should be defined');
assert(
params.derivationPath.length > 3,
'derivationPath should be greater than 3',
params.derivationPath.length > 5,
'derivationPath should be greater than 5',
);

await sdk.checkAppCompatibility(APP_VERSION);
Expand Down
25 changes: 25 additions & 0 deletions packages/app-starknet/tests/01.create/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MockDeviceConnection } from '@cypherock/sdk-interfaces';
import { afterEach, beforeEach, describe, expect, test } from '@jest/globals';

import * as sdkMocks from '../../src/__mocks__/sdk';
import { StarknetApp } from '../../src/index';

describe('StarknetApp.create', () => {
let connection: MockDeviceConnection;

beforeEach(async () => {
connection = await MockDeviceConnection.create();
sdkMocks.create.mockClear();
});

afterEach(async () => {
await connection.destroy();
});

test('should be able to create solana app instance', async () => {
await StarknetApp.create(connection);

expect(sdkMocks.create).toHaveBeenCalledTimes(1);
expect(sdkMocks.create.mock.lastCall).toContainEqual(connection);
});
});
94 changes: 94 additions & 0 deletions packages/app-starknet/tests/02.getPublicKeys/__fixtures__/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {
DeviceAppError,
DeviceAppErrorType,
deviceAppErrorTypeDetails,
} from '@cypherock/sdk-interfaces';
import { IGetPublicKeysTestCase } from './types';
import { Query } from '../../../src/proto/generated/starknet/core';

const commonParams = {
params: {
walletId: new Uint8Array([
199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, 233, 62,
110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, 26, 3, 187, 121,
64,
]),
derivationPaths: [
{
path: [
0x80000000 + 0xa55,
0x80000000 + 0x4741e9c9,
0x80000000 + 0x447a6028,
0x80000000,
0x80000000,
0xc,
],
},
],
chainId: 1,
},
queries: [
{
name: 'Initate query',
data: Uint8Array.from(
Query.encode(
Query.create({
getPublicKeys: {
initiate: {
walletId: new Uint8Array([
199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160,
103, 233, 62, 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53,
86, 128, 26, 3, 187, 121, 64,
]),
derivationPaths: [
{
path: [
0x80000000 + 0xa55,
0x80000000 + 0x4741e9c9,
0x80000000 + 0x447a6028,
0x80000000,
0x80000000,
0xc,
],
},
],
},
},
}),
).finish(),
),
},
],
};

const withUnknownError: IGetPublicKeysTestCase = {
name: 'With unknown error',
...commonParams,
results: [
{
name: 'error',
data: new Uint8Array([10, 4, 18, 2, 8, 1]),
},
],
errorInstance: DeviceAppError,
errorMessage:
deviceAppErrorTypeDetails[DeviceAppErrorType.UNKNOWN_ERROR].message,
};

const withInvalidAppId: IGetPublicKeysTestCase = {
name: 'With invalid msg from device',
...commonParams,
results: [
{
name: 'error',
data: new Uint8Array([10, 4, 18, 2, 16, 1]),
},
],
errorInstance: DeviceAppError,
errorMessage:
deviceAppErrorTypeDetails[DeviceAppErrorType.CORRUPT_DATA].message,
};

const error: IGetPublicKeysTestCase[] = [withUnknownError, withInvalidAppId];

export default error;
14 changes: 14 additions & 0 deletions packages/app-starknet/tests/02.getPublicKeys/__fixtures__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IFixtures } from './types';
import error from './error';
import invalidData from './invalidData';
import valid from './valid';
import invalidArgs from './invalidArgs';

const fixtures: IFixtures = {
valid,
error,
invalidData,
invalidArgs,
};

export default fixtures;
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { IGetPublicKeysTestCase } from './types';

const commonParams = {
queries: [{ name: 'empty', data: new Uint8Array([]) }],
results: [{ name: 'empty', data: new Uint8Array([]) }],
errorInstance: Error,
errorMessage: /AssertionError/,
};

const validParams = {
walletId: new Uint8Array([
199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, 233, 62,
110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, 26, 3, 187, 121,
64,
]),
derivationPaths: [
{
path: [
0x80000000 + 0xa55,
0x80000000 + 0x4741e9c9,
0x80000000 + 0x447a6028,
0x80000000,
0x80000000,
0xc,
],
},
],
chainId: '1',
};

const invalidArgs: IGetPublicKeysTestCase[] = [
{
name: 'Null',
...commonParams,
params: null as any,
},
{
name: 'Undefined',
...commonParams,
params: null as any,
},
{
name: 'Empty Object',
...commonParams,
params: {} as any,
},
{
name: 'No derivation paths',
...commonParams,
params: { ...validParams, derivationPaths: undefined } as any,
},
{
name: 'No wallet id',
...commonParams,
params: { ...validParams, walletId: undefined } as any,
},
{
name: 'Empty derivation path',
...commonParams,
params: {
...validParams,
derivationPaths: [],
} as any,
},
{
name: 'invalid derivation path in array (depth:1)',
...commonParams,
params: {
...validParams,
derivationPaths: [
{
path: [0x80000000],
},
],
},
},
];

export default invalidArgs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import {
DeviceAppError,
DeviceAppErrorType,
deviceAppErrorTypeDetails,
} from '@cypherock/sdk-interfaces';
import { IGetPublicKeysTestCase } from './types';
import { Query } from '../../../src/proto/generated/starknet/core';

const commonParams = {
params: {
walletId: new Uint8Array([
199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160, 103, 233, 62,
110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53, 86, 128, 26, 3, 187, 121,
64,
]),
derivationPaths: [
{
path: [
0x80000000 + 0xa55,
0x80000000 + 0x4741e9c9,
0x80000000 + 0x447a6028,
0x80000000,
0x80000000,
0xc,
],
},
],
chainId: 1,
},
queries: [
{
name: 'Initate query',
data: Uint8Array.from(
Query.encode(
Query.create({
getPublicKeys: {
initiate: {
walletId: new Uint8Array([
199, 89, 252, 26, 32, 135, 183, 211, 90, 220, 38, 17, 160,
103, 233, 62, 110, 172, 92, 20, 35, 250, 190, 146, 62, 8, 53,
86, 128, 26, 3, 187, 121, 64,
]),
derivationPaths: [
{
path: [
0x80000000 + 0xa55,
0x80000000 + 0x4741e9c9,
0x80000000 + 0x447a6028,
0x80000000,
0x80000000,
0xc,
],
},
],
},
},
}),
).finish(),
),
},
],
errorInstance: DeviceAppError,
errorMessage:
deviceAppErrorTypeDetails[DeviceAppErrorType.INVALID_MSG_FROM_DEVICE]
.message,
};

const invalidData: IGetPublicKeysTestCase[] = [
{
name: 'Invalid data',
...commonParams,
results: [
{
name: 'error',
data: new Uint8Array([
109, 112, 102, 98, 72, 57, 117, 109, 75, 69, 83, 117, 117, 49, 103,
78, 100, 105, 87, 83, 116, 106, 71, 54, 67, 110, 104, 77, 86, 49, 113,
97, 78, 111, 50, 98, 118, 52, 67, 113, 72, 122, 120, 85, 98, 53, 86,
68, 115, 86, 52, 77, 86, 112, 83, 70, 86, 78, 121, 121, 109, 83, 112,
98, 74, 76, 55, 57, 75, 89, 86, 57, 75, 56, 88, 82, 100, 105, 98, 70,
109, 118, 54, 116, 86, 54, 116, 50, 122, 52, 100, 87, 110, 111, 110,
78, 52, 78, 77, 89, 109,
]),
},
],
},
{
name: 'Invalid data',
...commonParams,
results: [
{
name: 'error',
data: new Uint8Array([
10, 34, 10, 3, 90, 221, 135, 18, 2, 8, 1, 24, 1, 34, 11, 8, 2, 18, 7,
8,
]),
},
],
},
{
name: 'Invalid data',
...commonParams,

results: [
{
name: 'error',
data: new Uint8Array([10]),
},
],
},
{
name: 'Invalid data',
...commonParams,
results: [
{
name: 'error',
data: new Uint8Array([]),
},
],
},
];

export default invalidData;
Loading

0 comments on commit 10bcf39

Please sign in to comment.