1
- import { NostrEvent } from "../core/nips/01.ts" ;
1
+ import {
2
+ EventId ,
3
+ NostrEvent ,
4
+ OkMessage ,
5
+ PublishMessage ,
6
+ } from "../core/nips/01.ts" ;
2
7
import { Relay } from "../client.ts" ;
3
8
import { afterAll , beforeAll , describe , it } from "../lib/std/testing.ts" ;
4
- import { assert , assertEquals , assertObjectMatch } from "../lib/std/assert.ts" ;
9
+ import {
10
+ assert ,
11
+ assertEquals ,
12
+ assertObjectMatch ,
13
+ assertRejects ,
14
+ } from "../lib/std/assert.ts" ;
5
15
import { MockWebSocket } from "../lib/testing.ts" ;
6
16
7
17
const url = "wss://localhost:8080" ;
@@ -13,19 +23,15 @@ describe("Relay constructor", () => {
13
23
beforeAll ( ( ) => {
14
24
relay = new Relay ( url ) ;
15
25
} ) ;
16
-
17
26
it ( "should be constructable" , ( ) => {
18
27
assert ( relay instanceof Relay ) ;
19
28
} ) ;
20
-
21
29
it ( "should have a url" , ( ) => {
22
30
assertEquals ( relay . config . url , url ) ;
23
31
} ) ;
24
-
25
32
it ( "should have a name" , ( ) => {
26
33
assertEquals ( relay . config . name , "localhost:8080" ) ;
27
34
} ) ;
28
-
29
35
it ( "should have default options" , ( ) => {
30
36
assertObjectMatch ( relay . config , {
31
37
nbuffer : 10 ,
@@ -37,7 +43,6 @@ describe("Relay constructor", () => {
37
43
38
44
describe ( "called with url and options" , ( ) => {
39
45
const logger = { info : ( ) => { } } ;
40
-
41
46
beforeAll ( ( ) => {
42
47
relay = new Relay ( url , {
43
48
name : "test" ,
@@ -47,15 +52,12 @@ describe("Relay constructor", () => {
47
52
logger,
48
53
} ) ;
49
54
} ) ;
50
-
51
- afterAll ( async ( ) => {
52
- await relay . close ( ) ;
55
+ afterAll ( ( ) => {
56
+ relay . close ( ) ;
53
57
} ) ;
54
-
55
58
it ( "should be constructable" , ( ) => {
56
59
assert ( relay instanceof Relay ) ;
57
60
} ) ;
58
-
59
61
it ( "should have the given options" , ( ) => {
60
62
assertObjectMatch ( relay . config , {
61
63
name : "test" ,
@@ -79,21 +81,18 @@ describe("Relay", () => {
79
81
globalThis . WebSocket = MockWebSocket ;
80
82
relay = new Relay ( url ) ;
81
83
} ) ;
82
-
83
84
afterAll ( ( ) => {
84
85
relay . close ( ) ;
85
86
} ) ;
86
87
87
88
it ( "should not be connected initially" , ( ) => {
88
89
assertEquals ( relay . status , WebSocket . CLOSED ) ;
89
90
} ) ;
90
-
91
91
it ( "should not connect when a subscription is created" , ( ) => {
92
92
sub_1 = relay . subscribe ( { kinds : [ 1 ] } , { id : "test-1" } ) ;
93
93
assert ( sub_1 instanceof ReadableStream ) ;
94
94
assertEquals ( relay . status , WebSocket . CLOSED ) ;
95
95
} ) ;
96
-
97
96
it ( "should receive text notes" , async ( ) => {
98
97
const reader = sub_1 . getReader ( ) ;
99
98
const read = reader . read ( ) ;
@@ -108,15 +107,13 @@ describe("Relay", () => {
108
107
assertEquals ( value . kind , 1 ) ;
109
108
reader . releaseLock ( ) ;
110
109
} ) ;
111
-
112
110
it ( "should be able to open multiple subscriptions" , ( ) => {
113
111
sub_0 = relay . subscribe ( { kinds : [ 0 ] , limit : 1 } , { id : "test-0" } ) ;
114
112
assert ( sub_0 instanceof ReadableStream ) ;
115
113
} ) ;
116
-
117
114
it ( "should recieve metas and notes simultaneously" , async ( ) => {
118
- const read_0 = sub_0 . getReader ( ) . read ( ) ;
119
- const read_1 = sub_1 . getReader ( ) . read ( ) ;
115
+ const reader_0 = sub_0 . getReader ( ) ;
116
+ const reader_1 = sub_1 . getReader ( ) ;
120
117
const ws = MockWebSocket . instances [ 0 ] ;
121
118
ws . dispatchEvent (
122
119
new MessageEvent ( "message" , {
@@ -129,12 +126,58 @@ describe("Relay", () => {
129
126
} ) ,
130
127
) ;
131
128
const [ { value : value_0 } , { value : value_1 } ] = await Promise . all ( [
132
- read_0 ,
133
- read_1 ,
129
+ reader_0 . read ( ) ,
130
+ reader_1 . read ( ) ,
134
131
] ) ;
135
132
assert ( value_0 ) ;
136
133
assertEquals ( value_0 . kind , 0 ) ;
137
134
assert ( value_1 ) ;
138
135
assertEquals ( value_1 . kind , 1 ) ;
136
+ reader_0 . releaseLock ( ) ;
137
+ reader_1 . releaseLock ( ) ;
138
+ } ) ;
139
+ it ( "should publish an event and recieve an accepting OK message" , async ( ) => {
140
+ const eid = "test-true" as EventId ;
141
+ const ok = [ "OK" , eid , true , "" ] satisfies OkMessage < true > ;
142
+ const ws = MockWebSocket . instances [ 0 ] ;
143
+ const arrived = new Promise < true > ( ( resolve ) => {
144
+ ws . remote . addEventListener (
145
+ "message" ,
146
+ ( ev : MessageEvent < string > ) => {
147
+ const [ , event ] = JSON . parse ( ev . data ) as PublishMessage < 1 > ;
148
+ if ( event . id === eid ) {
149
+ assertObjectMatch ( event , { kind : 1 } ) ;
150
+ resolve ( true ) ;
151
+ ws . remote . send ( JSON . stringify ( ok ) ) ;
152
+ }
153
+ } ,
154
+ ) ;
155
+ } ) ;
156
+ const event = { id : eid , kind : 1 } ;
157
+ // deno-lint-ignore no-explicit-any
158
+ await relay . publish ( event as any ) ;
159
+ assert ( await arrived ) ;
160
+ } ) ;
161
+ it ( "should receieve a rejecting OK message" , async ( ) => {
162
+ const eid = "test-false" as EventId ;
163
+ const ok = [ "OK" , eid , false , "error: test" ] satisfies OkMessage < false > ;
164
+ const ws = MockWebSocket . instances [ 0 ] ;
165
+ const arrived = new Promise < true > ( ( resolve ) => {
166
+ ws . remote . addEventListener (
167
+ "message" ,
168
+ ( ev : MessageEvent < string > ) => {
169
+ const [ , event ] = JSON . parse ( ev . data ) as PublishMessage < 1 > ;
170
+ if ( event . id === eid ) {
171
+ assertEquals ( event . kind , 1 ) ;
172
+ resolve ( true ) ;
173
+ ws . remote . send ( JSON . stringify ( ok ) ) ;
174
+ }
175
+ } ,
176
+ ) ;
177
+ } ) ;
178
+ const event = { id : eid , kind : 1 } ;
179
+ // deno-lint-ignore no-explicit-any
180
+ assertRejects ( ( ) => relay . publish ( event as any ) ) ;
181
+ await arrived ;
139
182
} ) ;
140
183
} ) ;
0 commit comments