Compare commits

...

5 Commits

Author SHA1 Message Date
Logan Cusano
4df3de4d4a Passing local server tests
Some checks failed
Run Socket Server Tests / test (push) Failing after 30s
2024-04-07 19:01:00 -04:00
Logan Cusano
461b449194 Added success emits for server functions 2024-04-07 17:01:51 -04:00
Logan Cusano
757fdfb3b2 Rename tests file to test for mocha 2024-04-07 16:59:51 -04:00
Logan Cusano
9bc80887ce Updated Package 2024-04-07 16:59:16 -04:00
Logan Cusano
0ce0f72ed3 Update Server
- Move socket server start to main server file
- This way tests and the main function can use the same code
2024-04-07 16:58:50 -04:00
7 changed files with 1296 additions and 92 deletions

View File

@@ -17,13 +17,19 @@ app.get('/', (req, res) => {
nodeIo.on('connection', (socket) => {
console.log('a user connected', socket.id);
socket.on('node-login', (data) => {
nodeLoginWrapper(data, socket);
socket.on('node-login', async (data) => {
await nodeLoginWrapper(data, socket);
await socket.emit('node-login-successful');
})
socket.on('node-update', (data) => {
nodeUpdateWrapper(data.node);
nearbySystemsUpdateWraper(data.node.nuid, data.nearbySystems)
socket.on('node-update', async (data) => {
let tempPromises = [];
tempPromises.push(nodeUpdateWrapper(data.node));
tempPromises.push(nearbySystemsUpdateWraper(data.node.nuid, data.nearbySystems));
await Promise.all(tempPromises);
await socket.emit('node-update-successful')
})
socket.on('disconnect', () => {
@@ -31,8 +37,3 @@ nodeIo.on('connection', (socket) => {
});
});
// Startup the node server
server.listen(3000, () => {
console.log('server running at http://localhost:3000');
});

View File

@@ -28,15 +28,17 @@ export const nodeLoginWrapper = async (data, socket) => {
console.log("After grabbing", node);
if (!node) {
const insertedId = await createNode(data);
node = await getNodeByNuid(data.nuid);
console.log("Added new node to the database:", insertedId);
} else {
// Check for updates
const updatedNode = await updateNodeByNuid(data.nuid, data)
console.log("Updated node:", updatedNode);
}
// Check for updates if so
// Check for System updates
node = await getNodeByNuid(data.nuid);
// Add the socket/node connection
socket.node = node;
//socket.id = node.nuid;
return;
}

980
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,15 +4,16 @@
"description": "",
"main": "server.js",
"scripts": {
"test": "mocha",
"test": "mocha --timeout 5000",
"start": "node server.js"
},
"author": "Logan Cusano",
"license": "ISC",
"type": "module",
"devDependencies": {
"chai": "^5.1.0",
"mocha": "^10.4.0",
"chai": "^5.1.0"
"socket.io-client": "^4.7.5"
},
"dependencies": {
"discord.js": "^14.14.1",

View File

@@ -5,6 +5,11 @@ import { serverClient, addEnabledEventListeners } from './discordBot/discordBot.
import dotenv from 'dotenv';
dotenv.config()
// Startup the node server
server.listen(process.env.SERVER_PORT || 3000, () => {
console.log(`server running at http://localhost:${process.env.SERVER_PORT}`);
});
// Add objects to the others
serverClient.nodeIo = nodeIo;
nodeIo.serverClient = serverClient;

View File

@@ -0,0 +1,291 @@
// 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 { deleteSystemByName, getSystemByName } from '../modules/mongoSystemsWrappers.mjs';
import { logIntoServerWrapper } from '../../client/modules/socketClientWrappers.mjs';
import dotenv from 'dotenv';
dotenv.config()
process.env.SERVER_PORT = 6000
// Define necessary variables for testing, such as mocked database connections or socket instances
const localNodeConfig = {
serverIp: 'localhost',
serverPort: process.env.SERVER_PORT,
node: {
nuid: "4f29a6340901a12affc87047c0ac16b01b92496c460c880a2459abe8c7928374",
name: "testyv7",
location: "china",
capabilities: ["radio"]
},
nearbySystems: {
"Testing P25 System Name": {
"frequencies": [
155344000,
155444000,
155555000,
155588550
],
"mode": "p25",
"trunkFile": "trunk.tsv",
"whitelistFile": "whitelist.tsv"
}
}
};
const updatedLocalNodeConfig = {
node: {
nuid: localNodeConfig.node.nuid,
name: "updatedName",
location: "updatedLocation",
capabilities: ["radio", "weather"] // Updated capabilities
},
nearbySystems: {
"Testing P25 System Name": {
"frequencies": [
155444000,
155555000,
155500000
],
"mode": "p25",
"trunkFile": "trunk2.tsv",
"whitelistFile": "whitelist2.tsv"
}
}
};
// 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 data
deleteNodeByNuid(localNodeConfig.node.nuid); // Delete the user
deleteSystemByName(Object.keys(localNodeConfig.nearbySystems)[0])
});
describe('Node Core Server Tests', () => {
// 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 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 matches the existing data before logging in
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();
});
});
// Emit the login command
clientSocket.emit("node-login", updatedLocalNodeConfig.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', updatedLocalNodeConfig.node.nuid);
expect(updatedNode).to.have.property('name', updatedLocalNodeConfig.node.name);
expect(updatedNode).to.have.property('location', updatedLocalNodeConfig.node.location);
expect(updatedNode).to.have.deep.property('capabilities', updatedLocalNodeConfig.node.capabilities);
})
});
// Test Node Update functionality
describe('Node Update', () => {
it('should add a node\'s nearby systems', async () => {
// Simulate an update request sent from the client to the server
// Get the existing node in the database
const existingNode = await getNodeByNuid(localNodeConfig.node.nuid);
// Assert that existingNode matches the updatedLocalNodeConfig
expect(existingNode).to.have.property('_id'); // Check if _id property exists
expect(existingNode).to.have.property('nuid', updatedLocalNodeConfig.node.nuid);
expect(existingNode).to.have.property('name', updatedLocalNodeConfig.node.name);
expect(existingNode).to.have.property('location', updatedLocalNodeConfig.node.location);
expect(existingNode).to.have.deep.property('capabilities', updatedLocalNodeConfig.node.capabilities);
// Get the system from the DB
const existsingSystem = await getSystemByName("Testing P25 System Name");
// Assert that there is no existing system in the DB
expect(existsingSystem).to.be.null;
// Wait for the update
const node_system_update = new Promise(res => {
clientSocket.on('node-update-successful', async () => {
res();
});
});
// Emit the update command
clientSocket.emit("node-update", updatedLocalNodeConfig);
// Wait for the successful update event
await node_system_update;
// Now we need to check if the system 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', updatedLocalNodeConfig.node.nuid);
expect(updatedNode).to.have.property('name', updatedLocalNodeConfig.node.name);
expect(updatedNode).to.have.property('location', updatedLocalNodeConfig.node.location);
expect(updatedNode).to.have.deep.property('capabilities', updatedLocalNodeConfig.node.capabilities);
// Get the updated system
const addedSystem = await getSystemByName("Testing P25 System Name");
console.log("Added system:", addedSystem);
expect(addedSystem).to.have.property('_id'); // Check if _id property exists
expect(addedSystem).to.have.property('nodes'); // Check if nodes property exists
expect(addedSystem.nodes).to.include(updatedLocalNodeConfig.node.nuid) // Check if this node ID is in the nodes array
expect(addedSystem).to.have.deep.property('frequencies', updatedLocalNodeConfig.nearbySystems['Testing P25 System Name'].frequencies);
expect(addedSystem).to.have.property('mode', updatedLocalNodeConfig.nearbySystems['Testing P25 System Name'].mode);
});
it('should update a node and its nearby systems', async () => {
// Get the existing node in the database
const existingNode = await getNodeByNuid(localNodeConfig.node.nuid);
// Assert that existingNode matches the updatedLocalNodeConfig
expect(existingNode).to.have.property('_id'); // Check if _id property exists
expect(existingNode).to.have.property('nuid', updatedLocalNodeConfig.node.nuid);
expect(existingNode).to.have.property('name', updatedLocalNodeConfig.node.name);
expect(existingNode).to.have.property('location', updatedLocalNodeConfig.node.location);
expect(existingNode).to.have.deep.property('capabilities', updatedLocalNodeConfig.node.capabilities);
// Get the updated system
const existingSystem = await getSystemByName("Testing P25 System Name");
expect(existingSystem).to.have.property('_id'); // Check if _id property exists
expect(existingSystem).to.have.property('nodes'); // Check if nodes property exists
expect(existingSystem.nodes).to.include(updatedLocalNodeConfig.node.nuid) // Check if this node ID is in the nodes array
expect(existingSystem).to.have.deep.property('frequencies', updatedLocalNodeConfig.nearbySystems['Testing P25 System Name'].frequencies);
expect(existingSystem).to.have.property('mode', updatedLocalNodeConfig.nearbySystems['Testing P25 System Name'].mode);
// Wait for the update
const node_update = new Promise(res => {
clientSocket.on('node-update-successful', async () => {
res();
});
});
// Emit the update command
clientSocket.emit("node-update", localNodeConfig);
// Wait for the successful update event
await node_update;
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', localNodeConfig.node.nuid);
expect(updatedNode).to.have.property('name', localNodeConfig.node.name);
expect(updatedNode).to.have.property('location', localNodeConfig.node.location);
expect(updatedNode).to.have.deep.property('capabilities', localNodeConfig.node.capabilities);
// Get the updated system
const updatedSystem = await getSystemByName("Testing P25 System Name");
console.log("Updated system:", updatedSystem);
expect(updatedSystem).to.have.property('_id'); // Check if _id property exists
expect(updatedSystem).to.have.property('nodes'); // Check if nodes property exists
expect(updatedSystem.nodes).include(localNodeConfig.node.nuid) // Check if this node ID is in the nodes array
expect(updatedSystem).to.have.deep.property('frequencies', localNodeConfig.nearbySystems['Testing P25 System Name'].frequencies);
expect(updatedSystem).to.have.property('mode', localNodeConfig.nearbySystems['Testing P25 System Name'].mode);
});
});
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
});
})
});

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
});