Files
minstrel/web/src/lib/player/mediaSession.svelte.test.ts
T
bvandeusenandClaude Opus 5.5 ca9917d8cd
release / govulncheck (push) Successful in 23s
release / web (push) Successful in 1m22s
release / go (push) Successful in 1m41s
release / integration (push) Successful in 4m49s
release / android (push) Successful in 5m32s
release / Build signed APK (releases and dev) (push) Successful in 5m39s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 28s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(web): clear kit 3's alias and Vite's config-loader deprecations (#5021)
CI run 8789 warned on both:

- kit 3 deprecates `config.alias`. The $test-utils alias becomes a
  `#test-utils/*` subpath import, matching #lib, and its 43 import sites
  move with it.
- Vite's coming native config loader needs the extension on
  vitest.config.ts's import of vite.config.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 11:03:38 -04:00

129 lines
4.0 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { flushSync } from 'svelte';
import type { TrackRef } from '#lib/api/types.js';
import { makeTrack } from '#test-utils/fixtures/track.js';
function track(id: string): TrackRef {
return makeTrack({
id,
title: `T ${id}`,
stream_url: `/api/tracks/${id}/stream`
});
}
type Handler = (details: MediaSessionActionDetails) => void;
// Spy MediaSession replaces navigator.mediaSession for the duration of each test.
function makeSpyMediaSession() {
const handlers: Record<string, Handler> = {};
const spy = {
metadata: null as MediaMetadata | null,
playbackState: 'none' as MediaSessionPlaybackState,
setActionHandler: vi.fn((action: string, h: Handler | null) => {
if (h) handlers[action] = h;
else delete handlers[action];
}),
setPositionState: vi.fn(),
handlers
};
return spy;
}
describe('useMediaSession', () => {
let spy: ReturnType<typeof makeSpyMediaSession>;
let originalMS: unknown;
beforeEach(async () => {
spy = makeSpyMediaSession();
originalMS = (navigator as unknown as { mediaSession?: unknown }).mediaSession;
Object.defineProperty(navigator, 'mediaSession', {
configurable: true,
value: spy
});
// Also stub MediaMetadata so we can construct it in jsdom.
(globalThis as unknown as { MediaMetadata: typeof Object }).MediaMetadata =
class { constructor(public init: unknown) {} } as unknown as typeof Object;
const store = await import('./store.svelte');
store.playQueue([]);
});
afterEach(() => {
if (originalMS === undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (navigator as any).mediaSession;
} else {
Object.defineProperty(navigator, 'mediaSession', {
configurable: true,
value: originalMS
});
}
vi.restoreAllMocks();
});
test('sets metadata when current track changes; clears when queue empties', async () => {
const store = await import('./store.svelte');
const { useMediaSession } = await import('./mediaSession.svelte');
const cleanup = $effect.root(() => useMediaSession());
store.playQueue([track('1')]);
flushSync();
expect(spy.metadata).not.toBeNull();
store.playQueue([]);
flushSync();
expect(spy.metadata).toBeNull();
cleanup();
});
test('registers play/pause/previoustrack/nexttrack/seekto handlers', async () => {
const { useMediaSession } = await import('./mediaSession.svelte');
const cleanup = $effect.root(() => useMediaSession());
flushSync();
expect(spy.handlers['play']).toBeTypeOf('function');
expect(spy.handlers['pause']).toBeTypeOf('function');
expect(spy.handlers['previoustrack']).toBeTypeOf('function');
expect(spy.handlers['nexttrack']).toBeTypeOf('function');
expect(spy.handlers['seekto']).toBeTypeOf('function');
cleanup();
});
test('playbackState mirrors player.isPlaying', async () => {
const store = await import('./store.svelte');
const { useMediaSession } = await import('./mediaSession.svelte');
const cleanup = $effect.root(() => useMediaSession());
store.playQueue([track('1')]);
store.reportStateFromAudio('playing');
flushSync();
expect(spy.playbackState).toBe('playing');
store.reportStateFromAudio('paused');
flushSync();
expect(spy.playbackState).toBe('paused');
cleanup();
});
test('setPositionState called when duration > 0', async () => {
const store = await import('./store.svelte');
const { useMediaSession } = await import('./mediaSession.svelte');
const cleanup = $effect.root(() => useMediaSession());
store.playQueue([track('1')]);
store.reportDuration(240);
store.reportTimeUpdate(30);
flushSync();
expect(spy.setPositionState).toHaveBeenCalled();
const call = spy.setPositionState.mock.calls.at(-1)?.[0];
expect(call?.duration).toBe(240);
expect(call?.position).toBeLessThanOrEqual(240);
cleanup();
});
});