Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stream lookup with 'undefined' stream ID #1552

Merged
53 changes: 53 additions & 0 deletions packages/app/app/actions/queue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { QueueItem } from '../reducers/queue';
import { Queue } from './actionTypes';
import * as QueueOperations from './queue';

describe('Queue actions tests', () => {

describe('finds streams for track', () => {
const getSelectedStreamProvider = jest.spyOn(QueueOperations, 'getSelectedStreamProvider');
const getTrackStreams = jest.spyOn(QueueOperations, 'getTrackStreams');

afterEach(() => {
getSelectedStreamProvider.mockReset();
getTrackStreams.mockReset();
});

test('remote track is removed from the queue when no streams are available', () => {
// Mock an empty search result for streams.
getTrackStreams.mockResolvedValueOnce([]);

// Configure a dummy stream provider. It is not actually used in this execution path.
getSelectedStreamProvider.mockReturnValueOnce({});

// Set up the queue with an arbitrary track, which doesn't have any stream.
const trackIndex = 123;
const queueItems: QueueItem[] = [];
queueItems[trackIndex] = {
artist: 'Artist Name',
name: 'Track Name',
local: false,
streams: null
};
const stateResolver = () => ({
queue: {
queueItems
},
settings: {
useStreamVerification: false
}
});

const dispatchOperation = jest.fn();
const findStreamsForTrackOperation = QueueOperations.findStreamsForTrack(trackIndex);
findStreamsForTrackOperation(dispatchOperation, stateResolver)
.then(() => {
// The track without streams should have been removed from the queue.
expect(dispatchOperation).toHaveBeenCalledWith({
type: Queue.REMOVE_QUEUE_ITEM,
payload: { index: trackIndex }
});
});
});
});
});
5 changes: 3 additions & 2 deletions packages/app/app/actions/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export const toQueueItem = (track: Track): QueueItem => ({
streams: track.streams ?? []
});

const getSelectedStreamProvider = (getState) => {
// Exported to facilitate testing.
export const getSelectedStreamProvider = (getState) => {
const {
plugin: {
plugins: { streamProviders },
Expand Down Expand Up @@ -201,7 +202,7 @@ export const findStreamsForTrack = (index: number) => async (dispatch, getState)
}
}

if (streamData === undefined) {
if (streamData?.length === 0) {
dispatch(removeFromQueue(index));
} else {
streamData = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,36 @@ describe('PlayerBar container', () => {
expect(state.queue.currentSong).toBe(0);
});

it('should remove the track when no streams are available for the track', async () => {
const { component, store } = mountComponent({
queue: {
currentSong: 0,
queueItems: [
{
uuid: 'uuid1',
artist: 'test artist name',
name: 'track without streams'
}
]
},
plugin: {
plugins: {
streamProviders: [
{
sourceName: 'Mocked Stream Provider',
search: jest.fn().mockResolvedValueOnce([])
}
]
},
selected: {
streamProviders: 'Mocked Stream Provider'
}
}
});
const state = store.getState();
waitFor(() => expect(state.queue.queueItems.length).toBe(0));
});

const mountComponent = (initialStore?: AnyProps) => {
const store = configureMockStore({
...buildStoreState()
Expand Down
Loading