Skip to content

Commit

Permalink
Test AWC behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralu committed Jul 31, 2024
1 parent 50ffcc7 commit fe9f2c5
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/api/wallet_rpc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Keychain from "../../src/keychain";
import Account from "../../src/account";
import Archethic, { ArchethicWalletClient, AWCStreamChannel, AWCStreamChannelState, ConnectionState } from "../../src/index";
import { deriveKeyPair, randomSecretKey } from "../../src/crypto";
import { uint8ArrayToHex } from "../../src/utils";
const nock = require("nock");


class AWCStreamChannelMock<T> implements AWCStreamChannel<T> {
_state = AWCStreamChannelState.CLOSED;
onReceive: ((data: T) => Promise<void>) | null;
onReady: (() => Promise<void>) | null;
onClose: ((reason: string) => Promise<void>) | null;

constructor(
onReceive?: ((data: T) => Promise<void>),
onReady?: (() => Promise<void>),
onClose?: ((reason: string) => Promise<void>),
) {
this.onReceive = onReceive ?? null;
this.onReady = onReady ?? null;
this.onClose = onClose ?? null;
}

get state(): AWCStreamChannelState {
return this._state;
}

_simulateQuickOperation(): Promise<void> {
return new Promise(resolve => setTimeout(resolve, 200));
}

_connectStateResult = AWCStreamChannelState.OPEN
set testUtilSetConnectResult(state: AWCStreamChannelState) {
this._connectStateResult = state;
}
async connect(): Promise<void> {
this._state = AWCStreamChannelState.CONNECTING;
await this._simulateQuickOperation();
this._state = this._connectStateResult;
this.onReady?.();
}
async close(): Promise<void> {
this._state = AWCStreamChannelState.CLOSING;
await this._simulateQuickOperation();
this._state = AWCStreamChannelState.CLOSED;
this.onClose?.('');
}

async send(data: T): Promise<void> {
await this._simulateQuickOperation();
}

}


describe("WalletRPC", () => {
it("should dispatch correct states while connecting", async () => {
const awc = new ArchethicWalletClient(new AWCStreamChannelMock<string>());

expect(awc.connectionState).toBe(ConnectionState.Closed);

const connectPromise = awc.connect();
expect(awc.connectionState).toBe(ConnectionState.Connecting);

await connectPromise;
expect(awc.connectionState).toBe(ConnectionState.Open);
});

it("should dispatch correct states while disconnecting", async () => {
const awc = new ArchethicWalletClient(new AWCStreamChannelMock<string>());
await awc.connect();


expect(awc.connectionState).toBe(ConnectionState.Open);
const disconnectPromise = awc.close();
expect(awc.connectionState).toBe(ConnectionState.Closing);

await disconnectPromise;
expect(awc.connectionState).toBe(ConnectionState.Closed);
});
});

0 comments on commit fe9f2c5

Please sign in to comment.