Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jackson-dean committed Jun 23, 2023
1 parent 07aaf86 commit 0219eec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/identity/identity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { utils as ecUtils, getPublicKey } from '@noble/secp256k1';
import { verify } from 'jsonwebtoken';
import KeyEncoder from 'key-encoder';
import { ChatType, NewMessageEntryResponse } from '../backend-types/index.js';
import { getAPIFake, getWindowFake } from '../test-utils.js';
import { AsyncStorageFake, getAPIFake, getWindowFake } from '../test-utils.js';
import { APIError } from './api.js';
import { DEFAULT_IDENTITY_URI, LOCAL_STORAGE_KEYS } from './constants.js';
import {
Expand All @@ -19,7 +19,7 @@ import {
TransactionMetadataBasicTransfer,
TransactionNonce,
} from './transaction-transcoders.js';
import { APIProvider } from './types.js';
import { APIProvider, AsyncStorage } from './types.js';

function getPemEncodePublicKey(privateKey: Uint8Array): string {
const publicKey = getPublicKey(privateKey, true);
Expand Down Expand Up @@ -973,14 +973,15 @@ describe('identity', () => {
});
});
describe('setActiveUser', () => {
it('sets the active user', () => {
it('sets the active user', async () => {
const pubKey1 = 'fake-pub-key-1';
const pubKey2 = 'fake-pub-key-2';
windowFake.localStorage.setItem(
const storageProvider = new AsyncStorageFake();
await storageProvider.setItem(
LOCAL_STORAGE_KEYS.activePublicKey,
pubKey1
);
windowFake.localStorage.setItem(
await storageProvider.setItem(
LOCAL_STORAGE_KEYS.identityUsers,
JSON.stringify({
[pubKey1]: {
Expand All @@ -998,8 +999,11 @@ describe('identity', () => {
})
);

identity.setActiveUser(pubKey2);
const snapshot = identity.snapshot();
const asyncIdentity = new Identity<AsyncStorage>(windowFake, apiFake);
asyncIdentity.configure({ storageProvider });

asyncIdentity.setActiveUser(pubKey2);
const snapshot = await asyncIdentity.snapshot();
expect(snapshot.currentUser?.publicKey).toEqual(pubKey2);
});
});
Expand Down
35 changes: 34 additions & 1 deletion src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APIProvider } from './identity/index.js';
import { APIProvider, AsyncStorage } from './identity/index.js';

class LocalStorageFake implements Storage {
db: Record<string, string> = {};
Expand Down Expand Up @@ -28,6 +28,39 @@ class LocalStorageFake implements Storage {
}
}

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export class AsyncStorageFake implements AsyncStorage {
db: Record<string, string> = {};

get length() {
return Object.keys(this.db).length;
}

async clear() {
await sleep(1);
this.db = {};
}

key(index: number) {
return Object.keys(this.db)[index];
}

async getItem(key: string) {
await sleep(1);
return this.db[key] ?? null;
}

async setItem(key: string, value: string) {
await sleep(1);
this.db[key] = value;
}

async removeItem(key: string) {
await sleep(1);
delete this.db[key];
}
}

export function getWindowFake(overrides: Partial<Window> = {}): Window {
overrides.location = {
...window.location,
Expand Down

0 comments on commit 0219eec

Please sign in to comment.