- Adding a track or someone joining the voice channel now resumes a queue suspended because the bot was alone; before, only the Play button did. - Retry the voice connection with backoff instead of giving up after one failed attempt. - Keep playing when the head of the queue changes while connecting. - Requeue the interrupted track when the bot leaves an empty channel or loses its voice connection. - Removing the current track no longer answers 404; other bots no longer count as listeners. - Accept uploads whatever MIME type the browser sends (ffprobe validates the content) and clean temporary files hourly, sparing queued ones. - Web page: refresh again after an action instead of reusing a stale request, disable buttons while a request runs, show the real start time and custom titles, and support drag and drop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
196 lines
5.9 KiB
JavaScript
196 lines
5.9 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { EventEmitter } from "node:events";
|
|
|
|
Object.assign(process.env, {
|
|
NODE_ENV: "test",
|
|
DISCORD_GUILD_ID: "1",
|
|
SHINUWA_USER_ID: "1",
|
|
WEB_SERVER_PORT: "3000",
|
|
WEB_API_KEY: "test-key",
|
|
DISCORD_APPLICATION_ID: "1",
|
|
DISCORD_TOKEN: "test-token",
|
|
VOICE_CHANNEL_ID: "1",
|
|
TEXT_CHANNEL_ID: "1",
|
|
GAME_SERVERS_MESSAGE_ID: "1",
|
|
});
|
|
|
|
const { MusicQueue } = await import("../src/music/queue.js");
|
|
|
|
test("playNext shares one in-flight operation", async () => {
|
|
const queue = Object.create(MusicQueue.prototype);
|
|
queue.playNextPromise = null;
|
|
let calls = 0;
|
|
let resolveOperation;
|
|
queue.runPlayNext = () => {
|
|
calls += 1;
|
|
return new Promise((resolve) => {
|
|
resolveOperation = resolve;
|
|
});
|
|
};
|
|
|
|
const first = queue.playNext();
|
|
const second = queue.playNext();
|
|
assert.equal(first, second);
|
|
assert.equal(calls, 1);
|
|
|
|
resolveOperation();
|
|
await first;
|
|
assert.equal(queue.playNextPromise, null);
|
|
});
|
|
|
|
test("queue snapshots do not expose filesystem paths", () => {
|
|
const queue = Object.create(MusicQueue.prototype);
|
|
queue.currentTrack = { id: "current", path: "/private/current.mp3" };
|
|
queue.queue = [{ id: "next", path: "/private/next.mp3" }];
|
|
queue.playbackStatus = "playing";
|
|
|
|
assert.deepEqual(queue.snapshot(), {
|
|
current: { id: "current" },
|
|
upcoming: [{ id: "next" }],
|
|
status: "playing",
|
|
});
|
|
});
|
|
|
|
const BOT_ID = "bot";
|
|
|
|
// Faux client Discord : un salon vocal (identifiant « 1 », comme VOICE_CHANNEL_ID) dont on
|
|
// choisit les membres.
|
|
function fakeClient(memberIds = []) {
|
|
const client = new EventEmitter();
|
|
const members = new Map(
|
|
memberIds.map((id) => [id, { id, user: { bot: id === BOT_ID || id.startsWith("otherbot") } }])
|
|
);
|
|
client.user = { id: BOT_ID };
|
|
client.channels = { cache: new Map([["1", { id: "1", members }]]) };
|
|
return { client, members };
|
|
}
|
|
|
|
// File dont la connexion vocale et la lecture sont simulées.
|
|
function fakeQueue(client, { connect = async () => {}, retryDelays } = {}) {
|
|
const queue = new MusicQueue(client, retryDelays ? { retryDelays } : undefined);
|
|
const played = [];
|
|
queue.connectAttempts = 0;
|
|
queue.ensureConnection = async () => {
|
|
if (queue.connection) return;
|
|
queue.connectAttempts += 1;
|
|
await connect();
|
|
queue.connection = { destroy() {} };
|
|
};
|
|
queue.createResource = (track) => track;
|
|
queue.audioPlayer.play = (resource) => played.push(resource.id);
|
|
return { queue, played };
|
|
}
|
|
|
|
const track = (id) => ({ id, path: `/nonexistent/${id}`, permanent: true });
|
|
const settle = () => new Promise((resolve) => setTimeout(resolve, 20));
|
|
|
|
test("adding a track resumes a queue suspended because the bot was alone", async () => {
|
|
const { client } = fakeClient();
|
|
const { queue, played } = fakeQueue(client);
|
|
queue.suspendedForAlone = true;
|
|
|
|
queue.enqueue(track("a"));
|
|
await settle();
|
|
|
|
assert.deepEqual(played, ["a"]);
|
|
assert.equal(queue.suspendedForAlone, false);
|
|
queue.shutdown();
|
|
});
|
|
|
|
test("someone joining the voice channel resumes a suspended queue", async () => {
|
|
const { client, members } = fakeClient();
|
|
const { queue, played } = fakeQueue(client);
|
|
queue.queue.push(track("a"));
|
|
queue.suspendedForAlone = true;
|
|
|
|
members.set("otherbot-1", { id: "otherbot-1", user: { bot: true } });
|
|
client.emit("voiceStateUpdate", { channelId: null }, { channelId: "1" });
|
|
await settle();
|
|
assert.deepEqual(played, [], "another bot joining is not a listener");
|
|
|
|
members.set("user-1", { id: "user-1", user: { bot: false } });
|
|
client.emit("voiceStateUpdate", { channelId: null }, { channelId: "1" });
|
|
await settle();
|
|
assert.deepEqual(played, ["a"]);
|
|
queue.shutdown();
|
|
});
|
|
|
|
test("a failed voice connection is retried", async () => {
|
|
const { client } = fakeClient();
|
|
let failures = 1;
|
|
const { queue, played } = fakeQueue(client, {
|
|
retryDelays: [10],
|
|
connect: async () => {
|
|
if (failures-- > 0) throw new Error("Ready timeout");
|
|
},
|
|
});
|
|
const originalConsoleError = console.error;
|
|
console.error = () => {};
|
|
try {
|
|
queue.enqueue(track("a"));
|
|
await settle();
|
|
await settle();
|
|
} finally {
|
|
console.error = originalConsoleError;
|
|
}
|
|
|
|
assert.equal(queue.connectAttempts, 2);
|
|
assert.deepEqual(played, ["a"]);
|
|
queue.shutdown();
|
|
});
|
|
|
|
test("removing the head of the queue while connecting plays the next track", async () => {
|
|
const { client } = fakeClient();
|
|
let release;
|
|
const { queue, played } = fakeQueue(client, {
|
|
connect: () => new Promise((resolve) => (release = resolve)),
|
|
});
|
|
|
|
queue.enqueue(track("a"));
|
|
queue.enqueue(track("b"));
|
|
await queue.remove("a");
|
|
release();
|
|
await settle();
|
|
|
|
assert.deepEqual(played, ["b"]);
|
|
queue.shutdown();
|
|
});
|
|
|
|
test("removing the current track returns it", async () => {
|
|
const { client } = fakeClient();
|
|
const { queue } = fakeQueue(client);
|
|
queue.currentTrack = track("a");
|
|
queue.audioPlayer.stop = () => {
|
|
queue.currentTrack = null;
|
|
return true;
|
|
};
|
|
|
|
assert.equal((await queue.remove("a"))?.id, "a");
|
|
queue.shutdown();
|
|
});
|
|
|
|
test("leaving an empty channel keeps the interrupted track at the head of the queue", () => {
|
|
const { client } = fakeClient([BOT_ID]);
|
|
const { queue } = fakeQueue(client);
|
|
queue.connection = { destroy() {} };
|
|
queue.currentTrack = { ...track("a"), startedAt: "2026-01-01T00:00:00.000Z" };
|
|
queue.queue.push(track("b"));
|
|
|
|
queue.destroyConnection("alone timeout", { suspend: true, requeue: true });
|
|
|
|
assert.deepEqual(queue.queue.map(({ id }) => id), ["a", "b"]);
|
|
assert.equal("startedAt" in queue.queue[0], false);
|
|
assert.equal(queue.suspendedForAlone, true);
|
|
queue.shutdown();
|
|
});
|
|
|
|
test("other bots do not count as listeners", () => {
|
|
const { client, members } = fakeClient([BOT_ID, "otherbot-1"]);
|
|
const { queue } = fakeQueue(client);
|
|
assert.equal(queue.isBotAlone(), true);
|
|
|
|
members.set("user-1", { id: "user-1", user: { bot: false } });
|
|
assert.equal(queue.isBotAlone(), false);
|
|
queue.shutdown();
|
|
});
|