-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue-builder.test.js
More file actions
366 lines (284 loc) Β· 11.2 KB
/
Copy pathqueue-builder.test.js
File metadata and controls
366 lines (284 loc) Β· 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Tests for queue-builder handleDoubleClickPlay and player.updateTrackState
*
* Verifies the atomic play-context flow: single IPC round-trip that clears queue,
* installs tracks, triggers playback, and returns full result.
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
// Mock the queue API module
vi.mock('../js/api/queue.js', () => ({
queue: {
playContext: vi.fn(),
},
}));
import { queue as queueApi } from '../js/api/queue.js';
import { handleDoubleClickPlay } from '../js/utils/queue-builder.js';
function makeTracks(names) {
return names.map((name, i) => ({
id: i + 1,
title: name,
artist: 'Test',
album: 'Test',
duration: 180,
filepath: `/music/${name}.mp3`,
file_size: 5000000,
}));
}
function makePlayContextResult(tracks, currentIndex, durationMs = 180000) {
return {
items: tracks.map((t) => ({ track: t })),
current_index: currentIndex,
track: tracks[currentIndex],
shuffle_enabled: false,
duration_ms: durationMs,
};
}
function createMockCtx(queueOverrides = {}, playerOverrides = {}) {
return {
queue: {
items: [],
currentIndex: -1,
shuffle: false,
_updating: false,
...queueOverrides,
},
player: {
playTrack: vi.fn().mockResolvedValue(undefined),
updateTrackState: vi.fn().mockResolvedValue(undefined),
...playerOverrides,
},
};
}
describe('handleDoubleClickPlay', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('falls back to playTrack when index is out of bounds (negative)', async () => {
const tracks = makeTracks(['A', 'B', 'C']);
const ctx = createMockCtx();
await handleDoubleClickPlay(ctx, tracks[0], tracks, -1, 'test');
expect(ctx.player.playTrack).toHaveBeenCalledWith(tracks[0]);
expect(queueApi.playContext).not.toHaveBeenCalled();
});
it('falls back to playTrack when index is beyond array length', async () => {
const tracks = makeTracks(['A', 'B', 'C']);
const ctx = createMockCtx();
await handleDoubleClickPlay(ctx, tracks[0], tracks, 5, 'test');
expect(ctx.player.playTrack).toHaveBeenCalledWith(tracks[0]);
expect(queueApi.playContext).not.toHaveBeenCalled();
});
it('calls playContext with correct arguments', async () => {
const tracks = makeTracks(['A', 'B', 'C']);
const result = makePlayContextResult(tracks, 1);
queueApi.playContext.mockResolvedValue(result);
const ctx = createMockCtx({ shuffle: false });
await handleDoubleClickPlay(ctx, tracks[1], tracks, 1, 'test');
expect(queueApi.playContext).toHaveBeenCalledWith(
[1, 2, 3], // track IDs
1, // index
false, // shuffle
);
});
it('passes shuffle state from queue store', async () => {
const tracks = makeTracks(['A', 'B', 'C']);
const result = makePlayContextResult(tracks, 0);
queueApi.playContext.mockResolvedValue(result);
const ctx = createMockCtx({ shuffle: true });
await handleDoubleClickPlay(ctx, tracks[0], tracks, 0, 'test');
expect(queueApi.playContext).toHaveBeenCalledWith(
[1, 2, 3],
0,
true, // shuffle enabled
);
});
it('updates queue store items from result', async () => {
const tracks = makeTracks(['A', 'B', 'C']);
const result = makePlayContextResult(tracks, 1);
queueApi.playContext.mockResolvedValue(result);
const ctx = createMockCtx();
await handleDoubleClickPlay(ctx, tracks[1], tracks, 1, 'test');
expect(ctx.queue.items).toEqual(tracks);
expect(ctx.queue.currentIndex).toBe(1);
});
it('applies shuffle_enabled from result', async () => {
const tracks = makeTracks(['A', 'B']);
const result = makePlayContextResult(tracks, 0);
result.shuffle_enabled = true;
queueApi.playContext.mockResolvedValue(result);
const ctx = createMockCtx({ shuffle: false });
await handleDoubleClickPlay(ctx, tracks[0], tracks, 0, 'test');
expect(ctx.queue.shuffle).toBe(true);
});
it('calls updateTrackState with track and duration_ms', async () => {
const tracks = makeTracks(['A', 'B']);
const result = makePlayContextResult(tracks, 0, 195000);
queueApi.playContext.mockResolvedValue(result);
const ctx = createMockCtx();
await handleDoubleClickPlay(ctx, tracks[0], tracks, 0, 'test');
expect(ctx.player.updateTrackState).toHaveBeenCalledWith(tracks[0], 195000);
expect(ctx.player.playTrack).not.toHaveBeenCalled();
});
it('calls beforePlay hook before making IPC call', async () => {
const tracks = makeTracks(['A', 'B']);
const callOrder = [];
queueApi.playContext.mockImplementation(async () => {
callOrder.push('playContext');
return makePlayContextResult(tracks, 0);
});
const ctx = createMockCtx();
ctx.player.updateTrackState.mockImplementation(async () => {
callOrder.push('updateTrackState');
});
const beforePlay = vi.fn(() => {
callOrder.push('beforePlay');
});
await handleDoubleClickPlay(ctx, tracks[0], tracks, 0, 'test', { beforePlay });
expect(beforePlay).toHaveBeenCalled();
expect(callOrder).toEqual(['beforePlay', 'playContext', 'updateTrackState']);
});
it('sets _updating flag during operation', async () => {
const tracks = makeTracks(['A', 'B']);
let updatingDuringCall = false;
queueApi.playContext.mockImplementation(async (ids, idx, shuffle) => {
updatingDuringCall = true; // can't check ctx._updating from here, but we know the flag was set before try block
return makePlayContextResult(tracks, 0);
});
const ctx = createMockCtx();
await handleDoubleClickPlay(ctx, tracks[0], tracks, 0, 'test');
// _updating was set to true before the try block; after the finally,
// a setTimeout schedules setting it to false. Since we're in a sync
// context here, the setTimeout hasn't fired yet.
// The important thing is it was set to true during the call.
expect(updatingDuringCall).toBe(true);
});
it('handles playContext errors gracefully', async () => {
const tracks = makeTracks(['A', 'B']);
queueApi.playContext.mockRejectedValue(new Error('IPC failed'));
const ctx = createMockCtx();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
await handleDoubleClickPlay(ctx, tracks[0], tracks, 0, 'test-prefix');
expect(consoleSpy).toHaveBeenCalledWith(
'[test-prefix] Failed to play context:',
expect.any(Error),
);
// Player should not have been called
expect(ctx.player.updateTrackState).not.toHaveBeenCalled();
consoleSpy.mockRestore();
});
it('extracts tracks from items with track property', async () => {
const tracks = makeTracks(['A', 'B', 'C']);
const result = {
items: tracks.map((t) => ({ track: t, some_other_field: 'ignored' })),
current_index: 0,
track: tracks[0],
shuffle_enabled: false,
duration_ms: 180000,
};
queueApi.playContext.mockResolvedValue(result);
const ctx = createMockCtx();
await handleDoubleClickPlay(ctx, tracks[0], tracks, 0, 'test');
// items should be unwrapped from { track: ... } wrapper
expect(ctx.queue.items).toEqual(tracks);
});
it('handles items without track wrapper (passthrough)', async () => {
const tracks = makeTracks(['A', 'B']);
const result = {
items: tracks, // no { track: ... } wrapper
current_index: 0,
track: tracks[0],
shuffle_enabled: false,
duration_ms: 180000,
};
queueApi.playContext.mockResolvedValue(result);
const ctx = createMockCtx();
await handleDoubleClickPlay(ctx, tracks[0], tracks, 0, 'test');
// Without .track property, the item itself is used
expect(ctx.queue.items).toEqual(tracks);
});
});
describe('player.updateTrackState', () => {
// Test the updateTrackState method logic in isolation
// (simulating what the real player store does)
function createPlayerState() {
let _playRequestId = 0;
return {
currentTrack: null,
duration: 0,
currentTime: 100,
progress: 50,
isPlaying: false,
_playRequestId,
checkFavoriteStatus: vi.fn().mockResolvedValue(undefined),
loadArtwork: vi.fn().mockResolvedValue(undefined),
_updateNowPlayingMetadata: vi.fn().mockResolvedValue(undefined),
_updateNowPlayingState: vi.fn().mockResolvedValue(undefined),
async updateTrackState(track, durationMs) {
++this._playRequestId;
const trackDurationMs = track.duration ? Math.round(track.duration * 1000) : 0;
const finalDuration = (durationMs > 0 ? durationMs : trackDurationMs) || 0;
this.currentTrack = { ...track, duration: finalDuration };
this.duration = finalDuration;
this.currentTime = 0;
this.progress = 0;
this.isPlaying = true;
await this.checkFavoriteStatus();
await this.loadArtwork();
await this._updateNowPlayingMetadata();
await this._updateNowPlayingState();
},
};
}
it('sets currentTrack with engine duration when available', async () => {
const player = createPlayerState();
const track = { id: 1, title: 'Song', duration: 180 };
await player.updateTrackState(track, 195000);
expect(player.currentTrack.duration).toBe(195000);
expect(player.duration).toBe(195000);
});
it('falls back to track.duration (converted to ms) when engine returns 0', async () => {
const player = createPlayerState();
const track = { id: 1, title: 'Song', duration: 180 }; // 180 seconds
await player.updateTrackState(track, 0);
expect(player.currentTrack.duration).toBe(180000); // 180 * 1000
expect(player.duration).toBe(180000);
});
it('uses 0 when both sources are missing', async () => {
const player = createPlayerState();
const track = { id: 1, title: 'Song' }; // no duration
await player.updateTrackState(track, 0);
expect(player.currentTrack.duration).toBe(0);
expect(player.duration).toBe(0);
});
it('resets playback position', async () => {
const player = createPlayerState();
player.currentTime = 50000;
player.progress = 75;
const track = { id: 1, title: 'Song', duration: 180 };
await player.updateTrackState(track, 180000);
expect(player.currentTime).toBe(0);
expect(player.progress).toBe(0);
});
it('sets isPlaying to true', async () => {
const player = createPlayerState();
expect(player.isPlaying).toBe(false);
const track = { id: 1, title: 'Song', duration: 180 };
await player.updateTrackState(track, 180000);
expect(player.isPlaying).toBe(true);
});
it('increments _playRequestId to invalidate pending playTrack calls', async () => {
const player = createPlayerState();
const initialId = player._playRequestId;
const track = { id: 1, title: 'Song', duration: 180 };
await player.updateTrackState(track, 180000);
expect(player._playRequestId).toBe(initialId + 1);
});
it('calls all UI update methods', async () => {
const player = createPlayerState();
const track = { id: 1, title: 'Song', duration: 180 };
await player.updateTrackState(track, 180000);
expect(player.checkFavoriteStatus).toHaveBeenCalled();
expect(player.loadArtwork).toHaveBeenCalled();
expect(player._updateNowPlayingMetadata).toHaveBeenCalled();
expect(player._updateNowPlayingState).toHaveBeenCalled();
});
});