Skip to content

Commit

Permalink
sdk: WIP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrevmatos committed Aug 27, 2020
1 parent 3f1168a commit da442ed
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 115 deletions.
4 changes: 3 additions & 1 deletion raiden-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"memdown": "^5.1.0",
"node-pre-gyp": "^0.15.0",
"pouchdb-adapter-memory": "^7.2.2",
"pouchdb-debug": "^7.2.1",
"prettier": "^2.1.1",
"rimraf": "^3.0.2",
"rxjs-marbles": "^6.0.1",
Expand Down Expand Up @@ -207,7 +208,8 @@
],
"globals": {
"ts-jest": {
"tsConfig": "<rootDir>/tests/tsconfig.json"
"tsConfig": "<rootDir>/tests/tsconfig.json",
"isolatedModules": true
}
}
},
Expand Down
17 changes: 0 additions & 17 deletions raiden-ts/tests/e2e/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Storage } from 'raiden-ts/utils/types';

export interface RequestOpts {
uri: string;
Expand All @@ -15,22 +14,6 @@ export interface RequestOpts {
}
export type RequestCallback = (err?: Error, response?: any, body?: any) => void;

export const MockStorage: jest.Mock<jest.Mocked<Storage>, [{ [key: string]: string }?]> = jest.fn(
function (init?: { [key: string]: string }) {
const storage: NonNullable<typeof init> = init || {};
return {
storage,
getItem: jest.fn(async (key: string) => storage[key] || null),
setItem: jest.fn(async (key: string, value: string) => {
storage[key] = value;
}),
removeItem: jest.fn(async (key: string) => {
delete storage[key];
}),
};
},
);

export class MockMatrixRequestFn {
public constructor(server: string) {
this.endpoints['/login'] = ({}, callback) =>
Expand Down
126 changes: 63 additions & 63 deletions raiden-ts/tests/e2e/raiden.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { timer } from 'rxjs';
import { first, filter, takeUntil, take, toArray } from 'rxjs/operators';
import { Zero } from 'ethers/constants';
import { parseEther, parseUnits, bigNumberify, BigNumber, keccak256, Network } from 'ethers/utils';
import PouchDB from 'pouchdb';
import MemAdapter from 'pouchdb-adapter-memory';
PouchDB.plugin(MemAdapter);

import { TestProvider } from './provider';
import { MockStorage, MockMatrixRequestFn } from './mocks';
import { MockMatrixRequestFn } from './mocks';

// mock waitConfirmation Raiden helper to also trigger provider.mine
jest.mock('raiden-ts/helpers', () => {
Expand Down Expand Up @@ -48,7 +51,6 @@ describe('Raiden', () => {
contractsInfo: ContractsInfo,
snapId: number | undefined,
raiden: Raiden,
storage: jest.Mocked<Storage>,
token: string,
tokenNetwork: string,
partner: string,
Expand All @@ -74,15 +76,16 @@ describe('Raiden', () => {

async function createRaiden(
account: number | string,
stateOrStorage?: RaidenState | Storage,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
storage?: { state?: any; storage?: Storage; adapter?: any; prefix?: string },
subkey?: true,
): Promise<Raiden> {
const raiden = await Raiden.create(
// we need to create a new test provider, to avoid sharing provider instances,
// but with same underlying ganache instance
new TestProvider(provider._web3Provider),
account,
stateOrStorage,
{ adapter: 'memory', ...storage },
contractsInfo,
config,
subkey,
Expand Down Expand Up @@ -141,7 +144,6 @@ describe('Raiden', () => {
if (snapId !== undefined) await provider.revert(snapId);
snapId = await provider.snapshot();
await provider.getBlockNumber();
storage = new MockStorage();

fetch.mockResolvedValue({
ok: true,
Expand All @@ -155,7 +157,7 @@ describe('Raiden', () => {
httpBackend = new MockMatrixRequestFn(matrixServer);
request(httpBackend.requestFn.bind(httpBackend));

raiden = await createRaiden(0, storage);
raiden = await createRaiden(0);
raiden.start();
});

Expand All @@ -178,7 +180,7 @@ describe('Raiden', () => {
Raiden.create(
provider,
0,
{ storage, state: { ...raiden0State, blockNumber: raiden0State.blockNumber - 2 } },
{ state: { ...raiden0State, blockNumber: raiden0State.blockNumber - 2 } },
contractsInfo,
config,
),
Expand All @@ -188,34 +190,36 @@ describe('Raiden', () => {
Raiden.create(
provider,
0,
{ storage, state: { ...raiden0State, blockNumber: raiden0State.blockNumber + 2 } },
{ state: { ...raiden0State, blockNumber: raiden0State.blockNumber + 2 } },
contractsInfo,
config,
),
).resolves.toBeInstanceOf(Raiden);

await expect(
Raiden.create(provider, 0, { storage }, contractsInfo, config),
Raiden.create(provider, 0, undefined, contractsInfo, config),
).resolves.toBeInstanceOf(Raiden);

// token address not found as an account in provider
await expect(Raiden.create(provider, token, storage, contractsInfo, config)).rejects.toThrow(
await expect(Raiden.create(provider, token, undefined, contractsInfo, config)).rejects.toThrow(
ErrorCodes.RDN_ACCOUNT_NOT_FOUND,
);

// neither account index, address nor private key
await expect(
Raiden.create(provider, '0x1234', storage, contractsInfo, config),
Raiden.create(provider, '0x1234', undefined, contractsInfo, config),
).rejects.toThrow(ErrorCodes.RDN_STRING_ACCOUNT_INVALID);

// from hex-encoded private key, initial unknown state (decodable) but invalid address inside
await expect(
Raiden.create(
provider,
'0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
JSON.stringify(
makeInitialState({ network, contractsInfo, address: token as Address }, { config }),
),
{
state: JSON.stringify(
makeInitialState({ network, contractsInfo, address: token as Address }, { config }),
),
},
contractsInfo,
),
).rejects.toThrow(/Mismatch between provided account and loaded state/i);
Expand All @@ -224,23 +228,25 @@ describe('Raiden', () => {
Raiden.create(
provider,
1,
JSON.stringify(
makeInitialState(
{
network,
contractsInfo: {
TokenNetworkRegistry: { address: token as Address, block_number: 0 },
ServiceRegistry: { address: partner as Address, block_number: 1 },
UserDeposit: { address: partner as Address, block_number: 2 },
SecretRegistry: { address: partner as Address, block_number: 3 },
MonitoringService: { address: partner as Address, block_number: 3 },
OneToN: { address: partner as Address, block_number: 3 },
{
state: JSON.stringify(
makeInitialState(
{
network,
contractsInfo: {
TokenNetworkRegistry: { address: token as Address, block_number: 0 },
ServiceRegistry: { address: partner as Address, block_number: 1 },
UserDeposit: { address: partner as Address, block_number: 2 },
SecretRegistry: { address: partner as Address, block_number: 3 },
MonitoringService: { address: partner as Address, block_number: 3 },
OneToN: { address: partner as Address, block_number: 3 },
},
address: accounts[1] as Address,
},
address: accounts[1] as Address,
},
{ config },
{ config },
),
),
),
},
contractsInfo,
),
).rejects.toThrow(/Mismatch between network or registry address and loaded state/i);
Expand All @@ -251,7 +257,12 @@ describe('Raiden', () => {
const raiden1 = await Raiden.create(
provider,
accounts[1],
makeInitialState({ network, contractsInfo, address: accounts[1] as Address }, { config }),
{
state: makeInitialState(
{ network, contractsInfo, address: accounts[1] as Address },
{ config },
),
},
contractsInfo.UserDeposit.address,
config,
);
Expand Down Expand Up @@ -293,7 +304,7 @@ describe('Raiden', () => {
expect(raiden1.started).toBe(false);

// success when creating using subkey
const raiden2 = await Raiden.create(provider, 0, storage, contractsInfo, config, true);
const raiden2 = await Raiden.create(provider, 0, undefined, contractsInfo, config, true);
expect(raiden2).toBeInstanceOf(Raiden);
expect(raiden2.mainAddress).toBe(accounts[0]);
});
Expand Down Expand Up @@ -531,7 +542,7 @@ describe('Raiden', () => {
).blockNumber!;

raidenState = undefined;
raiden = await createRaiden(0, storage);
raiden = await createRaiden(0);
raiden.state$.subscribe((state) => (raidenState = state));
raiden.start();

Expand Down Expand Up @@ -729,10 +740,12 @@ describe('Raiden', () => {
await expect(raiden.getAvailability(partner)).rejects.toThrow(ErrorCodes.TRNS_NO_VALID_USER);

// success when using address of account on provider and initial state
const raiden1 = await createRaiden(
accounts[2],
makeInitialState({ network, contractsInfo, address: accounts[2] as Address }, { config }),
);
const raiden1 = await createRaiden(accounts[2], {
state: makeInitialState(
{ network, contractsInfo, address: accounts[2] as Address },
{ config },
),
});
expect(raiden1).toBeInstanceOf(Raiden);
raiden1.start();

Expand Down Expand Up @@ -816,10 +829,12 @@ describe('Raiden', () => {
let raiden1: Raiden;

beforeEach(async () => {
raiden1 = await createRaiden(
partner,
makeInitialState({ network, contractsInfo, address: partner as Address }, { config }),
);
raiden1 = await createRaiden(partner, {
state: makeInitialState(
{ network, contractsInfo, address: partner as Address },
{ config },
),
});
raiden1.start();

// await raiden1 client matrix initialization
Expand Down Expand Up @@ -850,10 +865,7 @@ describe('Raiden', () => {
// there's some channel with enough capacity, but not the one returned by PFS
const partner3 = accounts[3];
await raiden.openChannel(token, partner3, { deposit: 300 });
const raiden3 = await createRaiden(
partner3,
makeInitialState({ network, contractsInfo, address: partner3 as Address }, { config }),
);
const raiden3 = await createRaiden(partner3);
raiden3.start();
await raiden3.action$.pipe(filter(isActionOf(matrixSetup)), first()).toPromise();
await expect(raiden.getAvailability(partner3)).resolves.toBeDefined();
Expand Down Expand Up @@ -888,10 +900,7 @@ describe('Raiden', () => {
expect.assertions(7);

const target = accounts[2],
raiden2 = await createRaiden(
target,
makeInitialState({ network, contractsInfo, address: target as Address }, { config }),
),
raiden2 = await createRaiden(target),
matrix2Promise = raiden2.action$
.pipe(filter(isActionOf(matrixSetup)), first())
.toPromise();
Expand Down Expand Up @@ -1027,14 +1036,8 @@ describe('Raiden', () => {
await raiden.openChannel(token, partner);
await raiden.depositChannel(token, partner, 200);

raiden1 = await createRaiden(
partner,
makeInitialState({ network, contractsInfo, address: partner as Address }, { config }),
);
raiden2 = await createRaiden(
target,
makeInitialState({ network, contractsInfo, address: target as Address }, { config }),
);
raiden1 = await createRaiden(partner);
raiden2 = await createRaiden(target);
raiden1.start();
raiden2.start();

Expand Down Expand Up @@ -1177,10 +1180,7 @@ describe('Raiden', () => {
// there's some channel with enough capacity, but not the one returned by PFS
const partner3 = accounts[3];
await raiden.openChannel(token, partner3, { deposit: 300 });
const raiden3 = await createRaiden(
partner3,
makeInitialState({ network, contractsInfo, address: partner3 as Address }, { config }),
);
const raiden3 = await createRaiden(partner3);
raiden3.start();
await raiden3.action$.pipe(filter(isActionOf(matrixSetup)), first()).toPromise();
await expect(raiden.getAvailability(partner3)).resolves.toBeDefined();
Expand Down Expand Up @@ -1246,7 +1246,7 @@ describe('Raiden', () => {
raiden = await Raiden.create(
new TestProvider(undefined, { network_id: 1 }),
0,
storage,
undefined,
contractsInfo,
config,
);
Expand Down Expand Up @@ -1316,7 +1316,7 @@ describe('Raiden', () => {
// stop and restart node
raiden.stop();
await provider.mine(confirmationBlocks);
raiden = await createRaiden(0, storage);
raiden = await createRaiden(0);
raiden.start();

const result = raiden.action$.pipe(filter(udcWithdrawn.is), take(2), toArray()).toPromise();
Expand Down Expand Up @@ -1350,7 +1350,7 @@ describe('Raiden', () => {

test('subkey', async () => {
expect.assertions(30);
const sub = await createRaiden(0, storage, true);
const sub = await createRaiden(0, undefined, true);

const subStarted = sub.action$.pipe(filter(isActionOf(matrixSetup)), first()).toPromise();
sub.start();
Expand Down
2 changes: 2 additions & 0 deletions raiden-ts/tests/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ declare module 'ganache-cli' {
const ganache: Ganache;
export default ganache;
}

declare module 'pouchdb-debug';
Loading

0 comments on commit da442ed

Please sign in to comment.