diff --git a/src/web/api/play-artist.test.ts b/src/web/api/play-artist.test.ts index 0940a1f..ba334c2 100644 --- a/src/web/api/play-artist.test.ts +++ b/src/web/api/play-artist.test.ts @@ -174,3 +174,43 @@ describe("collection preflight", () => { expect(queue.current()?.id).toBe("playing"); }); }); + +describe("play a queue position", () => { + function fixture() { + const queue = new PlayQueue(); + queue.add(song("playing")); + queue.add(song("unavailable")); + queue.play(); + const player = { stop: vi.fn(), resetFailures: vi.fn(), getPlaybackSessionId: () => 1 }; + const bot = { + getQueueManager: () => queue, + getPlayer: () => player, + runExclusive: (work: () => Promise) => work(), + resolveAndPlay: vi.fn(async (_song: Song, onFailure?: (failure: { message: string }) => void) => { + onFailure?.({ message: "Source returned no stream" }); + return false; + }), + emit: vi.fn(), + }; + const app = express(); + app.use(express.json()); + app.use((req, _res, next) => { (req as any).user = { role: "admin" }; next(); }); + app.use("/api/player", createPlayerRouter({ getBot: () => bot } as any, { error: vi.fn() } as any)); + return { app, bot, player, queue }; + } + + it("rejects fractional positions without stopping the playing track", async () => { + const { app, player, queue } = fixture(); + expect((await request(app).post("/api/player/b/play-at").send({ index: 0.5 })).status).toBe(400); + expect(player.stop).not.toHaveBeenCalled(); + expect(queue.current()?.id).toBe("playing"); + }); + + it("returns the URL failure and publishes the selected stopped track", async () => { + const { app, bot, queue } = fixture(); + const response = await request(app).post("/api/player/b/play-at").send({ index: 1 }); + expect(response.body).toEqual({ ok: false, message: expect.stringContaining("Source returned no stream") }); + expect(bot.emit).toHaveBeenCalledWith("stateChange"); + expect(queue.current()?.id).toBe("unavailable"); + }); +}); diff --git a/src/web/api/player.ts b/src/web/api/player.ts index 1a62478..3399db8 100644 --- a/src/web/api/player.ts +++ b/src/web/api/player.ts @@ -128,7 +128,7 @@ export function createPlayerRouter( let failure: PlayFailure | undefined; const capture = (reason: PlayFailure) => { failure = reason; }; let started = first ? await bot.resolveAndPlay(first, capture) : false; - if (first && !started && !blocksProviderForAdvance(failure?.code) && queue.current() === first) { + if (first && !started && failure && !blocksProviderForAdvance(failure.code) && queue.current() === first) { // Many collections contain several consecutive unavailable tracks. started = await bot.playNext(20, capture); } @@ -346,10 +346,10 @@ export function createPlayerRouter( // Jump to a specific index in the queue (without clearing it) router.post("/:botId/play-at", authorize({ capability: "player.control" }), async (req, res) => { try { - const bot = (req as any).bot; + const bot: BotInstance = (req as any).bot; const { index } = req.body; - if (typeof index !== "number" || index < 0) { - res.status(400).json({ error: "index is required" }); + if (!Number.isSafeInteger(index) || index < 0) { + res.status(400).json({ error: "index must be a non-negative integer" }); return; } // Serialize the index-validation + stop/reset/playAt/resolveAndPlay so a @@ -360,21 +360,23 @@ export function createPlayerRouter( // Validate the index BEFORE stopping current playback — otherwise an // invalid index silently kills the user's current song and leaves the // queue idle. - if (index >= queue.size()) { - return { status: 400 as const, body: { error: "Invalid queue index" } }; - } - bot.getPlayer().stop(true); - bot.getPlayer().resetFailures(); const song = queue.playAt(index); if (!song) { return { status: 400 as const, body: { error: "Invalid queue index" } }; } - const ok = await bot.resolveAndPlay(song); + const player = bot.getPlayer(); + player.stop(true); + player.resetFailures(); + const session = player.getPlaybackSessionId(); + bot.emit("stateChange"); + let reason: string | undefined; + const ok = await bot.resolveAndPlay(song, failure => { reason = failure.message; }); if (!ok) { - bot.getPlayer().stop(); - return { body: { message: `Cannot play: ${song.name}` } }; + // Abandon this transition's silence loop, leaving newer playback alone. + if (queue.current() === song && player.getPlaybackSessionId() === session) player.stop(); + return { body: { ok: false, message: whyUnplayable(song, reason) } }; } - return { body: { message: `Now playing: ${song.name} - ${song.artist}` } }; + return { body: { ok: true, message: `Now playing: ${song.name} - ${song.artist}` } }; }); if (result.status) { res.status(result.status).json(result.body); diff --git a/web/src/stores/elapsed.test.ts b/web/src/stores/elapsed.test.ts index 3110d45..fc96ffe 100644 --- a/web/src/stores/elapsed.test.ts +++ b/web/src/stores/elapsed.test.ts @@ -59,7 +59,7 @@ describe("playback request timing", () => { vi.unstubAllGlobals(); }); - it.each(["playSong", "playPlaylist", "playAlbum", "playArtist", "next"] as const)( + it.each(["playSong", "playPlaylist", "playAlbum", "playArtist", "next", "playAtIndex"] as const)( "keeps the current clock when %s fails", async (action) => { const store = usePlayerStore(); @@ -70,6 +70,7 @@ describe("playback request timing", () => { if (action === "playSong") await store.playSong(track); else if (action === "next") await store.next(); + else if (action === "playAtIndex") await store.playAtIndex(1); else await store[action]("collection"); expect(store.timings.a).toEqual(current); diff --git a/web/src/stores/player.ts b/web/src/stores/player.ts index 7968e31..7cbaf0d 100644 --- a/web/src/stores/player.ts +++ b/web/src/stores/player.ts @@ -402,7 +402,8 @@ export const usePlayerStore = defineStore('player', { }, /** Poll the same bot for authoritative elapsed time. */ - async syncElapsed(botId = this.activeBotId) { + async syncElapsed(botId?: string) { + botId ??= this.activeBotId ?? undefined; if (!botId || !this.bots.some(bot => bot.id === botId && bot.playing && !bot.paused)) return; const timing = this.timings[botId]; try { @@ -449,10 +450,11 @@ export const usePlayerStore = defineStore('player', { }, async playAtIndex(index: number) { - if (!this.activeBotId) return; - await axios.post(`/api/player/${this.activeBotId}/play-at`, { index }); - this._setTiming(this.activeBotId, { serverElapsed: 0 }); - this._syncAfterAction(); + const botId = this.activeBotId; + if (!botId) return; + const res = await axios.post(`/api/player/${botId}/play-at`, { index }); + if (res.data?.ok === false && res.data?.message) this.notify(res.data.message, 'error'); + if (res.data?.ok === true) void this.syncElapsed(botId); }, async play(query: string, platform = 'netease') {