@escapeaihq/client-core 0.6.0 → 0.7.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.
Files changed (120) hide show
  1. package/README.md +27 -2
  2. package/dist/api.d.ts +180 -2
  3. package/dist/api.d.ts.map +1 -1
  4. package/dist/api.js +134 -0
  5. package/dist/api.js.map +1 -1
  6. package/dist/audio.d.ts +1 -1
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +2 -0
  10. package/dist/index.js.map +1 -1
  11. package/dist/reactor/entityContext.d.ts +34 -0
  12. package/dist/reactor/entityContext.d.ts.map +1 -0
  13. package/dist/reactor/entityContext.js +119 -0
  14. package/dist/reactor/entityContext.js.map +1 -0
  15. package/dist/reactor/entitySearch.d.ts +71 -0
  16. package/dist/reactor/entitySearch.d.ts.map +1 -0
  17. package/dist/reactor/entitySearch.js +93 -0
  18. package/dist/reactor/entitySearch.js.map +1 -0
  19. package/dist/reactor/heliosCommandDispatcher.d.ts +98 -0
  20. package/dist/reactor/heliosCommandDispatcher.d.ts.map +1 -0
  21. package/dist/reactor/heliosCommandDispatcher.js +181 -0
  22. package/dist/reactor/heliosCommandDispatcher.js.map +1 -0
  23. package/dist/reactor/heliosTools.d.ts +156 -0
  24. package/dist/reactor/heliosTools.d.ts.map +1 -0
  25. package/dist/reactor/heliosTools.js +241 -0
  26. package/dist/reactor/heliosTools.js.map +1 -0
  27. package/dist/reactor/index.d.ts +17 -0
  28. package/dist/reactor/index.d.ts.map +1 -0
  29. package/dist/reactor/index.js +17 -0
  30. package/dist/reactor/index.js.map +1 -0
  31. package/dist/reactor/promptAddonRegistry.d.ts +37 -0
  32. package/dist/reactor/promptAddonRegistry.d.ts.map +1 -0
  33. package/dist/reactor/promptAddonRegistry.js +38 -0
  34. package/dist/reactor/promptAddonRegistry.js.map +1 -0
  35. package/dist/reactor/regenTemplate.d.ts +23 -0
  36. package/dist/reactor/regenTemplate.d.ts.map +1 -0
  37. package/dist/reactor/regenTemplate.js +103 -0
  38. package/dist/reactor/regenTemplate.js.map +1 -0
  39. package/dist/reactor/sceneEntry.d.ts +79 -0
  40. package/dist/reactor/sceneEntry.d.ts.map +1 -0
  41. package/dist/reactor/sceneEntry.js +177 -0
  42. package/dist/reactor/sceneEntry.js.map +1 -0
  43. package/dist/reactor/scenesFromEnrichment.d.ts +67 -0
  44. package/dist/reactor/scenesFromEnrichment.d.ts.map +1 -0
  45. package/dist/reactor/scenesFromEnrichment.js +344 -0
  46. package/dist/reactor/scenesFromEnrichment.js.map +1 -0
  47. package/dist/reactor/seedImageOrchestrator.d.ts +81 -0
  48. package/dist/reactor/seedImageOrchestrator.d.ts.map +1 -0
  49. package/dist/reactor/seedImageOrchestrator.js +79 -0
  50. package/dist/reactor/seedImageOrchestrator.js.map +1 -0
  51. package/dist/reactor/toolPromptAddon.d.ts +44 -0
  52. package/dist/reactor/toolPromptAddon.d.ts.map +1 -0
  53. package/dist/reactor/toolPromptAddon.js +129 -0
  54. package/dist/reactor/toolPromptAddon.js.map +1 -0
  55. package/dist/reactor/toolPromptAddonId.d.ts +10 -0
  56. package/dist/reactor/toolPromptAddonId.d.ts.map +1 -0
  57. package/dist/reactor/toolPromptAddonId.js +10 -0
  58. package/dist/reactor/toolPromptAddonId.js.map +1 -0
  59. package/dist/reactor/types.d.ts +149 -0
  60. package/dist/reactor/types.d.ts.map +1 -0
  61. package/dist/reactor/types.js +2 -0
  62. package/dist/reactor/types.js.map +1 -0
  63. package/dist/reactor/upsamplePrompt.d.ts +30 -0
  64. package/dist/reactor/upsamplePrompt.d.ts.map +1 -0
  65. package/dist/reactor/upsamplePrompt.js +118 -0
  66. package/dist/reactor/upsamplePrompt.js.map +1 -0
  67. package/dist/timeFormat.d.ts +28 -0
  68. package/dist/timeFormat.d.ts.map +1 -0
  69. package/dist/timeFormat.js +48 -0
  70. package/dist/timeFormat.js.map +1 -0
  71. package/dist/visemeGeometry.d.ts +15 -0
  72. package/dist/visemeGeometry.d.ts.map +1 -1
  73. package/dist/visemeGeometry.js +26 -0
  74. package/dist/visemeGeometry.js.map +1 -1
  75. package/dist/visemeScheduler.d.ts +12 -0
  76. package/dist/visemeScheduler.d.ts.map +1 -1
  77. package/dist/visemeScheduler.js +24 -0
  78. package/dist/visemeScheduler.js.map +1 -1
  79. package/dist/websocket.d.ts +51 -3
  80. package/dist/websocket.d.ts.map +1 -1
  81. package/dist/websocket.js +52 -7
  82. package/dist/websocket.js.map +1 -1
  83. package/package.json +1 -1
  84. package/src/__tests__/reactor/__snapshots__/regenTemplate.test.ts.snap +5 -0
  85. package/src/__tests__/reactor/__snapshots__/scenesFromEnrichment.test.ts.snap +34 -0
  86. package/src/__tests__/reactor/api-reactor.test.ts +253 -0
  87. package/src/__tests__/reactor/entityContext.test.ts +237 -0
  88. package/src/__tests__/reactor/entitySearch.test.ts +223 -0
  89. package/src/__tests__/reactor/heliosCommandDispatcher.test.ts +438 -0
  90. package/src/__tests__/reactor/heliosTools.test.ts +164 -0
  91. package/src/__tests__/reactor/promptAddonRegistry.test.ts +40 -0
  92. package/src/__tests__/reactor/regenTemplate.test.ts +156 -0
  93. package/src/__tests__/reactor/sceneEntry.test.ts +342 -0
  94. package/src/__tests__/reactor/scenesFromEnrichment.test.ts +760 -0
  95. package/src/__tests__/reactor/seedImageOrchestrator.test.ts +139 -0
  96. package/src/__tests__/reactor/toolPromptAddon.test.ts +104 -0
  97. package/src/__tests__/reactor/upsamplePrompt.test.ts +245 -0
  98. package/src/__tests__/timeFormat.test.ts +80 -0
  99. package/src/__tests__/tools-wrappers.test.ts +219 -0
  100. package/src/__tests__/websocket.test.ts +278 -0
  101. package/src/api.ts +279 -2
  102. package/src/index.ts +2 -0
  103. package/src/reactor/entityContext.ts +147 -0
  104. package/src/reactor/entitySearch.ts +139 -0
  105. package/src/reactor/heliosCommandDispatcher.ts +293 -0
  106. package/src/reactor/heliosTools.ts +260 -0
  107. package/src/reactor/index.ts +17 -0
  108. package/src/reactor/promptAddonRegistry.ts +52 -0
  109. package/src/reactor/regenTemplate.ts +142 -0
  110. package/src/reactor/sceneEntry.ts +267 -0
  111. package/src/reactor/scenesFromEnrichment.ts +393 -0
  112. package/src/reactor/seedImageOrchestrator.ts +132 -0
  113. package/src/reactor/toolPromptAddon.ts +135 -0
  114. package/src/reactor/toolPromptAddonId.ts +9 -0
  115. package/src/reactor/types.ts +158 -0
  116. package/src/reactor/upsamplePrompt.ts +154 -0
  117. package/src/timeFormat.ts +57 -0
  118. package/src/visemeGeometry.ts +30 -0
  119. package/src/visemeScheduler.ts +25 -0
  120. package/src/websocket.ts +86 -7
@@ -0,0 +1,760 @@
1
+ /**
2
+ * Tests for `mapEnrichmentToTimeline` — the Pegasus-enrichment →
3
+ * Helios-scene-timeline mapper.
4
+ *
5
+ * Coverage:
6
+ * - status !== SUCCEEDED → []
7
+ * - missing `result` → []
8
+ * - `result.scenes` empty → []
9
+ * - `result.scenes` with start/end timings → proportional chunk placement (snapshot)
10
+ * - `result.scenes` without timings → even distribution
11
+ * - `result.narrative.acts[].scenes` shape → flattened across acts in order
12
+ * - `result.summary` degenerate case → single beat at chunk 0
13
+ * - totally unknown shape → []
14
+ * - `minChunkSpacing` enforcement — beats too close are dropped
15
+ * - `maxChunks` clamping — >maxChunks beats are clamped + spacing-deduped
16
+ * - `styleHint` is prepended (with single space) and trimmed
17
+ * - Edge cases: single scene, zero-length timings, overlapping ranges
18
+ */
19
+
20
+ import {
21
+ mapEnrichmentToTimeline,
22
+ type MapEnrichmentOptions,
23
+ type TimelineBeat,
24
+ } from '../../reactor/scenesFromEnrichment';
25
+ import type { EnrichmentRecord } from '../../api';
26
+
27
+ const buildEnrichment = (
28
+ overrides: Partial<EnrichmentRecord> = {},
29
+ ): EnrichmentRecord => ({
30
+ enrichmentId: 'enr-1',
31
+ videoId: 'vid-1',
32
+ jobId: 'job-1',
33
+ status: 'SUCCEEDED',
34
+ createdAt: '2026-05-15T00:00:00Z',
35
+ ...overrides,
36
+ });
37
+
38
+ describe('mapEnrichmentToTimeline — guard rails', () => {
39
+ it('returns [] when status is QUEUED', () => {
40
+ const out = mapEnrichmentToTimeline(
41
+ buildEnrichment({
42
+ status: 'QUEUED',
43
+ result: {
44
+ scenes: [{ start: 0, end: 10, description: 'opening' }],
45
+ },
46
+ }),
47
+ );
48
+ expect(out).toEqual([]);
49
+ });
50
+
51
+ it('returns [] when status is RUNNING', () => {
52
+ const out = mapEnrichmentToTimeline(
53
+ buildEnrichment({
54
+ status: 'RUNNING',
55
+ result: {
56
+ scenes: [{ start: 0, end: 10, description: 'opening' }],
57
+ },
58
+ }),
59
+ );
60
+ expect(out).toEqual([]);
61
+ });
62
+
63
+ it('returns [] when status is FAILED', () => {
64
+ const out = mapEnrichmentToTimeline(
65
+ buildEnrichment({
66
+ status: 'FAILED',
67
+ result: {
68
+ scenes: [{ start: 0, end: 10, description: 'opening' }],
69
+ },
70
+ }),
71
+ );
72
+ expect(out).toEqual([]);
73
+ });
74
+
75
+ it('returns [] when result is missing', () => {
76
+ const out = mapEnrichmentToTimeline(buildEnrichment({ result: undefined }));
77
+ expect(out).toEqual([]);
78
+ });
79
+
80
+ it('returns [] when maxChunks is 0', () => {
81
+ const out = mapEnrichmentToTimeline(
82
+ buildEnrichment({
83
+ result: { scenes: [{ start: 0, end: 10, description: 'foo' }] },
84
+ }),
85
+ { maxChunks: 0 },
86
+ );
87
+ expect(out).toEqual([]);
88
+ });
89
+ });
90
+
91
+ describe('mapEnrichmentToTimeline — shape A (scenes array, timed)', () => {
92
+ it('maps a representative happy-path enrichment (snapshot)', () => {
93
+ const out = mapEnrichmentToTimeline(
94
+ buildEnrichment({
95
+ result: {
96
+ scenes: [
97
+ { start: 0, end: 10, description: 'A lion surveys the savanna at dawn', title: 'Sunrise' },
98
+ { start: 10, end: 30, description: 'The lion descends from his rocky outcrop' },
99
+ { start: 30, end: 60, description: 'A butterfly catches his eye and he gives chase' },
100
+ { start: 60, end: 90, description: 'He returns to the pride at sunset' },
101
+ ],
102
+ },
103
+ }),
104
+ );
105
+ expect(out).toMatchSnapshot();
106
+ });
107
+
108
+ it('places the first beat at chunk 0', () => {
109
+ const out = mapEnrichmentToTimeline(
110
+ buildEnrichment({
111
+ result: {
112
+ scenes: [
113
+ { start: 5, end: 15, description: 'first' },
114
+ { start: 50, end: 60, description: 'second' },
115
+ ],
116
+ },
117
+ }),
118
+ );
119
+ expect(out[0].chunk).toBe(0);
120
+ });
121
+
122
+ it('scales chunks proportionally to the source-video span', () => {
123
+ const out = mapEnrichmentToTimeline(
124
+ buildEnrichment({
125
+ result: {
126
+ scenes: [
127
+ { start: 0, end: 10, description: 'start' },
128
+ { start: 50, end: 60, description: 'mid' },
129
+ { start: 100, end: 110, description: 'end' },
130
+ ],
131
+ },
132
+ }),
133
+ { maxChunks: 30, minChunkSpacing: 1 },
134
+ );
135
+ // span = 110, maxChunks = 30. Scaled by start time:
136
+ // scene 0 start=0 → chunk 0
137
+ // scene 1 start=50 → round(50/110 * 29) = 13
138
+ // scene 2 start=100 → round(100/110 * 29) = 26
139
+ expect(out.length).toBe(3);
140
+ expect(out[0].chunk).toBe(0);
141
+ expect(out[1].chunk).toBe(13);
142
+ expect(out[2].chunk).toBe(26);
143
+ expect(out[1].chunk).toBeGreaterThan(out[0].chunk);
144
+ expect(out[1].chunk).toBeLessThan(out[2].chunk);
145
+ });
146
+
147
+ it('exposes source timings on each beat', () => {
148
+ const out = mapEnrichmentToTimeline(
149
+ buildEnrichment({
150
+ result: {
151
+ scenes: [
152
+ { start: 0, end: 10, description: 'first' },
153
+ { start: 20, end: 30, description: 'second' },
154
+ ],
155
+ },
156
+ }),
157
+ );
158
+ expect(out[0].sourceStart).toBe(0);
159
+ expect(out[0].sourceEnd).toBe(10);
160
+ expect(out[1].sourceStart).toBe(20);
161
+ expect(out[1].sourceEnd).toBe(30);
162
+ });
163
+
164
+ it('uses scene.title when present, else "Scene N"', () => {
165
+ const out = mapEnrichmentToTimeline(
166
+ buildEnrichment({
167
+ result: {
168
+ scenes: [
169
+ { start: 0, end: 10, description: 'first', title: 'Opening' },
170
+ { start: 50, end: 60, description: 'second' },
171
+ ],
172
+ },
173
+ }),
174
+ );
175
+ expect(out[0].label).toBe('Opening');
176
+ expect(out[1].label).toBe('Scene 2');
177
+ });
178
+
179
+ it('returns [] when scenes array is empty', () => {
180
+ const out = mapEnrichmentToTimeline(
181
+ buildEnrichment({ result: { scenes: [] } }),
182
+ );
183
+ expect(out).toEqual([]);
184
+ });
185
+
186
+ it('handles a single scene gracefully', () => {
187
+ const out = mapEnrichmentToTimeline(
188
+ buildEnrichment({
189
+ result: { scenes: [{ start: 5, end: 15, description: 'only one' }] },
190
+ }),
191
+ );
192
+ expect(out.length).toBe(1);
193
+ expect(out[0].chunk).toBe(0);
194
+ expect(out[0].prompt).toBe('only one');
195
+ });
196
+
197
+ it('handles zero-span timings by falling back to even distribution', () => {
198
+ const out = mapEnrichmentToTimeline(
199
+ buildEnrichment({
200
+ result: {
201
+ scenes: [
202
+ { start: 5, end: 5, description: 'first' },
203
+ { start: 5, end: 5, description: 'second' },
204
+ { start: 5, end: 5, description: 'third' },
205
+ ],
206
+ },
207
+ }),
208
+ { maxChunks: 30, minChunkSpacing: 1 },
209
+ );
210
+ // Span is 0 → fallback to even distribution
211
+ expect(out.length).toBe(3);
212
+ expect(out[0].chunk).toBe(0);
213
+ expect(out[2].chunk).toBe(29);
214
+ });
215
+
216
+ it('handles overlapping scene ranges without throwing', () => {
217
+ expect(() =>
218
+ mapEnrichmentToTimeline(
219
+ buildEnrichment({
220
+ result: {
221
+ scenes: [
222
+ { start: 0, end: 20, description: 'first' },
223
+ { start: 10, end: 30, description: 'overlapping' },
224
+ { start: 25, end: 50, description: 'overlapping again' },
225
+ ],
226
+ },
227
+ }),
228
+ ),
229
+ ).not.toThrow();
230
+ });
231
+ });
232
+
233
+ describe('mapEnrichmentToTimeline — shape A (scenes array, untimed)', () => {
234
+ it('distributes evenly when no scenes have timings', () => {
235
+ const out = mapEnrichmentToTimeline(
236
+ buildEnrichment({
237
+ result: {
238
+ scenes: [
239
+ { description: 'first' },
240
+ { description: 'second' },
241
+ { description: 'third' },
242
+ { description: 'fourth' },
243
+ ],
244
+ },
245
+ }),
246
+ { maxChunks: 30, minChunkSpacing: 1 },
247
+ );
248
+ expect(out.length).toBe(4);
249
+ expect(out[0].chunk).toBe(0);
250
+ expect(out[3].chunk).toBe(29);
251
+ // Should be roughly evenly spaced — last fraction is 1/3, 2/3, 1
252
+ expect(out[1].chunk).toBe(Math.round((1 / 3) * 29));
253
+ expect(out[2].chunk).toBe(Math.round((2 / 3) * 29));
254
+ });
255
+
256
+ it('omits sourceStart/sourceEnd when scenes have no timings', () => {
257
+ const out = mapEnrichmentToTimeline(
258
+ buildEnrichment({
259
+ result: {
260
+ scenes: [{ description: 'first' }, { description: 'second' }],
261
+ },
262
+ }),
263
+ );
264
+ expect(out[0].sourceStart).toBeUndefined();
265
+ expect(out[0].sourceEnd).toBeUndefined();
266
+ });
267
+ });
268
+
269
+ describe('mapEnrichmentToTimeline — shape B (narrative.acts)', () => {
270
+ it('flattens scenes across acts in order', () => {
271
+ const out = mapEnrichmentToTimeline(
272
+ buildEnrichment({
273
+ result: {
274
+ narrative: {
275
+ acts: [
276
+ {
277
+ scenes: [
278
+ { start: 0, end: 10, description: 'act1-scene1' },
279
+ { start: 10, end: 20, description: 'act1-scene2' },
280
+ ],
281
+ },
282
+ {
283
+ scenes: [
284
+ { start: 20, end: 30, description: 'act2-scene1' },
285
+ ],
286
+ },
287
+ ],
288
+ },
289
+ },
290
+ }),
291
+ { maxChunks: 30, minChunkSpacing: 1 },
292
+ );
293
+ expect(out.length).toBe(3);
294
+ expect(out[0].prompt).toBe('act1-scene1');
295
+ expect(out[1].prompt).toBe('act1-scene2');
296
+ expect(out[2].prompt).toBe('act2-scene1');
297
+ });
298
+
299
+ it('tolerates malformed acts (non-objects, missing scenes)', () => {
300
+ const out = mapEnrichmentToTimeline(
301
+ buildEnrichment({
302
+ result: {
303
+ narrative: {
304
+ acts: [
305
+ null,
306
+ 'not an act',
307
+ { scenes: 'not an array' },
308
+ { scenes: [{ description: 'real scene' }] },
309
+ ],
310
+ },
311
+ },
312
+ }),
313
+ );
314
+ expect(out.length).toBe(1);
315
+ expect(out[0].prompt).toBe('real scene');
316
+ });
317
+ });
318
+
319
+ describe('mapEnrichmentToTimeline — shape C (summary degenerate)', () => {
320
+ it('emits a single beat at chunk 0 for `{summary: "..."}`', () => {
321
+ const out = mapEnrichmentToTimeline(
322
+ buildEnrichment({
323
+ result: { summary: 'A noble jungle king on his rocky throne.' },
324
+ }),
325
+ );
326
+ expect(out.length).toBe(1);
327
+ expect(out[0].chunk).toBe(0);
328
+ expect(out[0].prompt).toBe('A noble jungle king on his rocky throne.');
329
+ expect(out[0].label).toBe('Summary');
330
+ });
331
+
332
+ it('summary degenerate respects styleHint prefix', () => {
333
+ const out = mapEnrichmentToTimeline(
334
+ buildEnrichment({
335
+ result: { summary: 'A king.' },
336
+ }),
337
+ { styleHint: 'cinematic 16:9,' },
338
+ );
339
+ expect(out[0].prompt).toBe('cinematic 16:9, A king.');
340
+ });
341
+ });
342
+
343
+ describe('mapEnrichmentToTimeline — unknown shapes', () => {
344
+ it('returns [] when result is an empty object', () => {
345
+ const out = mapEnrichmentToTimeline(buildEnrichment({ result: {} }));
346
+ expect(out).toEqual([]);
347
+ });
348
+
349
+ it('returns [] when result has unrelated keys', () => {
350
+ const out = mapEnrichmentToTimeline(
351
+ buildEnrichment({
352
+ result: { foo: 'bar', baz: 42 } as Record<string, unknown>,
353
+ }),
354
+ );
355
+ expect(out).toEqual([]);
356
+ });
357
+
358
+ it('returns [] when scenes is not an array', () => {
359
+ const out = mapEnrichmentToTimeline(
360
+ buildEnrichment({
361
+ result: { scenes: 'not an array' } as Record<string, unknown>,
362
+ }),
363
+ );
364
+ // `scenes` was probed and found non-array → falls through to summary check
365
+ expect(out).toEqual([]);
366
+ });
367
+
368
+ it('returns [] when scenes contains only invalid entries', () => {
369
+ const out = mapEnrichmentToTimeline(
370
+ buildEnrichment({
371
+ result: {
372
+ scenes: [null, 'not an object', { description: 42 }, {}],
373
+ } as Record<string, unknown>,
374
+ }),
375
+ );
376
+ expect(out).toEqual([]);
377
+ });
378
+
379
+ it('never throws on garbage `result` shape', () => {
380
+ expect(() =>
381
+ mapEnrichmentToTimeline(
382
+ buildEnrichment({
383
+ result: {
384
+ scenes: [null, { description: null }],
385
+ narrative: 'not an object',
386
+ summary: 42,
387
+ } as Record<string, unknown>,
388
+ }),
389
+ ),
390
+ ).not.toThrow();
391
+ });
392
+ });
393
+
394
+ describe('mapEnrichmentToTimeline — minChunkSpacing', () => {
395
+ it('drops beats that would land closer than minChunkSpacing to the previous beat', () => {
396
+ const out = mapEnrichmentToTimeline(
397
+ buildEnrichment({
398
+ result: {
399
+ scenes: [
400
+ { start: 0, end: 1, description: 'first' },
401
+ { start: 1, end: 2, description: 'too-close-1' },
402
+ { start: 2, end: 3, description: 'too-close-2' },
403
+ { start: 100, end: 101, description: 'far-away' },
404
+ ],
405
+ },
406
+ }),
407
+ { maxChunks: 30, minChunkSpacing: 5 },
408
+ );
409
+ // First beat at 0, "far-away" should be at 29 (last). The two
410
+ // close-by middle beats land near chunk 0 and should be dropped.
411
+ expect(out.length).toBe(2);
412
+ expect(out[0].chunk).toBe(0);
413
+ expect(out[0].prompt).toBe('first');
414
+ expect(out[1].prompt).toBe('far-away');
415
+ expect(out[1].chunk - out[0].chunk).toBeGreaterThanOrEqual(5);
416
+ });
417
+
418
+ it('keeps the first beat even when its raw chunk would be skipped', () => {
419
+ // Even with a minChunkSpacing larger than maxChunks, the first beat
420
+ // is always pinned to chunk 0.
421
+ const out = mapEnrichmentToTimeline(
422
+ buildEnrichment({
423
+ result: {
424
+ scenes: [{ description: 'only one' }, { description: 'second' }],
425
+ },
426
+ }),
427
+ { maxChunks: 5, minChunkSpacing: 100 },
428
+ );
429
+ expect(out.length).toBe(1);
430
+ expect(out[0].chunk).toBe(0);
431
+ });
432
+ });
433
+
434
+ describe('mapEnrichmentToTimeline — maxChunks clamping', () => {
435
+ it('clamps a long scenes array to within maxChunks', () => {
436
+ // 50 scenes, untimed → evenly distributed across maxChunks=10
437
+ const scenes = Array.from({ length: 50 }, (_, i) => ({
438
+ description: `scene ${i}`,
439
+ }));
440
+ const out = mapEnrichmentToTimeline(
441
+ buildEnrichment({ result: { scenes } }),
442
+ { maxChunks: 10, minChunkSpacing: 1 },
443
+ );
444
+ // No beat should exceed maxChunks - 1
445
+ for (const beat of out) {
446
+ expect(beat.chunk).toBeLessThanOrEqual(9);
447
+ expect(beat.chunk).toBeGreaterThanOrEqual(0);
448
+ }
449
+ // Default spacing collapses many adjacent beats to a smaller set
450
+ expect(out.length).toBeLessThanOrEqual(10);
451
+ });
452
+
453
+ it('collapses >30 beats down via minChunkSpacing (default)', () => {
454
+ const scenes = Array.from({ length: 60 }, (_, i) => ({
455
+ description: `scene ${i}`,
456
+ }));
457
+ const out = mapEnrichmentToTimeline(
458
+ buildEnrichment({ result: { scenes } }),
459
+ );
460
+ // With default maxChunks=30 and minChunkSpacing=2, at most ~15 beats
461
+ expect(out.length).toBeLessThanOrEqual(16);
462
+ for (const beat of out) {
463
+ expect(beat.chunk).toBeLessThanOrEqual(29);
464
+ }
465
+ });
466
+
467
+ it('emits monotonically increasing chunks', () => {
468
+ const scenes = Array.from({ length: 10 }, (_, i) => ({
469
+ start: i * 10,
470
+ end: i * 10 + 5,
471
+ description: `scene ${i}`,
472
+ }));
473
+ const out = mapEnrichmentToTimeline(
474
+ buildEnrichment({ result: { scenes } }),
475
+ );
476
+ for (let i = 1; i < out.length; i++) {
477
+ expect(out[i].chunk).toBeGreaterThan(out[i - 1].chunk);
478
+ }
479
+ });
480
+ });
481
+
482
+ describe('mapEnrichmentToTimeline — styleHint', () => {
483
+ const opts = (overrides: Partial<MapEnrichmentOptions> = {}): MapEnrichmentOptions => ({
484
+ maxChunks: 30,
485
+ minChunkSpacing: 1,
486
+ ...overrides,
487
+ });
488
+
489
+ it('prepends styleHint with a single space to each beat prompt', () => {
490
+ const out = mapEnrichmentToTimeline(
491
+ buildEnrichment({
492
+ result: {
493
+ scenes: [
494
+ { start: 0, end: 10, description: 'lion roars' },
495
+ { start: 100, end: 110, description: 'lion sleeps' },
496
+ ],
497
+ },
498
+ }),
499
+ opts({ styleHint: 'cinematic 16:9, golden hour' }),
500
+ );
501
+ expect(out[0].prompt).toBe('cinematic 16:9, golden hour lion roars');
502
+ expect(out[1].prompt).toBe('cinematic 16:9, golden hour lion sleeps');
503
+ });
504
+
505
+ it('trims trailing whitespace on styleHint', () => {
506
+ const out = mapEnrichmentToTimeline(
507
+ buildEnrichment({
508
+ result: { scenes: [{ description: 'a scene' }] },
509
+ }),
510
+ opts({ styleHint: ' cinematic 16:9 ' }),
511
+ );
512
+ expect(out[0].prompt).toBe('cinematic 16:9 a scene');
513
+ });
514
+
515
+ it('omits the prefix when styleHint is empty or whitespace-only', () => {
516
+ const out1 = mapEnrichmentToTimeline(
517
+ buildEnrichment({
518
+ result: { scenes: [{ description: 'a scene' }] },
519
+ }),
520
+ opts({ styleHint: '' }),
521
+ );
522
+ expect(out1[0].prompt).toBe('a scene');
523
+
524
+ const out2 = mapEnrichmentToTimeline(
525
+ buildEnrichment({
526
+ result: { scenes: [{ description: 'a scene' }] },
527
+ }),
528
+ opts({ styleHint: ' ' }),
529
+ );
530
+ expect(out2[0].prompt).toBe('a scene');
531
+ });
532
+
533
+ it('does not append any trailing punctuation', () => {
534
+ const out = mapEnrichmentToTimeline(
535
+ buildEnrichment({
536
+ result: { scenes: [{ description: 'a scene' }] },
537
+ }),
538
+ opts({ styleHint: 'cinematic' }),
539
+ );
540
+ expect(out[0].prompt.endsWith('a scene')).toBe(true);
541
+ expect(out[0].prompt.endsWith('.')).toBe(false);
542
+ expect(out[0].prompt.endsWith(',')).toBe(false);
543
+ });
544
+ });
545
+
546
+ describe('mapEnrichmentToTimeline — TypeScript surface', () => {
547
+ it('returns TimelineBeat[] (type-level smoke test)', () => {
548
+ const out: TimelineBeat[] = mapEnrichmentToTimeline(
549
+ buildEnrichment({
550
+ result: { scenes: [{ description: 'foo' }] },
551
+ }),
552
+ );
553
+ expect(Array.isArray(out)).toBe(true);
554
+ });
555
+ });
556
+
557
+ // PR #192 Codex P1 — regression tests for the production Pegasus shape
558
+ // (`{shots: [{t0, t1, caption, shotIndex}], narrative: {acts: [{act,
559
+ // timeRange, description}]}}`) emitted by
560
+ // `services/enrichment/full_video_pegasus.py`. Earlier versions of the
561
+ // mapper only probed `result.scenes` / `narrative.acts[].scenes`, which
562
+ // the production writer never produces — so auto-populate returned []
563
+ // for every real enrichment.
564
+ describe('mapEnrichmentToTimeline — production Pegasus shape', () => {
565
+ it('maps `shots[]` with `caption` + `t0` / `t1` to TimelineBeats', () => {
566
+ const out = mapEnrichmentToTimeline(
567
+ buildEnrichment({
568
+ result: {
569
+ shots: [
570
+ {
571
+ shotIndex: 1,
572
+ t0: 0,
573
+ t1: 5,
574
+ caption: 'Wide shot of a foggy harbor at dawn',
575
+ },
576
+ {
577
+ shotIndex: 2,
578
+ t0: 5,
579
+ t1: 12,
580
+ caption: 'A man walks down a wooden pier toward a moored boat',
581
+ },
582
+ {
583
+ shotIndex: 3,
584
+ t0: 12,
585
+ t1: 18,
586
+ caption: 'Close-up of his weathered hands gripping the rope',
587
+ },
588
+ ],
589
+ },
590
+ }),
591
+ );
592
+
593
+ expect(out).toHaveLength(3);
594
+ expect(out[0].chunk).toBe(0);
595
+ expect(out[0].prompt).toBe('Wide shot of a foggy harbor at dawn');
596
+ expect(out[0].sourceStart).toBe(0);
597
+ expect(out[0].sourceEnd).toBe(5);
598
+ expect(out[0].label).toBe('Shot 1');
599
+ expect(out[2].label).toBe('Shot 3');
600
+ const chunks = out.map(b => b.chunk);
601
+ expect(new Set(chunks).size).toBe(chunks.length);
602
+ expect(chunks).toEqual([...chunks].sort((a, b) => a - b));
603
+ });
604
+
605
+ it('prefers `shots` over `narrative.acts` when both are present', () => {
606
+ const out = mapEnrichmentToTimeline(
607
+ buildEnrichment({
608
+ result: {
609
+ shots: [
610
+ { shotIndex: 1, t0: 0, t1: 5, caption: 'Shot caption A' },
611
+ { shotIndex: 2, t0: 5, t1: 10, caption: 'Shot caption B' },
612
+ ],
613
+ narrative: {
614
+ summary: 'A summary that should NOT be used.',
615
+ acts: [
616
+ { act: 1, timeRange: '0:00-0:10', description: 'Act description' },
617
+ ],
618
+ },
619
+ },
620
+ }),
621
+ );
622
+
623
+ expect(out).toHaveLength(2);
624
+ expect(out[0].prompt).toBe('Shot caption A');
625
+ expect(out[1].prompt).toBe('Shot caption B');
626
+ expect(out.every(b => !b.label?.startsWith('Act'))).toBe(true);
627
+ });
628
+
629
+ it('falls back to `narrative.acts[]` (flat shape) when `shots` is missing', () => {
630
+ const out = mapEnrichmentToTimeline(
631
+ buildEnrichment({
632
+ result: {
633
+ narrative: {
634
+ acts: [
635
+ {
636
+ act: 1,
637
+ timeRange: '0:00-1:30',
638
+ description: 'Act 1 — the protagonist arrives in the city',
639
+ },
640
+ {
641
+ act: 2,
642
+ timeRange: '1:30-3:00',
643
+ description: 'Act 2 — confrontation at the marketplace',
644
+ },
645
+ ],
646
+ },
647
+ },
648
+ }),
649
+ );
650
+
651
+ expect(out).toHaveLength(2);
652
+ expect(out[0].prompt).toBe(
653
+ 'Act 1 — the protagonist arrives in the city',
654
+ );
655
+ expect(out[0].sourceStart).toBe(0);
656
+ expect(out[0].sourceEnd).toBe(90);
657
+ expect(out[0].label).toBe('Act 1');
658
+ expect(out[1].sourceStart).toBe(90);
659
+ expect(out[1].sourceEnd).toBe(180);
660
+ });
661
+
662
+ it('handles `timeRange` as raw seconds', () => {
663
+ const out = mapEnrichmentToTimeline(
664
+ buildEnrichment({
665
+ result: {
666
+ narrative: {
667
+ acts: [
668
+ { act: 1, timeRange: '0-30', description: 'Opening' },
669
+ { act: 2, timeRange: '30-90', description: 'Middle' },
670
+ ],
671
+ },
672
+ },
673
+ }),
674
+ );
675
+
676
+ expect(out).toHaveLength(2);
677
+ expect(out[0].sourceStart).toBe(0);
678
+ expect(out[0].sourceEnd).toBe(30);
679
+ expect(out[1].sourceStart).toBe(30);
680
+ expect(out[1].sourceEnd).toBe(90);
681
+ });
682
+
683
+ it('falls back to even distribution when `timeRange` cannot be parsed', () => {
684
+ const out = mapEnrichmentToTimeline(
685
+ buildEnrichment({
686
+ result: {
687
+ narrative: {
688
+ acts: [
689
+ { act: 1, timeRange: 'not-a-range', description: 'A' },
690
+ { act: 2, timeRange: 'still-not', description: 'B' },
691
+ { act: 3, timeRange: '', description: 'C' },
692
+ ],
693
+ },
694
+ },
695
+ }),
696
+ );
697
+
698
+ expect(out).toHaveLength(3);
699
+ expect(out[0].chunk).toBe(0);
700
+ expect(out.every(b => b.sourceStart === undefined)).toBe(true);
701
+ });
702
+
703
+ it('still emits beats when `shots[]` entries are caption-only', () => {
704
+ const out = mapEnrichmentToTimeline(
705
+ buildEnrichment({
706
+ result: {
707
+ shots: [
708
+ { caption: 'A first shot' },
709
+ { caption: 'A second shot' },
710
+ ],
711
+ },
712
+ }),
713
+ );
714
+
715
+ expect(out).toHaveLength(2);
716
+ expect(out[0].prompt).toBe('A first shot');
717
+ expect(out.every(b => b.sourceStart === undefined)).toBe(true);
718
+ });
719
+
720
+ it('drops shots with no caption rather than throwing', () => {
721
+ const out = mapEnrichmentToTimeline(
722
+ buildEnrichment({
723
+ result: {
724
+ shots: [
725
+ { shotIndex: 1, t0: 0, t1: 5, caption: 'Has caption' },
726
+ { shotIndex: 2, t0: 5, t1: 10 },
727
+ { shotIndex: 3, t0: 10, t1: 15, caption: '' },
728
+ { shotIndex: 4, t0: 15, t1: 20, caption: 'Also has caption' },
729
+ ],
730
+ },
731
+ }),
732
+ );
733
+
734
+ expect(out).toHaveLength(2);
735
+ expect(out[0].prompt).toBe('Has caption');
736
+ expect(out[1].prompt).toBe('Also has caption');
737
+ });
738
+
739
+ it('back-compat: nested `narrative.acts[].scenes[]` still works', () => {
740
+ const out = mapEnrichmentToTimeline(
741
+ buildEnrichment({
742
+ result: {
743
+ narrative: {
744
+ acts: [
745
+ {
746
+ scenes: [
747
+ { description: 'Nested A' },
748
+ { description: 'Nested B' },
749
+ ],
750
+ },
751
+ ],
752
+ },
753
+ },
754
+ }),
755
+ );
756
+
757
+ expect(out).toHaveLength(2);
758
+ expect(out[0].prompt).toBe('Nested A');
759
+ });
760
+ });