mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 04:52:50 +08:00
feat(autopause): pure occupancy-decision function
This commit is contained in:
1 parent
9f0ac74fbc
commit
484202e90d
2 files changed
+52
No files matched your search
@@ -0,0 +1,29 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { decideOccupancyAction } from "./auto-pause.js";
|
||||||
|
|
||||||
|
describe("decideOccupancyAction", () => {
|
||||||
|
it("pauses when empty while playing and enabled", () => {
|
||||||
|
expect(decideOccupancyAction("playing", false, true, 0)).toBe("pause");
|
||||||
|
});
|
||||||
|
it("does not pause when the feature is disabled", () => {
|
||||||
|
expect(decideOccupancyAction("playing", false, false, 0)).toBe("none");
|
||||||
|
});
|
||||||
|
it("does not pause when idle (nothing playing)", () => {
|
||||||
|
expect(decideOccupancyAction("idle", false, true, 0)).toBe("none");
|
||||||
|
});
|
||||||
|
it("does not pause when already paused", () => {
|
||||||
|
expect(decideOccupancyAction("paused", false, true, 0)).toBe("none");
|
||||||
|
});
|
||||||
|
it("resumes when re-populated and we auto-paused", () => {
|
||||||
|
expect(decideOccupancyAction("paused", true, true, 2)).toBe("resume");
|
||||||
|
});
|
||||||
|
it("does NOT resume a user-paused track on re-population", () => {
|
||||||
|
expect(decideOccupancyAction("paused", false, true, 2)).toBe("none");
|
||||||
|
});
|
||||||
|
it("does nothing when re-populated and already playing", () => {
|
||||||
|
expect(decideOccupancyAction("playing", false, true, 2)).toBe("none");
|
||||||
|
});
|
||||||
|
it("resume is independent of the enabled flag (we already auto-paused)", () => {
|
||||||
|
expect(decideOccupancyAction("paused", true, false, 1)).toBe("resume");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
export type PlayerStateName = "idle" | "playing" | "paused";
|
||||||
|
export type OccupancyAction = "pause" | "resume" | "none";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decide what auto-pause should do given channel occupancy.
|
||||||
|
* - empty (userCount <= 0): pause iff enabled and currently playing.
|
||||||
|
* - re-populated (userCount > 0): resume iff we previously auto-paused and are still paused.
|
||||||
|
* `autoPaused` distinguishes our auto-pause from a user pause, so user pauses are never resumed.
|
||||||
|
*/
|
||||||
|
export function decideOccupancyAction(
|
||||||
|
playerState: PlayerStateName,
|
||||||
|
autoPaused: boolean,
|
||||||
|
enabled: boolean,
|
||||||
|
userCount: number,
|
||||||
|
): OccupancyAction {
|
||||||
|
const empty = userCount <= 0;
|
||||||
|
if (empty) {
|
||||||
|
if (enabled && playerState === "playing") return "pause";
|
||||||
|
return "none";
|
||||||
|
}
|
||||||
|
if (autoPaused && playerState === "paused") return "resume";
|
||||||
|
return "none";
|
||||||
|
}
|
||||||
Reference in new issue
Block a user