improve: tests
This commit is contained in:
+141
-13
@@ -1,5 +1,5 @@
|
||||
import { jest, describe, beforeEach, afterEach, it, expect } from '@jest/globals';
|
||||
import { sep } from 'node:path';
|
||||
import { resolve } from 'node:path';
|
||||
import { PassThrough } from 'node:stream';
|
||||
|
||||
import AudioManager from '../src/audio-manager';
|
||||
@@ -59,11 +59,14 @@ const connectionOptions: VoiceConnectionOptions = {
|
||||
adapterCreator: jest.fn() as unknown as VoiceConnectionOptions['adapterCreator'],
|
||||
};
|
||||
|
||||
const source: AudioSource = {
|
||||
const liveStreamSource: AudioSource = {
|
||||
type: 'url',
|
||||
url: 'https://example.com/audio.mp3',
|
||||
url: 'https://synradiode.stream.laut.fm/synradiode',
|
||||
};
|
||||
|
||||
const fileSourcePath = 'tests/audio.mp3';
|
||||
const resolvedFileSourcePath = resolve(process.cwd(), fileSourcePath);
|
||||
|
||||
describe('AudioManager', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
@@ -82,7 +85,7 @@ describe('AudioManager', () => {
|
||||
it('connects and starts playback with resolved URL source', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
@@ -92,23 +95,91 @@ describe('AudioManager', () => {
|
||||
expect(manager.isConnected).toBe(true);
|
||||
expect(manager.isPlaying).toBe(true);
|
||||
expect(mockConnection.subscribe).toHaveBeenCalledWith(mockAudioPlayer);
|
||||
expect(startFfmpeg).toHaveBeenCalledWith('https://example.com/audio.mp3', undefined);
|
||||
expect(startFfmpeg).toHaveBeenCalledWith('https://synradiode.stream.laut.fm/synradiode', undefined);
|
||||
expect(mockAudioPlayer.play).toHaveBeenCalledWith(mockAudioResource);
|
||||
});
|
||||
|
||||
it('resolves file sources against the current working directory', async () => {
|
||||
it('starts playback with the committed mp3 test file', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source: {
|
||||
type: 'file',
|
||||
path: 'audio/song.mp3',
|
||||
path: fileSourcePath,
|
||||
},
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
await manager.start();
|
||||
|
||||
expect(startFfmpeg).toHaveBeenCalledWith(expect.stringContaining(`audio${sep}song.mp3`), undefined);
|
||||
expect(startFfmpeg).toHaveBeenCalledWith(resolvedFileSourcePath, undefined);
|
||||
expect(mockAudioPlayer.play).toHaveBeenCalledWith(mockAudioResource);
|
||||
});
|
||||
|
||||
it('accepts the live stream URL as a valid source', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
await manager.connect();
|
||||
await manager.play();
|
||||
|
||||
expect(startFfmpeg).toHaveBeenCalledWith('https://synradiode.stream.laut.fm/synradiode', undefined);
|
||||
});
|
||||
|
||||
it('plays a source passed directly to play()', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
await manager.connect();
|
||||
await manager.play({
|
||||
type: 'file',
|
||||
path: fileSourcePath,
|
||||
});
|
||||
|
||||
expect(startFfmpeg).toHaveBeenCalledWith(resolvedFileSourcePath, undefined);
|
||||
expect(manager.state).toBe('playing');
|
||||
});
|
||||
|
||||
it('replaces active playback when a new source is played', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
await manager.start();
|
||||
await manager.play({
|
||||
type: 'file',
|
||||
path: fileSourcePath,
|
||||
});
|
||||
|
||||
expect(mockFfmpegHandle.stop).toHaveBeenCalledTimes(1);
|
||||
expect(mockAudioResource.playStream.destroy).toHaveBeenCalledTimes(1);
|
||||
expect(startFfmpeg).toHaveBeenLastCalledWith(resolvedFileSourcePath, undefined);
|
||||
});
|
||||
|
||||
it('requires an active connection before play()', async () => {
|
||||
const manager = new AudioManager({
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
await expect(manager.play()).rejects.toThrow(AudioManagerStateError);
|
||||
});
|
||||
|
||||
it('requires a source before play()', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
await manager.connect();
|
||||
|
||||
await expect(manager.play()).rejects.toThrow(AudioManagerConfigError);
|
||||
});
|
||||
|
||||
it('rejects invalid URL sources', async () => {
|
||||
@@ -129,7 +200,7 @@ describe('AudioManager', () => {
|
||||
it('applies initial volume only when volume support is enabled', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
volume: {
|
||||
enabled: true,
|
||||
@@ -148,10 +219,37 @@ describe('AudioManager', () => {
|
||||
expect(() => manager.setVolume(50)).toThrow(AudioManagerStateError);
|
||||
});
|
||||
|
||||
it('rejects invalid volume percentages', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
volume: {
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
await manager.start();
|
||||
|
||||
expect(() => manager.setVolume(-1)).toThrow(AudioManagerConfigError);
|
||||
expect(() => manager.setVolume(101)).toThrow(AudioManagerConfigError);
|
||||
});
|
||||
|
||||
it('rejects volume changes before any resource exists', () => {
|
||||
const manager = new AudioManager({
|
||||
renewIntervalMs: false,
|
||||
volume: {
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
|
||||
expect(() => manager.setVolume(50)).toThrow(AudioManagerStateError);
|
||||
});
|
||||
|
||||
it('pauses and resumes only from valid playback states', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
@@ -169,7 +267,7 @@ describe('AudioManager', () => {
|
||||
it('stops playback and voice resources idempotently', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
@@ -183,12 +281,29 @@ describe('AudioManager', () => {
|
||||
expect(manager.state).toBe('stopped');
|
||||
});
|
||||
|
||||
it('restarts playback when the renewal timer fires', async () => {
|
||||
jest.useFakeTimers();
|
||||
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: 10_000,
|
||||
});
|
||||
const startSpy = jest.spyOn(manager, 'start').mockResolvedValue(undefined);
|
||||
|
||||
await manager.connect();
|
||||
jest.advanceTimersByTime(10_000);
|
||||
await Promise.resolve();
|
||||
|
||||
expect(startSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('clears existing renewal timer before reconnecting', async () => {
|
||||
jest.useFakeTimers();
|
||||
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: 10_000,
|
||||
});
|
||||
|
||||
@@ -200,10 +315,23 @@ describe('AudioManager', () => {
|
||||
expect(jest.getTimerCount()).toBe(0);
|
||||
});
|
||||
|
||||
it('blocks source and connection changes after disposal', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
manager.dispose();
|
||||
|
||||
expect(() => manager.setSource(liveStreamSource)).toThrow(AudioManagerStateError);
|
||||
expect(() => manager.setConnection(connectionOptions)).toThrow(AudioManagerStateError);
|
||||
});
|
||||
|
||||
it('prevents use after disposal', async () => {
|
||||
const manager = new AudioManager({
|
||||
connection: connectionOptions,
|
||||
source,
|
||||
source: liveStreamSource,
|
||||
renewIntervalMs: false,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user