- Added client tests, started with the pdabHandler interactions - Updated caps in socket server tests - Updated pdabHandler with uniform 'guild_id' - Updated pdabHandler with production check to launch or not launch the python client -
134 lines
4.7 KiB
JavaScript
134 lines
4.7 KiB
JavaScript
// Import necessary modules for testing
|
|
import { expect } from 'chai';
|
|
import io from 'socket.io-client';
|
|
|
|
import dotenv from 'dotenv';
|
|
dotenv.config()
|
|
|
|
process.env.DISCORD_CLIENT_ID = "MTE5NjAwNTM2ODYzNjExMjk3Nw.GuCMXg.24iNNofNNumq46FIj68zMe9RmQgugAgfrvelEA"
|
|
process.env.TEST_CHANNEL_ID = 757417670466469989
|
|
process.env.TEST_GUILD_ID = 367396189529833472
|
|
process.env.EXPECTED_CLIENT_ID = 1196005368636112977
|
|
process.env.NODE_ENV = 'development'
|
|
process.env.PDAB_PORT = 3110
|
|
|
|
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 () => {
|
|
// Any teardown needed after tests
|
|
try{
|
|
await socket.close();
|
|
}
|
|
catch {
|
|
console.log("Socket already closed");
|
|
}
|
|
console.log('Closing PDAB Socker Server');
|
|
await closePdabSocketServer();
|
|
});
|
|
|
|
describe('Python Discord Bot Socket 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.DISCORD_CLIENT_ID;
|
|
|
|
const callback = () => {
|
|
clientConnected = true;
|
|
expect(clientConnected).to.be.true;
|
|
|
|
done();
|
|
};
|
|
|
|
initDiscordBotClient(clientId, callback);
|
|
|
|
socket = io.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('!bot-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("!bot-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();
|
|
});
|
|
|
|
}); |