diff --git a/crates/compositor/build.rs b/crates/compositor/build.rs index c55d8ee381..f625a2d501 100644 --- a/crates/compositor/build.rs +++ b/crates/compositor/build.rs @@ -1,6 +1,20 @@ use std::env; use std::path::{Path, PathBuf}; +/// Header comments are C/Doxygen source, not implicit Rust examples. Preserve +/// them as text instead of letting indentation turn prose/formulas into doctests. +#[derive(Debug)] +struct FfmpegHeaderDocs; + +impl bindgen::callbacks::ParseCallbacks for FfmpegHeaderDocs { + fn process_comment(&self, comment: &str) -> Option { + // A longer fence also preserves upstream comments containing Markdown. + let longest = comment.split(|c| c != '`').map(str::len).max().unwrap_or(0); + let fence = "`".repeat(3.max(longest + 1)); + Some(format!("{fence}text\n{comment}\n{fence}")) + } +} + fn main() { // La cible réelle est connue via `CARGO_CFG_TARGET_OS` (renseigné par cargo // pour chaque build). `cfg!(target_os = "macos")` est faux ici : build.rs @@ -106,6 +120,7 @@ fn main() { // pinne ses enums/structs pour clang sur macOS, MSVC sur Windows). let mut builder = bindgen::Builder::default() .header(wrapper) + .parse_callbacks(Box::new(FfmpegHeaderDocs)) .clang_arg(format!("-I{}", include_dir)) .allowlist_function("av.*") .allowlist_function("avcodec_.*") diff --git a/crates/compositor/src/d3d_linux.rs b/crates/compositor/src/d3d_linux.rs index 1b11fadb64..a51a884046 100644 --- a/crates/compositor/src/d3d_linux.rs +++ b/crates/compositor/src/d3d_linux.rs @@ -286,12 +286,33 @@ impl Gpu { #[cfg(test)] pub fn shared_for_tests() -> Option<&'static Gpu> { static GPU: OnceLock> = OnceLock::new(); - GPU.get_or_init(|| { - Gpu::create_auto(false) - .map_err(|e| eprintln!("pas d'adaptateur Vulkan ({e:#})")) - .ok() - }) - .as_ref() + let gpu = GPU + .get_or_init(|| { + Gpu::create_auto(false) + .map_err(|e| eprintln!("pas d'adaptateur Vulkan ({e:#})")) + .ok() + }) + .as_ref()?; + // The GLES fallback has one adapter context with a finite lock timeout. + // Concurrent shader tests can time it out even though the device is shared. + // SAFETY: inspect only the backend kind; do not retain/destroy the HAL handle. + if unsafe { + gpu.device + .as_hal::(|d| d.is_some()) + } { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + thread_local! { + // libtest gives each test its own thread. Hold through resource Drop, + // release at thread exit, and allow repeated gpu() calls in that test. + static GUARD: std::cell::OnceCell> = const { + std::cell::OnceCell::new() + }; + } + GUARD.with(|guard| { + guard.get_or_init(|| LOCK.lock().unwrap_or_else(|e| e.into_inner())); + }); + } + Some(gpu) } } @@ -332,6 +353,13 @@ fn any_adapter_exists() -> bool { mod tests { use super::*; + #[test] + fn shared_test_device_can_be_reused_on_the_same_test_thread() { + if let Some(first) = Gpu::shared_for_tests() { + assert!(std::ptr::eq(first, Gpu::shared_for_tests().unwrap())); + } + } + #[test] fn classify_cpu_pour_lavapipe() { let info = wgpu::AdapterInfo { diff --git a/crates/compositor/tests/ffi_header_docs.rs b/crates/compositor/tests/ffi_header_docs.rs new file mode 100644 index 0000000000..fe0d60b8a4 --- /dev/null +++ b/crates/compositor/tests/ffi_header_docs.rs @@ -0,0 +1,71 @@ +//! C-header prose/examples must remain visible docs, not implicit Rust examples. +use std::{ + fs, + process::Command, + time::{SystemTime, UNIX_EPOCH}, +}; + +#[test] +fn generated_header_docs_do_not_disable_real_rust_doctests() { + let bindings = include_str!(concat!(env!("OUT_DIR"), "/ffi.rs")); + let docs = bindings + .lines() + .map(str::trim_start) + .filter(|line| line.starts_with("#[doc = ")) + .collect::>(); + assert!(docs.len() > 100, "keep upstream header documentation"); + assert!(docs + .join("\n") + .contains("Return an informative version string")); + let dir = std::env::temp_dir().join(format!( + "openscreen-ffi-docs-{}-{}", + std::process::id(), + SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + fs::create_dir(&dir).unwrap(); + let fixture = dir.join("foreign_docs.rs"); + fs::write( + &fixture, + format!( + "{}\npub struct ForeignHeader;\n\ + /// A genuine Rust example must still execute.\n\ + /// ```rust\n\ + /// assert_eq!(2 + 2, 4);\n\ + /// ```\n\ + pub struct RustExample;\n", + docs.join("\n") + ), + ) + .unwrap(); + let output = Command::new(std::env::var_os("RUSTDOC").unwrap_or_else(|| "rustdoc".into())) + .args(["--test", "--edition=2021"]) + .arg(&fixture) + .output() + .unwrap(); + let report = format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + assert!(output.status.success(), "{report}"); + assert!(report.contains("1 passed; 0 failed; 0 ignored"), "{report}"); + // A broken *Rust* example must still fail: no global doctest disable/ignore. + let source = fs::read_to_string(&fixture) + .unwrap() + .replace("assert_eq!(2 + 2, 4)", "assert_eq!(2 + 2, 5)"); + fs::write(&fixture, source).unwrap(); + let broken = Command::new(std::env::var_os("RUSTDOC").unwrap_or_else(|| "rustdoc".into())) + .args(["--test", "--edition=2021"]) + .arg(&fixture) + .output() + .unwrap(); + fs::remove_dir_all(&dir).unwrap(); + assert!( + !broken.status.success(), + "real Rust doctests must not be skipped" + ); + assert!(String::from_utf8_lossy(&broken.stdout).contains("1 failed")); +} diff --git a/electron/ipc/browserCursorCapability.test.ts b/electron/ipc/browserCursorCapability.test.ts new file mode 100644 index 0000000000..6709877260 --- /dev/null +++ b/electron/ipc/browserCursorCapability.test.ts @@ -0,0 +1,143 @@ +import fs from "node:fs"; +import path from "node:path"; +import { runInNewContext } from "node:vm"; +import ts from "typescript"; +import { describe, expect, it, vi } from "vitest"; +import { browserCursorCaptureError } from "../../src/lib/browserCursorCapture"; + +const source = fs.readFileSync(new URL("./handlers.ts", import.meta.url), "utf8"); +function execute(code: string, context: object) { + runInNewContext( + ts.transpileModule(code, { + compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None }, + }).outputText, + context, + ); +} +function stateFixture(platform = "linux") { + const start = source.indexOf('ipcMain.handle(\n\t\t"set-recording-state",'); + expect(start).toBeGreaterThan(-1); + const end = source.indexOf("\n\t);", start) + 5; + const handle = vi.fn(); + const context = { + ipcMain: { handle }, + process: { platform }, + browserCursorCaptureError, + isWindowsUpdatePending: () => false, + selectedSource: { name: "Chosen screen" }, + normalizeCursorCaptureMode: (mode: string) => + mode === "system" || mode === "editable-overlay" ? mode : undefined, + setAppRecording: vi.fn(), + startCursorRecording: vi.fn(async () => undefined), + stopCursorRecording: vi.fn(async () => undefined), + }; + execute(source.slice(start, end), context); + return { + context, + invoke: handle.mock.calls[0][1] as ( + ...args: unknown[] + ) => Promise<{ success: boolean; error?: string }>, + }; +} +describe("browser cursor main-process capability boundary", () => { + it.each([ + true, + false, + ])("refuses an editable browser start before reservation/sampler (reserve=%s)", async (reserve) => { + const { context, invoke } = stateFixture(); + expect(await invoke({}, true, 123, "editable-overlay", reserve)).toMatchObject({ + success: false, + error: expect.stringContaining("native Linux capture"), + }); + expect(context.setAppRecording).not.toHaveBeenCalled(); + expect(context.startCursorRecording).not.toHaveBeenCalled(); + expect(context.stopCursorRecording).not.toHaveBeenCalled(); + }); + it("does not silently treat a missing mode as a supported editable take", async () => { + const { context, invoke } = stateFixture(); + expect(await invoke({}, true)).toMatchObject({ success: false }); + expect(context.setAppRecording).not.toHaveBeenCalled(); + }); + it("still allows stop/cleanup regardless of the unsupported requested mode", async () => { + const { context, invoke } = stateFixture(); + expect(await invoke({}, false, 123, "editable-overlay")).toMatchObject({ success: true }); + expect(context.stopCursorRecording).toHaveBeenCalledOnce(); + }); + it.each([ + ["linux", "system"], + ["win32", "editable-overlay"], + ["darwin", "editable-overlay"], + ])("preserves %s %s recording", async (platform, mode) => { + const { context, invoke } = stateFixture(platform); + expect(await invoke({}, true, 123, mode)).toMatchObject({ success: true }); + expect(context.setAppRecording).toHaveBeenCalled(); + if (mode === "system") expect(context.startCursorRecording).not.toHaveBeenCalled(); + else expect(context.startCursorRecording).toHaveBeenCalledWith(123); + }); +}); + +function storeFixture() { + const start = source.indexOf("async function storeRecordedSessionFiles("); + const end = source.indexOf('\n\tipcMain.handle("store-recorded-video"', start); + const context = { + process: { platform: "linux" }, + browserCursorCaptureError, + normalizeCursorCaptureMode: (mode: string) => + mode === "system" || mode === "editable-overlay" ? mode : undefined, + resolveRecordingOutputPath: vi.fn((name: string) => `/rec/${name}`), + finalizeRecordingFile: vi.fn(async () => false), + recordingStreams: new Map(), + stoppedBrowserCursorRecordings: new Map(), + fs: { stat: vi.fn(async () => ({ size: 4096 })), writeFile: vi.fn(async () => undefined) }, + isValidDurationMs: () => false, + pendingCursorRecordingData: { samples: [{ timeMs: 10 }] }, + cursorTelemetryRecordingId: 999, + finalizeBrowserCursorTelemetry: vi.fn((data: unknown) => data), + writePendingCursorTelemetry: vi.fn(async () => undefined), + setCurrentRecordingSessionState: vi.fn(), + currentProjectPath: null, + RECORDINGS_DIR: "/rec", + RECORDING_SESSION_SUFFIX: ".session.json", + path, + registerRecordingMediaLinks: vi.fn(async () => undefined), + storeRecordedSessionFiles: undefined as unknown as ( + payload: object, + ) => Promise<{ success: boolean; error?: string; session?: { cursorCaptureMode: string } }>, + }; + execute(source.slice(start, end), context); + return context; +} +describe("browser store capability boundary", () => { + it.each([ + "editable-overlay", + undefined, + "invalid", + ])("rejects unsupported/ambiguous mode %s before file or ownership changes", async (mode) => { + const c = storeFixture(); + const owned = c.pendingCursorRecordingData; + expect( + await c.storeRecordedSessionFiles({ + screen: { fileName: "screen.webm" }, + cursorCaptureMode: mode, + }), + ).toMatchObject({ success: false, error: expect.stringContaining("--cursor system") }); + expect(c.finalizeRecordingFile).not.toHaveBeenCalled(); + expect(c.fs.writeFile).not.toHaveBeenCalled(); + expect(c.setCurrentRecordingSessionState).not.toHaveBeenCalled(); + expect(c.pendingCursorRecordingData).toBe(owned); + expect(c.cursorTelemetryRecordingId).toBe(999); + }); + it("stores explicit system without attaching or clearing another take's pending telemetry", async () => { + const c = storeFixture(); + const owned = c.pendingCursorRecordingData; + expect( + await c.storeRecordedSessionFiles({ + screen: { fileName: "screen.webm" }, + cursorCaptureMode: "system", + }), + ).toMatchObject({ success: true, session: { cursorCaptureMode: "system" } }); + expect(c.writePendingCursorTelemetry).not.toHaveBeenCalled(); + expect(c.finalizeBrowserCursorTelemetry).not.toHaveBeenCalled(); + expect(c.pendingCursorRecordingData).toBe(owned); + }); +}); diff --git a/electron/ipc/browserCursorOwnership.test.ts b/electron/ipc/browserCursorOwnership.test.ts new file mode 100644 index 0000000000..9abee99700 --- /dev/null +++ b/electron/ipc/browserCursorOwnership.test.ts @@ -0,0 +1,181 @@ +import fs from "node:fs"; +import path from "node:path"; +import { runInNewContext } from "node:vm"; +import ts from "typescript"; +import { describe, expect, it, vi } from "vitest"; +import type { StoreRecordedSessionInput } from "../../src/lib/recordingSession"; +import { finalizeBrowserCursorTelemetry } from "../native-bridge/cursor/recording/cursorPauseRanges"; + +const source = fs.readFileSync(new URL("./handlers.ts", import.meta.url), "utf8"); +function execute(code: string, context: object) { + runInNewContext( + ts.transpileModule(code, { + compilerOptions: { target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.None }, + }).outputText, + context, + ); +} +function data(timeMs: number) { + return { + version: 2 as const, + provider: "none" as const, + assets: [], + samples: [{ timeMs, cx: 0.5, cy: 0.5 }], + }; +} +function fixture() { + const a = data(100); + const b = data(200); + let release!: () => void; + const disk = new Promise((done) => { + release = done; + }); + const context = { + pendingCursorRecordingData: a as ReturnType | null, + cursorTelemetryRecordingId: 7 as number | null, + stoppedBrowserCursorRecordings: new Map([[7, a]]), + fs: { + writeFile: vi.fn(async (_p: string, _b: string, _encoding?: string) => undefined), + stat: vi.fn(async () => ({ size: 2048 })), + }, + normalizeCursorCaptureMode: (mode: string) => mode, + browserCursorCaptureError: () => null, + process: { platform: "win32" }, + resolveRecordingOutputPath: (name: string) => path.join("/recordings", name), + finalizeRecordingFile: vi.fn(async () => { + await disk; + return false; + }), + recordingStreams: new Map(), + isValidDurationMs: () => false, + finalizeBrowserCursorTelemetry, + setCurrentRecordingSessionState: vi.fn(), + currentProjectPath: null, + path, + RECORDINGS_DIR: "/recordings", + RECORDING_SESSION_SUFFIX: ".json", + registerRecordingMediaLinks: vi.fn(), + storeRecordedSessionFiles: undefined as unknown as ( + input: StoreRecordedSessionInput, + ) => Promise, + }; + let start = source.indexOf("async function writePendingCursorTelemetry("); + execute(source.slice(start, source.indexOf("\n}", start) + 2), context); + start = source.indexOf("async function storeRecordedSessionFiles("); + execute(source.slice(start, source.indexOf("\n\t}", start) + 3), context); + const input: StoreRecordedSessionInput = { + screen: { fileName: "recording-7.webm", videoData: new ArrayBuffer(0) }, + createdAt: 7, + cursorCaptureMode: "editable-overlay", + durationMs: 500, + }; + return { + context, + a, + b, + release, + save: (overrides: Partial = {}) => + context.storeRecordedSessionFiles({ ...input, ...overrides }), + }; +} + +describe("browser telemetry belongs to the stopped take", () => { + it.each([ + "started", + "stopped", + ])("saves A's cursor when B has %s while A finalizes", async (state) => { + const { context, a, b, release, save } = fixture(); + const pending = save(); + context.cursorTelemetryRecordingId = 8; + context.pendingCursorRecordingData = state === "started" ? null : b; + if (state === "stopped") context.stoppedBrowserCursorRecordings.set(8, b); + release(); + await pending; + const write = context.fs.writeFile.mock.calls.find(([file]) => file.endsWith(".cursor.json")); + expect(write).toBeDefined(); + expect(JSON.parse(write![1])).toEqual(a); + expect(context.stoppedBrowserCursorRecordings.has(7)).toBe(false); + expect(context.cursorTelemetryRecordingId).toBe(8); + expect(context.pendingCursorRecordingData).toBe(state === "started" ? null : b); + if (state === "stopped") expect(context.stoppedBrowserCursorRecordings.get(8)).toBe(b); + }); + it("never borrows an unmatched take's cursor", async () => { + const { context, a, release, save } = fixture(); + release(); + await save({ createdAt: 9 }); + expect( + context.fs.writeFile.mock.calls.filter(([file]) => file.endsWith(".cursor.json")), + ).toEqual([]); + expect(context.stoppedBrowserCursorRecordings.get(7)).toBe(a); + expect(context.pendingCursorRecordingData).toBe(a); + }); + it("keeps the owned raw samples if the sidecar write fails", async () => { + const { context, a, release, save } = fixture(); + context.fs.writeFile.mockRejectedValueOnce(new Error("disk full")); + release(); + await expect(save()).rejects.toThrow("disk full"); + expect(context.stoppedBrowserCursorRecordings.get(7)).toBe(a); + }); + it("does not write telemetry in deliberate system mode", async () => { + const { context, release, save } = fixture(); + release(); + await save({ cursorCaptureMode: "system" }); + expect( + context.fs.writeFile.mock.calls.filter(([file]) => file.endsWith(".cursor.json")), + ).toEqual([]); + }); + it("retains the stopped browser session by original ID even if another sampler starts", async () => { + const a = data(100); + const b = data(200); + let release!: (value: typeof a) => void; + const session = { + stop: () => + new Promise((done) => { + release = done; + }), + }; + const context = { + cursorRecordingSession: session, + cursorTelemetryRecordingId: 7, + pendingCursorRecordingData: b, + stoppedBrowserCursorRecordings: new Map(), + console, + stopCursorRecording: undefined as unknown as (retain: boolean) => Promise, + }; + const start = source.indexOf("async function stopCursorRecording("); + execute( + source.slice(start, source.indexOf("\nasync function writePendingCursorTelemetry", start)), + context, + ); + const stopped = context.stopCursorRecording(true); + context.cursorRecordingSession = { stop: async () => b }; + context.cursorTelemetryRecordingId = 8; + release(a); + await stopped; + expect(context.stoppedBrowserCursorRecordings.get(7)).toBe(a); + expect(context.pendingCursorRecordingData).toBe(b); + expect(context.cursorTelemetryRecordingId).toBe(8); + }); + it("discards an old retained take without stopping or deleting the newer one", async () => { + const a = data(100); + const b = data(200); + const handle = vi.fn(); + const context = { + ipcMain: { handle }, + cursorTelemetryRecordingId: 8, + pendingCursorRecordingData: b, + stoppedBrowserCursorRecordings: new Map([ + [7, a], + [8, b], + ]), + stopCursorRecording: vi.fn(async () => undefined), + }; + const start = source.indexOf('ipcMain.handle("discard-cursor-telemetry"'); + execute(source.slice(start, source.indexOf("\n\t});", start) + 7), context); + await handle.mock.calls[0][1]({}, 7); + expect(context.stoppedBrowserCursorRecordings.has(7)).toBe(false); + expect(context.stoppedBrowserCursorRecordings.get(8)).toBe(b); + expect(context.stopCursorRecording).not.toHaveBeenCalled(); + expect(context.pendingCursorRecordingData).toBe(b); + }); +}); diff --git a/electron/ipc/cursorTelemetryLifecycle.test.ts b/electron/ipc/cursorTelemetryLifecycle.test.ts new file mode 100644 index 0000000000..99a7342cc9 --- /dev/null +++ b/electron/ipc/cursorTelemetryLifecycle.test.ts @@ -0,0 +1,362 @@ +import fs from "node:fs"; +import path from "node:path"; +import { runInNewContext } from "node:vm"; +import ts from "typescript"; +import { describe, expect, it, vi } from "vitest"; +import { + compactCursorTelemetryPauseRanges, + finalizeBrowserCursorTelemetry, +} from "../native-bridge/cursor/recording/cursorPauseRanges"; + +const source = fs.readFileSync(new URL("./handlers.ts", import.meta.url), "utf8"); + +// Execute the small registration in isolation: importing handlers.ts requires +// the complete Electron runtime, but a source-only assertion misses broken cleanup. +function fixture() { + const start = source.indexOf('ipcMain.handle("discard-cursor-telemetry"'); + expect(start).toBeGreaterThan(-1); + const end = source.indexOf("\n\t});", start) + 7; + const handle = vi.fn(); + const context = { + ipcMain: { handle }, + cursorTelemetryRecordingId: 7 as number | null, + pendingCursorRecordingData: { samples: [1] } as unknown, + stoppedBrowserCursorRecordings: new Map(), + stopCursorRecording: vi.fn(async () => undefined), + }; + runInNewContext(source.slice(start, end).replace("recordingId: number", "recordingId"), context); + return { + context, + discard: handle.mock.calls[0][1] as (event: unknown, id: number) => Promise, + }; +} + +describe("discard cursor telemetry IPC", () => { + it("registers the preload channel and clears only the matching take after stopping", async () => { + const { context, discard } = fixture(); + await discard({}, 7); + expect(context.stopCursorRecording).toHaveBeenCalledTimes(1); + expect(context.pendingCursorRecordingData).toBeNull(); + expect(context.cursorTelemetryRecordingId).toBeNull(); + }); + it("does not clear a newer take if stopping the old sampler yields", async () => { + const { context, discard } = fixture(); + context.stopCursorRecording.mockImplementation(async () => { + context.cursorTelemetryRecordingId = 8; + context.pendingCursorRecordingData = { samples: [2] }; + }); + await discard({}, 7); + expect(context.cursorTelemetryRecordingId).toBe(8); + expect(context.pendingCursorRecordingData).toEqual({ samples: [2] }); + }); + + it.each([6, NaN, Infinity])("ignores stale or invalid discard id %s", async (id) => { + const { context, discard } = fixture(); + await discard({}, id); + expect(context.stopCursorRecording).not.toHaveBeenCalled(); + expect(context.pendingCursorRecordingData).not.toBeNull(); + expect(context.cursorTelemetryRecordingId).toBe(7); + }); + it("compacts and bounds the browser track before publishing its sidecar", () => { + const body = source.slice(source.indexOf("async function storeRecordedSessionFiles")); + const finalize = body.indexOf("finalizeBrowserCursorTelemetry("); + const sidecar = body.indexOf("`${screenVideoPath}.cursor.json`"); + expect(finalize).toBeGreaterThanOrEqual(0); + expect(sidecar).toBeGreaterThan(finalize); + expect(body).toContain("payload.cursorPauseRanges"); + expect(body).toContain("payload.durationMs"); + }); +}); + +function execute(code: string, context: object) { + runInNewContext( + ts.transpileModule(code, { + compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None }, + }).outputText, + context, + ); +} +function writeFixture() { + const start = source.indexOf("async function writePendingCursorTelemetry("); + const end = source.indexOf("\n}", start) + 2; + const context = { + fs: { writeFile: vi.fn(async (_path: string, _body: string, _encoding: string) => undefined) }, + pendingCursorRecordingData: { samples: [{ timeMs: 1 }] }, + cursorTelemetryRecordingId: 7 as number | null, + writePendingCursorTelemetry: undefined as unknown as (path: string) => Promise, + }; + execute(source.slice(start, end), context); + return { context, write: context.writePendingCursorTelemetry }; +} +describe("sidecar write ownership", () => { + it("does not clear a newer take during an in-flight write", async () => { + const { context, write } = writeFixture(); + const old = context.pendingCursorRecordingData; + const newer = { samples: [{ timeMs: 2 }] }; + context.fs.writeFile.mockImplementation(async () => { + context.cursorTelemetryRecordingId = 8; + context.pendingCursorRecordingData = newer; + }); + await write("old.mp4"); + expect(JSON.parse(context.fs.writeFile.mock.calls[0][1])).toEqual(old); + expect(context.pendingCursorRecordingData).toBe(newer); + expect(context.cursorTelemetryRecordingId).toBe(8); + }); + it("clears the completed take", async () => { + const { context, write } = writeFixture(); + await write("old.mp4"); + expect(context.pendingCursorRecordingData).toBeNull(); + expect(context.cursorTelemetryRecordingId).toBeNull(); + }); + it("keeps data available if the write fails", async () => { + const { context, write } = writeFixture(); + const data = context.pendingCursorRecordingData; + context.fs.writeFile.mockRejectedValue(new Error("disk full")); + await expect(write("old.mp4")).rejects.toThrow("disk full"); + expect(context.pendingCursorRecordingData).toBe(data); + expect(context.cursorTelemetryRecordingId).toBe(7); + }); +}); + +describe("native Linux fallback timing", () => { + it("starts the sampler on the helper pause-range clock, not picker or delayed sampler time", async () => { + const start = source.indexOf("async function startCursorRecording("); + const end = source.indexOf("\nasync function stopCursorRecording", start); + const create = vi.fn((_options: { startTimeMs?: number }) => ({ + start: vi.fn(async () => undefined), + })); + const context = { + cursorTelemetryRecordingId: null, + linuxNativeCaptureSession: { cursorStartTimeMs: 1000 }, + isX11PointerFallbackEnabled: () => true, + cursorRecordingSession: null, + pendingCursorRecordingData: null, + createX11PointerRecordingSession: create, + getX11CaptureBounds: () => null, + MAX_CURSOR_SAMPLES: 100, + CURSOR_SAMPLE_INTERVAL_MS: 33, + console: { info: vi.fn() }, + startCursorRecording: undefined as unknown as (id: number) => Promise, + }; + execute(source.slice(start, end), context); + await context.startCursorRecording(100); + expect(create.mock.calls[0][0]).toMatchObject({ startTimeMs: 1000 }); + }); + function fixture(mode = "editable-overlay", helperHasSamples = false) { + const start = source.indexOf('ipcMain.handle("stop-native-linux-recording"'); + const end = source.indexOf("\n\t});", start) + 7; + const handle = vi.fn(); + let finish!: () => void; + const done = new Promise((resolve) => { + finish = resolve; + }); + const cursor = { + version: 2 as const, + provider: "none" as const, + assets: [], + samples: (helperHasSamples ? [42] : []).map((timeMs) => ({ timeMs, cx: 0.5, cy: 0.5 })), + }; + const result = { + path: "/take.mp4", + durationMs: 500, + interrupted: false, + cursor, + cursorPauseRanges: [{ startMs: 200, endMs: 400 }], + }; + const session = { + stop: vi.fn(async () => { + await done; + return result; + }), + discard: vi.fn(), + }; + const context = { + ipcMain: { handle }, + linuxNativeCaptureSession: session, + linuxNativeCaptureRecordingId: 7, + linuxNativeCaptureCursorMode: mode, + pendingCursorRecordingData: null as unknown, + stopCursorRecording: vi.fn(async () => { + context.pendingCursorRecordingData = { + ...cursor, + samples: [100, 200, 300, 400, 500, 700, 701, 1500].map((timeMs) => ({ + timeMs, + cx: 0.5, + cy: 0.5, + })), + }; + }), + fs: { + writeFile: vi.fn(async (_path: string, _body: string) => undefined), + rm: vi.fn(async () => undefined), + }, + compactCursorTelemetryPauseRanges, + finalizeBrowserCursorTelemetry, + path, + RECORDINGS_DIR: "/recordings", + RECORDING_FILE_PREFIX: "recording-", + RECORDING_SESSION_SUFFIX: ".json", + setCurrentRecordingSessionState: vi.fn(), + currentProjectPath: null, + nativeRecordingWarning: null, + mainT: () => "interrupted", + formatDuration: () => "0:00", + registerRecordingMediaLinks: vi.fn(), + console: { info: vi.fn(), error: vi.fn() }, + linuxNativeCaptureSourceLabel: null, + linuxSourceLabel: () => "screen", + setAppRecording: vi.fn(), + }; + execute(source.slice(start, end), context); + return { + context, + session, + finish, + result, + stop: handle.mock.calls[0][1] as (e: unknown, discard?: boolean) => Promise, + }; + } + it("stops the sampler before helper teardown completes and bounds compacted samples to video", async () => { + const { context, finish, stop } = fixture(); + const pending = stop({}); + const stoppedBeforeHelper = context.stopCursorRecording.mock.calls.length; + finish(); + await pending; + const call = context.fs.writeFile.mock.calls.find(([p]) => p.endsWith(".cursor.json")); + expect(call).toBeDefined(); + expect(JSON.parse(call![1]).samples.map((s: { timeMs: number }) => s.timeMs)).toEqual([ + 100, 300, 500, + ]); + expect(stoppedBeforeHelper).toBe(1); + }); + it("bounds interrupted takes and closes a pause still active at stop", async () => { + const { context, finish, stop, result } = fixture(); + result.interrupted = true; + result.cursorPauseRanges = [{ startMs: 200, endMs: 1500 }]; + result.durationMs = 200; + const pending = stop({}); + finish(); + await pending; + const call = context.fs.writeFile.mock.calls.find(([p]) => p.endsWith(".cursor.json")); + expect(JSON.parse(call![1]).samples.map((s: { timeMs: number }) => s.timeMs)).toEqual([100]); + }); + it.each([ + "system", + "editable-overlay", + ])("starts a sampler only for editable native mode (%s)", async (mode) => { + const nativeStart = source.indexOf('ipcMain.handle(\n\t\t"start-native-linux-recording"'); + expect(nativeStart).toBeGreaterThan(-1); + const start = source.indexOf('if (cursorCaptureMode === "editable-overlay")', nativeStart); + const end = source.indexOf("\n\t\t\t\treturn { success: true, recordingId", start); + expect(end).toBeGreaterThan(start); + const startCursorRecording = vi.fn(); + const context = { cursorCaptureMode: mode, startCursorRecording, recordingId: 7 }; + const code = `(async () => {${source.slice(start, end)}})()`; + await runInNewContext( + ts.transpileModule(code, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText, + context, + ); + expect(startCursorRecording).toHaveBeenCalledTimes(mode === "editable-overlay" ? 1 : 0); + }); + + it("prefers helper telemetry without re-compacting it", async () => { + const { context, finish, stop } = fixture("editable-overlay", true); + const pending = stop({}); + finish(); + await pending; + const call = context.fs.writeFile.mock.calls.find(([p]) => p.endsWith(".cursor.json")); + expect(JSON.parse(call![1]).samples.map((s: { timeMs: number }) => s.timeMs)).toEqual([42]); + }); + it("system cursor publishes no editable sidecar", async () => { + const { context, finish, stop } = fixture("system"); + const pending = stop({}); + finish(); + await pending; + expect(context.fs.writeFile.mock.calls.some(([p]) => p.endsWith(".cursor.json"))).toBe(false); + }); + it("discard publishes no sidecar and does not stop the helper for storage", async () => { + const { context, session, stop } = fixture(); + await stop({}, true); + expect(session.discard).toHaveBeenCalledOnce(); + expect(session.stop).not.toHaveBeenCalled(); + expect(context.fs.writeFile).not.toHaveBeenCalled(); + }); +}); + +describe("final browser container cursor boundary", () => { + it.each([ + 1966, + null, + ])("uses actual remux duration, or caller duration when unavailable: %s", async (mediaDuration) => { + const store = source.indexOf("async function storeRecordedSessionFiles"); + const start = source.indexOf( + 'if (cursorCaptureMode !== "system" && cursorDataForTake) {', + store, + ); + const end = source.indexOf( + '\n\t\tif (cursorCaptureMode !== "system" && cursorDataForTake?.samples.length)', + start, + ); + expect(start).toBeGreaterThan(store); + expect(end).toBeGreaterThan(start); + const context = { + cursorCaptureMode: "editable-overlay", + cursorDataForTake: { + version: 2, + provider: "none", + assets: [], + samples: [0, 1951, 1966, 1985].map((timeMs) => ({ timeMs, cx: 0.5, cy: 0.5 })), + }, + payload: { durationMs: 2058, cursorPauseRanges: [] }, + cursorDurationMs: mediaDuration ?? 2058, + screenVideoPath: "/final.webm", + readWebmDurationOnDisk: vi.fn(async () => mediaDuration), + finalizeBrowserCursorTelemetry, + }; + const code = `(async () => {${source.slice(start, end)}})()`; + await runInNewContext( + ts.transpileModule(code, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText, + context, + ); + expect(context.cursorDataForTake.samples.map((s) => s.timeMs)).toEqual( + mediaDuration === null ? [0, 1951, 1966, 1985] : [0, 1951, 1966], + ); + }); +}); + +describe("container repair returns the duration used by cursor finalization", () => { + it.each([ + 1966, + null, + ])("returns measured remux duration or falls back when unknown: %s", async (measured) => { + const start = source.indexOf("async function repairRecordingContainer("); + const end = source.indexOf("\n}", start) + 2; + const context = { + reindexRecordingOnDisk: vi.fn(async () => ({ + reindexed: true, + packets: 1, + streams: 1, + wallS: 0, + })), + readWebmDurationOnDisk: vi.fn(async () => measured), + patchWebmDurationOnDisk: vi.fn(), + console: { info: vi.fn() }, + path, + repairRecordingContainer: undefined as unknown as (p: string, d: number) => Promise, + }; + execute(source.slice(start, end), context); + expect(await context.repairRecordingContainer("/final.webm", 2058)).toBe(measured ?? 2058); + expect(context.patchWebmDurationOnDisk).not.toHaveBeenCalled(); + }); + it("uses repair output only for screen duration, not webcam duration", () => { + const start = source.indexOf("async function storeRecordedSessionFiles"); + const body = source.slice(start); + const screen = body.indexOf("repairRecordingContainer(screenVideoPath"); + expect(body.slice(screen, body.indexOf("if (webcamStreamed", screen))).toContain( + "cursorDurationMs = durationMs", + ); + expect(body.indexOf("await Promise.all(patches)")).toBeLessThan( + body.indexOf("finalizeBrowserCursorTelemetry("), + ); + }); +}); diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts index e4bab0f5cb..909c15eb06 100644 --- a/electron/ipc/handlers.ts +++ b/electron/ipc/handlers.ts @@ -25,6 +25,7 @@ import { isAxcutDocumentFile, parseDocumentFile, } from "../../src/lib/ai-edition/schema"; +import { browserCursorCaptureError } from "../../src/lib/browserCursorCapture"; import type { CliSourcesResult } from "../../src/lib/cliContracts"; import { pickRecordingSource } from "../../src/lib/cliSourcePick"; import { desktopSourceTimeoutMessage } from "../../src/lib/desktopSourceTimeout"; @@ -111,14 +112,22 @@ import { type LinuxCaptureSourceKind, LinuxNativeCaptureSession, } from "../native-bridge/capture/linuxNativeCaptureSession"; -import { compactCursorTelemetryPauseRanges } from "../native-bridge/cursor/recording/cursorPauseRanges"; -import { createCursorRecordingSession } from "../native-bridge/cursor/recording/factory"; +import { + compactCursorTelemetryPauseRanges, + finalizeBrowserCursorTelemetry, +} from "../native-bridge/cursor/recording/cursorPauseRanges"; +import { + createCursorRecordingSession, + createX11PointerRecordingSession, + isX11PointerFallbackEnabled, +} from "../native-bridge/cursor/recording/factory"; import { isMacCursorHelperUnavailable, requestMacCursorAccessibilityAccess, } from "../native-bridge/cursor/recording/macNativeCursorRecordingSession"; import { findLinuxCaptureHelperPath } from "../native-bridge/cursor/recording/pipeWireCursorRecordingSession"; import type { CursorRecordingSession } from "../native-bridge/cursor/recording/session"; +import { resolveX11CaptureBounds } from "../native-bridge/cursor/recording/x11CaptureBounds"; import { toHelperRect } from "../native-bridge/helperCoordinates"; import { isMacPickerSourceId, @@ -167,7 +176,7 @@ import { waitForNativeWindowsCaptureStop, } from "../recording/nativeWindowsCaptureStop"; import { removeEmptyCaptureOutput } from "../recording/removeEmptyCaptureOutput"; -import { patchWebmDurationOnDisk } from "../recording/webm-duration"; +import { patchWebmDurationOnDisk, readWebmDurationOnDisk } from "../recording/webm-duration"; import { reindexRecordingOnDisk } from "../recording/webm-seek-index"; import { describeMacPickerSource, @@ -595,16 +604,17 @@ async function finalizeRecordingFile( * it is the same I/O for a strictly better result. Either way a failure leaves * the original file intact; a recording is never lost to a failed repair. */ -async function repairRecordingContainer(filePath: string, durationMs: number): Promise { +async function repairRecordingContainer(filePath: string, durationMs: number): Promise { const reindexed = await reindexRecordingOnDisk(filePath); if (reindexed.reindexed) { console.info( `[recording] re-muxed ${path.basename(filePath)}: ${reindexed.packets} packets, ` + `${reindexed.streams} streams, ${reindexed.wallS.toFixed(3)}s`, ); - return; + return (await readWebmDurationOnDisk(filePath)) ?? durationMs; } await patchWebmDurationOnDisk(filePath, durationMs); + return durationMs; } async function getApprovedProjectSession( @@ -798,6 +808,10 @@ const MAX_CURSOR_SAMPLES = 60 * 60 * 30; // 1 hour @ 30Hz let cursorRecordingSession: CursorRecordingSession | null = null; let pendingCursorRecordingData: CursorRecordingData | null = null; +// Browser blobs/repairs may finish after another take starts. Never let its +// telemetry overwrite or consume samples retained for an earlier recording. +const stoppedBrowserCursorRecordings = new Map(); +let cursorTelemetryRecordingId: number | null = null; let nativeWindowsCaptureProcess: ChildProcessWithoutNullStreams | null = null; let nativeWindowsCaptureOutput = ""; let nativeWindowsCaptureTargetPath: string | null = null; @@ -1179,6 +1193,25 @@ function isMacWindowCaptureSourceId(sourceId: string | null | undefined) { ); } +// Electron pointer positions and display bounds are DIP; helper origins may +// instead be X11 pixels. Resolve conservatively, never mix those coordinate spaces. +function getX11CaptureBounds() { + const displays = screen.getAllDisplays(); + if (linuxNativeCaptureSession) { + return resolveX11CaptureBounds({ + displays, + sourceKind: linuxNativeCaptureSession.grantedSourceKind, + origin: linuxNativeCaptureSession.grantedCaptureOrigin, + size: linuxNativeCaptureSession.grantedCaptureSize, + }); + } + // The browser path has an explicit desktopCapturer selection, not a portal + // grant. Window geometry cannot be inferred from a monitor id. + const source = selectedSource; + if (!source?.id?.startsWith("screen:") || !source.display_id) return null; + return displays.find((display) => String(display.id) === source.display_id)?.bounds ?? null; +} + function getSelectedSourceBounds() { // A window take is normalised against the rect the helper is recording. That // rect is seeded from the source frame before the first cursor sample and @@ -1374,13 +1407,43 @@ async function resolveDirectShowWebcamClsid(deviceName?: string) { } async function startCursorRecording(recordingId?: number) { + cursorTelemetryRecordingId = recordingId ?? null; // On Linux the native capture helper already produces cursor samples, from // the SAME portal session that produces the pixels. Spawning the cursor-only - // helper alongside it would open a SECOND portal session — and since + // PipeWire helper alongside it would open a SECOND portal session — and since // SelectSources may be called once per session, that means a second picker // in front of the user, for a stream nothing consumes. The double prompt is // precisely what the native path exists to remove. + // + // X11 is the exception: the portal often has no METADATA cursor mode at all, + // so the helper records pixels with an empty cursor track. Sampling the + // pointer through X11 (Electron / xinput) does not talk to the portal, + // so it can run in parallel and fill `