Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Commit

Permalink
feat(profiles): implement Environment#bootFromObject (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian committed Jul 20, 2020
1 parent 5342de1 commit 031c600
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 19 deletions.
Binary file modified .yarn/install-state.gz
Binary file not shown.
7 changes: 6 additions & 1 deletion packages/platform-sdk-profiles/src/env.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface EnvironmentOptions {
}

export interface Storage {
all(): Promise<object>;
all(): Promise<Record<string, unknown>>;

get<T>(key: string): Promise<T | undefined>;

Expand All @@ -33,3 +33,8 @@ export interface Storage {

restore(): Promise<void>;
}

export interface StorageData {
data: object;
profiles: object;
}
15 changes: 15 additions & 0 deletions packages/platform-sdk-profiles/src/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ETH } from "@arkecosystem/platform-sdk-eth";
import nock from "nock";

import { Environment, Identifiers, Profile } from "../src";
import storageData from "../test/fixtures/env-storage.json";
import { identity } from "../test/fixtures/identity";
import { HttpClient } from "../test/stubs/client";
import { StubStorage } from "../test/stubs/storage";
Expand Down Expand Up @@ -106,3 +107,17 @@ it("should create a profile with data and persist it when instructed to do so",
expect(newProfile.data().all()).toEqual({ key: "value" });
expect(newProfile.settings().all()).toEqual({ ADVANCED_MODE: "value" });
});

it("should boot the environment from fixed data", async () => {
const env = new Environment({ coins: { ARK }, httpClient: new HttpClient(), storage: new StubStorage() });
await env.bootFromObject(storageData);

const newProfile = env.profiles().findById("b999d134-7a24-481e-a95d-bc47c543bfc9");

expect(newProfile).toBeInstanceOf(Profile);
expect(newProfile.wallets().keys()).toHaveLength(1);
expect(newProfile.contacts().keys()).toHaveLength(1);
expect(newProfile.notifications().keys()).toHaveLength(1);
expect(newProfile.data().all()).toEqual({ key: "value" });
expect(newProfile.settings().all()).toEqual({ ADVANCED_MODE: "value" });
});
43 changes: 31 additions & 12 deletions packages/platform-sdk-profiles/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Validator, ValidatorSchema } from "@arkecosystem/platform-sdk-support";

import { container } from "./container";
import { Identifiers } from "./container.models";
import { EnvironmentOptions, Storage, CoinList, CoinType } from "./env.models";
import { CoinList, CoinType, EnvironmentOptions, Storage, StorageData } from "./env.models";
import { Migrator } from "./migrator";
import { DataRepository } from "./repositories/data-repository";
import { ProfileRepository } from "./repositories/profile-repository";
Expand All @@ -23,15 +23,27 @@ export class Environment {
* @memberof Environment
*/
public async boot(): Promise<void> {
const { data, profiles } = await this.validateStorage();
const { data, profiles } = await container.get<Storage>(Identifiers.Storage).all();

if (data) {
this.data().fill(data);
}
const validated: StorageData = await this.validateStorage({ data, profiles });

if (profiles) {
await this.profiles().fill(profiles);
}
await this.restoreData(validated);
}

/**
* Load the data from an object.
*
* This has to be manually called and should be used the same as "bootFromStorage"
* with the exception of it not being used in production. This method should only
* be used in testing environments where you want to use a fixed set of data.
*
* @returns {Promise<void>}
* @memberof Environment
*/
public async bootFromObject({ data, profiles }: StorageData): Promise<void> {
const validated: StorageData = await this.validateStorage({ data, profiles });

await this.restoreData(validated);
}

/**
Expand Down Expand Up @@ -97,7 +109,7 @@ export class Environment {
container.set(Identifiers.Coins, options.coins);
}

private async validateStorage(): Promise<{ profiles; data }> {
private async validateStorage({ data, profiles }): Promise<StorageData> {
const mapRules = (map: object, rule: Function) =>
Object.keys(map).reduce((newMap, key) => ({ ...newMap, [key]: rule }), {});

Expand Down Expand Up @@ -174,9 +186,6 @@ export class Environment {
return object({ profiles: object(rules), data: object() });
});

// @ts-ignore
let { data, profiles } = await container.get<Storage>(Identifiers.Storage).all();

if (!data) {
data = {};
}
Expand All @@ -198,4 +207,14 @@ export class Environment {

return validated;
}

private async restoreData({ data, profiles }: StorageData): Promise<void> {
if (data) {
this.data().fill(data);
}

if (profiles) {
await this.profiles().fill(profiles);
}
}
}
2 changes: 1 addition & 1 deletion packages/platform-sdk-profiles/src/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import semver from "semver";

import { container } from "./container";
import { Identifiers } from "./container.models";
import { Storage } from "./env.models";
import { DataRepository } from "./repositories/data-repository";
import { ProfileRepository } from "./repositories/profile-repository";
import { Storage } from "./env.models";

export class Migrator {
readonly #profiles: ProfileRepository;
Expand Down
4 changes: 2 additions & 2 deletions packages/platform-sdk-profiles/src/storage/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export class LocalStorage implements Storage {
});
}

public async all(): Promise<object> {
const result: object = {};
public async all(): Promise<Record<string, unknown>> {
const result: Record<string, unknown> = {};

for (const key of await this.#storage.keys()) {
result[key] = await this.get(key);
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-sdk-profiles/src/storage/null.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Storage } from "../env.models";

export class NullStorage implements Storage {
public async all(): Promise<object> {
public async all(): Promise<Record<string, unknown>> {
return {};
}

Expand Down
60 changes: 60 additions & 0 deletions packages/platform-sdk-profiles/test/fixtures/env-storage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"profiles": {
"b999d134-7a24-481e-a95d-bc47c543bfc9": {
"id": "b999d134-7a24-481e-a95d-bc47c543bfc9",
"name": "John Doe",
"wallets": {
"ac38fe6d-4b67-4ef1-85be-17c5f6841129": {
"id": "ac38fe6d-4b67-4ef1-85be-17c5f6841129",
"coin": "ARK",
"coinConfig": {
"network": {
"id": "devnet",
"name": "Devnet",
"explorer": "https://dexplorer.ark.io/",
"currency": { "ticker": "DARK", "symbol": "" },
"crypto": { "slip44": 111 },
"hosts": [
"https://dexplorer.ark.io",
"http://167.114.29.51:4003",
"http://167.114.29.52:4003",
"http://167.114.29.53:4003",
"http://167.114.29.54:4003",
"http://167.114.29.55:4003"
],
"hostsMultiSignature": [],
"voting": { "enabled": true, "singular": true }
}
},
"network": "devnet",
"address": "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
"publicKey": "034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192",
"data": { "BALANCE": {}, "SEQUENCE": {} },
"settings": {
"AVATAR": "conic-gradient(from 138.92820875068205deg at 10.341003346631878% 56.34369708791203%, #9B2C2C, calc(0 * 100% / 5), transparent 0),conic-gradient(from 501.2404426352248deg at 6.193536608927738% 20.020924742851292%, #FEB2B2, calc(1 * 100% / 5), transparent 0),conic-gradient(from 538.0014084095758deg at 32.870727463865066% 41.693174698534285%, #F6AD55, calc(2 * 100% / 5), transparent 0),conic-gradient(from 86.8716993412581deg at 45.65092691320694% 53.97053076646755%, #2C7A7B, calc(3 * 100% / 5), transparent 0),conic-gradient(from 453.1648835420749deg at 5.017317358847957% 27.456901944771822%, #FC8181, calc(4 * 100% / 5), transparent 0)"
}
}
},
"contacts": {
"0e147f96-049f-4d89-bad4-ad3341109907": {
"id": "0e147f96-049f-4d89-bad4-ad3341109907",
"name": "Jane Doe",
"starred": false,
"addresses": []
}
},
"notifications": {
"b183aef3-2dba-471a-a588-0fcf8f01b645": {
"id": "b183aef3-2dba-471a-a588-0fcf8f01b645",
"icon": "warning",
"name": "Ledger Update Available",
"body": "...",
"action": "Read Changelog"
}
},
"data": { "key": "value" },
"settings": { "ADVANCED_MODE": "value" }
}
},
"data": { "key": "value" }
}
6 changes: 4 additions & 2 deletions packages/platform-sdk-profiles/test/stubs/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import "jest-extended";
import { readFileSync, writeFileSync } from "fs";
import { resolve } from "path";

export class StubStorage {
import { Storage } from "../../src/env.models";

export class StubStorage implements Storage {
readonly #storage;

public constructor() {
Expand All @@ -14,7 +16,7 @@ export class StubStorage {
}
}

public async all(): Promise<object> {
public async all(): Promise<Record<string, unknown>> {
return this.#storage;
}

Expand Down

0 comments on commit 031c600

Please sign in to comment.