Skip to content

Commit 2fbe04d

Browse files
committed
feat: added support for searching finding multiple addresses with nodes find
fix: tests regarding new `js-mdns` integration
1 parent fb8a94f commit 2fbe04d

7 files changed

Lines changed: 73 additions & 33 deletions

File tree

src/nodes/CommandFind.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ class CommandFind extends CommandPolykey {
5555
success: false,
5656
message: '',
5757
id: '',
58-
host: '',
59-
port: 0,
58+
addresses: [] as Array<{ host: string; port: number }>,
6059
};
60+
const builtAddresses: Array<string> = [];
6161
try {
6262
const response = await binUtils.retryAuthentication(
6363
(auth) =>
@@ -69,12 +69,13 @@ class CommandFind extends CommandPolykey {
6969
);
7070
result.success = true;
7171
result.id = nodesUtils.encodeNodeId(nodeId);
72-
result.host = response.host;
73-
result.port = response.port;
74-
result.message = `Found node at ${networkUtils.buildAddress(
75-
result.host as Host,
76-
result.port as Port,
77-
)}`;
72+
for (const { host, port } of response.addresses) {
73+
result.addresses.push({ host, port });
74+
builtAddresses.push(
75+
networkUtils.buildAddress(host as Host, port as Port),
76+
);
77+
}
78+
result.message = `Found node at ${builtAddresses.join(', ')}`;
7879
} catch (err) {
7980
if (
8081
!(err.cause instanceof nodesErrors.ErrorNodeGraphNodeIdNotFound)
@@ -84,16 +85,20 @@ class CommandFind extends CommandPolykey {
8485
// Else failed to find the node.
8586
result.success = false;
8687
result.id = nodesUtils.encodeNodeId(nodeId);
87-
result.host = '';
88-
result.port = 0;
8988
result.message = `Failed to find node ${result.id}`;
9089
}
91-
let output: any = result;
92-
if (options.format === 'human') output = [result.message];
93-
const outputFormatted = binUtils.outputFormatter({
94-
type: options.format === 'json' ? 'json' : 'list',
95-
data: output,
96-
});
90+
let outputFormatted: string | Uint8Array;
91+
if (options.format === 'json') {
92+
outputFormatted = binUtils.outputFormatter({
93+
type: 'json',
94+
data: result,
95+
});
96+
} else {
97+
outputFormatted = binUtils.outputFormatter({
98+
type: 'list',
99+
data: ['Found node at', ...builtAddresses],
100+
});
101+
}
97102
process.stdout.write(outputFormatted);
98103
// Like ping it should error when failing to find node for automation reasons.
99104
if (!result.success) {

tests/agent/start.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ describe('start', () => {
930930
[seedNodeId2]: {
931931
host: seedNodeHost2 as Host,
932932
port: seedNodePort2 as Port,
933+
scopes: ['global'],
933934
},
934935
},
935936
testnet: {},
@@ -998,6 +999,7 @@ describe('start', () => {
998999
[seedNodeId2]: {
9991000
host: seedNodeHost2 as Host,
10001001
port: seedNodePort2 as Port,
1002+
scopes: ['global'],
10011003
},
10021004
},
10031005
});

tests/nodes/add.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ describe('add', () => {
147147
expect(exitCode).toBe(0);
148148
// Checking if node was added.
149149
const node = await pkAgent.nodeGraph.getNode(validNodeId);
150-
expect(node?.address).toEqual({ host: validHost, port: port });
150+
expect(node?.address).toEqual({
151+
host: validHost,
152+
port: port,
153+
scopes: ['global'],
154+
});
151155
},
152156
);
153157
testUtils.testIf(testUtils.isTestPlatformEmpty)(
@@ -197,7 +201,11 @@ describe('add', () => {
197201
expect(exitCode).toBe(0);
198202
// Checking if node was added.
199203
const node = await pkAgent.nodeGraph.getNode(validNodeId);
200-
expect(node?.address).toEqual({ host: validHost, port: port });
204+
expect(node?.address).toEqual({
205+
host: validHost,
206+
port: port,
207+
scopes: ['global'],
208+
});
201209
},
202210
);
203211
});

tests/nodes/find.test.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,24 @@ describe('find', () => {
116116
},
117117
);
118118
expect(exitCode).toBe(0);
119-
expect(JSON.parse(stdout)).toEqual({
119+
const output = JSON.parse(stdout);
120+
expect(output).toMatchObject({
120121
success: true,
121-
message: `Found node at ${remoteOnlineHost}:${remoteOnlinePort}`,
122122
id: nodesUtils.encodeNodeId(remoteOnlineNodeId),
123-
host: remoteOnlineHost,
124-
port: remoteOnlinePort,
125123
});
124+
expect(output.addresses).toEqual(
125+
expect.arrayContaining([
126+
{
127+
host: remoteOnlineHost,
128+
port: remoteOnlinePort,
129+
},
130+
]),
131+
);
132+
expect(output.message).toMatch(
133+
new RegExp(
134+
`Found node at .*?${remoteOnlineHost}:${remoteOnlinePort}.*?`,
135+
),
136+
);
126137
},
127138
);
128139
testUtils.testIf(testUtils.isTestPlatformEmpty)(
@@ -145,13 +156,24 @@ describe('find', () => {
145156
},
146157
);
147158
expect(exitCode).toBe(0);
148-
expect(JSON.parse(stdout)).toEqual({
159+
const output = JSON.parse(stdout);
160+
expect(output).toMatchObject({
149161
success: true,
150-
message: `Found node at ${remoteOfflineHost}:${remoteOfflinePort}`,
151162
id: nodesUtils.encodeNodeId(remoteOfflineNodeId),
152-
host: remoteOfflineHost,
153-
port: remoteOfflinePort,
154163
});
164+
expect(output.addresses).toEqual(
165+
expect.arrayContaining([
166+
{
167+
host: remoteOfflineHost,
168+
port: remoteOfflinePort,
169+
},
170+
]),
171+
);
172+
expect(output.message).toMatch(
173+
new RegExp(
174+
`Found node at .*?${remoteOfflineHost}:${remoteOfflinePort}.*?`,
175+
),
176+
);
155177
},
156178
);
157179
testUtils.testIf(testUtils.isTestPlatformEmpty)(
@@ -183,8 +205,7 @@ describe('find', () => {
183205
unknownNodeId!,
184206
)}`,
185207
id: nodesUtils.encodeNodeId(unknownNodeId!),
186-
host: '',
187-
port: 0,
208+
addresses: [],
188209
});
189210
},
190211
globalThis.failedConnectionTimeout,

tests/nodes/ping.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('ping', () => {
3535
passwordMemLimit: keysUtils.passwordMemLimits.min,
3636
strictMemoryLock: false,
3737
},
38+
// TODO: fix this
3839
nodes: {
3940
connectionConnectTimeoutTime: 2000,
4041
},

tests/utils/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type PolykeyAgent from 'polykey/dist/PolykeyAgent';
2-
import type { NodeAddress } from 'polykey/dist/nodes/types';
32
import { promise } from 'polykey/dist/utils/utils';
43

54
function testIf(condition: boolean) {
@@ -69,12 +68,14 @@ async function nodesConnect(localNode: PolykeyAgent, remoteNode: PolykeyAgent) {
6968
await localNode.nodeManager.setNode(remoteNode.keyRing.getNodeId(), {
7069
host: remoteNode.agentServiceHost,
7170
port: remoteNode.agentServicePort,
72-
} as NodeAddress);
71+
scopes: ['global'],
72+
});
7373
// Add local node's details to remote node
7474
await remoteNode.nodeManager.setNode(localNode.keyRing.getNodeId(), {
7575
host: localNode.agentServiceHost,
7676
port: localNode.agentServicePort,
77-
} as NodeAddress);
77+
scopes: ['global'],
78+
});
7879
}
7980

8081
export { testIf, describeIf, trackTimers, nodesConnect };

tests/vaults/vaults.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { NodeAddress } from 'polykey/dist/nodes/types';
21
import type { VaultId, VaultName } from 'polykey/dist/vaults/types';
32
import type { GestaltNodeInfo } from 'polykey/dist/gestalts/types';
43
import path from 'path';
@@ -258,12 +257,14 @@ describe('CLI vaults', () => {
258257
await polykeyAgent.nodeManager.setNode(targetNodeId, {
259258
host: targetPolykeyAgent.agentServiceHost,
260259
port: targetPolykeyAgent.agentServicePort,
260+
scopes: ['global'],
261261
});
262262
await targetPolykeyAgent.nodeManager.setNode(
263263
polykeyAgent.keyRing.getNodeId(),
264264
{
265265
host: polykeyAgent.agentServiceHost,
266266
port: polykeyAgent.agentServicePort,
267+
scopes: ['global'],
267268
},
268269
);
269270
await polykeyAgent.acl.setNodePerm(targetNodeId, {
@@ -834,7 +835,8 @@ describe('CLI vaults', () => {
834835
await polykeyAgent.nodeManager.setNode(remoteOnlineNodeId, {
835836
host: remoteOnline.agentServiceHost,
836837
port: remoteOnline.agentServicePort,
837-
} as NodeAddress);
838+
scopes: ['global'],
839+
});
838840

839841
await remoteOnline.gestaltGraph.setNode({
840842
nodeId: polykeyAgent.keyRing.getNodeId(),

0 commit comments

Comments
 (0)