import {
DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER,
EnvironmentId,
ThreadId,
type DesktopPreviewBridge,
} from "@t3tools/contracts";
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
const {
captureScreenshot,
clientSettings,
events,
getDisplayMedia,
registrySet,
requestDisplayMediaCapture,
save,
startScreencast,
stopScreencast,
} = vi.hoisted(() => {
const events: string[] = [];
return {
captureScreenshot: vi.fn<DesktopPreviewBridge["captureScreenshot"]>(),
clientSettings: { browserRecordingFrameRate: 30 as 30 | 60 },
events,
getDisplayMedia: vi.fn(),
requestDisplayMediaCapture: vi.fn((_tabId: string) => undefined),
registrySet: vi.fn((_atom: unknown, value: { readonly tabIds: ReadonlySet<string> }) => {
events.push(
value.tabIds.size === 0 ? "clear" : `publish:${Array.from(value.tabIds).join(",")}`,
);
}),
save: vi.fn(async (tabId: string) => ({
id: "recording-test",
tabId,
path: "/tmp/recording-test.webm",
mimeType: "video/webm" as const,
sizeBytes: 0,
createdAt: "2026-06-26T00:00:00.000Z",
})),
startScreencast: vi.fn<DesktopPreviewBridge["recording"]["startScreencast"]>(async (_tabId) => {
events.push("start-screencast");
}),
stopScreencast: vi.fn(async () => undefined),
};
});
vi.mock(import("~/components/preview/previewBridge"), async (original) => {
vi.stubGlobal("window", {
desktopBridge: {
preview: {
captureScreenshot,
recording: {
onFrame: vi.fn(),
save,
startScreencast: async (tabId: string) => {
const result = await startScreencast(tabId);
if (result === undefined) requestDisplayMediaCapture(tabId);
return result;
},
stopScreencast,
},
},
},
});
try {
return await original();
} finally {
vi.unstubAllGlobals();
}
});
vi.mock("~/rpc/atomRegistry", () => ({
appAtomRegistry: { set: registrySet },
}));
vi.mock("~/hooks/useSettings", () => ({
ensureClientSettingsHydrated: vi.fn(async () => undefined),
getClientSettings: () => clientSettings,
}));
import {
BrowserRecordingConflictError,
findActiveBrowserRecordingRuntimeTabId,
readActiveBrowserRecordingTabIds,
readActiveBrowserRecordingTargets,
startBrowserRecording,
stopBrowserRecording,
} from "./browserRecording";
import { useBrowserSurfaceStore } from "./browserSurfaceStore";
import { previewRuntimeTabId } from "./previewRuntimeTabId";
class FakeMediaRecorder {
static readonly instances: FakeMediaRecorder[] = [];
static supportedTypes = new Set(["video/webm;codecs=vp9"]);
static outputMimeType: string | undefined;
static stopError: unknown;
static isTypeSupported(type: string): boolean {
return this.supportedTypes.has(type);
}
state: RecordingState = "inactive";
readonly mimeType: string;
readonly stream: MediaStream;
readonly options: MediaRecorderOptions | undefined;
private readonly listeners = new Map<string, Set<EventListenerOrEventListenerObject>>();
constructor(stream: MediaStream, options?: MediaRecorderOptions) {
this.stream = stream;
this.options = options;
this.mimeType =
FakeMediaRecorder.outputMimeType ?? options?.mimeType ?? "video/browser-default";
FakeMediaRecorder.instances.push(this);
}
addEventListener(type: string, listener: EventListenerOrEventListenerObject): void {
const listeners = this.listeners.get(type) ?? new Set();
listeners.add(listener);
this.listeners.set(type, listeners);
}
start(): void {
this.state = "recording";
}
stop(): void {
if (FakeMediaRecorder.stopError !== undefined) throw FakeMediaRecorder.stopError;
this.state = "inactive";
for (const listener of this.listeners.get("stop") ?? []) {
if (typeof listener === "function") listener(new Event("stop"));
else listener.handleEvent(new Event("stop"));
}
}
}
describe("browser recording", () => {
let animationFrameCount = 0;
beforeEach(() => {
events.length = 0;
vi.clearAllMocks();
FakeMediaRecorder.instances.length = 0;
FakeMediaRecorder.supportedTypes = new Set(["video/webm;codecs=vp9"]);
FakeMediaRecorder.outputMimeType = undefined;
FakeMediaRecorder.stopError = undefined;
clientSettings.browserRecordingFrameRate = 30;
animationFrameCount = 0;
vi.stubGlobal("window", globalThis);
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
animationFrameCount += 1;
callback(animationFrameCount);
return animationFrameCount;
});
vi.stubGlobal("cancelAnimationFrame", vi.fn());
vi.stubGlobal("MediaRecorder", FakeMediaRecorder as unknown as typeof MediaRecorder);
getDisplayMedia.mockResolvedValue({
getVideoTracks: () => [],
getTracks: () => [{ stop: vi.fn() }],
});
requestDisplayMediaCapture.mockImplementation((tabId: string) => {
const trigger = Reflect.get(globalThis, DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER);
if (typeof trigger !== "function" || trigger(tabId) !== true) {
throw new Error(`No pending display-media capture for ${tabId}.`);
}
});
vi.stubGlobal("navigator", { mediaDevices: { getDisplayMedia } });
useBrowserSurfaceStore.setState({ activityByTabId: {}, byTabId: {} });
});
afterEach(() => {
vi.useRealTimers();
vi.unstubAllGlobals();
});
it("stops the native stream when MediaRecorder cleanup fails", async () => {
const stopTrack = vi.fn();
getDisplayMedia.mockResolvedValueOnce({
getVideoTracks: () => [],
getTracks: () => [{ stop: stopTrack }],
});
await startBrowserRecording("recording-tab");
const cleanupCause = new DOMException("Could not start video source", "NotReadableError");
FakeMediaRecorder.stopError = cleanupCause;
await expect(stopBrowserRecording("recording-tab")).rejects.toMatchObject({
operation: "cleanup",
tabId: "recording-tab",
message: "Browser recording operation cleanup failed for tab recording-tab.",
});
expect(stopTrack).toHaveBeenCalledOnce();
});
it("releases the native capture lease when stream acquisition fails", async () => {
const cause = new Error("capture failed");
cause.name = "NotReadableError";
getDisplayMedia.mockRejectedValueOnce(cause);
await expect(startBrowserRecording("recording-tab")).rejects.toMatchObject({
operation: "capture-media-stream",
tabId: "recording-tab",
cause,
message: "Browser recording operation capture-media-stream failed for tab recording-tab.",
});
expect(stopScreencast).toHaveBeenCalledWith("recording-tab");
expect(events.at(-1)).toBe("clear");
});
it("records separate tabs concurrently", async () => {
const firstThreadRef = {
environmentId: EnvironmentId.make("environment-recording"),
threadId: ThreadId.make("thread-recording-first"),
};
const secondThreadRef = {
environmentId: EnvironmentId.make("environment-recording"),
threadId: ThreadId.make("thread-recording-second"),
};
await Promise.all([
startBrowserRecording("recording-tab", firstThreadRef),
startBrowserRecording("recording-tab-2", secondThreadRef),
]);
expect(startScreencast).toHaveBeenCalledTimes(2);
expect(events).toContain("publish:recording-tab,recording-tab-2");
expect(readActiveBrowserRecordingTabIds()).toEqual(
new Set(["recording-tab", "recording-tab-2"]),
);
expect(readActiveBrowserRecordingTabIds(firstThreadRef)).toEqual(new Set(["recording-tab"]));
expect(readActiveBrowserRecordingTabIds(secondThreadRef)).toEqual(new Set(["recording-tab-2"]));
await stopBrowserRecording("recording-tab");
expect(readActiveBrowserRecordingTabIds()).toEqual(new Set(["recording-tab-2"]));
await stopBrowserRecording("recording-tab-2");
expect(readActiveBrowserRecordingTabIds()).toEqual(new Set());
expect(save).toHaveBeenCalledTimes(2);
});
it("keeps a recording reachable through its runtime id after a server epoch changes", async () => {
const threadRef = {
environmentId: EnvironmentId.make("environment-recording"),
threadId: ThreadId.make("thread-recording-scoped"),
};
const runtimeTabId = previewRuntimeTabId(threadRef, "epoch-a", "tab_1");
await startBrowserRecording(runtimeTabId, threadRef, "tab_1");
expect(startScreencast).toHaveBeenCalledWith(runtimeTabId);
expect(readActiveBrowserRecordingTabIds(threadRef)).toEqual(new Set([runtimeTabId]));
expect(readActiveBrowserRecordingTargets(threadRef)).toEqual([
{ runtimeTabId, serverTabId: "tab_1" },
]);
expect(findActiveBrowserRecordingRuntimeTabId(threadRef, "tab_1")).toBe(runtimeTabId);
const replacementRuntimeTabId = previewRuntimeTabId(threadRef, "epoch-b", "tab_1");
await expect(
startBrowserRecording(replacementRuntimeTabId, threadRef, "tab_1"),
).rejects.toBeInstanceOf(BrowserRecordingConflictError);
expect(startScreencast).toHaveBeenCalledTimes(1);
await stopBrowserRecording(runtimeTabId);
});
});