Skip to content

Commit

Permalink
feat: added support for searching finding multiple addresses with `no…
Browse files Browse the repository at this point in the history
…des find`
  • Loading branch information
amydevs committed Nov 9, 2023
1 parent 0bb0e02 commit 036a91d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
37 changes: 21 additions & 16 deletions src/nodes/CommandFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class CommandFind extends CommandPolykey {
success: false,
message: '',
id: '',
host: '',
port: 0,
addresses: [] as Array<{ host: string; port: number }>,
};
const builtAddresses: Array<string> = [];
try {
const response = await binUtils.retryAuthentication(
(auth) =>
Expand All @@ -69,12 +69,13 @@ class CommandFind extends CommandPolykey {
);
result.success = true;
result.id = nodesUtils.encodeNodeId(nodeId);
result.host = response.host;
result.port = response.port;
result.message = `Found node at ${networkUtils.buildAddress(
result.host as Host,
result.port as Port,
)}`;
for (const { host, port } of response.addresses) {
result.addresses.push({ host, port });
builtAddresses.push(
networkUtils.buildAddress(host as Host, port as Port),
);
}
result.message = `Found node at ${builtAddresses.join(', ')}`;
} catch (err) {
if (
!(err.cause instanceof nodesErrors.ErrorNodeGraphNodeIdNotFound)
Expand All @@ -84,16 +85,20 @@ class CommandFind extends CommandPolykey {
// Else failed to find the node.
result.success = false;
result.id = nodesUtils.encodeNodeId(nodeId);
result.host = '';
result.port = 0;
result.message = `Failed to find node ${result.id}`;
}
let output: any = result;
if (options.format === 'human') output = [result.message];
const outputFormatted = binUtils.outputFormatter({
type: options.format === 'json' ? 'json' : 'list',
data: output,
});
let outputFormatted: string | Uint8Array;
if (options.format === 'json') {
outputFormatted = binUtils.outputFormatter({
type: 'json',
data: result,
});
} else {
outputFormatted = binUtils.outputFormatter({
type: 'list',
data: ['Found node at', ...builtAddresses],
});
}
process.stdout.write(outputFormatted);
// Like ping it should error when failing to find node for automation reasons.
if (!result.success) {
Expand Down
41 changes: 31 additions & 10 deletions tests/nodes/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,24 @@ describe('find', () => {
},
);
expect(exitCode).toBe(0);
expect(JSON.parse(stdout)).toEqual({
const output = JSON.parse(stdout);
expect(output).toMatchObject({
success: true,
message: `Found node at ${remoteOnlineHost}:${remoteOnlinePort}`,
id: nodesUtils.encodeNodeId(remoteOnlineNodeId),
host: remoteOnlineHost,
port: remoteOnlinePort,
});
expect(output.addresses).toEqual(
expect.arrayContaining([
{
host: remoteOnlineHost,
port: remoteOnlinePort,
},
]),
);
expect(output.message).toMatch(
new RegExp(
`Found node at .*?${remoteOnlineHost}:${remoteOnlinePort}.*?`,
),
);
},
);
testUtils.testIf(testUtils.isTestPlatformEmpty)(
Expand All @@ -145,13 +156,24 @@ describe('find', () => {
},
);
expect(exitCode).toBe(0);
expect(JSON.parse(stdout)).toEqual({
const output = JSON.parse(stdout);
expect(output).toMatchObject({
success: true,
message: `Found node at ${remoteOfflineHost}:${remoteOfflinePort}`,
id: nodesUtils.encodeNodeId(remoteOfflineNodeId),
host: remoteOfflineHost,
port: remoteOfflinePort,
});
expect(output.addresses).toEqual(
expect.arrayContaining([
{
host: remoteOfflineHost,
port: remoteOfflinePort,
},
]),
);
expect(output.message).toMatch(
new RegExp(
`Found node at .*?${remoteOfflineHost}:${remoteOfflinePort}.*?`,
),
);
},
);
testUtils.testIf(testUtils.isTestPlatformEmpty)(
Expand Down Expand Up @@ -183,8 +205,7 @@ describe('find', () => {
unknownNodeId!,
)}`,
id: nodesUtils.encodeNodeId(unknownNodeId!),
host: '',
port: 0,
addresses: [],
});
},
globalThis.failedConnectionTimeout,
Expand Down

0 comments on commit 036a91d

Please sign in to comment.