@applicaster/zapp-react-native-utils 15.1.18-rc.0 → 15.1.19-alpha.3927125430

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.
@@ -0,0 +1,97 @@
1
+ const mockRegister = jest.fn();
2
+ let mockXray: Record<string, unknown> | null = {};
3
+
4
+ jest.mock("../../../logger", () => ({
5
+ getXray: () => mockXray,
6
+ }));
7
+
8
+ const loadToolbox = () => {
9
+ let module;
10
+
11
+ jest.isolateModules(() => {
12
+ module = require("../playerToolbox");
13
+ });
14
+
15
+ return module as typeof import("../playerToolbox");
16
+ };
17
+
18
+ const player = (overrides = {}) => ({
19
+ getSeekableDuration: () => 100,
20
+ getContentPosition: () => 40,
21
+ seekTo: jest.fn(),
22
+ ...overrides,
23
+ });
24
+
25
+ const manager = (active) =>
26
+ ({ getActivePlayer: () => active, isCasting: () => false }) as any;
27
+
28
+ const registeredTools = async (active) => {
29
+ mockRegister.mockResolvedValue(undefined);
30
+ mockXray = { registerReactToolbox: mockRegister };
31
+ await loadToolbox().registerPlayerToolbox(manager(active));
32
+
33
+ const { tools } = mockRegister.mock.calls[0][0];
34
+
35
+ return Object.fromEntries(tools.map((tool) => [tool.name, tool.handler]));
36
+ };
37
+
38
+ describe("playerToolbox", () => {
39
+ beforeEach(() => {
40
+ mockRegister.mockReset();
41
+ });
42
+
43
+ it("does nothing when X-Ray has no React toolbox API", async () => {
44
+ mockXray = {};
45
+
46
+ await expect(
47
+ loadToolbox().registerPlayerToolbox(manager(null))
48
+ ).resolves.toBeUndefined();
49
+ });
50
+
51
+ it("does nothing when getXray returns null", async () => {
52
+ mockXray = null;
53
+
54
+ await expect(
55
+ loadToolbox().registerPlayerToolbox(manager(null))
56
+ ).resolves.toBeUndefined();
57
+ });
58
+
59
+ it("swallows a failed registration", async () => {
60
+ mockRegister.mockRejectedValue(new Error("no native bridge"));
61
+ mockXray = { registerReactToolbox: mockRegister };
62
+
63
+ await expect(
64
+ loadToolbox().registerPlayerToolbox(manager(null))
65
+ ).resolves.toBeUndefined();
66
+ });
67
+
68
+ it("seeks to an absolute position, from the end when negative, clamped", async () => {
69
+ const active = player();
70
+ const tools = await registeredTools(active);
71
+
72
+ expect(tools.seek({ position: 30 })).toEqual({ position: 30 });
73
+ expect(tools.seek({ position: -10 })).toEqual({ position: 90 });
74
+ expect(tools.seek({ position: 500 })).toEqual({ position: 100 });
75
+ expect(tools.seek({ position: -500 })).toEqual({ position: 0 });
76
+ expect(active.seekTo.mock.calls).toEqual([[30], [90], [100], [0]]);
77
+ });
78
+
79
+ it("offsets from the current position and refuses zero", async () => {
80
+ const tools = await registeredTools(player());
81
+
82
+ expect(tools.offset({ seconds: 15 })).toEqual({ from: 40, position: 55 });
83
+ expect(tools.offset({ seconds: -60 })).toEqual({ from: 40, position: 0 });
84
+
85
+ expect(() => tools.offset({ seconds: 0 })).toThrow(
86
+ "Ignored zero relative seek"
87
+ );
88
+ });
89
+
90
+ it("refuses to seek a stream that is not seekable", async () => {
91
+ const tools = await registeredTools(
92
+ player({ getSeekableDuration: () => 0 })
93
+ );
94
+
95
+ expect(() => tools.seek({ position: 10 })).toThrow("Seek is not supported");
96
+ });
97
+ });
@@ -0,0 +1,62 @@
1
+ import { renderHook } from "@testing-library/react-native";
2
+
3
+ import { usePlayerControllerSetup } from "../usePlayerControllerSetup";
4
+ import { PlayerAnalyticsManager } from "../../../analyticsUtils/PlayerAnalyticsManager";
5
+
6
+ const calls: string[] = [];
7
+
8
+ jest.mock("../index", () => ({
9
+ playerManager: {
10
+ isPlayerRegistered: jest.fn(() => false),
11
+ registerPlayer: jest.fn(() => calls.push("registerPlayer")),
12
+ unregisterPlayer: jest.fn(),
13
+ },
14
+ }));
15
+
16
+ jest.mock("../../../analyticsUtils/PlayerAnalyticsManager", () => ({
17
+ PlayerAnalyticsManager: {
18
+ getInstance: jest.fn(() => calls.push("analyticsManager")),
19
+ },
20
+ }));
21
+
22
+ jest.mock("../../../reactHooks/useConfiguration", () => ({
23
+ useConfiguration: () => ({}),
24
+ }));
25
+
26
+ jest.mock("../../../playerUtils/useValidatePlayerConfig", () => ({
27
+ useValidatePlayerConfig: jest.fn(),
28
+ }));
29
+
30
+ jest.mock("../../../reactUtils", () => ({
31
+ isTV: () => false,
32
+ }));
33
+
34
+ jest.mock(
35
+ "@applicaster/zapp-react-native-ui-components/Helpers/Platform",
36
+ () => ({
37
+ isAppleTV: () => false,
38
+ })
39
+ );
40
+
41
+ class Controller {
42
+ destroy = jest.fn();
43
+ }
44
+
45
+ describe("usePlayerControllerSetup", () => {
46
+ it("creates the analytics manager before the player registers", () => {
47
+ renderHook(() =>
48
+ usePlayerControllerSetup({
49
+ props: { playerId: "player-1", entry: { id: "entry-1" } },
50
+ ControllerType: Controller,
51
+ ref: null,
52
+ manifest: {},
53
+ pluginId: "plugin",
54
+ })
55
+ );
56
+
57
+ // The manager attaches analytics to players as they register, so a player
58
+ // registered before it exists would send no player analytics events
59
+ expect(PlayerAnalyticsManager.getInstance).toHaveBeenCalled();
60
+ expect(calls).toEqual(["analyticsManager", "registerPlayer"]);
61
+ });
62
+ });
@@ -11,6 +11,7 @@ import {
11
11
  import { setUserLanguagePreference } from "./userLanguagePreference";
12
12
  import { PlayerRole } from "./const";
13
13
  import { RNPlayerManager } from "./nativePlayerManager";
14
+ import { registerPlayerToolbox } from "./playerToolbox";
14
15
 
15
16
  const { log_error, log_debug, log_warning } = createLogger({
16
17
  category: "General",
@@ -54,6 +55,7 @@ export class PlayerManager {
54
55
  public static getInstance() {
55
56
  if (!PlayerManager.instance) {
56
57
  PlayerManager.instance = new PlayerManager();
58
+ registerPlayerToolbox(PlayerManager.instance);
57
59
  }
58
60
 
59
61
  return PlayerManager.instance;
@@ -0,0 +1,192 @@
1
+ import { getXray } from "../../logger";
2
+ import type { PlayerManager } from "./index";
3
+
4
+ /**
5
+ * X-Ray `player.*` toolbox: lets an agent read and drive the active player without screenshots.
6
+ * Registered by the PlayerManager singleton itself, so it exists wherever players do. Skipped
7
+ * when the installed X-Ray plugin has no React toolbox API.
8
+ *
9
+ * How the player is shown (video modal mode, mini player) is navigation state: see `nav.*`.
10
+ */
11
+
12
+ type ToolSpec = {
13
+ name: string;
14
+ description: string;
15
+ parameters?: Array<{
16
+ name: string;
17
+ type: "number";
18
+ description: string;
19
+ }>;
20
+ handler: (args: Record<string, unknown>) => unknown;
21
+ };
22
+
23
+ type RegisterReactToolbox = (toolbox: {
24
+ name: string;
25
+ description: string;
26
+ tools: ToolSpec[];
27
+ }) => Promise<void>;
28
+
29
+ let registration: Promise<void> | undefined;
30
+
31
+ const toNumber = (value: unknown) =>
32
+ typeof value === "number" && Number.isFinite(value) ? value : null;
33
+
34
+ const clamp = (value: number, max: number) => Math.min(Math.max(value, 0), max);
35
+
36
+ /** The range a seek can reach: the DVR window on live, the content on VOD. */
37
+ const requireSeekable = (player: {
38
+ getSeekableDuration: () => number | undefined;
39
+ }) => {
40
+ const seekable = toNumber(player.getSeekableDuration());
41
+
42
+ if (!seekable) throw new Error("Seek is not supported");
43
+
44
+ return seekable;
45
+ };
46
+
47
+ const tools = (manager: PlayerManager): ToolSpec[] => {
48
+ const requirePlayer = () => {
49
+ const player = manager.getActivePlayer();
50
+
51
+ if (!player) throw new Error("No active player");
52
+
53
+ return player;
54
+ };
55
+
56
+ return [
57
+ {
58
+ name: "state",
59
+ description:
60
+ "The active player: entry, position and duration in seconds, playing/buffering/ad/live/muted, " +
61
+ "role (Primary, Cell, Chromecast) and casting. active: false when nothing plays.",
62
+ handler: () => {
63
+ const player = manager.getActivePlayer();
64
+
65
+ if (!player) return { active: false, casting: manager.isCasting() };
66
+
67
+ const entry = player.getEntry();
68
+
69
+ return {
70
+ active: true,
71
+ playerId: player.playerId,
72
+ entry: entry ? { id: entry.id, title: entry.title } : null,
73
+ position: toNumber(player.getPosition()),
74
+ duration: toNumber(player.getDuration()),
75
+ // isPlaying() only means "not paused", which is also true before the stream starts.
76
+ playing: !!player.isReadyToPlay() && player.isPlaying(),
77
+ buffering: !!player.isBuffering(),
78
+ readyToPlay: !!player.isReadyToPlay(),
79
+ ad: player.isAd(),
80
+ live: !!player.isLive(),
81
+ muted: !!player.getIsMuted(),
82
+ role: player.playerRole,
83
+ casting: manager.isCasting(),
84
+ };
85
+ },
86
+ },
87
+ {
88
+ name: "play",
89
+ description: "Resume the active player.",
90
+ handler: () => {
91
+ requirePlayer().play();
92
+
93
+ return "OK";
94
+ },
95
+ },
96
+ {
97
+ name: "pause",
98
+ description: "Pause the active player.",
99
+ handler: () => {
100
+ requirePlayer().pause();
101
+
102
+ return "OK";
103
+ },
104
+ },
105
+ {
106
+ name: "seek",
107
+ description:
108
+ "Seek to an absolute position, as the player's console command does: seconds from the start, " +
109
+ "or from the end when negative. Clamped to the seekable range. Where it lands can differ: " +
110
+ "server-side ads snap back to the break before the target; read player.state afterwards.",
111
+ parameters: [
112
+ {
113
+ name: "position",
114
+ type: "number",
115
+ description:
116
+ "Seconds from the start; negative values count back from the end.",
117
+ },
118
+ ],
119
+ handler: ({ position }) => {
120
+ const player = requirePlayer();
121
+ const seconds = toNumber(position);
122
+
123
+ if (seconds === null) {
124
+ throw new Error("'position' must be a number of seconds");
125
+ }
126
+
127
+ const seekable = requireSeekable(player);
128
+
129
+ const target =
130
+ seconds >= 0
131
+ ? Math.min(seconds, seekable)
132
+ : clamp(seekable + seconds, seekable);
133
+
134
+ player.seekTo(target);
135
+
136
+ return { position: target };
137
+ },
138
+ },
139
+ {
140
+ name: "offset",
141
+ description:
142
+ "Seek relative to the current position, as the player's console command does: positive " +
143
+ "forward, negative back. Clamped to the seekable range.",
144
+ parameters: [
145
+ {
146
+ name: "seconds",
147
+ type: "number",
148
+ description: "Seconds to move: positive forward, negative back.",
149
+ },
150
+ ],
151
+ handler: ({ seconds }) => {
152
+ const player = requirePlayer();
153
+ const delta = toNumber(seconds);
154
+
155
+ if (delta === null) throw new Error("'seconds' must be a number");
156
+
157
+ if (delta === 0) throw new Error("Ignored zero relative seek");
158
+
159
+ const seekable = requireSeekable(player);
160
+ const from = toNumber(player.getContentPosition()) ?? 0;
161
+ const target = clamp(from + delta, seekable);
162
+
163
+ player.seekTo(target);
164
+
165
+ return { from, position: target };
166
+ },
167
+ },
168
+ ];
169
+ };
170
+
171
+ export const registerPlayerToolbox = (manager: PlayerManager) => {
172
+ if (registration) return registration;
173
+
174
+ const register = (
175
+ getXray() as
176
+ | { registerReactToolbox?: RegisterReactToolbox }
177
+ | null
178
+ | undefined
179
+ )?.registerReactToolbox;
180
+
181
+ if (!register) return Promise.resolve();
182
+
183
+ registration = register({
184
+ name: "player",
185
+ description: "Inspect and control the active player.",
186
+ tools: tools(manager),
187
+ }).catch(() => {
188
+ registration = undefined;
189
+ });
190
+
191
+ return registration;
192
+ };
@@ -8,6 +8,7 @@ import { isAppleTV } from "@applicaster/zapp-react-native-ui-components/Helpers/
8
8
  import { TVSeekController } from "../../reactHooks/player/TVSeekControlller/TVSeekController";
9
9
  import { KeyInputHandler } from "../keyInputHandler/KeyInputHandler";
10
10
  import { isTV } from "../../reactUtils";
11
+ import { PlayerAnalyticsManager } from "../../analyticsUtils/PlayerAnalyticsManager";
11
12
 
12
13
  // TODO: Rename to ControllerType
13
14
  export const usePlayerControllerSetup = ({
@@ -68,6 +69,11 @@ export const usePlayerControllerSetup = ({
68
69
  return;
69
70
  }
70
71
 
72
+ // The analytics manager attaches its listener when a player registers, so it
73
+ // must exist first; on web the Player wrapper that also creates it renders
74
+ // only after the lazily loaded engine chunk, i.e. after registration.
75
+ PlayerAnalyticsManager.getInstance();
76
+
71
77
  playerManager.registerPlayer({
72
78
  id: playerId,
73
79
  playerController,
@@ -256,6 +256,69 @@ describe("parseModalBlocksConfiguration", () => {
256
256
  );
257
257
  });
258
258
 
259
+ it("maps text label and now playing font keys onto the label style", () => {
260
+ const result = parseModalBlocksConfiguration(
261
+ {
262
+ "item.text_label1.ios.font_family": "SFProText-Bold",
263
+ "item.text_label1.letter_spacing": -0.4,
264
+ "item.text_label1.text_transform": "uppercase",
265
+ "now_playing_item.now_playing_label.ios.font_family":
266
+ "SFProText-Semibold",
267
+ "now_playing_item.now_playing_label.font_size": 18,
268
+ },
269
+ "queue-action"
270
+ );
271
+
272
+ expect(result.item.textLabel1.default).toEqual(
273
+ expect.objectContaining({
274
+ fontFamily: "SFProText-Bold",
275
+ letterSpacing: -0.4,
276
+ textTransform: "uppercase",
277
+ })
278
+ );
279
+
280
+ expect(result.nowPlayingItem?.nowPlayingLabel.default).toEqual(
281
+ expect.objectContaining({
282
+ fontFamily: "SFProText-Semibold",
283
+ fontSize: 18,
284
+ })
285
+ );
286
+ });
287
+
288
+ it("maps queue remove and reorder assets onto item action buttons", () => {
289
+ const result = parseModalBlocksConfiguration(
290
+ {
291
+ "item.remove_asset": "https://example.com/remove.png",
292
+ "item.reorder_handle_asset": "https://example.com/reorder.png",
293
+ },
294
+ "queue-action"
295
+ );
296
+
297
+ expect(result.item.actionButtons.default).toEqual(
298
+ expect.objectContaining({
299
+ removeAsset: "https://example.com/remove.png",
300
+ dragAsset: "https://example.com/reorder.png",
301
+ })
302
+ );
303
+ });
304
+
305
+ it("maps now playing play and pause assets onto action buttons", () => {
306
+ const result = parseModalBlocksConfiguration(
307
+ {
308
+ "now_playing_item.play_asset": "https://example.com/play.png",
309
+ "now_playing_item.pause_asset": "https://example.com/pause.png",
310
+ },
311
+ "queue-action"
312
+ );
313
+
314
+ expect(result.nowPlayingItem?.actionButtons.default).toEqual(
315
+ expect.objectContaining({
316
+ playAsset: "https://example.com/play.png",
317
+ pauseAsset: "https://example.com/pause.png",
318
+ })
319
+ );
320
+ });
321
+
259
322
  it("should leave sectionHeader empty when keys are absent", () => {
260
323
  const result = parseModalBlocksConfiguration(
261
324
  { "item.text_label1.font_color": "#FFFFFF" },
@@ -204,6 +204,44 @@ export const parseModalBlocksConfiguration = (
204
204
 
205
205
  parseBlock("button", ["background", "title", "icons", "leading_icon"]);
206
206
 
207
+ // Leaf uploader keys. They are not `<block>_<sub>_<prop>` styles, so
208
+ // getAllSpecificStyles never sees them.
209
+ const assignLeafAsset = (
210
+ block: ModalBlocksConfig["item"] | undefined,
211
+ key: string,
212
+ prop: "playAsset" | "pauseAsset" | "removeAsset" | "dragAsset"
213
+ ) => {
214
+ if (!block) return;
215
+
216
+ const value = configuration[key];
217
+
218
+ if (typeof value !== "string" || !value.trim()) return;
219
+
220
+ block.actionButtons = {
221
+ ...block.actionButtons,
222
+ default: {
223
+ ...(block.actionButtons?.default || {}),
224
+ [prop]: value,
225
+ },
226
+ };
227
+ };
228
+
229
+ assignLeafAsset(
230
+ result.nowPlayingItem,
231
+ "now_playing_item.play_asset",
232
+ "playAsset"
233
+ );
234
+
235
+ assignLeafAsset(
236
+ result.nowPlayingItem,
237
+ "now_playing_item.pause_asset",
238
+ "pauseAsset"
239
+ );
240
+
241
+ assignLeafAsset(result.item, "item.remove_asset", "removeAsset");
242
+
243
+ assignLeafAsset(result.item, "item.reorder_handle_asset", "dragAsset");
244
+
207
245
  // Playlist-experience Studio keys live in this shared parser because modal
208
246
  // blocks are themed by themePluginId. Gate them so other plugins are not
209
247
  // affected by playlist-only asset names.
@@ -224,12 +224,38 @@ class FocusManager {
224
224
 
225
225
  unregisterFocusable(focusableId: string) {
226
226
  const node = this.tree.find(focusableId);
227
+ const parentNode = this.tree.findParent(focusableId);
227
228
 
228
229
  this.focusableComponents = this.focusableComponents.filter(
229
230
  (c) => c !== node?.component
230
231
  );
231
232
 
232
233
  this.tree.remove(focusableId);
234
+
235
+ if (this._focusedId === focusableId) {
236
+ this._focusedId = null;
237
+ this.retainFocus(parentNode);
238
+ }
239
+ }
240
+
241
+ // Empty groups drop their focusable list after the feed fails, which is
242
+ // after Layout's one-shot focus pass. Land on the nearest ancestor that is
243
+ // still mounted so key handlers keep a target.
244
+ private retainFocus(node: TreeNode | null) {
245
+ let current = node;
246
+
247
+ while (current) {
248
+ const ref = current.component;
249
+
250
+ if (ref?.current?.props?.id) {
251
+ this.setFocusLocal(ref);
252
+ ref.current.onFocus?.(ref.current, {}, undefined);
253
+
254
+ return;
255
+ }
256
+
257
+ current = current.parentId ? this.tree.find(current.parentId) : null;
258
+ }
233
259
  }
234
260
 
235
261
  private setNextFocus(
@@ -231,6 +231,15 @@ describe("FocusManager", () => {
231
231
  ).toBeFalsy();
232
232
  });
233
233
 
234
+ it("moves focus to the mounted parent when the focused element unregisters", () => {
235
+ focusManager.setFocus("child1");
236
+
237
+ focusManager.unregisterFocusable("child1");
238
+
239
+ expect(focusManager.focusedId).toBe("group");
240
+ expect(focusManager.focused).toBe(group.current);
241
+ });
242
+
234
243
  it("focusManager registerFocusable should be defined", () => {
235
244
  expect(focusManager.registerFocusable).toBeDefined();
236
245
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "15.1.18-rc.0",
3
+ "version": "15.1.19-alpha.3927125430",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/applicaster/quickbrick#readme",
29
29
  "dependencies": {
30
- "@applicaster/applicaster-types": "15.1.18-rc.0",
30
+ "@applicaster/applicaster-types": "15.1.19-alpha.3927125430",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",