Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
test(DataStore): add missing tests (#165)
Browse files Browse the repository at this point in the history
* tests(DataStore): add missing tests

* test(DataStore): add function spys for frame prep
  • Loading branch information
ckohen authored Aug 8, 2021
1 parent c7e5381 commit dd5751b
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/__tests__/DataStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
/* eslint-disable @typescript-eslint/dot-notation */
import { GatewayOpcodes } from 'discord-api-types';
import * as DataStore from '../DataStore';
import * as _AudioPlayer from '../audio/AudioPlayer';
import { VoiceConnection } from '../VoiceConnection';
jest.mock('../VoiceConnection');
jest.mock('../audio/AudioPlayer');

const AudioPlayer = _AudioPlayer as unknown as jest.Mocked<typeof _AudioPlayer>;

function createVoiceConnection(joinConfig: Pick<DataStore.JoinConfig, 'group' | 'guildId'>): VoiceConnection {
return {
joinConfig: { channelId: '123', selfMute: false, selfDeaf: true, ...joinConfig },
} as any;
}

function waitForEventLoop() {
return new Promise((res) => setImmediate(res));
}

beforeEach(() => {
const groups = DataStore.getGroups();
for (const groupKey of groups.keys()) {
Expand All @@ -21,6 +30,24 @@ const voiceConnectionDefault = createVoiceConnection({ guildId: '123', group: 'd
const voiceConnectionAbc = createVoiceConnection({ guildId: '123', group: 'abc' });

describe('DataStore', () => {
test('VoiceConnection join payload creation', () => {
const joinConfig: DataStore.JoinConfig = {
guildId: '123',
channelId: '123',
selfDeaf: true,
selfMute: false,
group: 'default',
};
expect(DataStore.createJoinVoiceChannelPayload(joinConfig)).toStrictEqual({
op: GatewayOpcodes.VoiceStateUpdate,
d: {
guild_id: joinConfig.guildId,
channel_id: joinConfig.channelId,
self_deaf: joinConfig.selfDeaf,
self_mute: joinConfig.selfMute,
},
});
});
test('VoiceConnection management respects group', () => {
DataStore.trackVoiceConnection(voiceConnectionDefault);
DataStore.trackVoiceConnection(voiceConnectionAbc);
Expand All @@ -38,4 +65,36 @@ describe('DataStore', () => {
expect(DataStore.getVoiceConnection('123')).toBeUndefined();
expect(DataStore.getVoiceConnection('123', 'abc')).toBe(voiceConnectionAbc);
});
test('Managing Audio Players', async () => {
const player = DataStore.addAudioPlayer(new AudioPlayer.AudioPlayer());
const dispatchSpy = jest.spyOn(player as any, '_stepDispatch');
const prepareSpy = jest.spyOn(player as any, '_stepPrepare');
expect(DataStore.hasAudioPlayer(player)).toBe(true);
expect(DataStore.addAudioPlayer(player)).toBe(player);
DataStore.deleteAudioPlayer(player);
expect(DataStore.deleteAudioPlayer(player)).toBe(undefined);
expect(DataStore.hasAudioPlayer(player)).toBe(false);
// Tests audio cycle with nextTime === -1
await waitForEventLoop();
expect(dispatchSpy).toHaveBeenCalledTimes(0);
expect(prepareSpy).toHaveBeenCalledTimes(0);
});
test('Preparing Audio Frames', async () => {
// Test functional player
const player2 = DataStore.addAudioPlayer(new AudioPlayer.AudioPlayer());
player2['checkPlayable'] = jest.fn(() => true);
const player3 = DataStore.addAudioPlayer(new AudioPlayer.AudioPlayer());
const dispatchSpy2 = jest.spyOn(player2 as any, '_stepDispatch');
const prepareSpy2 = jest.spyOn(player2 as any, '_stepPrepare');
const dispatchSpy3 = jest.spyOn(player3 as any, '_stepDispatch');
const prepareSpy3 = jest.spyOn(player3 as any, '_stepPrepare');
await waitForEventLoop();
DataStore.deleteAudioPlayer(player2);
await waitForEventLoop();
DataStore.deleteAudioPlayer(player3);
expect(dispatchSpy2).toHaveBeenCalledTimes(1);
expect(prepareSpy2).toHaveBeenCalledTimes(1);
expect(dispatchSpy3).toHaveBeenCalledTimes(0);
expect(prepareSpy3).toHaveBeenCalledTimes(0);
});
});

0 comments on commit dd5751b

Please sign in to comment.