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:
parent
39d2b6acfb
commit
9fb54798dd
12 changed files with 390 additions and 18 deletions
|
|
@ -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();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue