Files
DRBv3/client/test/pdabHandler.test.js
Logan Cusano 6cae18e70c
Some checks failed
DRB Build Tests / drb_build_and_test (push) Failing after 58s
Update PDAB tests
- These new tests will likely not run in actions due to pyaudio
2024-04-27 23:34:30 -04:00

214 lines
7.4 KiB
JavaScript

// Import necessary modules for testing
import { use, expect } from 'chai'
import chaiHttp from 'chai-http'
const chai = use(chaiHttp)
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.DISCORD_CLIENT_ID;
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 => {
after(async () => {
// Any teardown needed after tests
try {
await socket.close();
}
catch {
console.log("Socket already closed");
}
});
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);
});
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 have opened OP25', async () => {
// Assuming your server is running on localhost:8081
const res = await chai.request('http://localhost:8081').get('/');
expect(res).to.have.status(200); // Assuming 200 is the expected status code
// Add more assertions if needed
});
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();
});
});