mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-01 20:42:50 +08:00
feat: add configurable default music source (#126)
Make the default playback source a user-configurable setting so servers that mostly play e.g. Bilibili no longer need to type `-b` on every `!play`. Previously defaultPlatform() always picked the first enabled provider by a fixed priority order, with no way to override it. - config: add optional `defaultPlatform: GateableProvider | null`. loadConfig sanitizes it — kept only when it names a known provider that is also currently enabled, else null. defaultPlatform() returns the preference when enabled, otherwise falls back to the fixed priority order. - POST /api/bot/settings accepts `defaultPlatform` (validated against the possibly-updated enabledProviders; null/"" clears it), and reconciles a stored default that a new enabledProviders list no longer allows. GET and POST responses expose the field. - WebUI: new "默认音源" section with a source picker; saving refreshes the store's default source so it takes effect immediately without a restart. - Tests: extend config defaultPlatform priority tests and add coverage for the settings endpoint and /providers routing. - README: document `defaultPlatform` in the enabledProviders section. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
051171b019
commit
987513a5f6
7 files changed
+279
-9
No files matched your search
@@ -647,4 +647,54 @@ describe("bot router /settings jellyfin block + enabledProviders", () => {
|
||||
// No jellyfin block in the request → no reconfigure call.
|
||||
expect(configureCalls).toHaveLength(0);
|
||||
});
|
||||
|
||||
// --- #126: operator-chosen default source ---
|
||||
|
||||
it("GET /settings exposes defaultPlatform (null by default)", async () => {
|
||||
const res = await request(mountBot()).get("/api/bot/settings");
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.defaultPlatform).toBeNull();
|
||||
});
|
||||
|
||||
it("POST /settings sets an enabled defaultPlatform and persists it", async () => {
|
||||
const res = await request(mountBot()).post("/api/bot/settings").send({
|
||||
defaultPlatform: "bilibili",
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.defaultPlatform).toBe("bilibili");
|
||||
expect(config.defaultPlatform).toBe("bilibili");
|
||||
const onDisk = JSON.parse(readFileSync(configPath, "utf-8"));
|
||||
expect(onDisk.defaultPlatform).toBe("bilibili");
|
||||
});
|
||||
|
||||
it("POST /settings ignores an unknown or disabled defaultPlatform", async () => {
|
||||
const app = mountBot();
|
||||
// jellyfin is opt-in and not enabled in the default config → rejected.
|
||||
await request(app).post("/api/bot/settings").send({ defaultPlatform: "jellyfin" });
|
||||
expect(config.defaultPlatform).toBeNull();
|
||||
// Unknown value → rejected.
|
||||
await request(app).post("/api/bot/settings").send({ defaultPlatform: "bogus" });
|
||||
expect(config.defaultPlatform).toBeNull();
|
||||
});
|
||||
|
||||
it("POST /settings clears defaultPlatform with null", async () => {
|
||||
const app = mountBot();
|
||||
await request(app).post("/api/bot/settings").send({ defaultPlatform: "qq" });
|
||||
expect(config.defaultPlatform).toBe("qq");
|
||||
const res = await request(app).post("/api/bot/settings").send({ defaultPlatform: null });
|
||||
expect(res.body.defaultPlatform).toBeNull();
|
||||
expect(config.defaultPlatform).toBeNull();
|
||||
});
|
||||
|
||||
it("POST /settings drops a default whose source gets disabled in the same request", async () => {
|
||||
const app = mountBot();
|
||||
await request(app).post("/api/bot/settings").send({ defaultPlatform: "qq" });
|
||||
expect(config.defaultPlatform).toBe("qq");
|
||||
// Disabling qq via enabledProviders clears the now-invalid default.
|
||||
const res = await request(app).post("/api/bot/settings").send({
|
||||
enabledProviders: ["netease", "bilibili"],
|
||||
});
|
||||
expect(res.body.defaultPlatform).toBeNull();
|
||||
expect(config.defaultPlatform).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user