1
1
import sinon from 'sinon' ;
2
2
import { expect } from 'chai' ;
3
3
import { utils as cryptoUtils } from '@web5/crypto' ;
4
- import { DwnConstant } from '@tbd54566975/dwn-sdk-js' ;
4
+ import { DwnConstant , ProtocolDefinition } from '@tbd54566975/dwn-sdk-js' ;
5
5
6
6
import type { BearerIdentity } from '../src/bearer-identity.js' ;
7
7
@@ -82,7 +82,56 @@ describe('SyncEngineLevel', () => {
82
82
await testHarness . closeStorage ( ) ;
83
83
} ) ;
84
84
85
- it ( 'syncs multiple records in both directions' , async ( ) => {
85
+ it ( 'syncs multiple messages in both directions' , async ( ) => {
86
+ // create 1 local protocol configure
87
+ const protocolDefinition1 : ProtocolDefinition = {
88
+ published : true ,
89
+ protocol : 'https://protocol.xyz/example/1' ,
90
+ types : {
91
+ foo : {
92
+ schema : 'https://schemas.xyz/foo' ,
93
+ dataFormats : [ 'text/plain' , 'application/json' ]
94
+ }
95
+ } ,
96
+ structure : {
97
+ foo : { }
98
+ }
99
+ } ;
100
+
101
+ const protocolsConfigure1 = await testHarness . agent . processDwnRequest ( {
102
+ author : alice . did . uri ,
103
+ target : alice . did . uri ,
104
+ messageType : DwnInterface . ProtocolsConfigure ,
105
+ messageParams : {
106
+ definition : protocolDefinition1
107
+ }
108
+ } ) ;
109
+
110
+ // create 1 remote protocol configure
111
+ const protocolDefinition2 : ProtocolDefinition = {
112
+ published : true ,
113
+ protocol : 'https://protocol.xyz/example/2' ,
114
+ types : {
115
+ bar : {
116
+ schema : 'https://schemas.xyz/bar' ,
117
+ dataFormats : [ 'text/plain' , 'application/json' ]
118
+ }
119
+ } ,
120
+ structure : {
121
+ bar : { }
122
+ }
123
+ } ;
124
+
125
+ const protocolsConfigure2 = await testHarness . agent . sendDwnRequest ( {
126
+ author : alice . did . uri ,
127
+ target : alice . did . uri ,
128
+ messageType : DwnInterface . ProtocolsConfigure ,
129
+ messageParams : {
130
+ definition : protocolDefinition2
131
+ }
132
+ } ) ;
133
+
134
+
86
135
// create 3 local records.
87
136
const localRecords : string [ ] = [ ] ;
88
137
for ( let i = 0 ; i < 3 ; i ++ ) {
@@ -152,8 +201,20 @@ describe('SyncEngineLevel', () => {
152
201
remoteRecords . push ( ( writeResponse . message ! ) . recordId ) ;
153
202
}
154
203
204
+ // check that protocol1 exists locally
205
+ let localProtocolsQueryResponse = await testHarness . agent . dwn . processRequest ( {
206
+ author : alice . did . uri ,
207
+ target : alice . did . uri ,
208
+ messageType : DwnInterface . ProtocolsQuery ,
209
+ messageParams : { }
210
+ } ) ;
211
+ let localProtocolsQueryReply = localProtocolsQueryResponse . reply ;
212
+ expect ( localProtocolsQueryReply . status . code ) . to . equal ( 200 ) ;
213
+ expect ( localProtocolsQueryReply . entries ?. length ) . to . equal ( 1 ) ;
214
+ expect ( localProtocolsQueryReply . entries ) . to . have . deep . equal ( [ protocolsConfigure1 . message ] ) ;
215
+
155
216
// query local and check for only local records
156
- let localQueryResponse = await testHarness . agent . dwn . processRequest ( {
217
+ let localRecordsQueryResponse = await testHarness . agent . dwn . processRequest ( {
157
218
author : alice . did . uri ,
158
219
target : alice . did . uri ,
159
220
messageType : DwnInterface . RecordsQuery ,
@@ -164,14 +225,26 @@ describe('SyncEngineLevel', () => {
164
225
}
165
226
}
166
227
} ) ;
167
- let localDwnQueryReply = localQueryResponse . reply ;
168
- expect ( localDwnQueryReply . status . code ) . to . equal ( 200 ) ;
169
- expect ( localDwnQueryReply . entries ) . to . have . length ( 3 ) ;
170
- let localRecordsFromQuery = localDwnQueryReply . entries ?. map ( entry => entry . recordId ) ;
228
+ let localRecordsQueryReply = localRecordsQueryResponse . reply ;
229
+ expect ( localRecordsQueryReply . status . code ) . to . equal ( 200 ) ;
230
+ expect ( localRecordsQueryReply . entries ) . to . have . length ( 3 ) ;
231
+ let localRecordsFromQuery = localRecordsQueryReply . entries ?. map ( entry => entry . recordId ) ;
171
232
expect ( localRecordsFromQuery ) . to . have . members ( localRecords ) ;
172
233
234
+ // check that protocol2 exists remotely
235
+ let remoteProtocolsQueryResponse = await testHarness . agent . dwn . sendRequest ( {
236
+ author : alice . did . uri ,
237
+ target : alice . did . uri ,
238
+ messageType : DwnInterface . ProtocolsQuery ,
239
+ messageParams : { }
240
+ } ) ;
241
+ let remoteProtocolsQueryReply = remoteProtocolsQueryResponse . reply ;
242
+ expect ( remoteProtocolsQueryReply . status . code ) . to . equal ( 200 ) ;
243
+ expect ( remoteProtocolsQueryReply . entries ?. length ) . to . equal ( 1 ) ;
244
+ expect ( remoteProtocolsQueryReply . entries ) . to . have . deep . equal ( [ protocolsConfigure2 . message ] ) ;
245
+
173
246
// query remote and check for only remote records
174
- let remoteQueryResponse = await testHarness . agent . dwn . sendRequest ( {
247
+ let remoteRecordsQueryResponse = await testHarness . agent . dwn . sendRequest ( {
175
248
author : alice . did . uri ,
176
249
target : alice . did . uri ,
177
250
messageType : DwnInterface . RecordsQuery ,
@@ -182,10 +255,10 @@ describe('SyncEngineLevel', () => {
182
255
}
183
256
}
184
257
} ) ;
185
- let remoteDwnQueryReply = remoteQueryResponse . reply ;
186
- expect ( remoteDwnQueryReply . status . code ) . to . equal ( 200 ) ;
187
- expect ( remoteDwnQueryReply . entries ) . to . have . length ( 3 ) ;
188
- let remoteRecordsFromQuery = remoteDwnQueryReply . entries ?. map ( entry => entry . recordId ) ;
258
+ let remoteRecordsQueryReply = remoteRecordsQueryResponse . reply ;
259
+ expect ( remoteRecordsQueryReply . status . code ) . to . equal ( 200 ) ;
260
+ expect ( remoteRecordsQueryReply . entries ) . to . have . length ( 3 ) ;
261
+ let remoteRecordsFromQuery = remoteRecordsQueryReply . entries ?. map ( entry => entry . recordId ) ;
189
262
expect ( remoteRecordsFromQuery ) . to . have . members ( remoteRecords ) ;
190
263
191
264
// Register Alice's DID to be synchronized.
@@ -197,8 +270,20 @@ describe('SyncEngineLevel', () => {
197
270
await syncEngine . push ( ) ;
198
271
await syncEngine . pull ( ) ;
199
272
273
+ // query local to see all protocols
274
+ localProtocolsQueryResponse = await testHarness . agent . dwn . processRequest ( {
275
+ author : alice . did . uri ,
276
+ target : alice . did . uri ,
277
+ messageType : DwnInterface . ProtocolsQuery ,
278
+ messageParams : { }
279
+ } ) ;
280
+ localProtocolsQueryReply = localProtocolsQueryResponse . reply ;
281
+ expect ( localProtocolsQueryReply . status . code ) . to . equal ( 200 ) ;
282
+ expect ( localProtocolsQueryReply . entries ?. length ) . to . equal ( 2 ) ;
283
+ expect ( localProtocolsQueryReply . entries ) . to . have . deep . equal ( [ protocolsConfigure1 . message , protocolsConfigure2 . message ] ) ;
284
+
200
285
// query local node to see all records
201
- localQueryResponse = await testHarness . agent . dwn . processRequest ( {
286
+ localRecordsQueryResponse = await testHarness . agent . dwn . processRequest ( {
202
287
author : alice . did . uri ,
203
288
target : alice . did . uri ,
204
289
messageType : DwnInterface . RecordsQuery ,
@@ -209,14 +294,26 @@ describe('SyncEngineLevel', () => {
209
294
}
210
295
}
211
296
} ) ;
212
- localDwnQueryReply = localQueryResponse . reply ;
213
- expect ( localDwnQueryReply . status . code ) . to . equal ( 200 ) ;
214
- expect ( localDwnQueryReply . entries ) . to . have . length ( 6 , 'local' ) ;
215
- localRecordsFromQuery = localDwnQueryReply . entries ?. map ( entry => entry . recordId ) ;
297
+ localRecordsQueryReply = localRecordsQueryResponse . reply ;
298
+ expect ( localRecordsQueryReply . status . code ) . to . equal ( 200 ) ;
299
+ expect ( localRecordsQueryReply . entries ) . to . have . length ( 6 , 'local' ) ;
300
+ localRecordsFromQuery = localRecordsQueryReply . entries ?. map ( entry => entry . recordId ) ;
216
301
expect ( localRecordsFromQuery ) . to . have . members ( [ ...localRecords , ...remoteRecords ] ) ;
217
302
218
- // query remote node to see all results
219
- remoteQueryResponse = await testHarness . agent . dwn . sendRequest ( {
303
+ // query remote node to see all protocols
304
+ remoteProtocolsQueryResponse = await testHarness . agent . dwn . sendRequest ( {
305
+ author : alice . did . uri ,
306
+ target : alice . did . uri ,
307
+ messageType : DwnInterface . ProtocolsQuery ,
308
+ messageParams : { }
309
+ } ) ;
310
+ remoteProtocolsQueryReply = remoteProtocolsQueryResponse . reply ;
311
+ expect ( remoteProtocolsQueryReply . status . code ) . to . equal ( 200 ) ;
312
+ expect ( remoteProtocolsQueryReply . entries ?. length ) . to . equal ( 2 ) ;
313
+ expect ( remoteProtocolsQueryReply . entries ) . to . have . deep . equal ( [ protocolsConfigure1 . message , protocolsConfigure2 . message ] ) ;
314
+
315
+ // query remote node to see all records
316
+ remoteRecordsQueryResponse = await testHarness . agent . dwn . sendRequest ( {
220
317
author : alice . did . uri ,
221
318
target : alice . did . uri ,
222
319
messageType : DwnInterface . RecordsQuery ,
@@ -227,10 +324,10 @@ describe('SyncEngineLevel', () => {
227
324
}
228
325
}
229
326
} ) ;
230
- remoteDwnQueryReply = remoteQueryResponse . reply ;
231
- expect ( remoteDwnQueryReply . status . code ) . to . equal ( 200 ) ;
232
- expect ( remoteDwnQueryReply . entries ) . to . have . length ( 6 , 'remote' ) ;
233
- remoteRecordsFromQuery = remoteDwnQueryReply . entries ?. map ( entry => entry . recordId ) ;
327
+ remoteRecordsQueryReply = remoteRecordsQueryResponse . reply ;
328
+ expect ( remoteRecordsQueryReply . status . code ) . to . equal ( 200 ) ;
329
+ expect ( remoteRecordsQueryReply . entries ) . to . have . length ( 6 , 'remote' ) ;
330
+ remoteRecordsFromQuery = remoteRecordsQueryReply . entries ?. map ( entry => entry . recordId ) ;
234
331
expect ( remoteRecordsFromQuery ) . to . have . members ( [ ...localRecords , ...remoteRecords ] ) ;
235
332
} ) . slow ( 1000 ) ; // Yellow at 500ms, Red at 1000ms.
236
333
@@ -412,7 +509,7 @@ describe('SyncEngineLevel', () => {
412
509
413
510
// spy on sendDwnRequest to the remote DWN
414
511
const sendDwnRequestSpy = sinon . spy ( testHarness . agent . rpc , 'sendDwnRequest' ) ;
415
- const processMessageSpy = sinon . spy ( testHarness . agent . dwn , 'processMessage' ) ;
512
+ const processMessageSpy = sinon . spy ( testHarness . agent . dwn . node , 'processMessage' ) ;
416
513
417
514
// Execute Sync to push records to Alice's remote node
418
515
await syncEngine . pull ( ) ;
0 commit comments