Files
DRBv3/server/test/socketServerWrappers.test.js
2024-04-07 16:59:51 -04:00

221 lines
9.2 KiB
JavaScript

// Import necessary modules for testing
import { expect } from 'chai';
import ioClient from 'socket.io-client';
import { app, nodeIo, server } from '../modules/socketServer.mjs';
import { getNodeByNuid, deleteNodeByNuid } from '../modules/mongoNodesWrappers.mjs';
import { logIntoServerWrapper } from '../../client/modules/socketClientWrappers.mjs';
import dotenv from 'dotenv';
dotenv.config()
process.env.SERVER_PORT = 6000
// Start the Socket.IO server before running tests
let clientSocket; // The socket client
let serverClientSocket // The open client socket on the server
before(done => {
// Startup the node server
nodeIo.listen(process.env.SERVER_PORT || 3000, () => {
console.log(`server running at http://localhost:${process.env.SERVER_PORT}`);
});
// Connect a client socket to the server
clientSocket = ioClient.connect(`http://localhost:${process.env.SERVER_PORT}`);
nodeIo.on('connection', (socket) => {
serverClientSocket = socket;
done();
})
});
// Close the Socket.IO server after running tests
after(async () => {
// Disconnect client socket
clientSocket.disconnect();
// Close the server
nodeIo.close();
// Remove the test user
deleteNodeByNuid("4f29a6340901a12affc87047c0ac16b01b92496c460c880a2459abe8c7928595")
});
describe('Node Core Server Tests', () => {
// Define necessary variables for testing, such as mocked database connections or socket instances
const localNodeConfig = {
serverIp: 'localhost',
serverPort: process.env.SERVER_PORT,
node: {
nuid: "4f29a6340901a12affc87047c0ac16b01b92496c460c880a2459abe8c7928595",
name: "testyv3",
location: "georgia",
capabilities: ["radio"]
},
nearbySystems: {
"Default P25 System Name": {
"frequencies": [
155344000,
155444000,
155555000,
155588550
],
"mode": "p25",
"trunkFile": "trunk.tsv",
"whitelistFile": "whitelist.tsv"
}
}
};
// Test Node Login functionality
describe('Node Login', () => {
it('should add a new node if it does not exist', async () => {
// Simulate a node login request
// Use the getNodeByNuid mock function to simulate checking if node exists
const existingNode = await getNodeByNuid(localNodeConfig.node.nuid);
// Assert that existingNode is null before node login
expect(existingNode).to.be.null;
// Wait for the update
const node_login = new Promise(res => {
clientSocket.on('node-login-successful', async () => {
res();
});
});
// Emit the login command
clientSocket.emit("node-login", localNodeConfig.node);
// Wait for the successful login event
await node_login;
// Now we need to check if the node is added to the database
// We can use getNodeByNuid again to verify if the node was added correctly
const addedNode = await getNodeByNuid(localNodeConfig.node.nuid);
console.log("Added Node:", addedNode);
// Assert that the node is added correctly
expect(addedNode).to.have.property('_id'); // Check if _id property exists
expect(addedNode).to.have.property('nuid', localNodeConfig.node.nuid);
expect(addedNode).to.have.property('name', localNodeConfig.node.name);
expect(addedNode).to.have.property('location', localNodeConfig.node.location);
expect(addedNode).to.have.deep.property('capabilities', localNodeConfig.node.capabilities);
})
it('should update a new node if it exists', async () => {
// Simulate a node login request
// Use the getNodeByNuid mock function to simulate checking if node exists
const existingNode = await getNodeByNuid(localNodeConfig.node.nuid);
// Assert that existingNode is null before node login
expect(existingNode).to.have.property('_id'); // Check if _id property exists
expect(existingNode).to.have.property('nuid', localNodeConfig.node.nuid);
expect(existingNode).to.have.property('name', localNodeConfig.node.name);
expect(existingNode).to.have.property('location', localNodeConfig.node.location);
expect(existingNode).to.have.deep.property('capabilities', localNodeConfig.node.capabilities);
// Wait for the update
const node_login = new Promise(res => {
clientSocket.on('node-login-successful', async () => {
res();
});
});
const updatedNodeConfig = {
node: {
nuid: localNodeConfig.node.nuid,
name: "updatedName",
location: "updatedLocation",
capabilities: ["radio", "weather"] // Updated capabilities
}
};
// Emit the login command
clientSocket.emit("node-login", updatedNodeConfig.node);
// Wait for the successful login event
await node_login;
// Now we need to check if the node is added to the database
// We can use getNodeByNuid again to verify if the node was added correctly
const updatedNode = await getNodeByNuid(localNodeConfig.node.nuid);
console.log("Updated Node:", updatedNode);
// Assert that the node is added correctly
expect(updatedNode).to.have.property('_id'); // Check if _id property exists
expect(updatedNode).to.have.property('nuid', updatedNodeConfig.node.nuid);
expect(updatedNode).to.have.property('name', updatedNodeConfig.node.name);
expect(updatedNode).to.have.property('location', updatedNodeConfig.node.location);
expect(updatedNode).to.have.deep.property('capabilities', updatedNodeConfig.node.capabilities);
})
});
// Test Node Update functionality
describe('Node Update', () => {
it('should update a new node if it exists', async () => {
// Simulate a node login request
// Use the getNodeByNuid mock function to simulate checking if node exists
const existingNode = await getNodeByNuid(localNodeConfig.node.nuid);
// Assert that existingNode is null before node login
expect(existingNode).to.have.property('_id'); // Check if _id property exists
expect(existingNode).to.have.property('nuid', localNodeConfig.node.nuid);
expect(existingNode).to.have.property('name', localNodeConfig.node.name);
expect(existingNode).to.have.property('location', localNodeConfig.node.location);
expect(existingNode).to.have.deep.property('capabilities', localNodeConfig.node.capabilities);
// Wait for the update
const node_login = new Promise(res => {
clientSocket.on('node-update-successful', async () => {
res();
});
});
const updatedNodeConfig = {
node: {
nuid: localNodeConfig.node.nuid,
name: "updatedName",
location: "updatedLocation",
capabilities: ["radio", "weather"] // Updated capabilities
}
};
// Emit the login command
clientSocket.emit("node-update", updatedNodeConfig.node);
// Wait for the successful login event
await node_login;
// Now we need to check if the node is added to the database
// We can use getNodeByNuid again to verify if the node was added correctly
const updatedNode = await getNodeByNuid(localNodeConfig.node.nuid);
console.log("Updated Node:", updatedNode);
// Assert that the node is added correctly
expect(updatedNode).to.have.property('_id'); // Check if _id property exists
expect(updatedNode).to.have.property('nuid', updatedNodeConfig.node.nuid);
expect(updatedNode).to.have.property('name', updatedNodeConfig.node.name);
expect(updatedNode).to.have.property('location', updatedNodeConfig.node.location);
expect(updatedNode).to.have.deep.property('capabilities', updatedNodeConfig.node.capabilities);
});
});
describe('Node Disconnect', () => {
it('should trigger cleanup actions upon socket disconnection', async () => {
// Write test code to simulate a socket disconnection
// Check if the appropriate cleanup actions are triggered
});
})
});
describe('Node Systems Server Tests', () => {
// Test Nearby Systems Update functionality
describe('Nearby Systems Update', () => {
it('should update nearby systems correctly', async () => {
// Write test code to simulate nearby systems update
// Check if systems are added, updated, or removed appropriately based on the provided data
});
});
});