Skip to content

Open source CapCut alternative.

TypeScript

test(cut): keep a decoder stand-in out of later files

David Lee8d8dfa31bb9
diff --git a/site/src/cut/lib/mediaProxy.test.ts b/site/src/cut/lib/mediaProxy.test.ts
index 1efd6dce..d3899ef3 100644
--- a/site/src/cut/lib/mediaProxy.test.ts
+++ b/site/src/cut/lib/mediaProxy.test.ts
@@ -1,4 +1,4 @@
-import { afterEach, describe, expect, mock, test } from "bun:test";
+import { afterAll, afterEach, describe, expect, mock, test } from "bun:test";
import { stubModule } from "@/lib/testing/stubModule";
import type { CutBackend } from "./backend/types";
import type { MediaAsset } from "./types";
@@ -73,9 +73,20 @@ const h264 = (id: string, extra: Partial<MediaAsset> = {}): MediaAsset => ({
/** What the browser answers when asked whether it decodes the proxy's HEVC. */
let hevc = true;
-(globalThis as { VideoDecoder?: unknown }).VideoDecoder = {
+// The suite shares one global scope, so the stand-in comes out after this
+// file: left in, every later file's H.264 probe reads as decodable.
+const decoder = globalThis as { VideoDecoder?: unknown };
+const realDecoder = Object.getOwnPropertyDescriptor(decoder, "VideoDecoder");
+decoder.VideoDecoder = {
isConfigSupported: async () => ({ supported: hevc }),
};
+afterAll(() => {
+ if (realDecoder) {
+ Object.defineProperty(decoder, "VideoDecoder", realDecoder);
+ return;
+ }
+ delete decoder.VideoDecoder;
+});
function backend(kind: CutBackend["kind"], answer: (path: string, body: unknown) => Response) {
const calls: { path: string; body: unknown }[] = [];
diff --git a/site/src/cut/lib/mediaRead.test.ts b/site/src/cut/lib/mediaRead.test.ts
index 6e5dd318..0b66d336 100644
--- a/site/src/cut/lib/mediaRead.test.ts
+++ b/site/src/cut/lib/mediaRead.test.ts
@@ -90,14 +90,19 @@ describe("H.264 in a browser without it", () => {
return new Blob([new Uint8Array(bytes)], { type: "video/mp4" });
};
+ // A page with no WebCodecs of its own: the stubbed codec list is the only
+ // answer, whatever decoder stand-in another test file left on the global.
const asPage = async <T>(run: () => Promise<T>): Promise<T> => {
const doc = Object.getOwnPropertyDescriptor(globalThis, "document");
+ const decoder = Object.getOwnPropertyDescriptor(globalThis, "VideoDecoder");
Object.defineProperty(globalThis, "document", { value: {}, configurable: true });
+ delete (globalThis as { VideoDecoder?: unknown }).VideoDecoder;
try {
return await run();
} finally {
if (doc) Object.defineProperty(globalThis, "document", doc);
else delete (globalThis as { document?: unknown }).document;
+ if (decoder) Object.defineProperty(globalThis, "VideoDecoder", decoder);
}
};