@simba-dev/react-native-media-player 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -39
- package/package.json +2 -2
- package/src/components/PlayerProvider.tsx +288 -7
- package/src/hooks/SimbaPlayer.tsx +144 -0
- package/src/hooks/SimbaPlayerRoot.tsx +73 -0
- package/src/hooks/useLaunchParams.tsx +64 -0
- package/src/hooks/useOpenFromUrl.tsx +174 -0
- package/src/hooks/useOpenWithResume.tsx +236 -0
- package/src/hooks/usePlayerActivity.ts +103 -0
- package/src/hooks/useSimbaPlayerLookup.tsx +58 -0
- package/src/index.ts +26 -0
- package/src/types/__tests__/player.test.tsx +494 -0
- package/src/types/player.ts +947 -72
- package/src/types/__tests__/player.test.ts +0 -166
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
getMpvPlayerModule,
|
|
4
|
+
type LaunchParams,
|
|
5
|
+
} from '../bridge/MpvPlayerModule';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options accepted by `usePlayerActivity().openPlayer(opts)`. This is
|
|
9
|
+
* the V13 module-level signature — the consumer's V11 `openPlayer`
|
|
10
|
+
* took `{uri, title, duration, source, type, mediaType}` (the
|
|
11
|
+
* extra fields were Redux-dispatch concerns that V13 drops because
|
|
12
|
+
* there's no Redux dispatch path anymore).
|
|
13
|
+
*/
|
|
14
|
+
export interface OpenPlayerOptions {
|
|
15
|
+
/** Media URI (`file://`, `content://`, or `https://`). */
|
|
16
|
+
uri: string;
|
|
17
|
+
/** Display title for the notification / top bar. */
|
|
18
|
+
title: string;
|
|
19
|
+
/** `'video'` launches the full-screen activity; `'audio'` plays in the background. */
|
|
20
|
+
type: 'video' | 'audio';
|
|
21
|
+
/** Resume position in milliseconds. 0 (or omitted) starts from the beginning. */
|
|
22
|
+
startPositionMs?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* V13 Phase 52: thin wrapper around `bridge.openPlayer` +
|
|
27
|
+
* `bridge.getLaunchParams`. Replaces the consumer's V11
|
|
28
|
+
* `usePlaybackCommands()` hook for the 33+ call sites that
|
|
29
|
+
* previously launched `PlayerActivity` from screens like
|
|
30
|
+
* `NowPlaying`, `AllVideos`, `Bookmarks`, etc.
|
|
31
|
+
*
|
|
32
|
+
* Why a separate hook (not just `usePlayer().commands.openPlayer`):
|
|
33
|
+
* - `usePlayer()` requires a `<PlayerProvider>` ancestor for
|
|
34
|
+
* live state; the activity-launch calls happen from screen
|
|
35
|
+
* components (lists, grids) that are NOT inside the player
|
|
36
|
+
* activity yet.
|
|
37
|
+
* - Returning a smaller surface keeps the screen-side code
|
|
38
|
+
* self-documenting: "this screen launches the player" vs
|
|
39
|
+
* "this screen is inside the player".
|
|
40
|
+
* - Easier to mock in consumer-side tests (jest.mock the
|
|
41
|
+
* whole `usePlayerActivity` module).
|
|
42
|
+
*
|
|
43
|
+
* The hook is non-throwing: the bridge resolves to a no-op
|
|
44
|
+
* fallback in jest / web previews, so `openPlayer` resolves with
|
|
45
|
+
* `false` and `getLaunchParams` returns `null` — both clearly
|
|
46
|
+
* falsy, easy to assert against.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* function NowPlaying() {
|
|
51
|
+
* const { openPlayer } = usePlayerActivity();
|
|
52
|
+
* const onPressSong = (song: Song) => {
|
|
53
|
+
* openPlayer({
|
|
54
|
+
* uri: song.uri,
|
|
55
|
+
* title: song.title,
|
|
56
|
+
* type: song.hasVideo ? 'video' : 'audio',
|
|
57
|
+
* startPositionMs: song.resumePositionMs,
|
|
58
|
+
* });
|
|
59
|
+
* };
|
|
60
|
+
* // ...
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export interface UsePlayerActivityResult {
|
|
65
|
+
/**
|
|
66
|
+
* Launch the dedicated `PlayerActivity` with the given media.
|
|
67
|
+
* Resolves with `true` on a successful `startActivity` and
|
|
68
|
+
* `false` otherwise (e.g. when the activity is unavailable on
|
|
69
|
+
* the device, the URI is unplayable, or the bridge is in
|
|
70
|
+
* no-op mode).
|
|
71
|
+
*/
|
|
72
|
+
openPlayer(opts: OpenPlayerOptions): Promise<boolean>;
|
|
73
|
+
/**
|
|
74
|
+
* One-shot accessor for the launch params the most recent
|
|
75
|
+
* `openPlayer` call handed to `PlayerActivity`. Returns
|
|
76
|
+
* `null` when called from MainActivity or after the first
|
|
77
|
+
* read has already consumed the value. The Kotlin side
|
|
78
|
+
* keeps a single-shot queue, so call this exactly once
|
|
79
|
+
* per `PlayerActivity.onCreate`.
|
|
80
|
+
*/
|
|
81
|
+
getLaunchParams(): LaunchParams | null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function usePlayerActivity(): UsePlayerActivityResult {
|
|
85
|
+
return useMemo<UsePlayerActivityResult>(
|
|
86
|
+
() => ({
|
|
87
|
+
openPlayer: (opts) => {
|
|
88
|
+
const bridge = getMpvPlayerModule();
|
|
89
|
+
return bridge.openPlayer(
|
|
90
|
+
opts.uri,
|
|
91
|
+
opts.title,
|
|
92
|
+
opts.type,
|
|
93
|
+
opts.startPositionMs ?? 0,
|
|
94
|
+
);
|
|
95
|
+
},
|
|
96
|
+
getLaunchParams: () => {
|
|
97
|
+
const bridge = getMpvPlayerModule();
|
|
98
|
+
return bridge.getLaunchParams();
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
[],
|
|
102
|
+
);
|
|
103
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {useMemo} from 'react';
|
|
2
|
+
import type {PlayerResumeLookup} from './useOpenWithResume';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* V14 Phase 61: a function the module calls to resolve a saved
|
|
6
|
+
* resume position for a media item. The consumer provides this
|
|
7
|
+
* once at the root of the app; every call site that wants
|
|
8
|
+
* resume-aware openPlayer just passes `resumeId: item.uri` to
|
|
9
|
+
* the hook's returned function.
|
|
10
|
+
*
|
|
11
|
+
* Mirrors the body of `PlayerResumeLookup.getResumePosition` —
|
|
12
|
+
* kept as a named type so `<SimbaPlayer getResumePosition={...}>`
|
|
13
|
+
* has a self-documenting prop type instead of an inline arrow.
|
|
14
|
+
*/
|
|
15
|
+
export type GetResumePosition = (itemId: string) => number | undefined;
|
|
16
|
+
|
|
17
|
+
const noopLookup: PlayerResumeLookup = {
|
|
18
|
+
getResumePosition: () => undefined,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* V14 Phase 61: factory hook for a stable `PlayerResumeLookup`.
|
|
23
|
+
*
|
|
24
|
+
* Wraps an optional `selector` function in a memoized
|
|
25
|
+
* `PlayerResumeLookup` object so the lookup reference stays
|
|
26
|
+
* stable across renders. The point of this hook is to ship the
|
|
27
|
+
* **shape** so the consumer's `lookup={}` is always type-safe
|
|
28
|
+
* + stable — the consumer never has to write `useMemo<PlayerResumeLookup>(...)`
|
|
29
|
+
* themselves.
|
|
30
|
+
*
|
|
31
|
+
* Without a selector, returns a no-op lookup (always returns
|
|
32
|
+
* `undefined`).
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* // With a selector (most common):
|
|
37
|
+
* const resumeLookup = useSimbaPlayerLookup((uri) =>
|
|
38
|
+
* store.getState().bookmarks.byFileUri[uri]?.positionMs,
|
|
39
|
+
* );
|
|
40
|
+
*
|
|
41
|
+
* return (
|
|
42
|
+
* <SimbaPlayer lookup={resumeLookup}>
|
|
43
|
+
* <AppContent />
|
|
44
|
+
* </SimbaPlayer>
|
|
45
|
+
* );
|
|
46
|
+
*
|
|
47
|
+
* // Or — the function-prop shape (recommended for new consumers):
|
|
48
|
+
* <SimbaPlayer getResumePosition={(uri) => store.getState().bookmarks.byFileUri[uri]?.positionMs} />
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function useSimbaPlayerLookup(
|
|
52
|
+
selector?: GetResumePosition,
|
|
53
|
+
): PlayerResumeLookup {
|
|
54
|
+
return useMemo<PlayerResumeLookup>(() => {
|
|
55
|
+
if (!selector) return noopLookup;
|
|
56
|
+
return {getResumePosition: selector};
|
|
57
|
+
}, [selector]);
|
|
58
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -54,9 +54,35 @@ export {
|
|
|
54
54
|
type PlayerCommands,
|
|
55
55
|
type PlayerProgress,
|
|
56
56
|
type PlayerState,
|
|
57
|
+
type PlaylistEntry,
|
|
57
58
|
type UsePlayerResult,
|
|
58
59
|
} from './types/player';
|
|
59
60
|
|
|
61
|
+
export {
|
|
62
|
+
usePlayerActivity,
|
|
63
|
+
type OpenPlayerOptions,
|
|
64
|
+
type UsePlayerActivityResult,
|
|
65
|
+
} from './hooks/usePlayerActivity';
|
|
66
|
+
|
|
67
|
+
export {
|
|
68
|
+
PlayerResumeProvider,
|
|
69
|
+
useOpenWithResume,
|
|
70
|
+
usePlayItem,
|
|
71
|
+
resolveStreamType,
|
|
72
|
+
type ContentKind,
|
|
73
|
+
type PlayerResumeLookup,
|
|
74
|
+
type PlayerResumeProviderProps,
|
|
75
|
+
} from './hooks/useOpenWithResume';
|
|
76
|
+
|
|
77
|
+
export {SimbaPlayer, type SimbaPlayerProps} from './hooks/SimbaPlayer';
|
|
78
|
+
export {
|
|
79
|
+
useSimbaPlayerLookup,
|
|
80
|
+
type GetResumePosition,
|
|
81
|
+
} from './hooks/useSimbaPlayerLookup';
|
|
82
|
+
export {SimbaPlayerRoot, type SimbaPlayerRootProps} from './hooks/SimbaPlayerRoot';
|
|
83
|
+
export {useOpenFromUrl} from './hooks/useOpenFromUrl';
|
|
84
|
+
export {useLaunchParams} from './hooks/useLaunchParams';
|
|
85
|
+
|
|
60
86
|
export {
|
|
61
87
|
getMpvPlayerModule,
|
|
62
88
|
subscribePlayerEvent,
|
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for `src/types/player.ts` — the player state/commands API.
|
|
3
|
+
*
|
|
4
|
+
* V13 Phase 51: the surface expanded from 4/2/6 to 20/7/38. The
|
|
5
|
+
* tests pin the documented baseline (via `EXPECTED_DEFAULT_STATE`)
|
|
6
|
+
* and verify the contract that matters for consumers:
|
|
7
|
+
* - `usePlayer()` outside a provider returns the default state
|
|
8
|
+
* (no throw — DefaultControls must be able to render anywhere)
|
|
9
|
+
* - `usePlayer()` inside a provider returns the live state
|
|
10
|
+
* - `usePlayer()` returns stable command references across renders
|
|
11
|
+
* - `commands.<method>` delegates to the native bridge
|
|
12
|
+
* - `usePlayerProgress()` returns the documented initial progress
|
|
13
|
+
* - The 1Hz-pollable progress hook is separate from the full state
|
|
14
|
+
*
|
|
15
|
+
* The MpvPlayerModule bridge mock is installed globally in
|
|
16
|
+
* `jest.setup.ts` (it extends the @react-native/jest-preset's own
|
|
17
|
+
* `NativeModules` mock rather than overriding it).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import React from 'react';
|
|
21
|
+
import { NativeModules } from 'react-native';
|
|
22
|
+
import { act, renderHook } from '@testing-library/react-native';
|
|
23
|
+
import {
|
|
24
|
+
usePlayer,
|
|
25
|
+
usePlayerProgress,
|
|
26
|
+
DEFAULT_PROGRESS,
|
|
27
|
+
DEFAULT_STATE,
|
|
28
|
+
type PlayerState,
|
|
29
|
+
type PlayerProgress,
|
|
30
|
+
} from '../player';
|
|
31
|
+
import { PlayerProvider } from '../../components/PlayerProvider';
|
|
32
|
+
import * as BridgeModule from '../../bridge/MpvPlayerModule';
|
|
33
|
+
|
|
34
|
+
// The default state is module-public (DEFAULT_STATE), so we import it
|
|
35
|
+
// directly. Pin the import here so a refactor that moves the default
|
|
36
|
+
// out of this module would surface as a TypeScript error at the
|
|
37
|
+
// import site — not as a silent test failure.
|
|
38
|
+
|
|
39
|
+
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
40
|
+
|
|
41
|
+
/** Clear all bridge mock call counts between tests. */
|
|
42
|
+
function clearBridgeMocks() {
|
|
43
|
+
for (const key of Object.keys(NativeModules.MpvPlayerModule)) {
|
|
44
|
+
const fn = (NativeModules.MpvPlayerModule as Record<string, jest.Mock>)[key];
|
|
45
|
+
if (typeof fn?.mockClear === 'function') {
|
|
46
|
+
fn.mockClear();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Install a `subscribePlayerEvent` mock that captures every
|
|
53
|
+
* registered handler so the test can fire events directly. Also
|
|
54
|
+
* mocks `removeAllListeners` to a no-op (the captured handlers
|
|
55
|
+
* are cleaned up in the `restoreSubscribes()` helper).
|
|
56
|
+
*/
|
|
57
|
+
function captureSubscribes(): {
|
|
58
|
+
handlers: Map<string, Array<(payload: unknown) => void>>;
|
|
59
|
+
restore: () => void;
|
|
60
|
+
} {
|
|
61
|
+
const handlers = new Map<string, Array<(payload: unknown) => void>>();
|
|
62
|
+
const subSpy = jest
|
|
63
|
+
.spyOn(BridgeModule, 'subscribePlayerEvent')
|
|
64
|
+
.mockImplementation(
|
|
65
|
+
(
|
|
66
|
+
event: string,
|
|
67
|
+
handler: (payload: unknown) => void,
|
|
68
|
+
): (() => void) => {
|
|
69
|
+
if (!handlers.has(event)) handlers.set(event, []);
|
|
70
|
+
handlers.get(event)!.push(handler);
|
|
71
|
+
return () => {
|
|
72
|
+
// No-op unsubscribe for tests; captured handlers live
|
|
73
|
+
// for the lifetime of the test.
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
const removeSpy = jest
|
|
78
|
+
.spyOn(BridgeModule, 'removeAllListeners')
|
|
79
|
+
.mockImplementation(() => {
|
|
80
|
+
// No-op for tests.
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
handlers,
|
|
84
|
+
restore: () => {
|
|
85
|
+
subSpy.mockRestore();
|
|
86
|
+
removeSpy.mockRestore();
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ── usePlayer (no provider) ────────────────────────────────────────────────
|
|
92
|
+
|
|
93
|
+
describe('usePlayer (no provider)', () => {
|
|
94
|
+
beforeEach(() => {
|
|
95
|
+
clearBridgeMocks();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('returns the documented default state', async () => {
|
|
99
|
+
// Outside a <PlayerProvider>, usePlayer must NOT throw — it
|
|
100
|
+
// returns DEFAULT_STATE so DefaultControls can render in any
|
|
101
|
+
// environment (jest, web preview, Storybook).
|
|
102
|
+
const { result } = await renderHook(() => usePlayer());
|
|
103
|
+
expect(result.current.state).toEqual(DEFAULT_STATE);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('returns the expected V12 fields at their baseline', async () => {
|
|
107
|
+
// Pin the V12 contract: isPlaying=false, title='Simba Player',
|
|
108
|
+
// artist='', album=''. A refactor that changes the baseline
|
|
109
|
+
// would silently break DefaultControls.
|
|
110
|
+
const { result } = await renderHook(() => usePlayer());
|
|
111
|
+
const s = result.current.state;
|
|
112
|
+
expect(s.isPlaying).toBe(false);
|
|
113
|
+
expect(s.title).toBe('Simba Player');
|
|
114
|
+
expect(s.artist).toBe('');
|
|
115
|
+
expect(s.album).toBe('');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('returns the V13 expanded fields at their baseline', async () => {
|
|
119
|
+
// Pin the V13 expansion: numeric fields at 0, booleans at false,
|
|
120
|
+
// collection fields empty, currentChapter / videoParams / error
|
|
121
|
+
// at null.
|
|
122
|
+
const { result } = await renderHook(() => usePlayer());
|
|
123
|
+
const s = result.current.state;
|
|
124
|
+
expect(s.positionMs).toBe(0);
|
|
125
|
+
expect(s.durationMs).toBe(0);
|
|
126
|
+
expect(s.isBuffering).toBe(false);
|
|
127
|
+
expect(s.isSeeking).toBe(false);
|
|
128
|
+
expect(s.seekable).toBe(false);
|
|
129
|
+
expect(s.volume).toBe(100);
|
|
130
|
+
expect(s.isMuted).toBe(false);
|
|
131
|
+
expect(s.speed).toBe(1);
|
|
132
|
+
expect(s.loopMode).toBe('none');
|
|
133
|
+
expect(s.playlist).toEqual([]);
|
|
134
|
+
expect(s.currentIndex).toBe(-1);
|
|
135
|
+
expect(s.tracks).toEqual([]);
|
|
136
|
+
expect(s.chapters).toEqual([]);
|
|
137
|
+
expect(s.currentChapter).toBeNull();
|
|
138
|
+
expect(s.videoParams).toBeNull();
|
|
139
|
+
expect(s.error).toBeNull();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('exposes the V12 commands object', async () => {
|
|
143
|
+
// The five V12 methods remain on the new commands object —
|
|
144
|
+
// DefaultControls depends on them.
|
|
145
|
+
const { result } = await renderHook(() => usePlayer());
|
|
146
|
+
expect(typeof result.current.commands.play).toBe('function');
|
|
147
|
+
expect(typeof result.current.commands.pause).toBe('function');
|
|
148
|
+
expect(typeof result.current.commands.seek).toBe('function');
|
|
149
|
+
expect(typeof result.current.commands.skipBackward).toBe('function');
|
|
150
|
+
expect(typeof result.current.commands.skipForward).toBe('function');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('exposes the V13 expanded commands object', async () => {
|
|
154
|
+
// Spot-check the new commands. (We don't enumerate every method —
|
|
155
|
+
// that would be a redundant duplication of the interface itself.)
|
|
156
|
+
const { result } = await renderHook(() => usePlayer());
|
|
157
|
+
const c = result.current.commands;
|
|
158
|
+
expect(typeof c.togglePlayPause).toBe('function');
|
|
159
|
+
expect(typeof c.stop).toBe('function');
|
|
160
|
+
expect(typeof c.seekBy).toBe('function');
|
|
161
|
+
expect(typeof c.next).toBe('function');
|
|
162
|
+
expect(typeof c.previous).toBe('function');
|
|
163
|
+
expect(typeof c.setVolume).toBe('function');
|
|
164
|
+
expect(typeof c.setMuted).toBe('function');
|
|
165
|
+
expect(typeof c.toggleMute).toBe('function');
|
|
166
|
+
expect(typeof c.setSpeed).toBe('function');
|
|
167
|
+
expect(typeof c.setLoopMode).toBe('function');
|
|
168
|
+
expect(typeof c.loadFile).toBe('function');
|
|
169
|
+
expect(typeof c.loadPlaylist).toBe('function');
|
|
170
|
+
expect(typeof c.playlistRemove).toBe('function');
|
|
171
|
+
expect(typeof c.shuffle).toBe('function');
|
|
172
|
+
expect(typeof c.clear).toBe('function');
|
|
173
|
+
expect(typeof c.selectTrack).toBe('function');
|
|
174
|
+
expect(typeof c.cycleTrack).toBe('function');
|
|
175
|
+
expect(typeof c.setTrack).toBe('function');
|
|
176
|
+
expect(typeof c.enterPip).toBe('function');
|
|
177
|
+
expect(typeof c.exitPip).toBe('function');
|
|
178
|
+
expect(typeof c.exitPipAndFinish).toBe('function');
|
|
179
|
+
expect(typeof c.setKeepScreenOn).toBe('function');
|
|
180
|
+
expect(typeof c.setOrientation).toBe('function');
|
|
181
|
+
expect(typeof c.setImmersive).toBe('function');
|
|
182
|
+
expect(typeof c.setScreenBrightness).toBe('function');
|
|
183
|
+
expect(typeof c.requestNotificationPermission).toBe('function');
|
|
184
|
+
expect(typeof c.openPlayer).toBe('function');
|
|
185
|
+
expect(typeof c.getLaunchParams).toBe('function');
|
|
186
|
+
expect(typeof c.getProperty).toBe('function');
|
|
187
|
+
expect(typeof c.setProperty).toBe('function');
|
|
188
|
+
expect(typeof c.observeProperty).toBe('function');
|
|
189
|
+
expect(typeof c.unobserveProperty).toBe('function');
|
|
190
|
+
expect(typeof c.grantPersistablePermission).toBe('function');
|
|
191
|
+
expect(typeof c.verifyContentUri).toBe('function');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('commands.play delegates to the native bridge', async () => {
|
|
195
|
+
const { result } = await renderHook(() => usePlayer());
|
|
196
|
+
await result.current.commands.play();
|
|
197
|
+
expect(NativeModules.MpvPlayerModule.play).toHaveBeenCalledTimes(1);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('commands.pause delegates to the native bridge', async () => {
|
|
201
|
+
const { result } = await renderHook(() => usePlayer());
|
|
202
|
+
await result.current.commands.pause();
|
|
203
|
+
expect(NativeModules.MpvPlayerModule.pause).toHaveBeenCalledTimes(1);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('commands.seek(ms) calls seekAbsolute on the bridge', async () => {
|
|
207
|
+
// V12 wiring: commands.seek() translates to
|
|
208
|
+
// bridge.seekAbsolute(positionSec) — seconds vs ms.
|
|
209
|
+
const { result } = await renderHook(() => usePlayer());
|
|
210
|
+
await result.current.commands.seek(10000); // 10s in ms
|
|
211
|
+
expect(NativeModules.MpvPlayerModule.seekAbsolute).toHaveBeenCalledWith(10);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('commands.seek accepts zero', async () => {
|
|
215
|
+
const { result } = await renderHook(() => usePlayer());
|
|
216
|
+
expect(() => result.current.commands.seek(0)).not.toThrow();
|
|
217
|
+
expect(NativeModules.MpvPlayerModule.seekAbsolute).toHaveBeenCalledWith(0);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('commands.skipForward(seconds) calls seekForward on bridge', async () => {
|
|
221
|
+
const { result } = await renderHook(() => usePlayer());
|
|
222
|
+
await result.current.commands.skipForward(15);
|
|
223
|
+
expect(NativeModules.MpvPlayerModule.seekForward).toHaveBeenCalledWith(15);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('commands.skipBackward(seconds) calls seekBackward on bridge', async () => {
|
|
227
|
+
const { result } = await renderHook(() => usePlayer());
|
|
228
|
+
await result.current.commands.skipBackward(10);
|
|
229
|
+
expect(NativeModules.MpvPlayerModule.seekBackward).toHaveBeenCalledWith(10);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('commands.seekBy(+ms) calls seekForward with the absolute seconds', async () => {
|
|
233
|
+
const { result } = await renderHook(() => usePlayer());
|
|
234
|
+
await result.current.commands.seekBy(5000); // +5s
|
|
235
|
+
expect(NativeModules.MpvPlayerModule.seekForward).toHaveBeenCalledWith(5);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('commands.seekBy(-ms) calls seekBackward with the absolute seconds', async () => {
|
|
239
|
+
const { result } = await renderHook(() => usePlayer());
|
|
240
|
+
await result.current.commands.seekBy(-3000); // -3s
|
|
241
|
+
expect(NativeModules.MpvPlayerModule.seekBackward).toHaveBeenCalledWith(3);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('commands.openPlayer reshapes {uri,title,type,startPositionMs} into positional bridge args', async () => {
|
|
245
|
+
const { result } = await renderHook(() => usePlayer());
|
|
246
|
+
await result.current.commands.openPlayer({
|
|
247
|
+
uri: 'file:///song.mp3',
|
|
248
|
+
title: 'Song',
|
|
249
|
+
type: 'audio',
|
|
250
|
+
startPositionMs: 12345,
|
|
251
|
+
});
|
|
252
|
+
expect(NativeModules.MpvPlayerModule.openPlayer).toHaveBeenCalledWith(
|
|
253
|
+
'file:///song.mp3',
|
|
254
|
+
'Song',
|
|
255
|
+
'audio',
|
|
256
|
+
12345,
|
|
257
|
+
);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('commands.openPlayer defaults startPositionMs to 0 when omitted', async () => {
|
|
261
|
+
const { result } = await renderHook(() => usePlayer());
|
|
262
|
+
await result.current.commands.openPlayer({
|
|
263
|
+
uri: 'file:///song.mp3',
|
|
264
|
+
title: 'Song',
|
|
265
|
+
type: 'video',
|
|
266
|
+
});
|
|
267
|
+
expect(NativeModules.MpvPlayerModule.openPlayer).toHaveBeenCalledWith(
|
|
268
|
+
'file:///song.mp3',
|
|
269
|
+
'Song',
|
|
270
|
+
'video',
|
|
271
|
+
0,
|
|
272
|
+
);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('returns stable command references across renders', async () => {
|
|
276
|
+
// Spec §Phase 34.5: stable command references let DefaultControls
|
|
277
|
+
// memo its handlers. The V13 commands object is a module-scope
|
|
278
|
+
// singleton, so the reference is identical for every render.
|
|
279
|
+
const { result, rerender } = await renderHook(() => usePlayer());
|
|
280
|
+
const firstCommands = result.current.commands;
|
|
281
|
+
await rerender({});
|
|
282
|
+
expect(result.current.commands).toBe(firstCommands);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('returns stable state reference when re-rendered with the same provider state', async () => {
|
|
286
|
+
// Outside a provider, usePlayer returns DEFAULT_STATE every time.
|
|
287
|
+
// The reference should be stable (same object identity) so
|
|
288
|
+
// downstream consumers can memoise on it.
|
|
289
|
+
const { result, rerender } = await renderHook(() => usePlayer());
|
|
290
|
+
const firstState = result.current.state;
|
|
291
|
+
await rerender({});
|
|
292
|
+
expect(result.current.state).toBe(firstState);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// ── usePlayer (inside provider) ────────────────────────────────────────────
|
|
297
|
+
|
|
298
|
+
describe('usePlayer (inside PlayerProvider)', () => {
|
|
299
|
+
let subs: ReturnType<typeof captureSubscribes>;
|
|
300
|
+
|
|
301
|
+
beforeEach(() => {
|
|
302
|
+
clearBridgeMocks();
|
|
303
|
+
subs = captureSubscribes();
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
afterEach(() => {
|
|
307
|
+
subs.restore();
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('returns the state from the provider context', async () => {
|
|
311
|
+
// The provider's mount effect hydrates state from the bridge
|
|
312
|
+
// (synchronous getters); the jest mock returns 0/0/'idle'/
|
|
313
|
+
// 100/false/1/'none'/'[]'/null — so the post-hydration state
|
|
314
|
+
// is just DEFAULT_STATE with `title` and `loopMode` re-derived
|
|
315
|
+
// from the bridge.
|
|
316
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
317
|
+
<PlayerProvider>{children}</PlayerProvider>
|
|
318
|
+
);
|
|
319
|
+
const { result } = await renderHook(() => usePlayer(), { wrapper });
|
|
320
|
+
// The bridge mock returns '' for getProperty, so title stays at
|
|
321
|
+
// DEFAULT_STATE.title. Position/duration at 0. isPlaying false.
|
|
322
|
+
expect(result.current.state.isPlaying).toBe(false);
|
|
323
|
+
expect(result.current.state.positionMs).toBe(0);
|
|
324
|
+
expect(result.current.state.durationMs).toBe(0);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it('subscribes to all 22 mpv events on mount', async () => {
|
|
328
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
329
|
+
<PlayerProvider>{children}</PlayerProvider>
|
|
330
|
+
);
|
|
331
|
+
await renderHook(() => usePlayer(), { wrapper });
|
|
332
|
+
// Every PlayerEventName should have at least one captured handler.
|
|
333
|
+
const expectedEvents = [
|
|
334
|
+
'onFileLoaded',
|
|
335
|
+
'onPlaybackStateChanged',
|
|
336
|
+
'onPositionChanged',
|
|
337
|
+
'onDurationChanged',
|
|
338
|
+
'onPropertyChanged',
|
|
339
|
+
'onTracksChanged',
|
|
340
|
+
'onChapterChanged',
|
|
341
|
+
'onVideoParamsChanged',
|
|
342
|
+
'onError',
|
|
343
|
+
'onBuffering',
|
|
344
|
+
'onCacheState',
|
|
345
|
+
'onSeekable',
|
|
346
|
+
'onSeeking',
|
|
347
|
+
'onEndFile',
|
|
348
|
+
'onPlaybackRestart',
|
|
349
|
+
'onEndReached',
|
|
350
|
+
'onAudioDeviceChanged',
|
|
351
|
+
'onVolumeChanged',
|
|
352
|
+
'onSpeedChanged',
|
|
353
|
+
'videoReconfig',
|
|
354
|
+
'onPipModeChanged',
|
|
355
|
+
'onPipPlayPause',
|
|
356
|
+
'onPipExpand',
|
|
357
|
+
'onPipClose',
|
|
358
|
+
];
|
|
359
|
+
for (const event of expectedEvents) {
|
|
360
|
+
expect(subs.handlers.get(event)?.length ?? 0).toBeGreaterThan(0);
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it('updates state when an onPlaybackStateChanged event fires', async () => {
|
|
365
|
+
// Mount the provider, then call the onPlaybackStateChanged
|
|
366
|
+
// handler directly. The provider's event subscription should
|
|
367
|
+
// dispatch through applyPlayerEvent and update the state.
|
|
368
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
369
|
+
<PlayerProvider>{children}</PlayerProvider>
|
|
370
|
+
);
|
|
371
|
+
const { result } = await renderHook(() => usePlayer(), { wrapper });
|
|
372
|
+
const listeners = subs.handlers.get('onPlaybackStateChanged') ?? [];
|
|
373
|
+
expect(listeners.length).toBeGreaterThan(0);
|
|
374
|
+
// Fire the event with a "playing" payload.
|
|
375
|
+
await act(async () => {
|
|
376
|
+
for (const l of listeners) {
|
|
377
|
+
l({ state: 'playing' });
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
expect(result.current.state.isPlaying).toBe(true);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it('updates state when an onError event fires', async () => {
|
|
384
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
385
|
+
<PlayerProvider>{children}</PlayerProvider>
|
|
386
|
+
);
|
|
387
|
+
const { result } = await renderHook(() => usePlayer(), { wrapper });
|
|
388
|
+
const listeners = subs.handlers.get('onError') ?? [];
|
|
389
|
+
expect(listeners.length).toBeGreaterThan(0);
|
|
390
|
+
await act(async () => {
|
|
391
|
+
for (const l of listeners) {
|
|
392
|
+
l({ code: 42, recoverable: true, message: 'test error' });
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
expect(result.current.state.error).toEqual({
|
|
396
|
+
code: 42,
|
|
397
|
+
recoverable: true,
|
|
398
|
+
message: 'test error',
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it('updates state when an onFileLoaded event fires', async () => {
|
|
403
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
404
|
+
<PlayerProvider>{children}</PlayerProvider>
|
|
405
|
+
);
|
|
406
|
+
const { result } = await renderHook(() => usePlayer(), { wrapper });
|
|
407
|
+
const listeners = subs.handlers.get('onFileLoaded') ?? [];
|
|
408
|
+
expect(listeners.length).toBeGreaterThan(0);
|
|
409
|
+
await act(async () => {
|
|
410
|
+
for (const l of listeners) {
|
|
411
|
+
l({
|
|
412
|
+
file: { path: '/song.mp3', title: 'New Title', duration: 120 },
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
expect(result.current.state.title).toBe('New Title');
|
|
417
|
+
expect(result.current.state.durationMs).toBe(120000);
|
|
418
|
+
expect(result.current.state.isPlaying).toBe(true);
|
|
419
|
+
expect(result.current.state.positionMs).toBe(0);
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// ── usePlayerProgress ──────────────────────────────────────────────────────
|
|
424
|
+
|
|
425
|
+
describe('usePlayerProgress', () => {
|
|
426
|
+
it('returns the documented default progress outside a provider', async () => {
|
|
427
|
+
const { result } = await renderHook(() => usePlayerProgress());
|
|
428
|
+
expect(result.current).toEqual(DEFAULT_PROGRESS);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it('returns the expected V12 fields at their baseline', async () => {
|
|
432
|
+
const { result } = await renderHook(() => usePlayerProgress());
|
|
433
|
+
expect(result.current.positionMs).toBe(0);
|
|
434
|
+
expect(result.current.durationMs).toBe(0);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it('returns the V13 expanded fields at their baseline', async () => {
|
|
438
|
+
const { result } = await renderHook(() => usePlayerProgress());
|
|
439
|
+
expect(result.current.isBuffering).toBe(false);
|
|
440
|
+
expect(result.current.isSeeking).toBe(false);
|
|
441
|
+
expect(result.current.seekable).toBe(false);
|
|
442
|
+
expect(result.current.cacheRanges).toEqual([]);
|
|
443
|
+
expect(result.current.cacheFill).toBe(0);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it('returns the full DEFAULT_PROGRESS shape', async () => {
|
|
447
|
+
// Pin the contract: usePlayerProgress returns all 7 fields, not
|
|
448
|
+
// just 2. The V12 surface is a strict subset.
|
|
449
|
+
const expected: PlayerProgress = {
|
|
450
|
+
positionMs: 0,
|
|
451
|
+
durationMs: 0,
|
|
452
|
+
isBuffering: false,
|
|
453
|
+
isSeeking: false,
|
|
454
|
+
seekable: false,
|
|
455
|
+
cacheRanges: [],
|
|
456
|
+
cacheFill: 0,
|
|
457
|
+
};
|
|
458
|
+
const { result } = await renderHook(() => usePlayerProgress());
|
|
459
|
+
expect(result.current).toEqual(expected);
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
// ── PlayerState default integrity ───────────────────────────────────────────
|
|
464
|
+
|
|
465
|
+
describe('PlayerState default integrity', () => {
|
|
466
|
+
it('PlayerState has 20 fields in DEFAULT_STATE', async () => {
|
|
467
|
+
// Sanity check: V13 expanded the V12 4-field state to 20 fields.
|
|
468
|
+
// If a future refactor accidentally drops a field (e.g. by
|
|
469
|
+
// using `Partial<PlayerState>` somewhere), this catches it.
|
|
470
|
+
const keys = Object.keys(DEFAULT_STATE).sort();
|
|
471
|
+
expect(keys).toEqual([
|
|
472
|
+
'album',
|
|
473
|
+
'artist',
|
|
474
|
+
'chapters',
|
|
475
|
+
'currentChapter',
|
|
476
|
+
'currentIndex',
|
|
477
|
+
'durationMs',
|
|
478
|
+
'error',
|
|
479
|
+
'isBuffering',
|
|
480
|
+
'isMuted',
|
|
481
|
+
'isPlaying',
|
|
482
|
+
'isSeeking',
|
|
483
|
+
'loopMode',
|
|
484
|
+
'playlist',
|
|
485
|
+
'positionMs',
|
|
486
|
+
'seekable',
|
|
487
|
+
'speed',
|
|
488
|
+
'title',
|
|
489
|
+
'tracks',
|
|
490
|
+
'videoParams',
|
|
491
|
+
'volume',
|
|
492
|
+
]);
|
|
493
|
+
});
|
|
494
|
+
});
|