@xyo-network/node 2.45.1 → 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
- await node.attach(archivist.address)
40
- node.register(diviner)
41
- await node.attach(diviner.address)
42
- expect(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)
@@ -146,16 +145,33 @@ describe('MemoryNode', () => {
146
145
  node.register(module)
147
146
  })
148
147
  it('attaches module', async () => {
149
- await node.attach(module.address)
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
- void node.attach(module.address)
159
175
  })
160
176
  })
161
177
  describe('attached', () => {
@@ -172,7 +188,7 @@ describe('MemoryNode', () => {
172
188
  })
173
189
  describe('with modules attached', () => {
174
190
  it('lists addresses of attached modules', async () => {
175
- await node.attach(module.address)
191
+ await node.attach(module.address, true)
176
192
  const result = await node.attached()
177
193
  expect(result).toBeArrayOfSize(1)
178
194
  expect(result).toEqual([module.address])
@@ -184,7 +200,7 @@ describe('MemoryNode', () => {
184
200
  beforeEach(async () => {
185
201
  module = await MemoryArchivist.create()
186
202
  node.register(module)
187
- await node.attach(module.address)
203
+ await node.attach(module.address, true)
188
204
  })
189
205
  it('deregisters existing module', () => {
190
206
  node.detach(module.address)
@@ -233,12 +249,14 @@ describe('MemoryNode', () => {
233
249
  }
234
250
  describe('node without child modules', () => {
235
251
  it('describes node alone', async () => {
236
- const description = await node.description()
252
+ const wrapper = NodeWrapper.wrap(node)
253
+ const description = await wrapper.describe()
237
254
  validateModuleDescription(description)
238
255
  expect(description.children).toBeArrayOfSize(0)
239
256
  })
240
257
  it('serializes to JSON consistently', async () => {
241
- const description = await node.description()
258
+ const wrapper = NodeWrapper.wrap(node)
259
+ const description = await wrapper.describe()
242
260
  expect(prettyPrintDescription(description)).toMatchSnapshot()
243
261
  })
244
262
  })
@@ -250,17 +268,19 @@ describe('MemoryNode', () => {
250
268
  ])
251
269
  modules.map(async (mod) => {
252
270
  node.register(mod)
253
- await node.attach(mod.address)
271
+ await node.attach(mod.address, true)
254
272
  })
255
273
  })
256
274
  it('describes node and child modules', async () => {
257
- const description = await node.description()
275
+ const wrapper = NodeWrapper.wrap(node)
276
+ const description = await wrapper.describe()
258
277
  validateModuleDescription(description)
259
278
  expect(description.children).toBeArrayOfSize(2)
260
279
  description.children?.map(validateModuleDescription)
261
280
  })
262
281
  it('serializes to JSON consistently', async () => {
263
- const description = await node.description()
282
+ const wrapper = NodeWrapper.wrap(node)
283
+ const description = await wrapper.describe()
264
284
  expect(prettyPrintDescription(description)).toMatchSnapshot()
265
285
  })
266
286
  })
@@ -270,23 +290,30 @@ describe('MemoryNode', () => {
270
290
  const nestedModules = await Promise.all([await MemoryArchivist.create({ account: testAccount3, config: archivistConfig })])
271
291
  nestedModules.map(async (mod) => {
272
292
  nestedNode.register(mod)
273
- await nestedNode.attach(mod.address)
293
+ await nestedNode.attach(mod.address, true)
274
294
  })
275
295
  const rootModules: AbstractModule[] = await Promise.all([await MemoryArchivist.create({ account: testAccount4, config: archivistConfig })])
276
296
  rootModules.push(nestedNode)
277
297
  rootModules.map(async (mod) => {
278
298
  node.register(mod)
279
- await node.attach(mod.address)
299
+ await node.attach(mod.address, true)
280
300
  })
281
301
  })
282
302
  it('describes node and all nested nodes and child modules', async () => {
283
- 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()
284
310
  validateModuleDescription(description)
285
311
  expect(description.children).toBeArrayOfSize(2)
286
312
  description.children?.map(validateModuleDescription)
287
313
  })
288
314
  it('serializes to JSON consistently', async () => {
289
- const description = await node.description()
315
+ const wrapper = NodeWrapper.wrap(node)
316
+ const description = await wrapper.describe()
290
317
  expect(prettyPrintDescription(description)).toMatchSnapshot()
291
318
  })
292
319
  })
@@ -301,7 +328,7 @@ describe('MemoryNode', () => {
301
328
  const config = response.find((p) => p.schema === mod.config.schema)
302
329
  expect(config).toBeObject()
303
330
  expect(config).toEqual(mod.config)
304
- const queries = response.filter((p) => mod.queries().includes(p.schema))
331
+ const queries = response.filter((p) => mod.queries.includes(p.schema))
305
332
  expect(queries.length).toBeGreaterThanOrEqual(0)
306
333
  queries.map((query) => {
307
334
  expect(query).toBeObject()
@@ -315,19 +342,22 @@ describe('MemoryNode', () => {
315
342
  })
316
343
  describe('node with child modules', () => {
317
344
  it('describes node and child modules', async () => {
345
+ const memoryNode = await MemoryNode.create()
318
346
  const modules = await Promise.all([
319
347
  await MemoryArchivist.create({ account: testAccount2, config: archivistConfig }),
320
348
  await MemoryArchivist.create({ account: testAccount3, config: archivistConfig }),
321
349
  ])
322
- modules.map(async (mod) => {
323
- node.register(mod)
324
- await node.attach(mod.address)
325
- })
326
- const description = await node.discover()
327
- validateDiscoveryResponse(node, description)
328
- 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
329
359
  expect(address0).toBeObject()
330
- 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
331
361
  expect(address1).toBeObject()
332
362
  })
333
363
  })
@@ -337,13 +367,13 @@ describe('MemoryNode', () => {
337
367
  const nestedModules = await Promise.all([await MemoryArchivist.create({ account: testAccount3, config: archivistConfig })])
338
368
  nestedModules.map(async (mod) => {
339
369
  nestedNode.register(mod)
340
- await nestedNode.attach(mod.address)
370
+ await nestedNode.attach(mod.address, true)
341
371
  })
342
372
  const rootModules: AbstractModule[] = await Promise.all([await MemoryArchivist.create({ account: testAccount4, config: archivistConfig })])
343
373
  rootModules.push(nestedNode)
344
374
  rootModules.map(async (mod) => {
345
375
  node.register(mod)
346
- await node.attach(mod.address)
376
+ await node.attach(mod.address, true)
347
377
  })
348
378
  })
349
379
  it('describes node and all nested nodes and child modules', async () => {
@@ -360,7 +390,7 @@ describe('MemoryNode', () => {
360
390
  ])
361
391
  modules.map(async (mod) => {
362
392
  node.register(mod)
363
- await node.attach(mod.address)
393
+ await node.attach(mod.address, true)
364
394
  })
365
395
  })
366
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
  ]