@xyo-network/node 2.45.0 → 2.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,7 @@ import { Account, PayloadWrapper, XyoPayload, XyoPayloadBuilder, XyoPayloadSchem
15
15
 
16
16
  import { NodeConfigSchema } from '../Config'
17
17
  import { MemoryNode } from '../MemoryNode'
18
+ import { NodeWrapper } from '../NodeWrapper'
18
19
 
19
20
  describe('MemoryNode', () => {
20
21
  const testAccount1 = new Account({ phrase: 'testPhrase1' })
@@ -35,12 +36,10 @@ describe('MemoryNode', () => {
35
36
  const diviner: AbstractModule = await ArchivistPayloadDiviner.create({
36
37
  config: { archivist: archivist.address, schema: XyoArchivistPayloadDivinerConfigSchema },
37
38
  })
38
- node.register(archivist)
39
- node.attach(archivist.address)
40
- node.register(diviner)
41
- node.attach(diviner.address)
42
- expect((await node.registered()).length).toBe(2)
43
- expect((await node.attached()).length).toBe(2)
39
+ await node.register(archivist).attach(archivist.address, true)
40
+ await node.register(diviner).attach(diviner.address, true)
41
+ expect(node.registered()).toBeArrayOfSize(2)
42
+ expect(await node.attached()).toBeArrayOfSize(2)
44
43
  const foundArchivist = (await node.resolve({ address: [archivist.address] })).shift()
45
44
  expect(foundArchivist).toBeDefined()
46
45
  expect(foundArchivist?.address).toBe(archivist.address)
@@ -145,17 +144,34 @@ describe('MemoryNode', () => {
145
144
  module = await MemoryArchivist.create()
146
145
  node.register(module)
147
146
  })
148
- it('attaches module', () => {
149
- node.attach(module.address)
147
+ it('attaches module', async () => {
148
+ await node.attach(module.address, true)
150
149
  })
151
- it('emits event on module attach', (done) => {
152
- node.on('moduleAttached', (args) => {
153
- expect(args.module).toBeObject()
154
- expect(args.module.address).toBe(module.address)
155
- expect(args.module).toBe(module)
156
- done()
150
+ it('emits event on module attach', async () => {
151
+ let attachDone = false
152
+ let eventDone = false
153
+ return await new Promise<void>((resolve, reject) => {
154
+ node.on('moduleAttached', (args) => {
155
+ expect(args.module).toBeObject()
156
+ expect(args.module.address).toBe(module.address)
157
+ expect(args.module).toBe(module)
158
+ eventDone = true
159
+ if (attachDone) {
160
+ resolve()
161
+ }
162
+ })
163
+ node
164
+ .attach(module.address, true)
165
+ .then(() => {
166
+ attachDone = true
167
+ if (eventDone) {
168
+ resolve()
169
+ }
170
+ })
171
+ .catch(() => {
172
+ reject('Attach failed')
173
+ })
157
174
  })
158
- node.attach(module.address)
159
175
  })
160
176
  })
161
177
  describe('attached', () => {
@@ -171,11 +187,8 @@ describe('MemoryNode', () => {
171
187
  })
172
188
  })
173
189
  describe('with modules attached', () => {
174
- beforeEach(() => {
175
- node.attach(module.address)
176
- })
177
190
  it('lists addresses of attached modules', async () => {
178
- node.attach(module.address)
191
+ await node.attach(module.address, true)
179
192
  const result = await node.attached()
180
193
  expect(result).toBeArrayOfSize(1)
181
194
  expect(result).toEqual([module.address])
@@ -187,7 +200,7 @@ describe('MemoryNode', () => {
187
200
  beforeEach(async () => {
188
201
  module = await MemoryArchivist.create()
189
202
  node.register(module)
190
- node.attach(module.address)
203
+ await node.attach(module.address, true)
191
204
  })
192
205
  it('deregisters existing module', () => {
193
206
  node.detach(module.address)
@@ -236,12 +249,14 @@ describe('MemoryNode', () => {
236
249
  }
237
250
  describe('node without child modules', () => {
238
251
  it('describes node alone', async () => {
239
- const description = await node.description()
252
+ const wrapper = NodeWrapper.wrap(node)
253
+ const description = await wrapper.describe()
240
254
  validateModuleDescription(description)
241
255
  expect(description.children).toBeArrayOfSize(0)
242
256
  })
243
257
  it('serializes to JSON consistently', async () => {
244
- const description = await node.description()
258
+ const wrapper = NodeWrapper.wrap(node)
259
+ const description = await wrapper.describe()
245
260
  expect(prettyPrintDescription(description)).toMatchSnapshot()
246
261
  })
247
262
  })
@@ -251,19 +266,21 @@ describe('MemoryNode', () => {
251
266
  await MemoryArchivist.create({ account: testAccount2, config: archivistConfig }),
252
267
  await MemoryArchivist.create({ account: testAccount3, config: archivistConfig }),
253
268
  ])
254
- modules.map((mod) => {
269
+ modules.map(async (mod) => {
255
270
  node.register(mod)
256
- node.attach(mod.address)
271
+ await node.attach(mod.address, true)
257
272
  })
258
273
  })
259
274
  it('describes node and child modules', async () => {
260
- const description = await node.description()
275
+ const wrapper = NodeWrapper.wrap(node)
276
+ const description = await wrapper.describe()
261
277
  validateModuleDescription(description)
262
278
  expect(description.children).toBeArrayOfSize(2)
263
279
  description.children?.map(validateModuleDescription)
264
280
  })
265
281
  it('serializes to JSON consistently', async () => {
266
- const description = await node.description()
282
+ const wrapper = NodeWrapper.wrap(node)
283
+ const description = await wrapper.describe()
267
284
  expect(prettyPrintDescription(description)).toMatchSnapshot()
268
285
  })
269
286
  })
@@ -271,25 +288,32 @@ describe('MemoryNode', () => {
271
288
  beforeEach(async () => {
272
289
  const nestedNode = await MemoryNode.create({ account: testAccount2, config: nodeConfig })
273
290
  const nestedModules = await Promise.all([await MemoryArchivist.create({ account: testAccount3, config: archivistConfig })])
274
- nestedModules.map((mod) => {
291
+ nestedModules.map(async (mod) => {
275
292
  nestedNode.register(mod)
276
- nestedNode.attach(mod.address)
293
+ await nestedNode.attach(mod.address, true)
277
294
  })
278
295
  const rootModules: AbstractModule[] = await Promise.all([await MemoryArchivist.create({ account: testAccount4, config: archivistConfig })])
279
296
  rootModules.push(nestedNode)
280
- rootModules.map((mod) => {
297
+ rootModules.map(async (mod) => {
281
298
  node.register(mod)
282
- node.attach(mod.address)
299
+ await node.attach(mod.address, true)
283
300
  })
284
301
  })
285
302
  it('describes node and all nested nodes and child modules', async () => {
286
- const description = await node.description()
303
+ const memoryNode = await MemoryNode.create()
304
+ const archivist1 = await MemoryArchivist.create()
305
+ const archivist2 = await MemoryArchivist.create()
306
+ const wrapper = NodeWrapper.wrap(memoryNode)
307
+ await memoryNode.register(archivist1).attach(archivist1.address, true)
308
+ await memoryNode.register(archivist2).attach(archivist2.address, true)
309
+ const description = await wrapper.describe()
287
310
  validateModuleDescription(description)
288
311
  expect(description.children).toBeArrayOfSize(2)
289
312
  description.children?.map(validateModuleDescription)
290
313
  })
291
314
  it('serializes to JSON consistently', async () => {
292
- const description = await node.description()
315
+ const wrapper = NodeWrapper.wrap(node)
316
+ const description = await wrapper.describe()
293
317
  expect(prettyPrintDescription(description)).toMatchSnapshot()
294
318
  })
295
319
  })
@@ -304,7 +328,7 @@ describe('MemoryNode', () => {
304
328
  const config = response.find((p) => p.schema === mod.config.schema)
305
329
  expect(config).toBeObject()
306
330
  expect(config).toEqual(mod.config)
307
- const queries = response.filter((p) => mod.queries().includes(p.schema))
331
+ const queries = response.filter((p) => mod.queries.includes(p.schema))
308
332
  expect(queries.length).toBeGreaterThanOrEqual(0)
309
333
  queries.map((query) => {
310
334
  expect(query).toBeObject()
@@ -318,19 +342,22 @@ describe('MemoryNode', () => {
318
342
  })
319
343
  describe('node with child modules', () => {
320
344
  it('describes node and child modules', async () => {
345
+ const memoryNode = await MemoryNode.create()
321
346
  const modules = await Promise.all([
322
347
  await MemoryArchivist.create({ account: testAccount2, config: archivistConfig }),
323
348
  await MemoryArchivist.create({ account: testAccount3, config: archivistConfig }),
324
349
  ])
325
- modules.map((mod) => {
326
- node.register(mod)
327
- node.attach(mod.address)
328
- })
329
- const description = await node.discover()
330
- validateDiscoveryResponse(node, description)
331
- const address0 = description.find((p) => p.schema === AddressSchema && (p as AddressPayload).address === modules[0].address) as AddressPayload
350
+ await Promise.all(
351
+ modules.map(async (mod) => {
352
+ memoryNode.register(mod)
353
+ await memoryNode.attach(mod.address, true)
354
+ }),
355
+ )
356
+ const discover = await memoryNode.discover()
357
+
358
+ const address0 = discover.find((p) => p.schema === AddressSchema && (p as AddressPayload).address === modules[0].address) as AddressPayload
332
359
  expect(address0).toBeObject()
333
- const address1 = description.find((p) => p.schema === AddressSchema && (p as AddressPayload).address === modules[1].address) as AddressPayload
360
+ const address1 = discover.find((p) => p.schema === AddressSchema && (p as AddressPayload).address === modules[1].address) as AddressPayload
334
361
  expect(address1).toBeObject()
335
362
  })
336
363
  })
@@ -338,15 +365,15 @@ describe('MemoryNode', () => {
338
365
  beforeEach(async () => {
339
366
  const nestedNode = await MemoryNode.create({ account: testAccount2, config: nodeConfig })
340
367
  const nestedModules = await Promise.all([await MemoryArchivist.create({ account: testAccount3, config: archivistConfig })])
341
- nestedModules.map((mod) => {
368
+ nestedModules.map(async (mod) => {
342
369
  nestedNode.register(mod)
343
- nestedNode.attach(mod.address)
370
+ await nestedNode.attach(mod.address, true)
344
371
  })
345
372
  const rootModules: AbstractModule[] = await Promise.all([await MemoryArchivist.create({ account: testAccount4, config: archivistConfig })])
346
373
  rootModules.push(nestedNode)
347
- rootModules.map((mod) => {
374
+ rootModules.map(async (mod) => {
348
375
  node.register(mod)
349
- node.attach(mod.address)
376
+ await node.attach(mod.address, true)
350
377
  })
351
378
  })
352
379
  it('describes node and all nested nodes and child modules', async () => {
@@ -361,9 +388,9 @@ describe('MemoryNode', () => {
361
388
  await MemoryArchivist.create({ account: testAccount2, config: archivistConfig }),
362
389
  await MemoryArchivist.create({ account: testAccount3, config: archivistConfig }),
363
390
  ])
364
- modules.map((mod) => {
391
+ modules.map(async (mod) => {
365
392
  node.register(mod)
366
- node.attach(mod.address)
393
+ await node.attach(mod.address, true)
367
394
  })
368
395
  })
369
396
  it('resolves modules wrapped as the specified type', async () => {
@@ -79,22 +79,20 @@ exports[`MemoryNode description node with nested nodes and modules serializes to
79
79
  "network.xyo.query.node.registered",
80
80
  "network.xyo.query.module.discover",
81
81
  "network.xyo.query.module.subscribe"
82
- ],
83
- "children": [
84
- {
85
- "address": "ca2e94ac3ae1084072b3a4407881bf940dc2019c",
86
- "queries": [
87
- "network.xyo.query.archivist.all",
88
- "network.xyo.query.archivist.delete",
89
- "network.xyo.query.archivist.clear",
90
- "network.xyo.query.archivist.find",
91
- "network.xyo.query.archivist.insert",
92
- "network.xyo.query.archivist.commit",
93
- "network.xyo.query.archivist.get",
94
- "network.xyo.query.module.discover",
95
- "network.xyo.query.module.subscribe"
96
- ]
97
- }
82
+ ]
83
+ },
84
+ {
85
+ "address": "ca2e94ac3ae1084072b3a4407881bf940dc2019c",
86
+ "queries": [
87
+ "network.xyo.query.archivist.all",
88
+ "network.xyo.query.archivist.delete",
89
+ "network.xyo.query.archivist.clear",
90
+ "network.xyo.query.archivist.find",
91
+ "network.xyo.query.archivist.insert",
92
+ "network.xyo.query.archivist.commit",
93
+ "network.xyo.query.archivist.get",
94
+ "network.xyo.query.module.discover",
95
+ "network.xyo.query.module.subscribe"
98
96
  ]
99
97
  }
100
98
  ]