@applicaster/zapp-react-native-utils 16.0.0-rc.107 → 16.0.0-rc.109

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
+ });
@@ -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
+ };
@@ -257,6 +257,69 @@ describe("getEmptyModalBlocksStructure", () => {
257
257
  );
258
258
  });
259
259
 
260
+ it("maps text label and now playing font keys onto the label style", () => {
261
+ const result = parseModalBlocksConfiguration(
262
+ {
263
+ "item.text_label1.ios.font_family": "SFProText-Bold",
264
+ "item.text_label1.letter_spacing": -0.4,
265
+ "item.text_label1.text_transform": "uppercase",
266
+ "now_playing_item.now_playing_label.ios.font_family":
267
+ "SFProText-Semibold",
268
+ "now_playing_item.now_playing_label.font_size": 18,
269
+ },
270
+ "queue-action"
271
+ );
272
+
273
+ expect(result.item.textLabel1.default).toEqual(
274
+ expect.objectContaining({
275
+ fontFamily: "SFProText-Bold",
276
+ letterSpacing: -0.4,
277
+ textTransform: "uppercase",
278
+ })
279
+ );
280
+
281
+ expect(result.nowPlayingItem?.nowPlayingLabel.default).toEqual(
282
+ expect.objectContaining({
283
+ fontFamily: "SFProText-Semibold",
284
+ fontSize: 18,
285
+ })
286
+ );
287
+ });
288
+
289
+ it("maps queue remove and reorder assets onto item action buttons", () => {
290
+ const result = parseModalBlocksConfiguration(
291
+ {
292
+ "item.remove_asset": "https://example.com/remove.png",
293
+ "item.reorder_handle_asset": "https://example.com/reorder.png",
294
+ },
295
+ "queue-action"
296
+ );
297
+
298
+ expect(result.item.actionButtons.default).toEqual(
299
+ expect.objectContaining({
300
+ removeAsset: "https://example.com/remove.png",
301
+ dragAsset: "https://example.com/reorder.png",
302
+ })
303
+ );
304
+ });
305
+
306
+ it("maps now playing play and pause assets onto action buttons", () => {
307
+ const result = parseModalBlocksConfiguration(
308
+ {
309
+ "now_playing_item.play_asset": "https://example.com/play.png",
310
+ "now_playing_item.pause_asset": "https://example.com/pause.png",
311
+ },
312
+ "queue-action"
313
+ );
314
+
315
+ expect(result.nowPlayingItem?.actionButtons.default).toEqual(
316
+ expect.objectContaining({
317
+ playAsset: "https://example.com/play.png",
318
+ pauseAsset: "https://example.com/pause.png",
319
+ })
320
+ );
321
+ });
322
+
260
323
  it("should leave sectionHeader empty when keys are absent", () => {
261
324
  const result = parseModalBlocksConfiguration(
262
325
  { "item.text_label1.font_color": "#FFFFFF" },
@@ -210,5 +210,43 @@ export const parseModalBlocksConfiguration = (
210
210
 
211
211
  parseBlock("button", ["background", "title", "icons", "leading_icon"]);
212
212
 
213
+ // Leaf uploader keys. They are not `<block>_<sub>_<prop>` styles, so
214
+ // getAllSpecificStyles never sees them.
215
+ const assignLeafAsset = (
216
+ block: ModalBlocksConfig["item"] | undefined,
217
+ key: string,
218
+ prop: "playAsset" | "pauseAsset" | "removeAsset" | "dragAsset"
219
+ ) => {
220
+ if (!block) return;
221
+
222
+ const value = configuration[key];
223
+
224
+ if (typeof value !== "string" || !value.trim()) return;
225
+
226
+ block.actionButtons = {
227
+ ...block.actionButtons,
228
+ default: {
229
+ ...(block.actionButtons?.default || {}),
230
+ [prop]: value,
231
+ },
232
+ };
233
+ };
234
+
235
+ assignLeafAsset(
236
+ result.nowPlayingItem,
237
+ "now_playing_item.play_asset",
238
+ "playAsset"
239
+ );
240
+
241
+ assignLeafAsset(
242
+ result.nowPlayingItem,
243
+ "now_playing_item.pause_asset",
244
+ "pauseAsset"
245
+ );
246
+
247
+ assignLeafAsset(result.item, "item.remove_asset", "removeAsset");
248
+
249
+ assignLeafAsset(result.item, "item.reorder_handle_asset", "dragAsset");
250
+
213
251
  return result;
214
252
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "16.0.0-rc.107",
3
+ "version": "16.0.0-rc.109",
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": "16.0.0-rc.107",
30
+ "@applicaster/applicaster-types": "16.0.0-rc.109",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",