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`

fix: tests regarding new `js-mdns` integration
  • Loading branch information
amydevs committed Nov 9, 2023
1 parent fb8a94f commit 2fbe04d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 33 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
2 changes: 2 additions & 0 deletions tests/agent/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ describe('start', () => {
[seedNodeId2]: {
host: seedNodeHost2 as Host,
port: seedNodePort2 as Port,
scopes: ['global'],
},
},
testnet: {},
Expand Down Expand Up @@ -998,6 +999,7 @@ describe('start', () => {
[seedNodeId2]: {
host: seedNodeHost2 as Host,
port: seedNodePort2 as Port,
scopes: ['global'],
},
},
});
Expand Down
12 changes: 10 additions & 2 deletions tests/nodes/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ describe('add', () => {
expect(exitCode).toBe(0);
// Checking if node was added.
const node = await pkAgent.nodeGraph.getNode(validNodeId);
expect(node?.address).toEqual({ host: validHost, port: port });
expect(node?.address).toEqual({
host: validHost,
port: port,
scopes: ['global'],
});
},
);
testUtils.testIf(testUtils.isTestPlatformEmpty)(
Expand Down Expand Up @@ -197,7 +201,11 @@ describe('add', () => {
expect(exitCode).toBe(0);
// Checking if node was added.
const node = await pkAgent.nodeGraph.getNode(validNodeId);
expect(node?.address).toEqual({ host: validHost, port: port });
expect(node?.address).toEqual({
host: validHost,
port: port,
scopes: ['global'],
});
},
);
});
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
1 change: 1 addition & 0 deletions tests/nodes/ping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('ping', () => {
passwordMemLimit: keysUtils.passwordMemLimits.min,
strictMemoryLock: false,
},
// TODO: fix this
nodes: {
connectionConnectTimeoutTime: 2000,
},
Expand Down
7 changes: 4 additions & 3 deletions tests/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type PolykeyAgent from 'polykey/dist/PolykeyAgent';
import type { NodeAddress } from 'polykey/dist/nodes/types';
import { promise } from 'polykey/dist/utils/utils';

function testIf(condition: boolean) {
Expand Down Expand Up @@ -69,12 +68,14 @@ async function nodesConnect(localNode: PolykeyAgent, remoteNode: PolykeyAgent) {
await localNode.nodeManager.setNode(remoteNode.keyRing.getNodeId(), {
host: remoteNode.agentServiceHost,
port: remoteNode.agentServicePort,
} as NodeAddress);
scopes: ['global'],
});
// Add local node's details to remote node
await remoteNode.nodeManager.setNode(localNode.keyRing.getNodeId(), {
host: localNode.agentServiceHost,
port: localNode.agentServicePort,
} as NodeAddress);
scopes: ['global'],
});
}

export { testIf, describeIf, trackTimers, nodesConnect };
6 changes: 4 additions & 2 deletions tests/vaults/vaults.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { NodeAddress } from 'polykey/dist/nodes/types';
import type { VaultId, VaultName } from 'polykey/dist/vaults/types';
import type { GestaltNodeInfo } from 'polykey/dist/gestalts/types';
import path from 'path';
Expand Down Expand Up @@ -258,12 +257,14 @@ describe('CLI vaults', () => {
await polykeyAgent.nodeManager.setNode(targetNodeId, {
host: targetPolykeyAgent.agentServiceHost,
port: targetPolykeyAgent.agentServicePort,
scopes: ['global'],
});
await targetPolykeyAgent.nodeManager.setNode(
polykeyAgent.keyRing.getNodeId(),
{
host: polykeyAgent.agentServiceHost,
port: polykeyAgent.agentServicePort,
scopes: ['global'],
},
);
await polykeyAgent.acl.setNodePerm(targetNodeId, {
Expand Down Expand Up @@ -834,7 +835,8 @@ describe('CLI vaults', () => {
await polykeyAgent.nodeManager.setNode(remoteOnlineNodeId, {
host: remoteOnline.agentServiceHost,
port: remoteOnline.agentServicePort,
} as NodeAddress);
scopes: ['global'],
});

await remoteOnline.gestaltGraph.setNode({
nodeId: polykeyAgent.keyRing.getNodeId(),
Expand Down

0 comments on commit 2fbe04d

Please sign in to comment.