forked from getopenscreen/openscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.test.ts
More file actions
1520 lines (1438 loc) · 48.3 KB
/
Copy pathtimeline.test.ts
File metadata and controls
1520 lines (1438 loc) · 48.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, expect, it } from "vitest";
import {
type AxcutClip,
type AxcutDocument,
type AxcutTrimRange,
axcutSchemaVersion,
} from "../schema";
import {
buildTimelineFromIntervals,
duplicateClip,
invertIntervals,
moveClip,
normalizeIntervals,
planTimelineReplacement,
primaryAssetDuration,
rederiveRegionMs,
removeClip,
removeRegion,
replaceTimeline,
resequenceClips,
resolvePlaybackSegments,
restoreFullTimeline,
setClipSourceRange,
subtractInterval,
timelineIntervals,
} from "./timeline";
function makeDoc(overrides: Partial<AxcutDocument> = {}): AxcutDocument {
return {
schemaVersion: axcutSchemaVersion,
project: {
id: "proj_1",
title: "Test",
createdAt: "2026-06-26T10:00:00Z",
updatedAt: "2026-06-26T10:00:00Z",
primaryAssetId: "asset_1",
},
assets: [
{
id: "asset_1",
kind: "video",
label: "screen.mp4",
originalPath: "/tmp/screen.mp4",
durationSec: 60,
cameraTrack: null,
},
],
transcript: null,
transcripts: [],
timeline: {
clips: [],
gaps: [],
trimRanges: [],
muteRanges: [],
speedRanges: [],
captionRanges: [],
},
annotations: [],
zoomRanges: [],
legacyEditor: null,
...overrides,
};
}
describe("timeline pure functions", () => {
describe("normalizeIntervals", () => {
it("sorts and merges overlapping intervals", () => {
const result = normalizeIntervals(100, [
{ startSec: 10, endSec: 20 },
{ startSec: 5, endSec: 15 },
{ startSec: 30, endSec: 40 },
]);
expect(result).toEqual([
{ startSec: 5, endSec: 20 },
{ startSec: 30, endSec: 40 },
]);
});
it("clamps to duration", () => {
const result = normalizeIntervals(50, [{ startSec: -10, endSec: 200 }]);
expect(result).toEqual([{ startSec: 0, endSec: 50 }]);
});
it("drops zero-length intervals", () => {
const result = normalizeIntervals(100, [
{ startSec: 10, endSec: 10 },
{ startSec: 5, endSec: 8 },
]);
expect(result).toEqual([{ startSec: 5, endSec: 8 }]);
});
});
describe("subtractInterval", () => {
it("splits an interval in two when the cut is in the middle", () => {
const result = subtractInterval([{ startSec: 0, endSec: 60 }], { startSec: 20, endSec: 30 });
expect(result).toEqual([
{ startSec: 0, endSec: 20 },
{ startSec: 30, endSec: 60 },
]);
});
it("trims the start when the cut overlaps the beginning", () => {
const result = subtractInterval([{ startSec: 10, endSec: 60 }], { startSec: 0, endSec: 20 });
expect(result).toEqual([{ startSec: 20, endSec: 60 }]);
});
it("returns the original when there is no overlap", () => {
const result = subtractInterval([{ startSec: 0, endSec: 10 }], { startSec: 20, endSec: 30 });
expect(result).toEqual([{ startSec: 0, endSec: 10 }]);
});
});
describe("invertIntervals", () => {
it("produces the complementary cuts", () => {
const cuts = invertIntervals(
[
{ startSec: 0, endSec: 20 },
{ startSec: 30, endSec: 60 },
],
60,
);
expect(cuts).toEqual([{ startSec: 20, endSec: 30 }]);
});
it("produces a full cut when intervals are empty", () => {
expect(invertIntervals([], 60)).toEqual([{ startSec: 0, endSec: 60 }]);
});
});
describe("buildTimelineFromIntervals", () => {
it("assigns sequential timelineStart/End and clip ids", () => {
const clips = buildTimelineFromIntervals(
"asset_1",
[
{ startSec: 0, endSec: 10 },
{ startSec: 20, endSec: 30 },
],
{ origin: "user", reason: "test", transcript: null },
);
expect(clips).toHaveLength(2);
expect(clips[0]).toMatchObject({
id: "clip_1",
sourceStartSec: 0,
sourceEndSec: 10,
timelineStartSec: 0,
timelineEndSec: 10,
});
expect(clips[1]).toMatchObject({
id: "clip_2",
sourceStartSec: 20,
sourceEndSec: 30,
timelineStartSec: 10,
timelineEndSec: 20,
});
});
});
describe("replaceTimeline", () => {
it("rebuilds clips and derives trimRanges from the inverse", () => {
const doc = makeDoc();
const updated = replaceTimeline(
doc,
[
{ startSec: 0, endSec: 20 },
{ startSec: 30, endSec: 60 },
],
"test cut",
);
expect(updated.timeline.clips).toHaveLength(2);
expect(updated.timeline.trimRanges).toHaveLength(1);
expect(updated.timeline.trimRanges[0]).toMatchObject({
startSec: 20,
endSec: 30,
});
});
it("throws when there is no primary asset", () => {
const doc = makeDoc({
assets: [],
project: { id: "p", title: "t", createdAt: "", updatedAt: "", primaryAssetId: undefined },
});
expect(() => replaceTimeline(doc, [], "x")).toThrow();
});
});
// ── D-DESTRUCT ────────────────────────────────────────────────────────────
//
// `replaceTimeline` was destructive in three separate ways, each invisible
// from its return value: it re-minted every id, it merged adjacent intervals
// (so handing it the timeline's OWN intervals collapsed two clips into one),
// it replaced `trimRanges` wholesale, and it re-anchored every modifier from
// its ruler position rather than its content. The document stayed
// schema-valid throughout, which is why nothing caught it.
describe("replaceTimeline is not destructive by default", () => {
/** Two adjacent clips over one asset, a user cut inside the first, and a
* zoom anchored to the second — the shape the workbench measures. */
function twoClipDoc(): AxcutDocument {
return makeDoc({
timeline: {
clips: [
makeClip({ id: "clip_1", sourceStartSec: 0, sourceEndSec: 30, timelineEndSec: 30 }),
makeClip({
id: "clip_2",
sourceStartSec: 30,
sourceEndSec: 60,
timelineStartSec: 30,
timelineEndSec: 60,
reason: "demo",
}),
],
gaps: [],
trimRanges: [makeTrim({ id: "trim_1", startSec: 12, endSec: 17 })],
muteRanges: [],
speedRanges: [],
captionRanges: [],
},
zoomRanges: [
{
id: "zoom_demo",
startMs: 40_000,
endMs: 45_000,
depth: 3,
focus: { cx: 0.5, cy: 0.5 },
clipId: "clip_2",
sourceStartSec: 40,
sourceEndSec: 45,
},
] as unknown as AxcutDocument["zoomRanges"],
});
}
it("keeps both clips and the user's cut when handed the timeline's own intervals", () => {
// The identity call. `normalizeIntervals` merges [0,30] and [30,60] into
// one span, so this used to return a single `clip_1` spanning 0–60 with
// zero trims: nothing asked for, everything changed.
const updated = replaceTimeline(
twoClipDoc(),
[
{ startSec: 0, endSec: 30 },
{ startSec: 30, endSec: 60 },
],
"identity",
"agent",
);
expect(updated.timeline.clips.map((c) => c.id)).toEqual(["clip_1", "clip_2"]);
expect(updated.timeline.trimRanges.map((t) => t.id)).toEqual(["trim_1"]);
expect(updated.timeline.trimRanges[0]).toMatchObject({ startSec: 12, endSec: 17 });
});
it("keeps a preserved clip's origin and label instead of stamping its own", () => {
const updated = replaceTimeline(
twoClipDoc(),
[
{ startSec: 0, endSec: 30 },
{ startSec: 30, endSec: 60 },
],
"rebuilt by the agent",
"agent",
);
expect(updated.timeline.clips[1]).toMatchObject({ origin: "user", reason: "demo" });
});
it("leaves an anchored zoom on its own footage, not on whatever slid under it", () => {
// clip_1 shrinks to 0–25, so clip_2 (kept intact) slides from ruler 30 to
// ruler 25 while its CONTENT is unchanged. Re-anchoring the zoom from its
// RAW ms — what a rebuild used to do to EVERY modifier — reads the ruler
// rather than the content: ruler 40 now falls at source 45, so the zoom
// came back five seconds into footage the user never pointed at, with a
// schema-valid document and nothing said.
const updated = replaceTimeline(
twoClipDoc(),
[
{ startSec: 0, endSec: 25 },
{ startSec: 30, endSec: 60 },
],
"shrink the intro",
"agent",
);
const clip2 = updated.timeline.clips.find((c) => c.id === "clip_2");
expect(clip2).toMatchObject({ timelineStartSec: 25, sourceStartSec: 30 });
expect(updated.zoomRanges[0]).toMatchObject({
id: "zoom_demo",
clipId: "clip_2",
sourceStartSec: 40,
sourceEndSec: 45,
});
// And its derived ms followed its clip: ruler 25 + (40 − 30) = 35 s.
expect(updated.zoomRanges[0]).toMatchObject({ startMs: 35_000, endMs: 40_000 });
});
it("narrows a straddling trim instead of deleting it, and keeps its id", () => {
const updated = replaceTimeline(twoClipDoc(), [{ startSec: 0, endSec: 15 }], "cut", "agent");
const kept = updated.timeline.trimRanges.find((t) => t.id === "trim_1");
expect(kept).toMatchObject({ startSec: 12, endSec: 15 });
// And the stretch beyond the kept interval is cut by the complement.
expect(updated.timeline.trimRanges.some((t) => t.startSec === 15 && t.endSec === 60)).toBe(
true,
);
});
it("never touches another asset's cuts", () => {
// The same bug `operations.ts` had already had to fix for add_trim_range:
// `trimRanges` was replaced in full by the complement of the PRIMARY
// asset's intervals, with no assetId filter anywhere.
const doc = makeDoc({
timeline: {
clips: [],
gaps: [],
trimRanges: [makeTrim({ id: "trim_other", assetId: "asset_2", startSec: 1, endSec: 2 })],
muteRanges: [],
speedRanges: [],
captionRanges: [],
},
});
const updated = replaceTimeline(doc, [{ startSec: 0, endSec: 60 }], "rebuild", "agent");
expect(updated.timeline.trimRanges.map((t) => t.id)).toContain("trim_other");
});
it("mints unique ids for the slots that match nothing", () => {
// Positional `clip_${i+1}` / `trim_${i+1}` are what let an id survive a
// rebuild while designating something else — and they would collide
// outright with a preserved id sitting at the same index.
const updated = replaceTimeline(
twoClipDoc(),
[
{ startSec: 0, endSec: 30 },
{ startSec: 40, endSec: 50 },
],
"cut",
"agent",
);
const ids = updated.timeline.clips.map((c) => c.id);
expect(ids[0]).toBe("clip_1");
expect(ids[1]).not.toBe("clip_2");
expect(new Set(ids).size).toBe(ids.length);
const trimIds = updated.timeline.trimRanges.map((t) => t.id);
expect(new Set(trimIds).size).toBe(trimIds.length);
});
it("still resets everything when the caller opts out — restoreFullTimeline", () => {
// The one caller whose semantics ARE "throw it all away". If preservation
// leaked in here, the Restore button would stop restoring.
const restored = restoreFullTimeline(twoClipDoc());
expect(restored.timeline.clips.map((c) => c.id)).toEqual(["clip_1"]);
expect(restored.timeline.clips[0]).toMatchObject({ sourceStartSec: 0, sourceEndSec: 60 });
expect(restored.timeline.trimRanges).toHaveLength(0);
});
});
// The anchoring contract itself, stated once and checked after each structural
// edit rather than re-derived per case. `timelineMap` holds that a fragment's
// `{clipId, sourceStartSec, sourceEndSec}` is the truth and its `startMs`/
// `endMs` are a CACHE of where that lands on the ruler — so any operation that
// moves clips must leave the two agreeing. Both halves of D-DESTRUCT were
// violations of exactly this: the rebuild recomputed the anchor from the
// cache (backwards), and nothing checked the result.
describe("the anchor/derived-ms invariant survives a structural edit", () => {
/** Every anchored region's cached ms equals what its clip's live position
* says they should be, and no region points at a clip that is gone. */
function assertAnchorsAgree(document: AxcutDocument): void {
const clips = document.timeline.clips;
for (const region of document.zoomRanges as unknown as Array<{
id: string;
startMs: number;
endMs: number;
clipId?: string;
sourceStartSec?: number;
sourceEndSec?: number;
}>) {
if (!region.clipId) continue;
const clip = clips.find((c) => c.id === region.clipId);
expect(clip, `${region.id} anchored to a clip that no longer exists`).toBeDefined();
if (!clip || region.sourceStartSec === undefined || region.sourceEndSec === undefined) {
continue;
}
const offset = clip.timelineStartSec - clip.sourceStartSec;
expect(region.startMs, `${region.id} startMs`).toBe(
Math.round((region.sourceStartSec + offset) * 1000),
);
expect(region.endMs, `${region.id} endMs`).toBe(
Math.round((region.sourceEndSec + offset) * 1000),
);
}
}
/** Three clips, and a zoom straddling the boundary between the last two —
* stored as TWO fragments, which is the case where a reorder can pull the
* halves of one pill apart. */
function straddled(): AxcutDocument {
return makeDoc({
timeline: {
clips: [
makeClip({ id: "clip_1", sourceStartSec: 0, sourceEndSec: 20, timelineEndSec: 20 }),
makeClip({
id: "clip_2",
sourceStartSec: 20,
sourceEndSec: 40,
timelineStartSec: 20,
timelineEndSec: 40,
}),
makeClip({
id: "clip_3",
sourceStartSec: 40,
sourceEndSec: 60,
timelineStartSec: 40,
timelineEndSec: 60,
}),
],
gaps: [],
trimRanges: [],
muteRanges: [],
speedRanges: [],
captionRanges: [],
},
zoomRanges: [
{
id: "zoom_a",
startMs: 35_000,
endMs: 40_000,
depth: 3,
focus: { cx: 0.5, cy: 0.5 },
clipId: "clip_2",
sourceStartSec: 35,
sourceEndSec: 40,
},
{
id: "zoom_b",
startMs: 40_000,
endMs: 45_000,
depth: 3,
focus: { cx: 0.5, cy: 0.5 },
clipId: "clip_3",
sourceStartSec: 40,
sourceEndSec: 45,
},
] as unknown as AxcutDocument["zoomRanges"],
});
}
it("holds after a moveClip", () => {
const moved = moveClip(straddled(), "clip_3", 0, "user", "");
expect(moved.timeline.clips.map((c) => c.id)).toEqual(["clip_3", "clip_1", "clip_2"]);
assertAnchorsAgree(moved);
// Each half of the straddling pill went with its OWN clip, so they are
// no longer adjacent on the ruler — which is correct, and is why
// fragments carry an anchor rather than a shared group marker.
const byId = new Map(moved.zoomRanges.map((z) => [z.id, z]));
expect(byId.get("zoom_b")?.startMs).toBe(0);
expect(byId.get("zoom_a")?.startMs).toBe(55_000);
});
it("holds after a rebuild that keeps every clip", () => {
const rebuilt = replaceTimeline(
straddled(),
[
{ startSec: 0, endSec: 20 },
{ startSec: 20, endSec: 40 },
{ startSec: 40, endSec: 60 },
],
"identity",
"agent",
);
assertAnchorsAgree(rebuilt);
expect(rebuilt.zoomRanges.map((z) => z.id)).toEqual(["zoom_a", "zoom_b"]);
});
it("holds after a rebuild that drops the clip a fragment lived on", () => {
// clip_3 is gone, so `zoom_b` has no content left. It must be DROPPED,
// not re-pointed at whatever now occupies its old ruler position.
const rebuilt = replaceTimeline(
straddled(),
[
{ startSec: 0, endSec: 20 },
{ startSec: 20, endSec: 40 },
],
"drop the tail",
"agent",
);
assertAnchorsAgree(rebuilt);
expect(rebuilt.zoomRanges.map((z) => z.id)).toEqual(["zoom_a"]);
});
it("keeps a region that was never anchored at all", () => {
// The other half of the orphan rule: a v2 migration produces regions
// with RAW ms and no anchor, because anchoring needs a clip with a real
// extent. Dropping "cannot place it" wholesale would delete those.
const doc = straddled();
const unanchored: AxcutDocument = {
...doc,
zoomRanges: [
{
id: "zoom_legacy",
startMs: 55_000,
endMs: 58_000,
depth: 3,
focus: { cx: 0.5, cy: 0.5 },
},
] as unknown as AxcutDocument["zoomRanges"],
};
const rebuilt = replaceTimeline(
unanchored,
[{ startSec: 0, endSec: 20 }],
"drop the tail",
"agent",
);
expect(rebuilt.zoomRanges.map((z) => z.id)).toEqual(["zoom_legacy"]);
});
it("holds after a setClipRange that narrows a fragment's window", () => {
const narrowed = setClipSourceRange(straddled(), "clip_2", 20, 37);
assertAnchorsAgree(narrowed);
// zoom_a covered 35–40 of a window that now ends at 37: clamped, kept.
expect(narrowed.zoomRanges.find((z) => z.id === "zoom_a")).toMatchObject({
sourceStartSec: 35,
sourceEndSec: 37,
});
});
});
describe("planTimelineReplacement", () => {
function twoClipDoc(): AxcutDocument {
return makeDoc({
timeline: {
clips: [
makeClip({ id: "clip_1", sourceStartSec: 0, sourceEndSec: 30, timelineEndSec: 30 }),
makeClip({
id: "clip_2",
sourceStartSec: 30,
sourceEndSec: 60,
timelineStartSec: 30,
timelineEndSec: 60,
}),
],
gaps: [],
trimRanges: [makeTrim({ id: "trim_1", startSec: 12, endSec: 17 })],
muteRanges: [],
speedRanges: [],
captionRanges: [],
},
});
}
it("reads a swap as a reorder — the intent only survives before the sort", () => {
const plan = planTimelineReplacement(twoClipDoc(), [
{ startSec: 30, endSec: 60 },
{ startSec: 0, endSec: 30 },
]);
expect(plan.reorderRequested).toBe(true);
});
it("does not call an ordinary rebuild a reorder", () => {
const plan = planTimelineReplacement(twoClipDoc(), [
{ startSec: 0, endSec: 30 },
{ startSec: 30, endSec: 60 },
]);
expect(plan.reorderRequested).toBe(false);
expect(plan.lostClipIds).toEqual([]);
expect(plan.slots.map((s) => s.keepClipId)).toEqual(["clip_1", "clip_2"]);
});
it("names the clips a narrower rebuild would cost", () => {
const plan = planTimelineReplacement(twoClipDoc(), [{ startSec: 0, endSec: 20 }]);
expect(plan.lostClipIds).toEqual(["clip_1", "clip_2"]);
});
it("reports a trim that falls outside the kept spans as absorbed, not lost", () => {
// Its id goes, its CUT does not: that stretch is excluded anyway. Calling
// it a loss would make the guard refuse a rebuild that costs nothing.
const plan = planTimelineReplacement(twoClipDoc(), [{ startSec: 0, endSec: 10 }]);
expect(plan.absorbedTrimIds).toEqual(["trim_1"]);
expect(plan.clippedTrimIds).toEqual([]);
});
it("counts the modifiers that would be re-anchored onto other footage", () => {
const doc = twoClipDoc();
const withZoom: AxcutDocument = {
...doc,
zoomRanges: [
{
id: "zoom_demo",
startMs: 40_000,
endMs: 45_000,
depth: 3,
focus: { cx: 0.5, cy: 0.5 },
clipId: "clip_2",
sourceStartSec: 40,
sourceEndSec: 45,
},
] as unknown as AxcutDocument["zoomRanges"],
};
expect(
planTimelineReplacement(withZoom, [{ startSec: 0, endSec: 20 }]).slidRegionIds,
).toEqual(["zoom_demo"]);
expect(
planTimelineReplacement(withZoom, [
{ startSec: 0, endSec: 30 },
{ startSec: 30, endSec: 60 },
]).slidRegionIds,
).toEqual([]);
});
});
describe("restoreFullTimeline", () => {
it("sets a single interval spanning the full duration", () => {
const doc = makeDoc({
timeline: {
clips: [],
gaps: [],
trimRanges: [
{ id: "s1", assetId: "asset_1", startSec: 10, endSec: 20, origin: "user", reason: "" },
],
muteRanges: [],
speedRanges: [],
captionRanges: [],
},
});
const restored = restoreFullTimeline(doc);
expect(restored.timeline.clips).toHaveLength(1);
expect(restored.timeline.clips[0]).toMatchObject({
sourceStartSec: 0,
sourceEndSec: 60,
});
expect(restored.timeline.trimRanges).toHaveLength(0);
});
});
describe("primaryAssetDuration + timelineIntervals", () => {
it("reads durationSec from the primary asset", () => {
expect(primaryAssetDuration(makeDoc())).toBe(60);
});
it("extracts intervals from existing clips", () => {
const doc = makeDoc({
timeline: {
clips: [
{
id: "c1",
assetId: "asset_1",
sourceStartSec: 5,
sourceEndSec: 15,
timelineStartSec: 0,
timelineEndSec: 10,
wordRefs: [],
origin: "user",
reason: "",
},
],
gaps: [],
trimRanges: [],
muteRanges: [],
speedRanges: [],
captionRanges: [],
},
});
expect(timelineIntervals(doc)).toEqual([{ startSec: 5, endSec: 15 }]);
});
});
});
function makeClip(overrides: Partial<AxcutClip> = {}): AxcutClip {
return {
id: "clip_a",
assetId: "asset_1",
sourceStartSec: 0,
sourceEndSec: 5,
timelineStartSec: 0,
timelineEndSec: 5,
wordRefs: [],
origin: "user",
reason: "",
...overrides,
};
}
function makeTrim(overrides: Partial<AxcutTrimRange> = {}): AxcutTrimRange {
return {
id: "trim_1",
assetId: "asset_1",
startSec: 0,
endSec: 0,
origin: "user",
reason: "",
...overrides,
};
}
describe("resolvePlaybackSegments", () => {
it("splits a clip around an interior trim into two contiguous segments", () => {
const clip = makeClip({
sourceStartSec: 0,
sourceEndSec: 10,
timelineStartSec: 0,
timelineEndSec: 10,
});
const trim = makeTrim({ startSec: 4, endSec: 6 });
const segments = resolvePlaybackSegments([clip], [trim]);
expect(segments).toHaveLength(2);
expect(segments[0]).toMatchObject({
sourceStartSec: 0,
sourceEndSec: 4,
timelineStartSec: 0,
timelineEndSec: 4,
});
expect(segments[1]).toMatchObject({
sourceStartSec: 6,
sourceEndSec: 10,
timelineStartSec: 4,
timelineEndSec: 8,
});
});
it("leaves a clip untouched when the trim belongs to a different asset", () => {
const clip = makeClip({ assetId: "asset_1", sourceStartSec: 0, sourceEndSec: 10 });
const trim = makeTrim({ assetId: "asset_2", startSec: 2, endSec: 4 });
const segments = resolvePlaybackSegments([clip], [trim]);
expect(segments).toHaveLength(1);
expect(segments[0]).toMatchObject({ sourceStartSec: 0, sourceEndSec: 10 });
});
it("drops a clip entirely when a trim fully covers it", () => {
const clip = makeClip({ sourceStartSec: 0, sourceEndSec: 10 });
const trim = makeTrim({ startSec: 0, endSec: 10 });
expect(resolvePlaybackSegments([clip], [trim])).toHaveLength(0);
});
it("narrows both clips when a trim is ventilated across a clip boundary (two DSL rows)", () => {
// Mirrors ventilateTimelineSpanToTrims's own output shape: one row per covered clip.
const clipA = makeClip({
id: "clip_a",
assetId: "asset_1",
sourceStartSec: 0,
sourceEndSec: 10,
timelineStartSec: 0,
timelineEndSec: 10,
});
const clipB = makeClip({
id: "clip_b",
assetId: "asset_1",
sourceStartSec: 10,
sourceEndSec: 20,
timelineStartSec: 10,
timelineEndSec: 20,
});
const trims = [
makeTrim({ id: "t1", startSec: 8, endSec: 10 }),
makeTrim({ id: "t2", startSec: 10, endSec: 12 }),
];
const segments = resolvePlaybackSegments([clipA, clipB], trims);
expect(segments).toHaveLength(2);
expect(segments[0]).toMatchObject({
sourceStartSec: 0,
sourceEndSec: 8,
timelineStartSec: 0,
timelineEndSec: 8,
});
expect(segments[1]).toMatchObject({
sourceStartSec: 12,
sourceEndSec: 20,
timelineStartSec: 8,
timelineEndSec: 16,
});
});
it("does not let a trim on one clip affect an unrelated same-asset clip elsewhere", () => {
// Regression guard for the exact cross-clip bug just fixed in operations.ts:
// two clips of the SAME asset, non-adjacent source windows; a trim scoped to
// the first must not touch the second.
const clipA = makeClip({
id: "clip_a",
sourceStartSec: 0,
sourceEndSec: 5,
timelineStartSec: 0,
timelineEndSec: 5,
});
const clipB = makeClip({
id: "clip_b",
sourceStartSec: 50,
sourceEndSec: 55,
timelineStartSec: 5,
timelineEndSec: 10,
});
const trim = makeTrim({ startSec: 1, endSec: 2 });
const segments = resolvePlaybackSegments([clipA, clipB], [trim]);
expect(segments.find((s) => s.id === "clip_b")).toMatchObject({
sourceStartSec: 50,
sourceEndSec: 55,
});
});
// Two clips over the SAME media, same source window — the shape the previous test
// deliberately avoided by keeping the windows disjoint. Here `assetId` + source
// overlap cannot tell the clips apart; only `clipId` can.
describe("two clips sharing one asset over the same source window", () => {
const sharedClips = () => [
makeClip({
id: "clip_1",
sourceStartSec: 0,
sourceEndSec: 11.8,
timelineStartSec: 0,
timelineEndSec: 11.8,
}),
makeClip({
id: "clip_2",
sourceStartSec: 0,
sourceEndSec: 11.8,
timelineStartSec: 11.8,
timelineEndSec: 23.6,
}),
];
it("cuts only the anchored clip, leaving its twin whole", () => {
const trim = makeTrim({ id: "t1", clipId: "clip_2", startSec: 3, endSec: 4 });
const segments = resolvePlaybackSegments(sharedClips(), [trim]);
// clip_1 survives as one untouched segment; clip_2 splits around [3,4].
expect(segments).toHaveLength(3);
expect(segments[0]).toMatchObject({ id: "clip_1", sourceStartSec: 0, sourceEndSec: 11.8 });
expect(segments[1]).toMatchObject({
id: "clip_2_seg1",
sourceStartSec: 0,
sourceEndSec: 3,
});
expect(segments[2]).toMatchObject({
id: "clip_2_seg2",
sourceStartSec: 4,
sourceEndSec: 11.8,
});
});
it("still cuts both clips for a pre-v7 trim that names no clip", () => {
// Back-compat: an un-anchored row keeps its historical asset-wide meaning, so a
// document written before the anchor existed renders exactly as it used to.
const trim = makeTrim({ id: "t1", startSec: 3, endSec: 4 });
const segments = resolvePlaybackSegments(sharedClips(), [trim]);
expect(segments.map((s) => s.id)).toEqual([
"clip_1_seg1",
"clip_1_seg2",
"clip_2_seg1",
"clip_2_seg2",
]);
});
});
});
describe("duplicateClip / moveClip", () => {
it("duplicateClip gives the copy a fresh, collision-free id even when called repeatedly", () => {
// Regression test: this used to id the copy as `clip_${clips.length + 1}_copy`,
// a counter that collides across repeated duplicates of a shrinking/growing
// array (e.g. duplicate then delete then duplicate again).
let doc = makeDoc({ timeline: { ...makeDoc().timeline, clips: [makeClip()] } });
doc = duplicateClip(doc, "clip_a");
doc = duplicateClip(doc, "clip_a");
const ids = doc.timeline.clips.map((c) => c.id);
expect(new Set(ids).size).toBe(ids.length);
});
it("duplicateClip inserts the copy immediately after the original", () => {
const doc = makeDoc({
timeline: {
...makeDoc().timeline,
clips: [
makeClip({ id: "clip_a" }),
makeClip({ id: "clip_b", timelineStartSec: 5, timelineEndSec: 10 }),
],
},
});
const next = duplicateClip(doc, "clip_a");
expect(next.timeline.clips.map((c) => c.id)[1]).not.toBe("clip_b");
expect(next.timeline.clips[0].id).toBe("clip_a");
expect(next.timeline.clips[2].id).toBe("clip_b");
});
// A trim used to reach the copy for free, by matching on `assetId`. Now that it names
// its clip the copy has to be given its own, or duplicating a cut clip would silently
// produce an uncut one.
it("duplicateClip copies the original's anchored trims onto the copy, independently", () => {
const doc = makeDoc({
timeline: {
...makeDoc().timeline,
clips: [makeClip({ id: "clip_a", sourceStartSec: 0, sourceEndSec: 10 })],
trimRanges: [makeTrim({ id: "t1", clipId: "clip_a", startSec: 2, endSec: 4 })],
},
});
const next = duplicateClip(doc, "clip_a");
const copyId = next.timeline.clips[1].id;
expect(next.timeline.trimRanges).toHaveLength(2);
const copied = next.timeline.trimRanges.find((t) => t.clipId === copyId);
expect(copied).toMatchObject({ startSec: 2, endSec: 4 });
// Fresh id — a shared one would make the two cuts one row again.
expect(copied?.id).not.toBe("t1");
// Both clips are cut, exactly as before the anchor existed.
expect(resolvePlaybackSegments(next.timeline.clips, next.timeline.trimRanges)).toHaveLength(4);
});
it("removeClip drops the deleted clip's trims but keeps a twin's", () => {
const doc = makeDoc({
timeline: {
...makeDoc().timeline,
clips: [
makeClip({ id: "clip_1", sourceStartSec: 0, sourceEndSec: 10 }),
makeClip({
id: "clip_2",
sourceStartSec: 0,
sourceEndSec: 10,
timelineStartSec: 10,
timelineEndSec: 20,
}),
],
trimRanges: [
makeTrim({ id: "t1", clipId: "clip_1", startSec: 2, endSec: 4 }),
makeTrim({ id: "t2", clipId: "clip_2", startSec: 6, endSec: 8 }),
makeTrim({ id: "legacy", startSec: 1, endSec: 2 }),
],
},
});
const next = removeClip(doc, "clip_2");
// `t2`'s content is gone with its clip; an asset-wide match would have moved it
// onto the surviving twin, which is the wrong-clip class this whole change removes.
expect(next.timeline.trimRanges.map((t) => t.id)).toEqual(["t1", "legacy"]);
});
it("moveClip reorders clips", () => {
const doc = makeDoc({
timeline: {
...makeDoc().timeline,
clips: [
makeClip({ id: "clip_a" }),
makeClip({ id: "clip_b", timelineStartSec: 5, timelineEndSec: 10 }),
],
},
});
const next = moveClip(doc, "clip_a", 1);
expect(next.timeline.clips.map((c) => c.id)).toEqual(["clip_b", "clip_a"]);
});
it("throws for an unknown clip id", () => {
const doc = makeDoc({ timeline: { ...makeDoc().timeline, clips: [makeClip()] } });
expect(() => duplicateClip(doc, "missing")).toThrow();
expect(() => moveClip(doc, "missing", 0)).toThrow();
});
it("carries zoom/annotation/speed regions along with the clip they sit on", () => {
// clip_a tl 0-10, clip_b tl 10-20. A zoom (tl 12-14), an annotation
// (tl 15-16) and a speed region (tl 11-13) all sit over clip_b.
const doc = makeDoc({
timeline: {
...makeDoc().timeline,
clips: [
makeClip({ id: "clip_a", timelineStartSec: 0, timelineEndSec: 10 }),
makeClip({
id: "clip_b",
sourceStartSec: 20,
sourceEndSec: 30,
timelineStartSec: 10,
timelineEndSec: 20,
}),
],
},
zoomRanges: [
{
id: "z1",
clipId: "clip_b",
sourceStartSec: 22,
sourceEndSec: 24,
startMs: 12000,
endMs: 14000,
depth: 3,
focus: { cx: 0.5, cy: 0.5 },
},
],
annotations: [
{
id: "a1",
groupId: "a1",
clipId: "clip_b",
sourceStartSec: 25,
sourceEndSec: 26,
startMs: 15000,
endMs: 16000,
type: "text",
content: "hi",
position: { x: 50, y: 50 },
size: { width: 30, height: 20 },
style: {
color: "#fff",
backgroundColor: "transparent",
fontSize: 32,
fontFamily: "Inter",
fontWeight: "bold",
fontStyle: "normal",
textDecoration: "none",
textAlign: "center",
textAnimation: "none",
},
zIndex: 1,
},
] as unknown as AxcutDocument["annotations"],
legacyEditor: {
speedRegions: [
{
id: "s1",
groupId: "s1",