Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSockets #1251

Open
wants to merge 31 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9f14d77
feat: ws init setup
tabaktoni Oct 17, 2024
efe7ce3
feat: init ws and test
tabaktoni Oct 17, 2024
3dcb47e
feat: subscribe, unsubscribe, waitForDisconnection, test flow for new…
tabaktoni Oct 18, 2024
f217192
feat: subscribeNewHeads done and tested
tabaktoni Oct 25, 2024
21e3f82
feat: test standard endpoint, update wait methods with statuses
tabaktoni Oct 28, 2024
923fa25
feat: transaction status and pending transactions
tabaktoni Oct 28, 2024
16c261a
fix: missing file
tabaktoni Oct 28, 2024
fc9e9b9
feat: onUnsubscribe, make bulk test works
tabaktoni Oct 28, 2024
9da3ab0
feat: beta types-js release and import, on methods data type definiti…
tabaktoni Oct 28, 2024
4bcc486
feat: docs, extended tests, events map, types
tabaktoni Oct 29, 2024
38a99bc
feat: manage subscriptions by map
tabaktoni Oct 30, 2024
a14a66e
test: onUnsubscribe, docs, cleanup
tabaktoni Oct 30, 2024
3db410a
fix: cleanup
tabaktoni Oct 30, 2024
bb95fec
Merge branch 'develop' into feat/ws
tabaktoni Oct 30, 2024
c27f80e
fix: tipo and test
tabaktoni Oct 30, 2024
6dbe82e
Update __tests__/WebSocketChannel.test.ts
tabaktoni Oct 30, 2024
9daddc7
Update src/channel/ws_0_8.ts
tabaktoni Oct 30, 2024
b084c59
Update src/channel/ws_0_8.ts
tabaktoni Oct 30, 2024
45448b0
Merge branch 'develop' into feat/ws
tabaktoni Nov 21, 2024
4e195c3
fix: a SUBSCRIPTION_RESULT, bump types beta.2, debug tests
tabaktoni Nov 25, 2024
1ebf37d
Merge branch 'temp/ws' into feat/ws
tabaktoni Nov 25, 2024
7ecc217
Merge branch 'develop' into feat/ws
tabaktoni Nov 25, 2024
e28ccec
chore: starknet-types to starknet-types-08
tabaktoni Nov 25, 2024
6b35a2d
Merge branch 'temp/ws' into feat/ws
tabaktoni Nov 25, 2024
61b88e0
docs: websocket channel docs init
tabaktoni Nov 25, 2024
10c2e87
docs: websocket channel docs init
tabaktoni Nov 25, 2024
d394574
fix: block_id and txStatus event name
tabaktoni Nov 26, 2024
ebb0d8a
feat: add types-beta-4, add T check on socket methods
tabaktoni Nov 26, 2024
1a9a1fd
Merge branch 'develop' into feat/ws
tabaktoni Nov 26, 2024
8fcf1a5
test: stabilize websocket tests (#1272)
penovicp Nov 27, 2024
ecf6290
feat: onReorg
tabaktoni Nov 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions __tests__/WebSocketChannel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import { WebSocket } from 'isows';

import { Provider, WSSubscriptions, WebSocketChannel } from '../src';
import { StarknetChainId } from '../src/constants';
import { getTestAccount, getTestProvider } from './config/fixtures';

const nodeUrl = 'wss://sepolia-pathfinder-rpc.spaceshard.io/rpc/v0_8';

describe('websocket specific endpoints - pathfinder test', () => {
// account provider
const provider = new Provider(getTestProvider());
const account = getTestAccount(provider);

// websocket
let webSocketChannel: WebSocketChannel;

beforeAll(async () => {
webSocketChannel = new WebSocketChannel({ nodeUrl });
expect(webSocketChannel.isConnected()).toBe(false);
try {
await webSocketChannel.waitForConnection();
} catch (error: any) {
console.log(error.message);
}
expect(webSocketChannel.isConnected()).toBe(true);
});

afterAll(async () => {
expect(webSocketChannel.isConnected()).toBe(true);
webSocketChannel.disconnect();
await expect(webSocketChannel.waitForDisconnection()).resolves.toBe(WebSocket.CLOSED);
});

test('Test WS Error and edge cases', async () => {
webSocketChannel.disconnect();

// should fail as disconnected
await expect(webSocketChannel.subscribeNewHeads()).rejects.toThrow();

// should reconnect
webSocketChannel.reconnect();
await webSocketChannel.waitForConnection();

// should succeed after reconnection
await expect(webSocketChannel.subscribeNewHeads()).resolves.toEqual(expect.any(Number));

// should fail because already subscribed
await expect(webSocketChannel.subscribeNewHeads()).resolves.toBe(false);
});

test('onUnsubscribe with unsubscribeNewHeads', async () => {
const mockOnUnsubscribe = jest.fn().mockImplementation((subId: number) => {
expect(subId).toEqual(expect.any(Number));
});
webSocketChannel.onUnsubscribe = mockOnUnsubscribe;

await webSocketChannel.subscribeNewHeads();
await expect(webSocketChannel.unsubscribeNewHeads()).resolves.toBe(true);
await expect(webSocketChannel.unsubscribeNewHeads()).rejects.toThrow();

expect(mockOnUnsubscribe).toHaveBeenCalled();
expect(webSocketChannel.subscriptions.has(WSSubscriptions.NEW_HEADS)).toBeFalsy();
});

test('Test subscribeNewHeads', async () => {
await webSocketChannel.subscribeNewHeads();

let i = 0;
webSocketChannel.onNewHeads = async function (data) {
expect(this).toBeInstanceOf(WebSocketChannel);
i += 1;
ivpavici marked this conversation as resolved.
Show resolved Hide resolved
// TODO : Add data format validation
expect(data.result).toBeDefined();
if (i === 2) {
const status = await webSocketChannel.unsubscribeNewHeads();
expect(status).toBe(true);
}
};
const expectedId = webSocketChannel.subscriptions.get(WSSubscriptions.NEW_HEADS);
const subscriptionId = await webSocketChannel.waitForUnsubscription(expectedId);
expect(subscriptionId).toBe(expectedId);
expect(webSocketChannel.subscriptions.get(WSSubscriptions.NEW_HEADS)).toBe(undefined);
});

test('Test subscribeEvents', async () => {
await webSocketChannel.subscribeEvents();

let i = 0;
webSocketChannel.onEvents = async (data) => {
i += 1;
// TODO : Add data format validation
expect(data.result).toBeDefined();
if (i === 5) {
const status = await webSocketChannel.unsubscribeEvents();
expect(status).toBe(true);
}
};
const expectedId = webSocketChannel.subscriptions.get(WSSubscriptions.EVENTS);
const subscriptionId = await webSocketChannel.waitForUnsubscription(expectedId);
expect(subscriptionId).toBe(expectedId);
expect(webSocketChannel.subscriptions.get(WSSubscriptions.EVENTS)).toBe(undefined);
});

test('Test subscribePendingTransaction', async () => {
await webSocketChannel.subscribePendingTransaction(true);

let i = 0;
webSocketChannel.onPendingTransaction = async (data) => {
i += 1;
// TODO : Add data format validation
expect(data.result).toBeDefined();
if (i === 5) {
const status = await webSocketChannel.unsubscribePendingTransaction();
expect(status).toBe(true);
}
};
const expectedId = webSocketChannel.subscriptions.get(WSSubscriptions.PENDING_TRANSACTION);
const subscriptionId = await webSocketChannel.waitForUnsubscription(expectedId);
expect(subscriptionId).toBe(expectedId);
expect(webSocketChannel.subscriptions.get(WSSubscriptions.PENDING_TRANSACTION)).toBe(undefined);
});

test('Test subscribeTransactionStatus', async () => {
const { transaction_hash } = await account.execute({
contractAddress: '0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d',
entrypoint: 'transfer',
calldata: [account.address, '10', '0'],
});

let i = 0;
webSocketChannel.onTransactionStatus = async (data) => {
i += 1;
// TODO : Add data format validation
expect(data.result).toBeDefined();
if (i >= 1) {
const status = await webSocketChannel.unsubscribeTransactionStatus();
expect(status).toBe(true);
}
};

const subid = await webSocketChannel.subscribeTransactionStatus(transaction_hash);
expect(subid).toEqual(expect.any(Number));
const expectedId = webSocketChannel.subscriptions.get(WSSubscriptions.TRANSACTION_STATUS);
const subscriptionId = await webSocketChannel.waitForUnsubscription(expectedId);
expect(subscriptionId).toEqual(expectedId);
expect(webSocketChannel.subscriptions.get(WSSubscriptions.TRANSACTION_STATUS)).toBe(undefined);
});

test('Test subscribeTransactionStatus and block_id', async () => {
const latestBlock = await account.getBlockLatestAccepted();
const blockId = latestBlock.block_number - 5;

const { transaction_hash } = await account.execute({
contractAddress: '0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d',
entrypoint: 'transfer',
calldata: [account.address, '10', '0'],
});

let i = 0;
webSocketChannel.onTransactionStatus = async (data) => {
i += 1;
// TODO : Add data format validation
expect(data.result).toBeDefined();
if (i >= 1) {
const status = await webSocketChannel.unsubscribeTransactionStatus();
expect(status).toBe(true);
}
};

const subid = await webSocketChannel.subscribeTransactionStatus(transaction_hash, blockId);
expect(subid).toEqual(expect.any(Number));
const expectedId = webSocketChannel.subscriptions.get(WSSubscriptions.TRANSACTION_STATUS);
const subscriptionId = await webSocketChannel.waitForUnsubscription(expectedId);
expect(subscriptionId).toEqual(expectedId);
expect(webSocketChannel.subscriptions.get(WSSubscriptions.TRANSACTION_STATUS)).toBe(undefined);
});
});

describe('websocket regular endpoints - pathfinder test', () => {
let webSocketChannel: WebSocketChannel;

beforeAll(async () => {
webSocketChannel = new WebSocketChannel({ nodeUrl });
expect(webSocketChannel.isConnected()).toBe(false);
const status = await webSocketChannel.waitForConnection();
expect(status).toBe(WebSocket.OPEN);
});

afterAll(async () => {
expect(webSocketChannel.isConnected()).toBe(true);
webSocketChannel.disconnect();
});

test('regular rpc endpoint', async () => {
const response = await webSocketChannel.sendReceive('starknet_chainId');
expect(response).toBe(StarknetChainId.SN_SEPOLIA);
});
});
5 changes: 5 additions & 0 deletions __tests__/utils/stark.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import * as json from '../../src/utils/json';

const { IS_BROWSER } = constants;

// isows has faulty module resolution in the browser emulation environment which prevents test execution
// it is not required for these tests so removing it with a mock circumvents the issue
jest.mock('isows', () => jest.fn());

test('isBrowser', () => {
expect(IS_BROWSER).toBe(true);
});

describe('compressProgram()', () => {
// the @noble/curves dependency that is included in this file through utils/stark executes precomputations
// that rely on TextEncoder. The jsdom environment does not expose TextEncoder and it also overrides
Expand Down
Loading
Loading