Fixes:
- Update dependencies within their ranges: npm audit reported 8
vulnerabilities (proxy-addr, multer, body-parser, undici...), now 0.
- Close open connections on shutdown: an open jukebox page kept its
/api/events stream alive, so every restart waited for the 10 s
forced exit and ended with code 1.
- Restore the ANSI escape characters of the game servers message, lost
in a269824 (the status showed "[2;32mOnline[0m" as plain text). They
are now written as \u001b so they cannot silently disappear again.
- Rename MusicQueue.listeners(), which shadowed EventEmitter's own
listeners(eventName), to humanListeners().
- Deploy: check the VM's SSH host key (new DEPLOY_KNOWN_HOSTS secret)
instead of StrictHostKeyChecking=no; run checks on push only, since
PR branches are already checked by their push.
Commands:
- /listcommand is built from the loaded commands and their descriptions.
- /deletemsgs reports messages older than 14 days that it cannot delete.
- Vocal commands act on the caller's voice channel, falling back to
VOICE_CHANNEL_ID.
- /maketeam accepts comma-separated names, so names can contain spaces.
- The game servers message is edited whenever its content changes, and
a failed first update is retried instead of stopping the refresh.
Cleanup:
- Shared command loader (src/loadCommands.js); drop the unused
"[disabled]" folders mechanism and the NODE_ENV requirement.
- SHINUWA_USER_ID becomes OWNER_USER_ID (the old name still works).
- One shuffle helper instead of three copies; remove dead code
(validateAudioFile, PORT fallback, needless async).
- README: prerequisites first, consistent Node version, deploy secrets.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
249 lines
7.7 KiB
JavaScript
249 lines
7.7 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";
|
|
queue.loop = false;
|
|
|
|
assert.deepEqual(queue.snapshot(), {
|
|
current: { id: "current" },
|
|
upcoming: [{ id: "next" }],
|
|
status: "playing",
|
|
loop: false,
|
|
position: 0,
|
|
});
|
|
});
|
|
|
|
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"));
|
|
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((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();
|
|
});
|
|
|
|
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();
|
|
});
|