Skip to content

Commit 18d3151

Browse files
committed
add e2e tests for events
1 parent 0b0e99e commit 18d3151

2 files changed

Lines changed: 113 additions & 64 deletions

File tree

src/test/chain-simulator/utils/test.utils.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios';
22
import { config } from '../config/env.config';
3-
import { DeployScArgs } from './chain.simulator.operations';
3+
import { DeployScArgs, sendTransaction, SendTransactionArgs } from './chain.simulator.operations';
44
import { fundAddress } from './chain.simulator.operations';
55
import { deploySc } from './chain.simulator.operations';
66
import fs from 'fs';
@@ -96,4 +96,23 @@ export class ChainSimulatorUtils {
9696
throw error;
9797
}
9898
}
99+
100+
public static async pingContract(sender: string, scAddress: string) {
101+
await sendTransaction(new SendTransactionArgs({
102+
chainSimulatorUrl: config.chainSimulatorUrl,
103+
sender,
104+
receiver: scAddress,
105+
value: '1000000000000000000',
106+
dataField: 'ping',
107+
}));
108+
}
109+
110+
public static async pongContract(sender: string, scAddress: string) {
111+
await sendTransaction(new SendTransactionArgs({
112+
chainSimulatorUrl: config.chainSimulatorUrl,
113+
sender,
114+
receiver: scAddress,
115+
dataField: 'pong',
116+
}));
117+
}
99118
}

src/test/chain-simulator/websocket.subscriptions.cs-e2e.ts

Lines changed: 93 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,142 +2,172 @@ import axios from "axios";
22
import { config } from "./config/env.config";
33
import { fundAddress, transferEgld } from "./utils/chain.simulator.operations";
44
import { io, Socket } from "socket.io-client";
5+
import { ChainSimulatorUtils } from "./utils/test.utils";
56

67
const WS_SERVER_URL = `${config.subscriptionsServiceUrl}`;
78

8-
const subscriptionsResponses: Map<string, any[]> = new Map();
9+
const txResponses: Map<string, any[]> = new Map();
10+
const eventResponses: Map<string, any[]> = new Map();
911

10-
const filters = {
12+
const txFilters = {
1113
CLIENT_1: { sender: config.aliceAddress },
1214
CLIENT_2: { sender: config.bobAddress },
1315
CLIENT_3: { sender: config.aliceAddress, receiver: config.bobAddress },
1416
};
1517

18+
const eventFilters = {
19+
CLIENT_1: { identifier: 'pong' },
20+
CLIENT_2: { address: '' }, // added dinamically after deploy sc
21+
CLIENT_3: { identifier: 'completedTxEvent', address: '' }, // added dinamically after deploy sc
22+
};
23+
1624
const filterKeys = {
17-
CLIENT_1: JSON.stringify(filters.CLIENT_1),
18-
CLIENT_2: JSON.stringify(filters.CLIENT_2),
19-
CLIENT_3: JSON.stringify(filters.CLIENT_3),
25+
CLIENT_1: "KEY_CLIENT_1",
26+
CLIENT_2: "KEY_CLIENT_2",
27+
CLIENT_3: "KEY_CLIENT_3",
2028
};
2129

2230
const filterMap = [
23-
{ key: filterKeys.CLIENT_1, filter: filters.CLIENT_1, clientId: "client1" },
24-
{ key: filterKeys.CLIENT_2, filter: filters.CLIENT_2, clientId: "client2" },
25-
{ key: filterKeys.CLIENT_3, filter: filters.CLIENT_3, clientId: "client3" },
31+
{ key: filterKeys.CLIENT_1, txFilter: txFilters.CLIENT_1, eventFilter: eventFilters.CLIENT_1, clientId: "client1" },
32+
{ key: filterKeys.CLIENT_2, txFilter: txFilters.CLIENT_2, eventFilter: eventFilters.CLIENT_2, clientId: "client2" },
33+
{ key: filterKeys.CLIENT_3, txFilter: txFilters.CLIENT_3, eventFilter: eventFilters.CLIENT_3, clientId: "client3" },
2634
];
2735

36+
let pingPongScAddress = '';
2837

29-
describe('Websocket subscriptions e2e tests with chain simulator', () => {
38+
describe('Websocket subscriptions e2e tests (Txs and Events)', () => {
3039
const clients: Socket[] = [];
3140

32-
const connectAndSubscribe = (filterKey: string, filter: any, clientId: string) => {
33-
const clientLabel = clientId;
41+
const connectAndSubscribe = (filterKey: string, txFilter: any, eventFilter: any, clientId: string) => {
3442
const receivedTxs: any[] = [];
43+
const receivedEvents: any[] = [];
3544

36-
subscriptionsResponses.set(filterKey, receivedTxs);
45+
txResponses.set(filterKey, receivedTxs);
46+
eventResponses.set(filterKey, receivedEvents);
3747

3848
const client: Socket = io(WS_SERVER_URL, {
3949
path: '/ws/subscription',
4050
});
4151
clients.push(client);
4252

43-
44-
4553
client.on("connect_error", (err) => {
46-
throw new Error(`${clientLabel} connection failed: ${err.message}`);
47-
});
48-
49-
client.on("error", (err) => {
50-
throw new Error(`Error for ${clientLabel}: ${err.message}`);
54+
throw new Error(`${clientId} connection failed: ${err.message}`);
5155
});
5256

5357
client.on("customTransactionUpdate", (data: { transactions: any[] }) => {
54-
console.log(`\n💸 ${clientLabel} received ${data.transactions.length} txs`);
58+
console.log(`\n💸 ${clientId} received ${data.transactions.length} txs`);
5559
receivedTxs.push(...data.transactions);
5660
});
5761

62+
client.on("customEventUpdate", (data: { events: any[] }) => {
63+
console.log(`\n🔔 ${clientId} received ${data.events.length} events`);
64+
receivedEvents.push(...data.events);
65+
});
66+
5867
client.on("connect", () => {
59-
console.log(`\n ${clientLabel} subscribing to TXs:`, JSON.stringify(filter));
68+
console.log(`\n ${clientId} connected.`);
6069

61-
client.emit("subscribeCustomTransactions", filter, (ack: any) => {
62-
console.log('ACK Response:', ack);
70+
client.emit("subscribeCustomTransactions", txFilter, (ack: any) => {
71+
console.log(` ACK TXs ${clientId}:`, ack);
6372
});
64-
});
6573

74+
client.emit("subscribeCustomEvents", eventFilter, (ack: any) => {
75+
console.log(` ACK Events ${clientId}:`, ack);
76+
});
77+
});
6678
};
6779

6880
beforeAll(async () => {
69-
console.log("--- Executing beforeAll (Setup) ---");
70-
7181
try {
7282
await fundAddress(config.chainSimulatorUrl, config.aliceAddress);
7383
await fundAddress(config.chainSimulatorUrl, config.bobAddress);
7484
await axios.post(`${config.chainSimulatorUrl}/simulator/generate-blocks/1`);
7585

86+
pingPongScAddress = await ChainSimulatorUtils.deployPingPongSc(config.bobAddress);
87+
eventFilters.CLIENT_2.address = pingPongScAddress;
88+
eventFilters.CLIENT_3.address = pingPongScAddress;
89+
7690
for (const item of filterMap) {
77-
connectAndSubscribe(item.key, item.filter, item.clientId);
91+
connectAndSubscribe(item.key, item.txFilter, item.eventFilter, item.clientId);
7892
}
7993

80-
// await for clients to connect
81-
console.log(`Awaiting for clients to connect...`);
8294
await new Promise(resolve => setTimeout(resolve, 5000));
8395

84-
console.log("\n--- Starting Transactions ---");
96+
console.log("\n--- Starting Operations ---");
8597

8698
await transferEgld(config.chainSimulatorUrl, config.aliceAddress, config.bobAddress, 1);
8799
await transferEgld(config.chainSimulatorUrl, config.bobAddress, config.aliceAddress, 2);
88100

89-
console.log("--- Generating Block and waiting for WS responses ---");
101+
102+
await ChainSimulatorUtils.pingContract(config.aliceAddress, pingPongScAddress);
103+
await ChainSimulatorUtils.pongContract(config.aliceAddress, pingPongScAddress);
104+
90105
await axios.post(`${config.chainSimulatorUrl}/simulator/generate-blocks/10`);
91106

92107
await new Promise(resolve => setTimeout(resolve, 15000));
93108

94-
console.log("--- Setup Complete ---");
95-
96109
} catch (e: any) {
97-
console.error("An error occured in beforeAll:", e.message);
98-
110+
console.error("Error in beforeAll:", e.message);
99111
throw e;
100112
}
101113
});
102114

103115
afterAll(() => {
104116
clients.forEach(client => client.connected && client.disconnect());
105-
console.log("\n--- All clients disconnected ---");
106117
});
107118

108-
// --- TESTE SEPARATE (itShould...) ---
119+
it('should receive TXs sent by Alice for Client 1', () => {
120+
const txs = txResponses.get(filterKeys.CLIENT_1);
121+
expect(txs?.length).toBe(3);
122+
123+
txs?.forEach((tx) => {
124+
expect(tx.sender).toEqual(config.aliceAddress);
125+
});
126+
});
109127

110-
it('should receive only the transaction sent by Alice (Tx 1: Alice -> Bob) when filtering by CLIENT_1', () => {
111-
const filterKey = filterKeys.CLIENT_1;
112-
const aliceTxs = subscriptionsResponses.get(filterKey);
113-
console.log(`\nRunning test for ${filterMap.find(f => f.key === filterKey)?.clientId}`);
128+
it('should receive Events with identifier "pong" for Client 1', () => {
129+
const events = eventResponses.get(filterKeys.CLIENT_1);
130+
expect(events?.length).toBe(1);
114131

115-
expect(aliceTxs?.length).toBe(1);
116-
const tx = aliceTxs?.[0];
117-
expect(tx.sender).toEqual(config.aliceAddress);
118-
expect(tx.sender).not.toEqual(config.bobAddress);
132+
events?.forEach((evt) => {
133+
expect(evt.identifier).toEqual('pong');
134+
});
119135
});
120136

121-
it('should receive only the transaction sent by Bob (Tx 2: Bob -> Alice) when filtering by CLIENT_2', () => {
122-
const filterKey = filterKeys.CLIENT_2;
123-
const bobTxs = subscriptionsResponses.get(filterKey);
124-
console.log(`\nRunning test for ${filterMap.find(f => f.key === filterKey)?.clientId}`);
137+
it('should receive TXs sent by Bob for Client 2', () => {
138+
const txs = txResponses.get(filterKeys.CLIENT_2);
139+
expect(txs?.length).toBe(2);
125140

126-
expect(bobTxs?.length).toBe(1);
127-
const tx = bobTxs?.[0];
128-
expect(tx.sender).toEqual(config.bobAddress);
129-
expect(tx.receiver).toEqual(config.aliceAddress);
130-
expect(tx.sender).not.toEqual(config.aliceAddress);
141+
txs?.forEach((tx) => {
142+
expect(tx.sender).toEqual(config.bobAddress);
143+
});
131144
});
132145

133-
it('should receive only the transaction sent by Alice to Bob (Tx 1) when filtering by CLIENT_3', () => {
134-
const filterKey = filterKeys.CLIENT_3;
135-
const aliceToBobTxs = subscriptionsResponses.get(filterKey);
136-
console.log(`\nRunning test for ${filterMap.find(f => f.key === filterKey)?.clientId}`);
146+
it('should receive Events generated by PingPong contract (address) for Client 2', () => {
147+
const events = eventResponses.get(filterKeys.CLIENT_2);
148+
expect(events?.length).toBe(7);
137149

138-
expect(aliceToBobTxs?.length).toBe(1);
139-
const tx = aliceToBobTxs?.[0];
140-
expect(tx.sender).toEqual(config.aliceAddress);
141-
expect(tx.receiver).toEqual(config.bobAddress);
150+
events?.forEach((evt) => {
151+
expect(evt.address).toEqual(pingPongScAddress);
152+
});
153+
});
154+
155+
it('should receive specific Alice-to-Bob TXs for Client 3', () => {
156+
const txs = txResponses.get(filterKeys.CLIENT_3);
157+
expect(txs?.length).toBeGreaterThanOrEqual(1);
158+
159+
txs?.forEach((tx) => {
160+
expect(tx.sender).toEqual(config.aliceAddress);
161+
expect(tx.receiver).toEqual(config.bobAddress);
162+
});
163+
});
164+
165+
it('should receive Events with identifier "completedTxEvent" and from Ping Pong sc address for Client 3', () => {
166+
const events = eventResponses.get(filterKeys.CLIENT_3);
167+
expect(events?.length).toBe(2);
168+
169+
events?.forEach((evt) => {
170+
expect(evt.identifier).toEqual('completedTxEvent');
171+
});
142172
});
143-
});
173+
});

0 commit comments

Comments
 (0)