Skip to content

Commit 947dab6

Browse files
committed
fix(timeline): stabilize tied segment ordering
1 parent 3543390 commit 947dab6

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/lib/ai-edition/timeline/virtual-preview.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,28 @@ describe("virtual-preview pure functions", () => {
458458
expect(getRawVirtualStartTime(next!, rawClips)).toBe(10);
459459
});
460460

461+
it("findNextKeptSegment breaks tied raw starts by playback order, not input order", () => {
462+
const earlier: AxcutClip = {
463+
...clips[1],
464+
id: "clip_2_seg1",
465+
timelineStartSec: 10,
466+
timelineEndSec: 12,
467+
};
468+
const later: AxcutClip = {
469+
...clips[1],
470+
id: "clip_2_seg2",
471+
timelineStartSec: 12,
472+
timelineEndSec: 14,
473+
};
474+
475+
for (const playbackClips of [
476+
[earlier, later],
477+
[later, earlier],
478+
]) {
479+
expect(findNextKeptSegment(playbackClips, clips, 5)?.id).toBe("clip_2_seg1");
480+
}
481+
});
482+
461483
describe("findNextKeptSegment never goes backwards", () => {
462484
// A slice from LATE in the recording laid down first, then a slice from early in
463485
// it, cut at source 5–10. Both draw on the same asset, so "later in source time"

src/lib/ai-edition/timeline/virtual-preview.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ export function findNextKeptSegment(
113113
const nextByRawTime = playbackClips
114114
.map((segment) => ({ segment, rawStart: getRawVirtualStartTime(segment, rawClips) }))
115115
.filter(({ rawStart }) => rawStart > currentRawTime + 0.001)
116-
.sort((a, b) => a.rawStart - b.rawStart)[0]?.segment;
116+
.sort(
117+
(a, b) =>
118+
a.rawStart - b.rawStart ||
119+
a.segment.timelineStartSec - b.segment.timelineStartSec ||
120+
a.segment.id.localeCompare(b.segment.id),
121+
)[0]?.segment;
117122
if (nextByRawTime) return nextByRawTime;
118123

119124
if (!activeSourceId || !activeClipId || currentSourceTime === undefined) return undefined;

0 commit comments

Comments
 (0)