Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): ignore speed tool errors after recording stops

Aditya Garudbb2b5480ad9b
diff --git a/apps/web/__tests__/unit/speed-controller-playback-failure.test.ts b/apps/web/__tests__/unit/speed-controller-playback-failure.test.ts
index c4f8b40a5..5d70d0584 100644
--- a/apps/web/__tests__/unit/speed-controller-playback-failure.test.ts
+++ b/apps/web/__tests__/unit/speed-controller-playback-failure.test.ts
@@ -27,7 +27,9 @@ class Recorder {
}
stop() {
this.state = "inactive";
- this.ondataavailable?.({ data: new Blob(["partial"]) } as BlobEvent);
+ this.ondataavailable?.({
+ data: new Blob(["partial"], { type: "video/mp4" }),
+ } as BlobEvent);
this.onstop?.();
}
}
@@ -35,6 +37,7 @@ class Recorder {
let root: Root;
let container: HTMLDivElement;
let processingVideo: HTMLVideoElement;
+let createdVideos: HTMLVideoElement[];
let revoked: string[];
let createObjectURL: typeof URL.createObjectURL;
let revokeObjectURL: typeof URL.revokeObjectURL;
@@ -53,6 +56,7 @@ beforeEach(() => {
new Error("Playback blocked"),
);
Recorder.instances = [];
+ createdVideos = [];
revoked = [];
stopTrack = vi.fn();
const nativeCreateElement = document.createElement.bind(document);
@@ -64,7 +68,7 @@ beforeEach(() => {
videoWidth: { value: 320 },
videoHeight: { value: 180 },
});
- processingVideo = element;
+ createdVideos.push(element);
}
return element;
});
@@ -111,7 +115,7 @@ afterEach(() => {
vi.unstubAllGlobals();
});
-it("discards a recording when playback fails and allows a retry", async () => {
+const startProcessing = async () => {
await vi.waitFor(() =>
expect(container.querySelector("input[type=file]")).not.toBeNull(),
);
@@ -121,12 +125,21 @@ it("discards a recording when playback fails and allows a retry", async () => {
});
input.dispatchEvent(new Event("change", { bubbles: true }));
await vi.waitFor(() => expect(container.textContent).toContain("clip.mp4"));
- const process = () =>
- Array.from(container.querySelectorAll("button")).find((button) =>
- button.textContent?.includes("Speed Up Video"),
- );
- process()?.click();
+ processVideo();
processingVideo.dispatchEvent(new Event("canplay"));
+};
+
+const processVideo = () => {
+ Array.from(container.querySelectorAll("button"))
+ .find((button) => button.textContent?.includes("Speed Up Video"))
+ ?.click();
+ processingVideo = createdVideos.findLast((video) =>
+ video.src.startsWith("blob:speed-"),
+ ) as HTMLVideoElement;
+};
+
+it("discards a recording when playback fails and allows a retry", async () => {
+ await startProcessing();
await vi.waitFor(() =>
expect(container.textContent).toContain("Playback blocked"),
);
@@ -136,7 +149,7 @@ it("discards a recording when playback fails and allows a retry", async () => {
expect(revoked).toContain("blob:speed-2");
expect(container.textContent).not.toContain("Download 1.5x Video");
vi.mocked(HTMLMediaElement.prototype.play).mockResolvedValueOnce();
- process()?.click();
+ processVideo();
processingVideo.dispatchEvent(new Event("canplay"));
await vi.waitFor(() =>
expect(Recorder.instances[1]?.state).toBe("recording"),
@@ -146,3 +159,41 @@ it("discards a recording when playback fails and allows a retry", async () => {
expect(container.textContent).toContain("Download 1.5x Video"),
);
});
+
+it("discards a recording when video playback reports an error", async () => {
+ vi.mocked(HTMLMediaElement.prototype.play).mockResolvedValue();
+ await startProcessing();
+ expect(Recorder.instances[0]?.state).toBe("recording");
+ processingVideo.dispatchEvent(new Event("error"));
+ await vi.waitFor(() =>
+ expect(container.textContent).toContain("Error loading video"),
+ );
+ expect(Recorder.instances[0]?.state).toBe("inactive");
+ expect(Recorder.instances[0]?.onstop).toBeNull();
+ expect(stopTrack).toHaveBeenCalledOnce();
+ expect(revoked).toContain("blob:speed-2");
+ expect(container.textContent).not.toContain("Download 1.5x Video");
+});
+
+it("keeps the completed recording downloadable after a video error", async () => {
+ vi.mocked(HTMLMediaElement.prototype.play).mockResolvedValue();
+ await startProcessing();
+ processingVideo.dispatchEvent(new Event("ended"));
+ await vi.waitFor(() =>
+ expect(container.textContent).toContain("Download 1.5x Video"),
+ );
+ expect(processingVideo.onerror).toBeNull();
+ processingVideo.dispatchEvent(new Event("error"));
+ let downloadedName = "";
+ const click = vi
+ .spyOn(HTMLAnchorElement.prototype, "click")
+ .mockImplementation(function (this: HTMLAnchorElement) {
+ downloadedName = this.download;
+ });
+ Array.from(container.querySelectorAll("button"))
+ .find((button) => button.textContent?.includes("Download 1.5x Video"))
+ ?.click();
+ expect(click).toHaveBeenCalledOnce();
+ expect(downloadedName).toBe("clip_1.5x.mp4");
+ expect(container.textContent).toContain("Download 1.5x Video");
+});
diff --git a/apps/web/components/tools/SpeedController.tsx b/apps/web/components/tools/SpeedController.tsx
index 5f3594b2f..155b01057 100644
--- a/apps/web/components/tools/SpeedController.tsx
+++ b/apps/web/components/tools/SpeedController.tsx
@@ -275,6 +275,7 @@ export const SpeedController = () => {
};
recorder.onstop = () => {
+ video.onerror = null;
const chunks = recordedChunksRef.current;
const blob = new Blob(chunks, {
type: selectedMimeType.split(";")[0],
@@ -355,6 +356,7 @@ export const SpeedController = () => {
const discardRecording = (err: unknown) => {
clearTimeout(safetyTimeout);
cancelAnimationFrame(animationFrameId);
+ video.onerror = null;
video.onended = null;
recorder.ondataavailable = null;
recorder.onstop = null;