Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(editor): serialise timeline adds, and make two tests prove their claim

Etienne Lescot68fffd3780f6
diff --git a/crates/compositor/src/audio.rs b/crates/compositor/src/audio.rs
index fb6e3da21..54432a559 100644
--- a/crates/compositor/src/audio.rs
+++ b/crates/compositor/src/audio.rs
@@ -1166,14 +1166,38 @@ mod tests {
fn out_of_range_settings_are_clamped_to_the_editor_bounds() {
// A hand-edited project (or a future UI change) must not be able to ask for an
// offset or a gain the sliders cannot display.
- let result = finish_audio(
- planar(&[0.5, 0.5]),
+ //
+ // The buffer has to outlast the clamp for this to mean anything: on a short input
+ // every delay past its length produces silence, so the assertion would hold for a
+ // 500 ms clamp and for no clamp at all. Track a single impulse instead and check
+ // WHERE it lands.
+ let limit_samples = AUDIO_OUTPUT_SAMPLE_RATE as usize / 2; // 500 ms
+ let mut input = vec![0.0f32; limit_samples + 2];
+ input[0] = 0.5;
+ let delayed = finish_audio(
+ planar(&input),
SceneAudio {
offset_ms: 9_999.0,
- gain_db: 99.0,
+ gain_db: 0.0,
+ },
+ );
+ assert_eq!(
+ delayed[0][limit_samples], 0.5,
+ "the impulse must land at 500 ms, not at the requested 9.999 s"
+ );
+ assert_eq!(delayed[0][limit_samples - 1], 0.0);
+ assert_eq!(delayed[0][0], 0.0);
+
+ let advanced = finish_audio(
+ planar(&input),
+ SceneAudio {
+ offset_ms: -9_999.0,
+ gain_db: 0.0,
},
);
- assert_eq!(result[0], vec![0.0, 0.0], "offset clamps to 500 ms, not 10 s");
+ // Advancing by the same clamp drops everything before it; the impulse at sample 0
+ // is gone and nothing is left behind it.
+ assert!(advanced[0].iter().all(|sample| *sample == 0.0));
let quiet = finish_audio(
planar(&[0.5]),
@@ -1184,5 +1208,15 @@ mod tests {
);
let floor = 0.5 * 10.0f32.powf(-12.0 / 20.0);
assert!((quiet[0][0] - floor).abs() < 1e-6);
+
+ let loud = finish_audio(
+ planar(&[0.1]),
+ SceneAudio {
+ offset_ms: 0.0,
+ gain_db: 99.0,
+ },
+ );
+ let ceiling = 0.1 * 10.0f32.powf(12.0 / 20.0);
+ assert!((loud[0][0] - ceiling).abs() < 1e-6);
}
}
diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx
index d942a8a7f..3145cea6d 100644
--- a/src/components/ai-edition/NewEditorShell.tsx
+++ b/src/components/ai-edition/NewEditorShell.tsx
@@ -393,15 +393,38 @@ export function NewEditorShell() {
[setCurrentTime],
);
+ // Same race, same shape of fix as `useSequentialTimelineOps` — see that file's header.
+ // `insertClipAt` is a read-modify-write of the whole document, so two adds in flight at
+ // once both read the pre-insert doc and the second `saveDocument` clobbers the first,
+ // silently dropping a clip. Two adds is one double-click on **Add to timeline** (the
+ // button has no pending state) or two quick drags. It can't route through `apply()`
+ // because inserting a clip is not an AxcutTimelineOperation — it carries its own
+ // background duration probe — so the queue is here, built the same way.
+ //
+ // The append index is read INSIDE the chain for the same reason the doc is: off the
+ // closure, `clips.length` stays frozen at the last render, so the second add lands
+ // before the first instead of after it.
+ const addToTimelineQueueRef = useRef<Promise<unknown>>(Promise.resolve());
const handleDropAsset = useCallback(
- (assetId: string) =>
- tl.insertClipAt(assetId, clips.length).catch((error) => {
+ (assetId: string) => {
+ const queued = addToTimelineQueueRef.current.then(() => {
+ const at = useProjectStore.getState().document?.timeline.clips.length ?? 0;
+ return tl.insertClipAt(assetId, at);
+ });
+ // Swallow on the STORED promise only, so a failed add doesn't poison the queue;
+ // the caller still gets `queued` and can observe the rejection.
+ addToTimelineQueueRef.current = queued.then(
+ () => undefined,
+ () => undefined,
+ );
+ return queued.catch((error) => {
toast.error(te("mediaStage.couldNotAddAsset"), {
description: error instanceof Error ? error.message : String(error),
});
throw error;
- }),
- [tl, clips.length, te],
+ });
+ },
+ [tl, te],
);
// Ref so the 'ended' listener below always sees the latest clips without tearing
diff --git a/src/components/ai-edition/v4/MediaStage.test.ts b/src/components/ai-edition/v4/MediaStage.test.ts
index cb2d79540..ee6f7a0c5 100644
--- a/src/components/ai-edition/v4/MediaStage.test.ts
+++ b/src/components/ai-edition/v4/MediaStage.test.ts
@@ -2,17 +2,32 @@ import { describe, expect, it, vi } from "vitest";
import { addSelectedAssetToTimeline } from "./MediaStage";
describe("addSelectedAssetToTimeline", () => {
- it("reports success after the selected asset is added", async () => {
- const onAdd = vi.fn(async () => undefined);
+ it("reports success only once the selected asset has been added", async () => {
+ // Deferred on purpose: with an immediately-resolved onAdd this test passes whether
+ // onSuccess fires before or after the insertion, which is the one thing it is here
+ // to pin — a success toast for a clip that is not on the timeline yet.
+ let resolveAdd!: () => void;
+ const onAdd = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ resolveAdd = resolve;
+ }),
+ );
const onSuccess = vi.fn();
- await addSelectedAssetToTimeline(
+ const pending = addSelectedAssetToTimeline(
{ id: "asset-7", label: "", originalPath: "/recordings/demo.mp4" },
onAdd,
onSuccess,
);
expect(onAdd).toHaveBeenCalledWith("asset-7");
+ expect(onSuccess).not.toHaveBeenCalled();
+
+ resolveAdd();
+ await pending;
+
+ // Empty label falls back to the basename.
expect(onSuccess).toHaveBeenCalledWith("demo.mp4");
});