Skip to content

Commit

Permalink
chore: jests for nodesGetAll and nodesListConnections callers.
Browse files Browse the repository at this point in the history
  • Loading branch information
addievo committed Oct 27, 2023
1 parent 77d2307 commit 5171a30
Showing 1 changed file with 279 additions and 2 deletions.
281 changes: 279 additions & 2 deletions tests/client/handlers/nodes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,22 @@ 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,
NodesListConnections,
} from '@/client/handlers';
import {
nodesAdd,
nodesClaim,
nodesFind,
nodesPing,
nodesGetAll,
nodesListConnections,
} 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 @@ -680,3 +694,266 @@ describe('nodesPing', () => {
);
});
});
describe('nodesGetAll', () => {
const logger = new Logger('nodesPing test', LogLevel.WARN, [
new StreamHandler(
formatting.format`${formatting.level}:${formatting.keys}:${formatting.msg}`,
),
]);
const password = 'helloWorld';
const localhost = '127.0.0.1';
let dataDir: string;
let db: DB;
let keyRing: KeyRing;
let tlsConfig: TLSConfig;
let clientService: ClientService;
let webSocketClient: WebSocketClient;
let rpcClient: RPCClient<{
nodesGetAll: typeof nodesGetAll;
}>;
let nodeGraph: NodeGraph;
let taskManager: TaskManager;
let nodeConnectionManager: NodeConnectionManager;
let nodeManager: NodeManager;
let sigchain: Sigchain;
let mockedPingNode: jest.SpyInstance;
beforeEach(async () => {
mockedPingNode = jest.spyOn(NodeManager.prototype, 'pingNode');
dataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-test-'),
);
const keysPath = path.join(dataDir, 'keys');
keyRing = await KeyRing.createKeyRing({
password,
keysPath,
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
passwordMemLimit: keysUtils.passwordMemLimits.min,
strictMemoryLock: false,
logger,
});
tlsConfig = await testsUtils.createTLSConfig(keyRing.keyPair);
const dbPath = path.join(dataDir, 'db');
db = await DB.createDB({
dbPath,
logger,
});
sigchain = await Sigchain.createSigchain({
db,
keyRing,
logger,
});
nodeGraph = await NodeGraph.createNodeGraph({
db,
keyRing,
logger: logger.getChild('NodeGraph'),
});
taskManager = await TaskManager.createTaskManager({
db,
logger,
lazy: true,
});
nodeConnectionManager = new NodeConnectionManager({
keyRing,
nodeGraph,
// TLS not needed for this test
tlsConfig: {} as TLSConfig,
connectionConnectTimeoutTime: 2000,
connectionIdleTimeoutTime: 2000,
logger: logger.getChild('NodeConnectionManager'),
});
nodeManager = new NodeManager({
db,
keyRing,
nodeConnectionManager,
nodeGraph,
sigchain,
taskManager,
gestaltGraph: {} as GestaltGraph,
logger,
});
await nodeConnectionManager.start({ host: localhost as Host });
await taskManager.startProcessing();
clientService = new ClientService({
tlsConfig,
logger: logger.getChild(ClientService.name),
});
await clientService.start({
manifest: {
nodesGetAll: new NodesGetAll({
nodeGraph,
keyRing,
}),
},
host: localhost,
});
webSocketClient = await WebSocketClient.createWebSocketClient({
config: {
verifyPeer: false,
},
host: localhost,
logger: logger.getChild(WebSocketClient.name),
port: clientService.port,
});
rpcClient = new RPCClient({
manifest: {
nodesGetAll,
},
streamFactory: () => webSocketClient.connection.newStream(),
toError: networkUtils.toError,
logger: logger.getChild(RPCClient.name),
});
});
afterEach(async () => {
mockedPingNode.mockRestore();
await taskManager.stopProcessing();
await taskManager.stopTasks();
await clientService.stop({ force: true });
await webSocketClient.destroy({ force: true });
await sigchain.stop();
await nodeGraph.stop();
await nodeConnectionManager.stop();
await db.stop();
await keyRing.stop();
await taskManager.stop();
await fs.promises.rm(dataDir, {
force: true,
recursive: true,
});
});
test('gets all nodes', async () => {
mockedPingNode.mockResolvedValue(false);
const response = await rpcClient.methods.nodesGetAll({});
expect(response).toBeTruthy();
});
});
describe('nodesListConnections', () => {
const logger = new Logger('nodesPing test', LogLevel.WARN, [
new StreamHandler(
formatting.format`${formatting.level}:${formatting.keys}:${formatting.msg}`,
),
]);
const password = 'helloWorld';
const localhost = '127.0.0.1';
let dataDir: string;
let db: DB;
let keyRing: KeyRing;
let tlsConfig: TLSConfig;
let clientService: ClientService;
let webSocketClient: WebSocketClient;
let rpcClient: RPCClient<{
nodesListConnections: typeof nodesListConnections;
}>;
let nodeGraph: NodeGraph;
let taskManager: TaskManager;
let nodeConnectionManager: NodeConnectionManager;
let nodeManager: NodeManager;
let sigchain: Sigchain;
let mockedPingNode: jest.SpyInstance;
beforeEach(async () => {
mockedPingNode = jest.spyOn(NodeManager.prototype, 'pingNode');
dataDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), 'polykey-test-'),
);
const keysPath = path.join(dataDir, 'keys');
keyRing = await KeyRing.createKeyRing({
password,
keysPath,
passwordOpsLimit: keysUtils.passwordOpsLimits.min,
passwordMemLimit: keysUtils.passwordMemLimits.min,
strictMemoryLock: false,
logger,
});
tlsConfig = await testsUtils.createTLSConfig(keyRing.keyPair);
const dbPath = path.join(dataDir, 'db');
db = await DB.createDB({
dbPath,
logger,
});
sigchain = await Sigchain.createSigchain({
db,
keyRing,
logger,
});
nodeGraph = await NodeGraph.createNodeGraph({
db,
keyRing,
logger: logger.getChild('NodeGraph'),
});
taskManager = await TaskManager.createTaskManager({
db,
logger,
lazy: true,
});
nodeConnectionManager = new NodeConnectionManager({
keyRing,
nodeGraph,
// TLS not needed for this test
tlsConfig: {} as TLSConfig,
connectionConnectTimeoutTime: 2000,
connectionIdleTimeoutTime: 2000,
logger: logger.getChild('NodeConnectionManager'),
});
nodeManager = new NodeManager({
db,
keyRing,
nodeConnectionManager,
nodeGraph,
sigchain,
taskManager,
gestaltGraph: {} as GestaltGraph,
logger,
});
await nodeConnectionManager.start({ host: localhost as Host });
await taskManager.startProcessing();
clientService = new ClientService({
tlsConfig,
logger: logger.getChild(ClientService.name),
});
await clientService.start({
manifest: {
nodesListConnections: new NodesListConnections({
nodeConnectionManager,
}),
},
host: localhost,
});
webSocketClient = await WebSocketClient.createWebSocketClient({
config: {
verifyPeer: false,
},
host: localhost,
logger: logger.getChild(WebSocketClient.name),
port: clientService.port,
});
rpcClient = new RPCClient({
manifest: {
nodesListConnections,
},
streamFactory: () => webSocketClient.connection.newStream(),
toError: networkUtils.toError,
logger: logger.getChild(RPCClient.name),
});
});
afterEach(async () => {
mockedPingNode.mockRestore();
await taskManager.stopProcessing();
await taskManager.stopTasks();
await clientService.stop({ force: true });
await webSocketClient.destroy({ force: true });
await sigchain.stop();
await nodeGraph.stop();
await nodeConnectionManager.stop();
await db.stop();
await keyRing.stop();
await taskManager.stop();
await fs.promises.rm(dataDir, {
force: true,
recursive: true,
});
});
test('lists all connections', async () => {
mockedPingNode.mockResolvedValue(false);
const response = await rpcClient.methods.nodesListConnections({});
expect(response).toBeTruthy();
});
});

0 comments on commit 5171a30

Please sign in to comment.