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 d84ad31 commit 12d7ee8
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/api/wallet_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ export class ArchethicWalletClient {
return this;
}

_dispatchConnectionState() {
const connectionState = this.connectionState
_dispatchConnectionState(state?: ConnectionState) {
const connectionState = state ?? this.connectionState
console.log(`Connection state updated : ${connectionState}`);
this._connectionStateEventTarget.dispatchEvent(new Event(connectionState));
}
Expand Down Expand Up @@ -145,7 +145,7 @@ export class ArchethicWalletClient {
this._dispatchConnectionState();
};

await this._channel.connect();
this._channel.connect();
this._dispatchConnectionState();
});
}
Expand All @@ -154,7 +154,8 @@ export class ArchethicWalletClient {
* @return {Promise<void>}
*/
async close(): Promise<void> {
this._channel?.close();
this._dispatchConnectionState(ConnectionState.Closing);
await this._channel?.close();
}

_ensuresConnectionAlive(): void {
Expand Down Expand Up @@ -230,13 +231,16 @@ export class ArchethicWalletClient {
* @param {function(String)} listener
* @return {ArchethicWalletClient}
*/
onconnectionstatechange(listener: Function): this {
onconnectionstatechange(listener: (state: ConnectionState) => void): this {
this._connectionStateEventTarget.addEventListener(ConnectionState.Connecting, () => {
listener(ConnectionState.Connecting);
});
this._connectionStateEventTarget.addEventListener(ConnectionState.Open, () => {
listener(ConnectionState.Open);
});
this._connectionStateEventTarget.addEventListener(ConnectionState.Closing, () => {
listener(ConnectionState.Closing);
});
this._connectionStateEventTarget.addEventListener(ConnectionState.Closed, () => {
listener(ConnectionState.Closed);
});
Expand All @@ -249,6 +253,7 @@ export class ArchethicWalletClient {
unsubscribeconnectionstatechange(): this {
this._connectionStateEventTarget.removeEventListener(ConnectionState.Connecting, null);
this._connectionStateEventTarget.removeEventListener(ConnectionState.Open, null);
this._connectionStateEventTarget.removeEventListener(ConnectionState.Closing, null);
this._connectionStateEventTarget.removeEventListener(ConnectionState.Closed, null);
return this;
}
Expand Down
108 changes: 108 additions & 0 deletions tests/api/wallet_rpc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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 expose 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 connecting", async () => {
const awc = new ArchethicWalletClient(new AWCStreamChannelMock<string>());

const connectionStates = new Array<ConnectionState>();
awc.onconnectionstatechange((state) => {
connectionStates.push(state)
})

await awc.connect();

expect(connectionStates).toStrictEqual([ConnectionState.Connecting, ConnectionState.Open])
});

it("should expose 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);
});

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

const connectionStates = new Array<ConnectionState>();
awc.onconnectionstatechange((state) => {
connectionStates.push(state)
})

await awc.close();
expect(connectionStates).toStrictEqual([ConnectionState.Closing, ConnectionState.Closed]);
});
});

0 comments on commit 12d7ee8

Please sign in to comment.