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

@ -127,3 +127,66 @@ test("/readyz reports whether the bot is connected to Discord", async () => {
await new Promise((resolve) => server.close(resolve));
}
});
test("the events stream sends the queue state, then every update", async () => {
const { EventEmitter } = await import("node:events");
const queue = new EventEmitter();
let snapshot = { current: null, upcoming: [], status: "idle", loop: false, position: 0 };
queue.snapshot = () => snapshot;
queue.setLoop = (enabled) => {
snapshot = { ...snapshot, loop: enabled };
queue.emit("queueUpdate", snapshot);
return snapshot;
};
const app = createApp({ queue, client: fakeClient() });
const server = app.listen(0, "127.0.0.1");
await new Promise((resolve) => server.once("listening", resolve));
const base = `http://127.0.0.1:${server.address().port}`;
const controller = new AbortController();
try {
const response = await fetch(`${base}/api/events`, {
headers: { authorization: "Bearer web-key" },
signal: controller.signal,
});
assert.equal(response.status, 200);
assert.match(response.headers.get("content-type"), /text\/event-stream/);
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
let received = "";
const readUntil = async (pattern) => {
while (!pattern.test(received)) {
const { value, done } = await reader.read();
if (done) break;
received += value;
}
};
await readUntil(/event: queue\ndata: .*"loop":false/);
const loopResponse = await fetch(`${base}/api/player/loop`, {
method: "POST",
headers: { authorization: "Bearer web-key", "content-type": "application/json" },
body: JSON.stringify({ enabled: true }),
});
assert.equal(loopResponse.status, 200);
await readUntil(/"loop":true/);
assert.match(received, /"loop":true/);
const invalid = await fetch(`${base}/api/player/loop`, {
method: "POST",
headers: { authorization: "Bearer web-key", "content-type": "application/json" },
body: JSON.stringify({ enabled: "yes" }),
});
assert.equal(invalid.status, 400);
} finally {
controller.abort();
server.closeAllConnections();
await new Promise((resolve) => server.close(resolve));
}
});
test("the events stream requires the jukebox key", async () => {
await withServer({}, async ({ base }) => {
assert.equal((await fetch(`${base}/api/events`)).status, 401);
});
});