fix(web): preserve playback on invalid queue selections

This commit is contained in:
TIANYAO ZHANG committed 2026-11-18 18:22:50 +08:00
1 parent afb1cd7c14
commit 5c2a9468bd
4 files changed
+64 -19

No files matched your search

+40
View File
@@ -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<unknown>) => 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");
});
});
+15 -13
View File
@@ -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);