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 d5723d4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 31 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
4 changes: 2 additions & 2 deletions tests/nodes/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ 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 +197,7 @@ 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
6 changes: 4 additions & 2 deletions tests/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,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 };
5 changes: 4 additions & 1 deletion tests/vaults/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,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 +836,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 d5723d4

Please sign in to comment.