Rename tests file to test for mocha

This commit is contained in:
Logan Cusano
2024-04-07 16:59:51 -04:00
parent 9bc80887ce
commit 757fdfb3b2
2 changed files with 221 additions and 76 deletions

View File

@@ -0,0 +1,221 @@
// 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
});
});
});

View File

@@ -1,76 +0,0 @@
import { expect } from 'chai';
import { createNode, getNodeByNuid, updateNodeByNuid } from '../modules/mongoNodesWrappers'; // Import necessary functions from your wrappers
import { nodeLoginWrapper, nodeUpdateWrapper, nearbySystemsUpdateWraper } from '../modules/socketServerWrappers'; // Import the functions you want to test
import { ClientNodeObject, ClientNodeConfig } from '../../client/modules/clientObjectDefinitions.mjs'; // Import the objects definitions
describe('Socket Server Wrappers', function() {
describe('nodeLoginWrapper', function() {
it('should create a new node if it does not exist', async function() {
const socket = {
node: undefined, // Initialize a socket with no node
id: 'mockSocketId' // Mock socket id
};
// Mock data using ClientNodeConfig object
const config = new ClientNodeConfig({
_nuid: 'mockNuid',
_name: 'mockName',
_location: 'mockLocation',
_capabilities: { /* mock capabilities */ }
});
await nodeLoginWrapper(config, socket);
// Check if the node was created
const createdNode = await getNodeByNuid('mockNuid');
expect(createdNode).to.exist;
});
// Add more test cases as needed
});
describe('nodeUpdateWrapper', function() {
it('should update the node data in the database', async function() {
// Mock node data using ClientNodeObject
const nodeData = new ClientNodeObject({
_nuid: 'mockNuid',
_name: 'mockName',
_location: 'mockLocation',
_capabilities: { /* mock capabilities */ }
});
await nodeUpdateWrapper(nodeData);
// Check if the node was updated in the database
const updatedNode = await getNodeByNuid('mockNuid');
// Assert that the node has been updated with the new data
expect(updatedNode).to.deep.include({
nuid: 'mockNuid',
name: 'mockName',
location: 'mockLocation',
capabilities: { /* mock capabilities */ }
});
});
// Add more test cases as needed
});
describe('nearbySystemsUpdateWraper', function() {
it('should update nearby systems in the database', async function() {
// Mock nearby systems data
const nearbySystems = {
// Mock nearby systems data
};
const nuid = 'mockNuid'; // Mock nuid
await nearbySystemsUpdateWraper(nuid, nearbySystems);
// Perform assertions to ensure nearby systems are updated correctly
// You may need to check the database for updated nearby systems
});
// Add more test cases as needed
});
// Add more describe blocks for other functions and their respective test cases
});