Skip to content

Commit

Permalink
fix: fix nodesGetAll Handler and add a jest to test the same.
Browse files Browse the repository at this point in the history
  • Loading branch information
addievo committed Oct 26, 2023
1 parent 07c3037 commit 935df0c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
21 changes: 7 additions & 14 deletions src/client/handlers/NodesGetAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,18 @@ class NodesGetAll extends ServerHandler<
): AsyncGenerator<ClientRPCResponseResult<NodesGetMessage>> {
if (ctx.signal.aborted) throw ctx.signal.reason;
const { nodeGraph, keyRing } = this.container;
for await (const bucket of nodeGraph.getBuckets()) {
let index;
for (const id of Object.keys(bucket)) {
const encodedId = nodesUtils.encodeNodeId(
IdInternal.fromString<NodeId>(id),
);
for await (const [index, bucket] of nodeGraph.getBuckets()) {
for (const [id, info] of bucket) {
const encodedId = nodesUtils.encodeNodeId(keyRing.getNodeId());
// For every node in every bucket, add it to our message
if (!index) {
index = nodesUtils.bucketIndex(
keyRing.getNodeId(),
IdInternal.fromString<NodeId>(id),
);
if (ctx.signal.aborted) {
throw ctx.signal.reason;
}
if (ctx.signal.aborted) throw ctx.signal.reason;
yield {
bucketIndex: index,
nodeIdEncoded: encodedId,
host: bucket[id].address.host,
port: bucket[id].address.port,
host: info.address.host,
port: info.address.port,
};
}
}
Expand Down
36 changes: 34 additions & 2 deletions tests/client/handlers/nodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ import Sigchain from '@/sigchain/Sigchain';
import NotificationsManager from '@/notifications/NotificationsManager';
import NodeConnectionManager from '@/nodes/NodeConnectionManager';
import ClientService from '@/client/ClientService';
import { NodesAdd, NodesClaim, NodesFind, NodesPing } from '@/client/handlers';
import { nodesAdd, nodesClaim, nodesFind, nodesPing } from '@/client/callers';
import {
NodesAdd,
NodesClaim,
NodesFind,
NodesPing,
NodesGetAll,
} from '@/client/handlers';
import {
nodesAdd,
nodesClaim,
nodesFind,
nodesPing,
nodesGetAll,
} from '@/client/callers';
import * as keysUtils from '@/keys/utils';
import * as nodesUtils from '@/nodes/utils';
import * as networkUtils from '@/network/utils';
Expand Down Expand Up @@ -233,6 +245,9 @@ describe('nodesClaim', () => {
let rpcClient: RPCClient<{
nodesClaim: typeof nodesClaim;
}>;
let rpcClient2: RPCClient<{
nodesGetAll: typeof nodesGetAll;
}>;
let nodeGraph: NodeGraph;
let taskManager: TaskManager;
let nodeConnectionManager: NodeConnectionManager;
Expand Down Expand Up @@ -352,6 +367,14 @@ describe('nodesClaim', () => {
toError: networkUtils.toError,
logger: logger.getChild(RPCClient.name),
});
rpcClient2 = new RPCClient({
manifest: {
nodesGetAll,
},
streamFactory: () => webSocketClient.connection.newStream(),
toError: networkUtils.toError,
logger: logger.getChild(RPCClient.name),
});
});
afterEach(async () => {
mockedFindGestaltInvite.mockRestore();
Expand Down Expand Up @@ -383,6 +406,15 @@ describe('nodesClaim', () => {
});
expect(response.success).toBeTruthy();
});
test('gets all nodes', async () => {
const response = await rpcClient.methods.nodesClaim({
nodeIdEncoded:
'vrsc24a1er424epq77dtoveo93meij0pc8ig4uvs9jbeld78n9nl0' as NodeIdEncoded,
forceInvite: false,
});
const response2 = await rpcClient2.methods.nodesGetAll({});
expect(response.success).toBeTruthy();
});
test('cannot claim an invalid node', async () => {
await testsUtils.expectRemoteError(
rpcClient.methods.nodesClaim({
Expand Down

0 comments on commit 935df0c

Please sign in to comment.