Files
jellytau/src/lib/services/playerEvents.test.ts
T
dtourolle ad48d89dfe chore(format): run prettier over src/ and scripts/
Formatting was configured but never enforced: `bun run format:check`
reported 199 unformatted files and ran in no workflow and in no git hook,
so .prettierrc (printWidth 100, trailing commas) described an intention
rather than the tree.

This is the one-time sweep that makes the check gateable. Whitespace and
token-reflow only -- no behavioural change: `bun run check` reports 0
errors and all 1053 frontend tests pass before and after.

Kept out of every other commit on purpose. A 199-file diff mixed with
real changes is unreviewable, and the next commit turns format:check
into a hard CI gate so this cannot silently accumulate again.
2026-08-21 17:41:44 +02:00

109 lines
2.8 KiB
TypeScript

/**
* Player Events Service tests
*
* TRACES: UR-005, UR-019, UR-023, UR-026 | DR-001, DR-028, DR-047
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import { isPlayerEventsInitialized, cleanupPlayerEvents } from "./playerEvents";
// Mock Tauri
vi.mock("@tauri-apps/api/event", () => ({
listen: vi.fn(async (event, handler) => {
return () => {}; // Return unlisten function
}),
}));
vi.mock("@tauri-apps/api/core", () => ({
invoke: vi.fn(),
}));
// Mock stores
vi.mock("$lib/stores/player", () => ({
player: {
updatePosition: vi.fn(),
setPlaying: vi.fn(),
setPaused: vi.fn(),
setLoading: vi.fn(),
setIdle: vi.fn(),
setError: vi.fn(),
setVolume: vi.fn(),
setMuted: vi.fn(),
},
playbackPosition: { subscribe: vi.fn() },
}));
vi.mock("$lib/stores/queue", () => ({
queue: { subscribe: vi.fn() },
currentQueueItem: { subscribe: vi.fn() },
}));
vi.mock("$lib/stores/playbackMode", () => ({
playbackMode: { setMode: vi.fn(), initializeSessionMonitoring: vi.fn() },
}));
vi.mock("$lib/stores/sleepTimer", () => ({
sleepTimer: { set: vi.fn() },
}));
vi.mock("$lib/stores/nextEpisode", () => ({
nextEpisode: {
showPopup: vi.fn(),
updateCountdown: vi.fn(),
},
}));
vi.mock("$lib/services/preload", () => ({
preloadUpcomingTracks: vi.fn(),
}));
describe("Player Events Service", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("should initialize player event listener", async () => {
const { initPlayerEvents } = await import("./playerEvents");
await initPlayerEvents();
expect(isPlayerEventsInitialized()).toBe(true);
});
it("should prevent duplicate initialization", async () => {
const { initPlayerEvents } = await import("./playerEvents");
await initPlayerEvents();
const consoleSpy = vi.spyOn(console, "warn");
await initPlayerEvents();
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("already initialized"));
});
it("should cleanup event listeners", async () => {
const { initPlayerEvents } = await import("./playerEvents");
await initPlayerEvents();
expect(isPlayerEventsInitialized()).toBe(true);
cleanupPlayerEvents();
expect(isPlayerEventsInitialized()).toBe(false);
});
it("should handle player event initialization errors", async () => {
const { listen } = await import("@tauri-apps/api/event");
(listen as any).mockRejectedValueOnce(new Error("Event setup failed"));
const { initPlayerEvents } = await import("./playerEvents");
const consoleSpy = vi.spyOn(console, "error");
await initPlayerEvents();
// console.error is called with: ("Failed to initialize player events:", Error)
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining("Failed to initialize player events"),
expect.any(Error),
);
});
});