-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |