From f5a27636b0013ec37d636ff3952ac4933adc0f0a Mon Sep 17 00:00:00 2001 From: Skick Date: Mon, 22 Apr 2024 17:52:45 +0700 Subject: [PATCH] test: update test cases --- src/core/DisTubeOptions.ts | 4 ++-- src/core/DisTubeStream.ts | 2 +- src/core/manager/QueueManager.ts | 3 ++- tests/core/DisTubeOptions.test.ts | 27 ++++++++++++++++++++++++- tests/core/DisTubeVoice.test.ts | 10 ++++----- tests/core/manager/QueueManager.test.ts | 10 ++++++--- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/core/DisTubeOptions.ts b/src/core/DisTubeOptions.ts index 10f8c81..35f16f1 100644 --- a/src/core/DisTubeOptions.ts +++ b/src/core/DisTubeOptions.ts @@ -23,9 +23,9 @@ export class Options { streamType: StreamType; directLink: boolean; /** @deprecated */ - ffmpegPath: null = null; + ffmpegPath: undefined = undefined; /** @deprecated */ - ffmpegDefaultArgs: null = null; + ffmpegDefaultArgs: undefined = undefined; ffmpeg: FFmpegOptions; constructor(options: DisTubeOptions) { if (typeof options !== "object" || Array.isArray(options)) { diff --git a/src/core/DisTubeStream.ts b/src/core/DisTubeStream.ts index 27ee0ba..6f306fa 100644 --- a/src/core/DisTubeStream.ts +++ b/src/core/DisTubeStream.ts @@ -18,7 +18,7 @@ export const chooseBestVideoFormat = ({ duration, formats, isLive }: Song) => .filter(f => f.hasAudio && (duration < 10 * 60 || f.hasVideo) && (!isLive || f.isHLS)) .sort((a, b) => Number(b.audioBitrate) - Number(a.audioBitrate) || Number(a.bitrate) - Number(b.bitrate))[0]; -let checked = false; +let checked = process.env.NODE_ENV === "test"; export const checkFFmpeg = (distube: DisTube) => { if (checked) return; const path = distube.options.ffmpeg.path; diff --git a/src/core/manager/QueueManager.ts b/src/core/manager/QueueManager.ts index cd20665..f32eb05 100644 --- a/src/core/manager/QueueManager.ts +++ b/src/core/manager/QueueManager.ts @@ -1,5 +1,5 @@ import { GuildIdManager } from "."; -import { DisTubeError, DisTubeStream, Events, Queue, RepeatMode, objectKeys } from "../.."; +import { DisTubeError, DisTubeStream, Events, Queue, RepeatMode, checkFFmpeg, objectKeys } from "../.."; import type { Song } from "../.."; import type { GuildTextBasedChannel, VoiceBasedChannel } from "discord.js"; @@ -30,6 +30,7 @@ export class QueueManager extends GuildIdManager { const queue = new Queue(this.distube, voice, song, textChannel); await queue._taskQueue.queuing(); try { + checkFFmpeg(this.distube); await voice.join(); this.#voiceEventHandler(queue); this.add(queue.id, queue); diff --git a/tests/core/DisTubeOptions.test.ts b/tests/core/DisTubeOptions.test.ts index 1ecff61..9ce410c 100644 --- a/tests/core/DisTubeOptions.test.ts +++ b/tests/core/DisTubeOptions.test.ts @@ -1,7 +1,32 @@ import { Options, defaultOptions } from "@"; test("Default DisTubeOptions", () => { - expect(new Options({})).toEqual(defaultOptions); + expect(new Options({})).toEqual({ + directLink: true, + emitAddListWhenCreatingQueue: true, + emitAddSongWhenCreatingQueue: true, + emitNewSongOnly: false, + emptyCooldown: 60, + ffmpeg: { + args: { + global: {}, + input: {}, + output: {}, + }, + path: "ffmpeg", + }, + joinNewVoiceChannel: true, + leaveOnEmpty: true, + leaveOnFinish: false, + leaveOnStop: true, + nsfw: false, + plugins: [], + savePreviousSongs: true, + searchCooldown: 60, + searchSongs: 0, + streamType: 0, + ytdlOptions: {}, + }); }); const typeOfOption = (option: string) => { diff --git a/tests/core/DisTubeVoice.test.ts b/tests/core/DisTubeVoice.test.ts index 0ea6044..9d9510b 100644 --- a/tests/core/DisTubeVoice.test.ts +++ b/tests/core/DisTubeVoice.test.ts @@ -374,22 +374,20 @@ describe("Methods", () => { voice.emittedError = true; DiscordVoice.createAudioResource.mockReturnValue(audioResource as any); const stream = { - stream: { - on: jest.fn(), - }, + on: jest.fn(), type: {}, }; expect(voice.play(stream as any)).toBeUndefined(); expect(voice.emittedError).toBe(false); - expect(stream.stream.on).toHaveBeenCalledWith("error", expect.any(Function)); + expect(stream.on).toHaveBeenCalledWith("error", expect.any(Function)); expect(voice.audioResource).toBe(audioResource); expect(audioPlayer.play).toHaveBeenCalledTimes(1); expect(audioPlayer.play).toHaveBeenNthCalledWith(1, audioResource); const error = {}; - stream.stream.on.mock.calls[0][1](error); + stream.on.mock.calls[0][1](error); expect(voice.emittedError).toBe(true); expect(voice.emit).toHaveBeenCalledWith("error", error); - stream.stream.on.mock.calls[0][1](error); + stream.on.mock.calls[0][1](error); expect(voice.emit).toHaveBeenCalledTimes(1); audioPlayer.state.status = DiscordVoice.AudioPlayerStatus.Paused; diff --git a/tests/core/manager/QueueManager.test.ts b/tests/core/manager/QueueManager.test.ts index 9abb186..a210311 100644 --- a/tests/core/manager/QueueManager.test.ts +++ b/tests/core/manager/QueueManager.test.ts @@ -21,7 +21,7 @@ const DisTubeVoiceManager = _DTVM as unknown as jest.Mocked; function createFakeDisTube() { return { - options: { ...defaultOptions }, + options: { ...defaultOptions, ffmpeg: { path: "ffmpeg", args: { global: {}, input: {}, output: {} } } }, voices: new DisTubeVoiceManager(this), emit: jest.fn(), emitError: jest.fn(), @@ -160,7 +160,7 @@ describe("QueueManager#createStream()", () => { expect.objectContaining({ ffmpeg: expect.objectContaining({ path: "ffmpeg", - args: { ...defaultOptions.ffmpegDefaultArgs }, + args: { global: expect.any(Object), input: expect.any(Object), output: expect.any(Object) }, }), seek: 1, }), @@ -189,7 +189,11 @@ describe("QueueManager#createStream()", () => { expect.objectContaining({ ffmpeg: expect.objectContaining({ path: "ffmpeg", - args: { ...defaultOptions.ffmpegDefaultArgs, af: `${distube.filters["3d"]},${distube.filters.bassboost}` }, + args: { + global: expect.any(Object), + input: expect.any(Object), + output: expect.objectContaining({ af: `${distube.filters["3d"]},${distube.filters.bassboost}` }), + }, }), seek: undefined, }),