mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-01 02:12:50 +08:00
Add three default-off capabilities that stop the play queue from being lost, all gated behind admin/independent toggles so existing behavior is unchanged until an operator opts in: - Named save/load of queues (Feature 1): new saved_queues table (per-user + reserved __shared__ owner, capped at 50 queues / 1000 songs, JSON song blob that degrades to empty on corruption); /api/saved-queues router (list/save/ load/delete with ownership 404s, inert 403 when disabled); chat commands !save / !load [-a] / !queues; BotInstance.loadSavedQueue (replace/append). - Auto-restore live queue across restart (Feature 2): PlayQueue.snapshot/restore, queue_state table (one row per bot), a debounced snapshot writer driven off stateChange, and restore+resume on connect. Cancels the pending snapshot on disconnect so a stale write can't wipe the row a restart must restore. - playKeepsQueue (Feature 3): BotInstance.playSingleSong funnels chat !play and the web /play-song route through one place; when enabled a single-song play inserts-after-current and jumps instead of clearing the queue. Config gains savedQueuesEnabled + playKeepsQueue (both default false, strict- coerced on load like spotify.enabled); the settings API round-trips them. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
120 lines
5.1 KiB
TypeScript
120 lines
5.1 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import express from "express";
|
|
import request from "supertest";
|
|
import pino from "pino";
|
|
import { createDatabase, SHARED_QUEUE_OWNER, type BotDatabase } from "../../data/database.js";
|
|
import type { BotManager } from "../../bot/manager.js";
|
|
import { createSavedQueuesRouter } from "./saved-queues.js";
|
|
|
|
const song = (id: string) => ({
|
|
id,
|
|
name: id,
|
|
artist: "",
|
|
album: "",
|
|
platform: "netease" as const,
|
|
coverUrl: "",
|
|
duration: 1,
|
|
});
|
|
|
|
function mount(enabled: boolean, opts: { queue?: unknown[] } = {}) {
|
|
const db = createDatabase(":memory:");
|
|
const loads: Array<{ songs: unknown[]; mode: string; by?: string }> = [];
|
|
const bot = {
|
|
getQueueManager: () => ({ list: () => opts.queue ?? [song("a"), song("b")] }),
|
|
loadSavedQueue: async (songs: unknown[], mode: string, by?: string) => {
|
|
loads.push({ songs, mode, by });
|
|
},
|
|
};
|
|
const botManager = { getBot: (_id: string) => bot } as unknown as BotManager;
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => {
|
|
(req as unknown as { user: unknown }).user = { id: "u1", username: "alice", role: "member" };
|
|
next();
|
|
});
|
|
app.use(
|
|
"/api/saved-queues",
|
|
createSavedQueuesRouter(db, botManager, () => enabled, pino({ level: "silent" })),
|
|
);
|
|
return { app, db, loads };
|
|
}
|
|
|
|
describe("saved-queues router", () => {
|
|
it("403s every route when the feature is disabled", async () => {
|
|
const { app } = mount(false);
|
|
expect((await request(app).get("/api/saved-queues")).status).toBe(403);
|
|
expect((await request(app).post("/api/saved-queues").send({ botId: "b", name: "x" })).status).toBe(403);
|
|
expect((await request(app).post("/api/saved-queues/1/load").send({ botId: "b" })).status).toBe(403);
|
|
expect((await request(app).delete("/api/saved-queues/1")).status).toBe(403);
|
|
});
|
|
|
|
it("saves the current queue (private) and lists it back", async () => {
|
|
const { app } = mount(true);
|
|
const save = await request(app).post("/api/saved-queues").send({ botId: "b", name: "night" });
|
|
expect(save.status).toBe(200);
|
|
expect(save.body.queue.name).toBe("night");
|
|
expect(save.body.queue.songCount).toBe(2);
|
|
expect(save.body.queue.ownerId).toBe("u1");
|
|
|
|
const list = await request(app).get("/api/saved-queues");
|
|
expect(list.status).toBe(200);
|
|
expect(list.body.queues.map((q: { name: string }) => q.name)).toContain("night");
|
|
});
|
|
|
|
it("saves to the shared bucket when shared:true", async () => {
|
|
const { app, db } = mount(true);
|
|
const save = await request(app).post("/api/saved-queues").send({ botId: "b", name: "party", shared: true });
|
|
expect(save.status).toBe(200);
|
|
expect(save.body.queue.ownerId).toBe(SHARED_QUEUE_OWNER);
|
|
expect(db.listSavedQueues(SHARED_QUEUE_OWNER, false).map((q) => q.name)).toEqual(["party"]);
|
|
});
|
|
|
|
it("rejects saving an empty queue", async () => {
|
|
const { app } = mount(true, { queue: [] });
|
|
const save = await request(app).post("/api/saved-queues").send({ botId: "b", name: "empty" });
|
|
expect(save.status).toBe(400);
|
|
});
|
|
|
|
it("requires botId and name", async () => {
|
|
const { app } = mount(true);
|
|
expect((await request(app).post("/api/saved-queues").send({ name: "x" })).status).toBe(400);
|
|
expect((await request(app).post("/api/saved-queues").send({ botId: "b" })).status).toBe(400);
|
|
});
|
|
|
|
it("loads a shared queue (replace by default) into the bot", async () => {
|
|
const { app, db, loads } = mount(true);
|
|
const saved = db.saveQueue(SHARED_QUEUE_OWNER, "party", [song("a"), song("b")]);
|
|
const load = await request(app).post(`/api/saved-queues/${saved.id}/load`).send({ botId: "b" });
|
|
expect(load.status).toBe(200);
|
|
expect(load.body).toMatchObject({ ok: true, loaded: 2, mode: "replace" });
|
|
expect(loads).toHaveLength(1);
|
|
expect(loads[0].mode).toBe("replace");
|
|
expect(loads[0].by).toBe("alice");
|
|
});
|
|
|
|
it("loads in append mode when requested", async () => {
|
|
const { app, db, loads } = mount(true);
|
|
const saved = db.saveQueue("u1", "mine", [song("a")]);
|
|
const load = await request(app).post(`/api/saved-queues/${saved.id}/load`).send({ botId: "b", mode: "append" });
|
|
expect(load.status).toBe(200);
|
|
expect(loads[0].mode).toBe("append");
|
|
});
|
|
|
|
it("404s loading another user's private queue (no existence leak)", async () => {
|
|
const { app, db } = mount(true);
|
|
db.saveQueue("someoneElse", "private", [song("z")]);
|
|
const other = db.listSavedQueues("someoneElse", false)[0];
|
|
const load = await request(app).post(`/api/saved-queues/${other.id}/load`).send({ botId: "b", mode: "replace" });
|
|
expect(load.status).toBe(404);
|
|
});
|
|
|
|
it("deletes an own queue but 404s another user's private one", async () => {
|
|
const { app, db } = mount(true);
|
|
const mine = db.saveQueue("u1", "mine", [song("a")]);
|
|
const theirs = db.saveQueue("someoneElse", "private", [song("z")]);
|
|
expect((await request(app).delete(`/api/saved-queues/${theirs.id}`)).status).toBe(404);
|
|
expect((await request(app).delete(`/api/saved-queues/${mine.id}`)).status).toBe(200);
|
|
expect(db.getSavedQueue(mine.id)).toBeNull();
|
|
});
|
|
});
|