@olane/o-node 0.7.13-alpha.0 → 0.7.13-alpha.1
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.
- package/dist/src/connection/interfaces/o-node-connection-manager.config.d.ts +1 -0
- package/dist/src/connection/interfaces/o-node-connection-manager.config.d.ts.map +1 -1
- package/dist/src/connection/o-node-connection.d.ts +0 -1
- package/dist/src/connection/o-node-connection.d.ts.map +1 -1
- package/dist/src/connection/o-node-connection.js +0 -8
- package/dist/src/connection/o-node-connection.manager.d.ts +33 -4
- package/dist/src/connection/o-node-connection.manager.d.ts.map +1 -1
- package/dist/src/connection/o-node-connection.manager.js +153 -44
- package/dist/src/connection/stream-handler.d.ts.map +1 -1
- package/dist/src/connection/stream-handler.js +0 -2
- package/dist/src/managers/o-connection-heartbeat.manager.d.ts.map +1 -1
- package/dist/src/managers/o-connection-heartbeat.manager.js +15 -1
- package/dist/src/managers/o-reconnection.manager.d.ts.map +1 -1
- package/dist/src/managers/o-reconnection.manager.js +12 -7
- package/dist/src/o-node.d.ts +5 -0
- package/dist/src/o-node.d.ts.map +1 -1
- package/dist/src/o-node.js +46 -8
- package/dist/src/o-node.tool.d.ts.map +1 -1
- package/dist/src/o-node.tool.js +5 -0
- package/dist/src/router/o-node.router.d.ts.map +1 -1
- package/dist/src/router/o-node.router.js +16 -6
- package/dist/src/router/o-node.routing-policy.d.ts.map +1 -1
- package/dist/src/router/o-node.routing-policy.js +4 -0
- package/dist/test/connection-management.spec.d.ts +2 -0
- package/dist/test/connection-management.spec.d.ts.map +1 -0
- package/dist/test/connection-management.spec.js +370 -0
- package/dist/test/helpers/connection-spy.d.ts +124 -0
- package/dist/test/helpers/connection-spy.d.ts.map +1 -0
- package/dist/test/helpers/connection-spy.js +229 -0
- package/dist/test/helpers/index.d.ts +6 -0
- package/dist/test/helpers/index.d.ts.map +1 -0
- package/dist/test/helpers/index.js +12 -0
- package/dist/test/helpers/network-builder.d.ts +109 -0
- package/dist/test/helpers/network-builder.d.ts.map +1 -0
- package/dist/test/helpers/network-builder.js +309 -0
- package/dist/test/helpers/simple-node-builder.d.ts +50 -0
- package/dist/test/helpers/simple-node-builder.d.ts.map +1 -0
- package/dist/test/helpers/simple-node-builder.js +66 -0
- package/dist/test/helpers/test-environment.d.ts +140 -0
- package/dist/test/helpers/test-environment.d.ts.map +1 -0
- package/dist/test/helpers/test-environment.js +184 -0
- package/dist/test/helpers/test-node.tool.d.ts +31 -0
- package/dist/test/helpers/test-node.tool.d.ts.map +1 -1
- package/dist/test/helpers/test-node.tool.js +49 -0
- package/dist/test/network-communication.spec.d.ts +2 -0
- package/dist/test/network-communication.spec.d.ts.map +1 -0
- package/dist/test/network-communication.spec.js +256 -0
- package/dist/test/o-node.spec.d.ts +2 -0
- package/dist/test/o-node.spec.d.ts.map +1 -0
- package/dist/test/o-node.spec.js +247 -0
- package/dist/test/parent-child-registration.spec.d.ts +2 -0
- package/dist/test/parent-child-registration.spec.d.ts.map +1 -0
- package/dist/test/parent-child-registration.spec.js +177 -0
- package/dist/test/search-resolver.spec.d.ts +2 -0
- package/dist/test/search-resolver.spec.d.ts.map +1 -0
- package/dist/test/search-resolver.spec.js +648 -0
- package/package.json +12 -7
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SimpleNodeBuilder - Fluent API for creating single-node tests
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* const node = await new SimpleNodeBuilder(MyTool)
|
|
7
|
+
* .withConfig({ apiKey: 'test' })
|
|
8
|
+
* .withAddress('o://test-tool')
|
|
9
|
+
* .build(env);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
import type { oNodeTool } from '../../src/o-node.tool.js';
|
|
13
|
+
import type { oNodeAddress } from '../../src/router/o-node.address.js';
|
|
14
|
+
import type { TestEnvironment } from './test-environment.js';
|
|
15
|
+
import type { TestNodeConfig } from './test-environment.js';
|
|
16
|
+
export declare class SimpleNodeBuilder<T extends oNodeTool = any> {
|
|
17
|
+
private nodeClass;
|
|
18
|
+
private config;
|
|
19
|
+
private autoStart;
|
|
20
|
+
constructor(NodeClass: new (config: any) => T);
|
|
21
|
+
/**
|
|
22
|
+
* Set node configuration
|
|
23
|
+
*/
|
|
24
|
+
withConfig(config: TestNodeConfig): this;
|
|
25
|
+
/**
|
|
26
|
+
* Set node address
|
|
27
|
+
*/
|
|
28
|
+
withAddress(address: oNodeAddress): this;
|
|
29
|
+
/**
|
|
30
|
+
* Set node description
|
|
31
|
+
*/
|
|
32
|
+
withDescription(description: string): this;
|
|
33
|
+
/**
|
|
34
|
+
* Set leader reference
|
|
35
|
+
*/
|
|
36
|
+
withLeader(leader: oNodeAddress | null): this;
|
|
37
|
+
/**
|
|
38
|
+
* Set parent reference
|
|
39
|
+
*/
|
|
40
|
+
withParent(parent: oNodeAddress | null): this;
|
|
41
|
+
/**
|
|
42
|
+
* Control automatic start
|
|
43
|
+
*/
|
|
44
|
+
withAutoStart(autoStart: boolean): this;
|
|
45
|
+
/**
|
|
46
|
+
* Build and create the node
|
|
47
|
+
*/
|
|
48
|
+
build(env: TestEnvironment): Promise<T>;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=simple-node-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simple-node-builder.d.ts","sourceRoot":"","sources":["../../../test/helpers/simple-node-builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AACvE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAE5D,qBAAa,iBAAiB,CAAC,CAAC,SAAS,SAAS,GAAG,GAAG;IACtD,OAAO,CAAC,SAAS,CAAyB;IAC1C,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,SAAS,CAAiB;gBAEtB,SAAS,EAAE,KAAK,MAAM,EAAE,GAAG,KAAK,CAAC;IAI7C;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI;IAKxC;;OAEG;IACH,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI;IAKxC;;OAEG;IACH,eAAe,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI;IAK1C;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,GAAG,IAAI;IAK7C;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,IAAI,GAAG,IAAI;IAK7C;;OAEG;IACH,aAAa,CAAC,SAAS,EAAE,OAAO,GAAG,IAAI;IAKvC;;OAEG;IACG,KAAK,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,CAAC,CAAC;CAG9C"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SimpleNodeBuilder - Fluent API for creating single-node tests
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* const node = await new SimpleNodeBuilder(MyTool)
|
|
7
|
+
* .withConfig({ apiKey: 'test' })
|
|
8
|
+
* .withAddress('o://test-tool')
|
|
9
|
+
* .build(env);
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export class SimpleNodeBuilder {
|
|
13
|
+
constructor(NodeClass) {
|
|
14
|
+
this.config = {};
|
|
15
|
+
this.autoStart = true;
|
|
16
|
+
this.nodeClass = NodeClass;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Set node configuration
|
|
20
|
+
*/
|
|
21
|
+
withConfig(config) {
|
|
22
|
+
this.config = { ...this.config, ...config };
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Set node address
|
|
27
|
+
*/
|
|
28
|
+
withAddress(address) {
|
|
29
|
+
this.config.address = address;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Set node description
|
|
34
|
+
*/
|
|
35
|
+
withDescription(description) {
|
|
36
|
+
this.config.description = description;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Set leader reference
|
|
41
|
+
*/
|
|
42
|
+
withLeader(leader) {
|
|
43
|
+
this.config.leader = leader;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Set parent reference
|
|
48
|
+
*/
|
|
49
|
+
withParent(parent) {
|
|
50
|
+
this.config.parent = parent;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Control automatic start
|
|
55
|
+
*/
|
|
56
|
+
withAutoStart(autoStart) {
|
|
57
|
+
this.autoStart = autoStart;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Build and create the node
|
|
62
|
+
*/
|
|
63
|
+
async build(env) {
|
|
64
|
+
return await env.createNode(this.nodeClass, this.config, this.autoStart);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TestEnvironment - Core test setup and lifecycle management for O-Network nodes
|
|
3
|
+
*
|
|
4
|
+
* Provides automatic cleanup, node factories, and common test utilities
|
|
5
|
+
* to eliminate boilerplate in O-Network package tests.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* describe('MyTool', () => {
|
|
10
|
+
* const env = new TestEnvironment();
|
|
11
|
+
*
|
|
12
|
+
* afterEach(async () => {
|
|
13
|
+
* await env.cleanup();
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* it('should work', async () => {
|
|
17
|
+
* const node = await env.createNode(MyTool);
|
|
18
|
+
* expect(node.state).to.equal(NodeState.RUNNING);
|
|
19
|
+
* });
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
import type { oNode } from '../../src/o-node.js';
|
|
24
|
+
import type { oNodeAddress } from '../../src/router/o-node.address.js';
|
|
25
|
+
/**
|
|
26
|
+
* Node configuration for test environment
|
|
27
|
+
*/
|
|
28
|
+
export interface TestNodeConfig {
|
|
29
|
+
address?: oNodeAddress;
|
|
30
|
+
leader?: oNodeAddress | null;
|
|
31
|
+
parent?: oNodeAddress | null;
|
|
32
|
+
description?: string;
|
|
33
|
+
[key: string]: any;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Core test environment class for O-Network testing
|
|
37
|
+
*
|
|
38
|
+
* Manages node lifecycle, automatic cleanup, and provides
|
|
39
|
+
* factory methods for common test scenarios.
|
|
40
|
+
*
|
|
41
|
+
* Note: This class does not include leader-related utilities to avoid
|
|
42
|
+
* circular dependencies with @olane/o-leader. Packages that need leader
|
|
43
|
+
* functionality should implement their own test utilities.
|
|
44
|
+
*/
|
|
45
|
+
export declare class TestEnvironment {
|
|
46
|
+
private nodes;
|
|
47
|
+
private cleanupCallbacks;
|
|
48
|
+
/**
|
|
49
|
+
* Create a node
|
|
50
|
+
*
|
|
51
|
+
* @param NodeClass - Node class to instantiate
|
|
52
|
+
* @param config - Node configuration
|
|
53
|
+
* @param autoStart - Whether to start node automatically (default: true)
|
|
54
|
+
* @returns Node instance
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const node = await env.createNode(MyTool, {
|
|
59
|
+
* address: new oNodeAddress('o://test')
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
createNode<T extends oNode>(NodeClass: new (config: any) => T, config?: TestNodeConfig, autoStart?: boolean): Promise<T>;
|
|
64
|
+
/**
|
|
65
|
+
* Track a node for automatic cleanup
|
|
66
|
+
*
|
|
67
|
+
* @param node - Node instance to track
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const node = new MyNode({});
|
|
72
|
+
* env.track(node);
|
|
73
|
+
* await node.start();
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
track(node: oNode): void;
|
|
77
|
+
/**
|
|
78
|
+
* Register a cleanup callback
|
|
79
|
+
*
|
|
80
|
+
* Useful for cleaning up external resources (databases, files, etc.)
|
|
81
|
+
*
|
|
82
|
+
* @param callback - Async cleanup function
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* const db = await createTestDB();
|
|
87
|
+
* env.onCleanup(async () => {
|
|
88
|
+
* await db.close();
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
onCleanup(callback: () => Promise<void>): void;
|
|
93
|
+
/**
|
|
94
|
+
* Stop all tracked nodes and execute cleanup callbacks
|
|
95
|
+
*
|
|
96
|
+
* Stops nodes in reverse order (children before parents)
|
|
97
|
+
* Call this in afterEach hooks.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* afterEach(async () => {
|
|
102
|
+
* await env.cleanup();
|
|
103
|
+
* });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
cleanup(): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Get all tracked nodes
|
|
109
|
+
*
|
|
110
|
+
* @returns Array of tracked node instances
|
|
111
|
+
*/
|
|
112
|
+
getNodes(): oNode[];
|
|
113
|
+
/**
|
|
114
|
+
* Get count of tracked nodes
|
|
115
|
+
*
|
|
116
|
+
* @returns Number of tracked nodes
|
|
117
|
+
*/
|
|
118
|
+
getNodeCount(): number;
|
|
119
|
+
/**
|
|
120
|
+
* Check if all tracked nodes are stopped
|
|
121
|
+
*
|
|
122
|
+
* @returns True if all nodes are stopped
|
|
123
|
+
*/
|
|
124
|
+
allNodesStopped(): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Wait for a condition to be true
|
|
127
|
+
*
|
|
128
|
+
* @param condition - Function that returns true when condition is met
|
|
129
|
+
* @param timeoutMs - Maximum time to wait in milliseconds (default: 5000)
|
|
130
|
+
* @param intervalMs - Check interval in milliseconds (default: 100)
|
|
131
|
+
* @returns Promise that resolves when condition is met or rejects on timeout
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* await env.waitFor(() => tool.isReady, 10000);
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
waitFor(condition: () => boolean, timeoutMs?: number, intervalMs?: number): Promise<void>;
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=test-environment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-environment.d.ts","sourceRoot":"","sources":["../../../test/helpers/test-environment.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAGH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oCAAoC,CAAC;AAEvE;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,MAAM,CAAC,EAAE,YAAY,GAAG,IAAI,CAAC;IAC7B,MAAM,CAAC,EAAE,YAAY,GAAG,IAAI,CAAC;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED;;;;;;;;;GASG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,KAAK,CAAe;IAC5B,OAAO,CAAC,gBAAgB,CAAkC;IAE1D;;;;;;;;;;;;;;OAcG;IACG,UAAU,CAAC,CAAC,SAAS,KAAK,EAC9B,SAAS,EAAE,KAAK,MAAM,EAAE,GAAG,KAAK,CAAC,EACjC,MAAM,GAAE,cAAmB,EAC3B,SAAS,GAAE,OAAc,GACxB,OAAO,CAAC,CAAC,CAAC;IAgBb;;;;;;;;;;;OAWG;IACH,KAAK,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI;IAIxB;;;;;;;;;;;;;;OAcG;IACH,SAAS,CAAC,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAI9C;;;;;;;;;;;;OAYG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IA4B9B;;;;OAIG;IACH,QAAQ,IAAI,KAAK,EAAE;IAInB;;;;OAIG;IACH,YAAY,IAAI,MAAM;IAItB;;;;OAIG;IACH,eAAe,IAAI,OAAO;IAI1B;;;;;;;;;;;;OAYG;IACG,OAAO,CACX,SAAS,EAAE,MAAM,OAAO,EACxB,SAAS,GAAE,MAAa,EACxB,UAAU,GAAE,MAAY,GACvB,OAAO,CAAC,IAAI,CAAC;CAUjB"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TestEnvironment - Core test setup and lifecycle management for O-Network nodes
|
|
3
|
+
*
|
|
4
|
+
* Provides automatic cleanup, node factories, and common test utilities
|
|
5
|
+
* to eliminate boilerplate in O-Network package tests.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* describe('MyTool', () => {
|
|
10
|
+
* const env = new TestEnvironment();
|
|
11
|
+
*
|
|
12
|
+
* afterEach(async () => {
|
|
13
|
+
* await env.cleanup();
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* it('should work', async () => {
|
|
17
|
+
* const node = await env.createNode(MyTool);
|
|
18
|
+
* expect(node.state).to.equal(NodeState.RUNNING);
|
|
19
|
+
* });
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
import { NodeState } from '@olane/o-core';
|
|
24
|
+
/**
|
|
25
|
+
* Core test environment class for O-Network testing
|
|
26
|
+
*
|
|
27
|
+
* Manages node lifecycle, automatic cleanup, and provides
|
|
28
|
+
* factory methods for common test scenarios.
|
|
29
|
+
*
|
|
30
|
+
* Note: This class does not include leader-related utilities to avoid
|
|
31
|
+
* circular dependencies with @olane/o-leader. Packages that need leader
|
|
32
|
+
* functionality should implement their own test utilities.
|
|
33
|
+
*/
|
|
34
|
+
export class TestEnvironment {
|
|
35
|
+
constructor() {
|
|
36
|
+
this.nodes = [];
|
|
37
|
+
this.cleanupCallbacks = [];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create a node
|
|
41
|
+
*
|
|
42
|
+
* @param NodeClass - Node class to instantiate
|
|
43
|
+
* @param config - Node configuration
|
|
44
|
+
* @param autoStart - Whether to start node automatically (default: true)
|
|
45
|
+
* @returns Node instance
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const node = await env.createNode(MyTool, {
|
|
50
|
+
* address: new oNodeAddress('o://test')
|
|
51
|
+
* });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
async createNode(NodeClass, config = {}, autoStart = true) {
|
|
55
|
+
const node = new NodeClass({
|
|
56
|
+
parent: null,
|
|
57
|
+
leader: null,
|
|
58
|
+
...config,
|
|
59
|
+
});
|
|
60
|
+
this.track(node);
|
|
61
|
+
if (autoStart) {
|
|
62
|
+
await node.start();
|
|
63
|
+
}
|
|
64
|
+
return node;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Track a node for automatic cleanup
|
|
68
|
+
*
|
|
69
|
+
* @param node - Node instance to track
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const node = new MyNode({});
|
|
74
|
+
* env.track(node);
|
|
75
|
+
* await node.start();
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
track(node) {
|
|
79
|
+
this.nodes.push(node);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Register a cleanup callback
|
|
83
|
+
*
|
|
84
|
+
* Useful for cleaning up external resources (databases, files, etc.)
|
|
85
|
+
*
|
|
86
|
+
* @param callback - Async cleanup function
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* const db = await createTestDB();
|
|
91
|
+
* env.onCleanup(async () => {
|
|
92
|
+
* await db.close();
|
|
93
|
+
* });
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
onCleanup(callback) {
|
|
97
|
+
this.cleanupCallbacks.push(callback);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Stop all tracked nodes and execute cleanup callbacks
|
|
101
|
+
*
|
|
102
|
+
* Stops nodes in reverse order (children before parents)
|
|
103
|
+
* Call this in afterEach hooks.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* afterEach(async () => {
|
|
108
|
+
* await env.cleanup();
|
|
109
|
+
* });
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
async cleanup() {
|
|
113
|
+
// Stop nodes in reverse order (children first)
|
|
114
|
+
const nodesToStop = [...this.nodes].reverse();
|
|
115
|
+
for (const node of nodesToStop) {
|
|
116
|
+
try {
|
|
117
|
+
if (node.state === NodeState.RUNNING) {
|
|
118
|
+
await node.stop();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
console.error('Error stopping node:', error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// Execute cleanup callbacks
|
|
126
|
+
for (const callback of this.cleanupCallbacks) {
|
|
127
|
+
try {
|
|
128
|
+
await callback();
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
console.error('Error in cleanup callback:', error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Clear tracking
|
|
135
|
+
this.nodes = [];
|
|
136
|
+
this.cleanupCallbacks = [];
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Get all tracked nodes
|
|
140
|
+
*
|
|
141
|
+
* @returns Array of tracked node instances
|
|
142
|
+
*/
|
|
143
|
+
getNodes() {
|
|
144
|
+
return [...this.nodes];
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get count of tracked nodes
|
|
148
|
+
*
|
|
149
|
+
* @returns Number of tracked nodes
|
|
150
|
+
*/
|
|
151
|
+
getNodeCount() {
|
|
152
|
+
return this.nodes.length;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Check if all tracked nodes are stopped
|
|
156
|
+
*
|
|
157
|
+
* @returns True if all nodes are stopped
|
|
158
|
+
*/
|
|
159
|
+
allNodesStopped() {
|
|
160
|
+
return this.nodes.every(node => node.state === NodeState.STOPPED);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Wait for a condition to be true
|
|
164
|
+
*
|
|
165
|
+
* @param condition - Function that returns true when condition is met
|
|
166
|
+
* @param timeoutMs - Maximum time to wait in milliseconds (default: 5000)
|
|
167
|
+
* @param intervalMs - Check interval in milliseconds (default: 100)
|
|
168
|
+
* @returns Promise that resolves when condition is met or rejects on timeout
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* await env.waitFor(() => tool.isReady, 10000);
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
async waitFor(condition, timeoutMs = 5000, intervalMs = 100) {
|
|
176
|
+
const startTime = Date.now();
|
|
177
|
+
while (!condition()) {
|
|
178
|
+
if (Date.now() - startTime > timeoutMs) {
|
|
179
|
+
throw new Error(`Timeout waiting for condition after ${timeoutMs}ms`);
|
|
180
|
+
}
|
|
181
|
+
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -1,14 +1,45 @@
|
|
|
1
1
|
import { oNodeTool } from '../../src/o-node.tool.js';
|
|
2
|
+
import { oNodeToolConfig } from '../../src/interfaces/o-node.tool-config.js';
|
|
3
|
+
import { Libp2pConfig } from '@olane/o-config';
|
|
2
4
|
/**
|
|
3
5
|
* Test-only extension of oNodeTool that adds streaming test methods.
|
|
4
6
|
* This class should only be used in test files and is not part of the production code.
|
|
7
|
+
*
|
|
8
|
+
* Compatible with TestEnvironment for automatic cleanup and lifecycle management.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { TestEnvironment } from './helpers/index.js';
|
|
13
|
+
* import { TestNodeTool } from './helpers/test-node.tool.js';
|
|
14
|
+
*
|
|
15
|
+
* const env = new TestEnvironment();
|
|
16
|
+
* const tool = new TestNodeTool({ address: new oNodeAddress('o://test') });
|
|
17
|
+
* await tool.start();
|
|
18
|
+
* // TestEnvironment will handle cleanup automatically
|
|
19
|
+
* ```
|
|
5
20
|
*/
|
|
6
21
|
export declare class TestNodeTool extends oNodeTool {
|
|
22
|
+
constructor(config: oNodeToolConfig);
|
|
23
|
+
configure(): Promise<Libp2pConfig>;
|
|
7
24
|
/**
|
|
8
25
|
* Test method that emits chunks for 10 seconds at 100ms intervals.
|
|
9
26
|
* Used for testing streaming functionality across hierarchical networks.
|
|
10
27
|
*
|
|
28
|
+
* This method demonstrates streaming responses with AsyncGenerator,
|
|
29
|
+
* which is a common pattern for real-time data processing and long-running operations.
|
|
30
|
+
*
|
|
11
31
|
* @returns AsyncGenerator that yields 100 chunks over 10 seconds
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const response = await tool.use(tool.address, {
|
|
35
|
+
* method: 'test_stream',
|
|
36
|
+
* params: {}
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* for await (const chunk of response) {
|
|
40
|
+
* console.log(`Received chunk ${chunk.chunk}`);
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
12
43
|
*/
|
|
13
44
|
_tool_test_stream(): AsyncGenerator<any>;
|
|
14
45
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-node.tool.d.ts","sourceRoot":"","sources":["../../../test/helpers/test-node.tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"test-node.tool.d.ts","sourceRoot":"","sources":["../../../test/helpers/test-node.tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,4CAA4C,CAAC;AAE7E,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,YAAa,SAAQ,SAAS;gBAC7B,MAAM,EAAE,eAAe;IAO7B,SAAS,IAAI,OAAO,CAAC,YAAY,CAAC;IAcxC;;;;;;;;;;;;;;;;;;;OAmBG;IACI,iBAAiB,IAAI,cAAc,CAAC,GAAG,CAAC;CAiBhD"}
|
|
@@ -1,14 +1,61 @@
|
|
|
1
1
|
import { oNodeTool } from '../../src/o-node.tool.js';
|
|
2
|
+
import { oNodeAddress } from '../../src/index.js';
|
|
2
3
|
/**
|
|
3
4
|
* Test-only extension of oNodeTool that adds streaming test methods.
|
|
4
5
|
* This class should only be used in test files and is not part of the production code.
|
|
6
|
+
*
|
|
7
|
+
* Compatible with TestEnvironment for automatic cleanup and lifecycle management.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { TestEnvironment } from './helpers/index.js';
|
|
12
|
+
* import { TestNodeTool } from './helpers/test-node.tool.js';
|
|
13
|
+
*
|
|
14
|
+
* const env = new TestEnvironment();
|
|
15
|
+
* const tool = new TestNodeTool({ address: new oNodeAddress('o://test') });
|
|
16
|
+
* await tool.start();
|
|
17
|
+
* // TestEnvironment will handle cleanup automatically
|
|
18
|
+
* ```
|
|
5
19
|
*/
|
|
6
20
|
export class TestNodeTool extends oNodeTool {
|
|
21
|
+
constructor(config) {
|
|
22
|
+
super({
|
|
23
|
+
...config,
|
|
24
|
+
address: new oNodeAddress('o://test')
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
async configure() {
|
|
28
|
+
const config = await super.configure();
|
|
29
|
+
config.connectionGater = {
|
|
30
|
+
denyDialPeer: (peerId) => {
|
|
31
|
+
return false;
|
|
32
|
+
},
|
|
33
|
+
// who can call us?
|
|
34
|
+
denyInboundEncryptedConnection: (peerId, maConn) => {
|
|
35
|
+
return false;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
return config;
|
|
39
|
+
}
|
|
7
40
|
/**
|
|
8
41
|
* Test method that emits chunks for 10 seconds at 100ms intervals.
|
|
9
42
|
* Used for testing streaming functionality across hierarchical networks.
|
|
10
43
|
*
|
|
44
|
+
* This method demonstrates streaming responses with AsyncGenerator,
|
|
45
|
+
* which is a common pattern for real-time data processing and long-running operations.
|
|
46
|
+
*
|
|
11
47
|
* @returns AsyncGenerator that yields 100 chunks over 10 seconds
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const response = await tool.use(tool.address, {
|
|
51
|
+
* method: 'test_stream',
|
|
52
|
+
* params: {}
|
|
53
|
+
* });
|
|
54
|
+
*
|
|
55
|
+
* for await (const chunk of response) {
|
|
56
|
+
* console.log(`Received chunk ${chunk.chunk}`);
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
12
59
|
*/
|
|
13
60
|
async *_tool_test_stream() {
|
|
14
61
|
const totalDuration = 10000; // 10 seconds
|
|
@@ -17,9 +64,11 @@ export class TestNodeTool extends oNodeTool {
|
|
|
17
64
|
for (let i = 0; i < totalChunks; i++) {
|
|
18
65
|
yield {
|
|
19
66
|
chunk: i + 1,
|
|
67
|
+
total: totalChunks,
|
|
20
68
|
timestamp: new Date().toISOString(),
|
|
21
69
|
nodeAddress: this.address.toString(),
|
|
22
70
|
message: `Chunk ${i + 1} of ${totalChunks}`,
|
|
71
|
+
progress: ((i + 1) / totalChunks) * 100,
|
|
23
72
|
};
|
|
24
73
|
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
25
74
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network-communication.spec.d.ts","sourceRoot":"","sources":["../../test/network-communication.spec.ts"],"names":[],"mappings":""}
|