1
1
import { createEncoder , waitForRemotePeer } from "@waku/core" ;
2
2
import { createLightNode } from "@waku/create" ;
3
- import type { LightNode } from "@waku/interfaces" ;
3
+ import { LightNode , SendError } from "@waku/interfaces" ;
4
4
import { Protocols } from "@waku/interfaces" ;
5
5
import { utf8ToBytes } from "@waku/utils/bytes" ;
6
6
import { expect } from "chai" ;
@@ -9,6 +9,7 @@ import debug from "debug";
9
9
import {
10
10
base64ToUtf8 ,
11
11
delay ,
12
+ generateRandomUint8Array ,
12
13
makeLogFileName ,
13
14
MessageRpcResponse ,
14
15
NOISE_KEY_1 ,
@@ -26,24 +27,43 @@ describe("Waku Light Push [node only]", () => {
26
27
let waku : LightNode ;
27
28
let nwaku : Nwaku ;
28
29
29
- afterEach ( async function ( ) {
30
- ! ! nwaku &&
31
- nwaku . stop ( ) . catch ( ( e ) => console . log ( "Nwaku failed to stop" , e ) ) ;
32
- ! ! waku && waku . stop ( ) . catch ( ( e ) => console . log ( "Waku failed to stop" , e ) ) ;
33
- } ) ;
34
-
35
- it ( "Push successfully" , async function ( ) {
36
- this . timeout ( 15_000 ) ;
37
-
38
- nwaku = new Nwaku ( makeLogFileName ( this ) ) ;
39
- await nwaku . start ( { lightpush : true , relay : true } ) ;
30
+ const runNodes = async (
31
+ context : Mocha . Context ,
32
+ pubSubTopic ?: string
33
+ ) : Promise < void > => {
34
+ const nwakuOptional = pubSubTopic ? { topics : pubSubTopic } : { } ;
35
+ nwaku = new Nwaku ( makeLogFileName ( context ) ) ;
36
+ await nwaku . start ( {
37
+ lightpush : true ,
38
+ relay : true ,
39
+ ... nwakuOptional ,
40
+ } ) ;
40
41
41
42
waku = await createLightNode ( {
43
+ pubSubTopic,
42
44
staticNoiseKey : NOISE_KEY_1 ,
43
45
} ) ;
44
46
await waku . start ( ) ;
45
47
await waku . dial ( await nwaku . getMultiaddrWithId ( ) ) ;
46
48
await waitForRemotePeer ( waku , [ Protocols . LightPush ] ) ;
49
+ } ;
50
+
51
+ beforeEach ( async function ( ) {
52
+ this . timeout ( 15_000 ) ;
53
+ await runNodes ( this ) ;
54
+ } ) ;
55
+
56
+ afterEach ( async function ( ) {
57
+ try {
58
+ nwaku ?. stop ( ) ;
59
+ waku ?. stop ( ) ;
60
+ } catch ( e ) {
61
+ console . error ( "Failed to stop nodes: " , e ) ;
62
+ }
63
+ } ) ;
64
+
65
+ it ( "Push successfully" , async function ( ) {
66
+ this . timeout ( 15_000 ) ;
47
67
48
68
const messageText = "Light Push works!" ;
49
69
@@ -63,28 +83,39 @@ describe("Waku Light Push [node only]", () => {
63
83
expect ( base64ToUtf8 ( msgs [ 0 ] . payload ) ) . to . equal ( messageText ) ;
64
84
} ) ;
65
85
66
- it ( "Push on custom pubsub topic " , async function ( ) {
86
+ it ( "Pushes messages equal or less that 1MB " , async function ( ) {
67
87
this . timeout ( 15_000 ) ;
88
+ const MB = 1024 ** 2 ;
68
89
69
- const customPubSubTopic = "/waku/2/custom-dapp/proto" ;
90
+ let pushResponse = await waku . lightPush . send ( TestEncoder , {
91
+ payload : generateRandomUint8Array ( MB ) ,
92
+ } ) ;
93
+ expect ( pushResponse . recipients . length ) . to . greaterThan ( 0 ) ;
70
94
71
- nwaku = new Nwaku ( makeLogFileName ( this ) ) ;
72
- await nwaku . start ( {
73
- lightpush : true ,
74
- topics : customPubSubTopic ,
75
- relay : true ,
95
+ pushResponse = await waku . lightPush . send ( TestEncoder , {
96
+ payload : generateRandomUint8Array ( 65536 ) ,
76
97
} ) ;
98
+ expect ( pushResponse . recipients . length ) . to . greaterThan ( 0 ) ;
99
+ } ) ;
77
100
78
- waku = await createLightNode ( {
79
- pubSubTopic : customPubSubTopic ,
80
- staticNoiseKey : NOISE_KEY_1 ,
101
+ it ( "Fails to push message bigger that 1MB" , async function ( ) {
102
+ this . timeout ( 15_000 ) ;
103
+ const MB = 1024 ** 2 ;
104
+
105
+ const pushResponse = await waku . lightPush . send ( TestEncoder , {
106
+ payload : generateRandomUint8Array ( MB + 65536 ) ,
81
107
} ) ;
82
- await waku . start ( ) ;
83
- await waku . dial ( await nwaku . getMultiaddrWithId ( ) ) ;
84
- await waitForRemotePeer ( waku , [ Protocols . LightPush ] ) ;
108
+ expect ( pushResponse . recipients . length ) . to . eq ( 0 ) ;
109
+ expect ( pushResponse . error ) . to . eq ( SendError . SIZE_TOO_BIG ) ;
110
+ } ) ;
85
111
86
- const nimPeerId = await nwaku . getPeerId ( ) ;
112
+ it ( "Push on custom pubsub topic" , async function ( ) {
113
+ this . timeout ( 15_000 ) ;
87
114
115
+ const customPubSubTopic = "/waku/2/custom-dapp/proto" ;
116
+ await runNodes ( this , customPubSubTopic ) ;
117
+
118
+ const nimPeerId = await nwaku . getPeerId ( ) ;
88
119
const messageText = "Light Push works!" ;
89
120
90
121
log ( "Send message via lightpush" ) ;
0 commit comments