Files
DRBv3/client/test/pdabHandler.test.js
Logan Cusano a353b9adbb Improvements to tests and from testing
- Moved OP25 start outside of PDAB start callback
- Added more logging
- Set the pdabProcess variable to false when closing the discord client
- Update test variable names
- Added new WIP pdabWrappers tests to test the full wrappers as the client would
2024-04-28 03:21:03 -04:00

194 lines
6.8 KiB
JavaScript

// Import necessary modules for testing
import { expect } from 'chai'
import io from 'socket.io-client';
const ioClient = io;
import dotenv from 'dotenv';
dotenv.config()
import { initDiscordBotClient, connectToChannel, leaveVoiceChannel, checkIfConnectedToVC, requestDiscordUsername, requestDiscordID, requestDiscordClientClose, closePdabSocketServer } from '../discordAudioBot/pdabHandler.mjs';
let socket;
before(async done => {
// Any setup needed before tests
done();
});
after(async () => {
await closePdabSocketServer();
})
describe('Socket Server Tests', done => {
after(async () => {
// Any teardown needed after tests
try {
await socket.close();
}
catch {
console.log("Socket already closed");
}
console.log('Closing PDAB Socker Server');
});
it('Should open a socket server and callback when the client is connected and ready', done => {
const clientId = process.env.TEST_CLIENT_TOKEN;
const callback = () => {
done();
};
initDiscordBotClient(clientId, callback, false);
socket = ioClient.connect(`http://localhost:${process.env.PDAB_PORT}`);
socket.on('connect', () => {
socket.emit('discord_ready')
});
});
it('Should emit command for and return status from join server', async () => {
socket.on('join_server', (data, callback) => {
console.log('Join data from server:', data);
expect(data).to.deep.equal({ channelId: process.env.TEST_CHANNEL_ID });
callback(true, true);
})
// Simulate emitting 'join_server' event
const status = await connectToChannel(process.env.TEST_CHANNEL_ID);
// Check the server sent the expected info
// Assert the status returned from the server
expect(status).to.be.true;
});
it('Should emit command for and return open status for leave server', async () => {
socket.on('leave_server', (data, callback) => {
console.log('Leave data from server:', data);
expect(data).to.deep.equal({ guildId: process.env.TEST_GUILD_ID });
callback(false, false);
});
// Simulate emitting 'leave_server' event
const openStatus = await leaveVoiceChannel(process.env.TEST_GUILD_ID);
// Assert the open status returned from the server
expect(openStatus).to.be.false;
});
it('Should emit command for and return status if connected to voice channel', async () => {
socket.on('check_discord_vc_connected', (data, callback) => {
console.log('Client Check data:', data);
expect(data).to.deep.equal({ guildId: process.env.TEST_GUILD_ID });
callback(true, true);
});
// Simulate emitting 'check_discord_vc_connected' event
const isConnected = await checkIfConnectedToVC(process.env.TEST_GUILD_ID);
// Assert the connection status returned from the server
expect(isConnected).to.be.true;
});
it('Should emit command for and return username for request discord username', async () => {
socket.on('request_discord_username', (data, callback) => {
console.log('Username Check data:', data);
expect(data).to.deep.equal({ guildId: process.env.TEST_GUILD_ID });
callback(process.env.EXPECTED_USERNAME);
});
// Simulate emitting 'request_discord_username' event
const username = await requestDiscordUsername(process.env.TEST_GUILD_ID);
// Assert the username returned from the server
expect(username).to.equal(process.env.EXPECTED_USERNAME);
});
it('Should emit command for and return discord client ID', async () => {
socket.on('request_discord_id', (callback) => {
callback(process.env.EXPECTED_CLIENT_ID);
});
// Simulate emitting 'request_discord_id' event
const clientId = await requestDiscordID();
// Assert the client ID returned from the server
expect(clientId).to.equal(process.env.EXPECTED_CLIENT_ID);
});
it('Should emit command for discord client to close', done => {
socket.on('request_client_close', async () => {
await socket.close();
done()
})
// Simulate emitting 'request_client_close' event
requestDiscordClientClose();
});
});
describe('Socket Client & Python IPC Tests', done => {
it('Should open a socket server and callback when the client is connected and ready', done => {
let clientConnected = false;
const clientId = process.env.TEST_CLIENT_TOKEN;
const callback = () => {
clientConnected = true;
expect(clientConnected).to.be.true;
done();
};
initDiscordBotClient(clientId, callback);
});
it('Should emit command for and return status from join server', async () => {
// Simulate emitting 'join_server' event
const status = await connectToChannel(process.env.TEST_CHANNEL_ID);
// Check the server sent the expected info
// Assert the status returned from the server
expect(status).to.be.true;
});
it('Should emit command for and return status if connected to voice channel', async () => {
// Simulate emitting 'check_discord_vc_connected' event
const isConnected = await checkIfConnectedToVC(process.env.TEST_GUILD_ID);
// Assert the connection status returned from the server
expect(isConnected).to.be.true;
});
it('Should emit command for and return username for request discord username', async () => {
// Simulate emitting 'request_discord_username' event
const username = await requestDiscordUsername(process.env.TEST_GUILD_ID);
// Assert the username returned from the server
expect(username).to.equal(process.env.EXPECTED_USERNAME);
});
it('Should emit command for and return discord client ID', async () => {
// Simulate emitting 'request_discord_id' event
const clientId = await requestDiscordID();
console.log("type of client id", typeof clientId, typeof process.env.EXPECTED_CLIENT_ID);
// Assert the client ID returned from the server
expect(clientId).to.equal(Number(process.env.EXPECTED_CLIENT_ID));
});
it('Should emit command for and return open status for leave server', async () => {
// Simulate emitting 'leave_server' event
const openStatus = await leaveVoiceChannel(process.env.TEST_GUILD_ID);
// Assert the open status returned from the server
expect(openStatus).to.be.false;
});
it('Should emit command for discord client to close', async () => {
// Simulate emitting 'request_client_close' event
await requestDiscordClientClose();
});
});