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
|
import { beforeEach, describe, expect, test } from "bun:test";
import { EditorCore } from "@/core";
import {
AddMediaAssetCommand,
BatchCommand,
InsertElementCommand,
} from "@/commands";
import { buildDefaultScene } from "@/timeline/scenes";
import { buildElementFromMedia } from "@/timeline";
import { mediaTimeFromSeconds, ZERO_MEDIA_TIME, type MediaTime } from "@/wasm";
import { storageService } from "@/services/storage/service";
const RECORDING_DURATION_SECONDS = 12.5;
const RECORDING_DURATION = mediaTimeFromSeconds({
seconds: RECORDING_DURATION_SECONDS,
});
const PLAYHEAD_TIME = mediaTimeFromSeconds({ seconds: 2 });
function seedEditorWithEmptyScene(): EditorCore {
const editor = EditorCore.getInstance();
const scene = buildDefaultScene({ name: "Main scene", isMain: true });
editor.project.setActiveProject({
project: {
metadata: {
id: "project-1",
name: "Test project",
duration: ZERO_MEDIA_TIME,
createdAt: new Date("2026-01-01T00:00:00Z"),
updatedAt: new Date("2026-01-01T00:00:00Z"),
},
scenes: [scene],
currentSceneId: scene.id,
settings: {
fps: { numerator: 30, denominator: 1 },
canvasSize: { width: 1920, height: 1080 },
background: { type: "color", color: "#000000" },
},
version: 1,
},
});
editor.scenes.setScenes({ scenes: [scene], activeSceneId: scene.id });
editor.media.setAssets({ assets: [] });
return editor;
}
/**
* Mirrors the stop-recording branch of the "voice-over" action handler in
* use-editor-actions.ts: one undoable batch that adds the recorded audio as a
* media asset and inserts it at the playhead with automatic audio placement.
*/
function buildVoiceOverBatch({
projectId,
startTime,
}: {
projectId: string;
startTime: MediaTime;
}): { batch: BatchCommand; assetId: string } {
const file = new File([new Blob(["fake-audio"])], "voice-over.webm", {
type: "audio/webm",
});
const addMediaCommand = new AddMediaAssetCommand({
projectId,
asset: {
file,
name: "Voice over",
type: "audio",
url: "blob:voice-over-test",
duration: RECORDING_DURATION_SECONDS,
},
});
const insertCommand = new InsertElementCommand({
element: buildElementFromMedia({
mediaId: addMediaCommand.getAssetId(),
mediaType: "audio",
name: "Voice over",
duration: RECORDING_DURATION,
startTime,
}),
placement: { mode: "auto", trackType: "audio" },
});
return {
batch: new BatchCommand([addMediaCommand, insertCommand]),
assetId: addMediaCommand.getAssetId(),
};
}
describe("voice-over batch command", () => {
beforeEach(() => {
EditorCore.reset();
// IndexedDB is unavailable under bun:test; the command treats a failed
// save as fatal and rolls back the asset, so stub persistence out.
storageService.saveMediaAsset = () => Promise.resolve();
storageService.deleteMediaAsset = () => Promise.resolve();
});
test("adds an audio asset and inserts it on a new audio track at the playhead", () => {
const editor = seedEditorWithEmptyScene();
const { batch, assetId } = buildVoiceOverBatch({
projectId: "project-1",
startTime: PLAYHEAD_TIME,
});
batch.execute();
const assets = editor.media.getAssets();
expect(assets).toHaveLength(1);
expect(assets[0].id).toBe(assetId);
expect(assets[0].type).toBe("audio");
expect(assets[0].name).toBe("Voice over");
expect(assets[0].duration).toBe(RECORDING_DURATION_SECONDS);
const { tracks } = editor.scenes.getActiveScene();
expect(tracks.audio).toHaveLength(1);
expect(tracks.audio[0].elements).toHaveLength(1);
const element = tracks.audio[0].elements[0];
expect(element.type).toBe("audio");
expect(element.startTime).toBe(PLAYHEAD_TIME);
expect(element.duration).toBe(RECORDING_DURATION);
if (element.type === "audio" && element.sourceType === "upload") {
expect(element.mediaId).toBe(assetId);
} else {
throw new Error("expected an upload audio element");
}
});
test("undo removes both the element and the asset as a single step", () => {
const editor = seedEditorWithEmptyScene();
const { batch } = buildVoiceOverBatch({
projectId: "project-1",
startTime: PLAYHEAD_TIME,
});
batch.execute();
batch.undo();
expect(editor.media.getAssets()).toHaveLength(0);
const { tracks } = editor.scenes.getActiveScene();
expect(
tracks.audio.flatMap((track) => track.elements),
).toHaveLength(0);
});
test("redo re-inserts the element and restores the asset", () => {
const editor = seedEditorWithEmptyScene();
const { batch, assetId } = buildVoiceOverBatch({
projectId: "project-1",
startTime: PLAYHEAD_TIME,
});
batch.execute();
batch.undo();
batch.redo();
expect(editor.media.getAssets().map((asset) => asset.id)).toEqual([
assetId,
]);
const { tracks } = editor.scenes.getActiveScene();
const elements = tracks.audio.flatMap((track) => track.elements);
expect(elements).toHaveLength(1);
expect(elements[0].startTime).toBe(PLAYHEAD_TIME);
});
test("reuses an existing audio track when the slot is free", () => {
const editor = seedEditorWithEmptyScene();
const first = buildVoiceOverBatch({
projectId: "project-1",
startTime: PLAYHEAD_TIME,
});
first.batch.execute();
const secondStart = mediaTimeFromSeconds({
seconds: 2 + RECORDING_DURATION_SECONDS + 1,
});
const second = buildVoiceOverBatch({
projectId: "project-1",
startTime: secondStart,
});
second.batch.execute();
const { tracks } = editor.scenes.getActiveScene();
expect(tracks.audio).toHaveLength(1);
expect(tracks.audio[0].elements).toHaveLength(2);
expect(
tracks.audio[0].elements.map((element) => element.startTime),
).toEqual([PLAYHEAD_TIME, secondStart]);
});
});
|