Keep the queue across restarts; add shuffle, loop, renaming and live events

- Save the queue to storage/runtime/queue.json and restore it at startup,
  the interrupted track first; playback resumes once someone is in the
  voice channel. Shutdown keeps the queued files instead of deleting them.
- Shuffle the upcoming tracks; loop mode sends finished tracks back to the
  end of the queue, except removed or failed ones.
- Rename library tracks (queued copies follow), enqueue the whole library,
  optionally shuffled.
- Stream queue and library changes to the web page with Server-Sent Events
  (GET /api/events).
- Sanitise the requester name sent when queueing a library track.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-29 12:15:25 +01:00
parent 39d2b6acfb
commit 9fb54798dd
12 changed files with 390 additions and 18 deletions

View file

@ -44,11 +44,13 @@ test("queue snapshots do not expose filesystem paths", () => {
queue.currentTrack = { id: "current", path: "/private/current.mp3" };
queue.queue = [{ id: "next", path: "/private/next.mp3" }];
queue.playbackStatus = "playing";
queue.loop = false;
assert.deepEqual(queue.snapshot(), {
current: { id: "current" },
upcoming: [{ id: "next" }],
status: "playing",
loop: false,
position: 0,
});
});
@ -195,3 +197,53 @@ test("other bots do not count as listeners", () => {
assert.equal(queue.isBotAlone(), false);
queue.shutdown();
});
test("in loop mode a finished track goes back to the end of the queue", () => {
const { client } = fakeClient();
const { queue } = fakeQueue(client);
queue.setLoop(true);
queue.currentTrack = track("a");
queue.queue.push(track("b"));
queue.playNext = () => {};
queue.audioPlayer.emit("idle");
assert.deepEqual(queue.queue.map(({ path }) => path), ["/nonexistent/b", "/nonexistent/a"]);
// Un morceau retiré ou en erreur ne revient pas.
queue.currentTrack = track("c");
queue.audioPlayer.emit("error", new Error("bad file"));
queue.audioPlayer.emit("idle");
assert.equal(queue.queue.some(({ path }) => path === "/nonexistent/c"), false);
queue.shutdown();
});
test("shuffle keeps every upcoming track", () => {
const { client } = fakeClient();
const { queue } = fakeQueue(client);
queue.queue.push(...["a", "b", "c", "d", "e"].map(track));
queue.shuffle();
assert.deepEqual(queue.queue.map(({ id }) => id).sort(), ["a", "b", "c", "d", "e"]);
queue.shutdown();
});
test("the queue survives a restart and waits for listeners", () => {
let saved = null;
const persistence = { load: () => saved, save: (state) => (saved = state) };
const { client } = fakeClient();
const first = new MusicQueue(client, { persistence });
first.queue.push(track("b"));
first.currentTrack = { ...track("a"), startedAt: "2026-01-01T00:00:00.000Z" };
first.connection = { destroy() {} };
first.setLoop(true);
first.shutdown();
assert.deepEqual(saved.queue.map(({ id }) => id), ["a", "b"]);
assert.equal("startedAt" in saved.queue[0], false);
const second = new MusicQueue(client, { persistence });
assert.deepEqual(second.queue.map(({ id }) => id), ["a", "b"]);
assert.equal(second.loop, true);
assert.equal(second.suspendedForAlone, true);
second.shutdown();
});