diff --git a/electron/ipc/cursorTelemetryLifecycle.test.ts b/electron/ipc/cursorTelemetryLifecycle.test.ts new file mode 100644 index 0000000000..947e424bc6 --- /dev/null +++ b/electron/ipc/cursorTelemetryLifecycle.test.ts @@ -0,0 +1,61 @@ +import fs from "node:fs"; +import { runInNewContext } from "node:vm"; +import { describe, expect, it, vi } from "vitest"; + +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, + 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")); + expect(body.indexOf("finalizeBrowserCursorTelemetry(")).toBeLessThan( + body.indexOf("await writePendingCursorTelemetry(screenVideoPath)"), + ); + expect(body).toContain("payload.cursorPauseRanges"); + expect(body).toContain("payload.durationMs"); + }); +}); diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts index e4bab0f5cb..70a5e1036d 100644 --- a/electron/ipc/handlers.ts +++ b/electron/ipc/handlers.ts @@ -111,14 +111,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, @@ -798,6 +806,7 @@ const MAX_CURSOR_SAMPLES = 60 * 60 * 30; // 1 hour @ 30Hz let cursorRecordingSession: CursorRecordingSession | null = null; let pendingCursorRecordingData: CursorRecordingData | null = null; +let cursorTelemetryRecordingId: number | null = null; let nativeWindowsCaptureProcess: ChildProcessWithoutNullStreams | null = null; let nativeWindowsCaptureOutput = ""; let nativeWindowsCaptureTargetPath: string | null = null; @@ -1179,6 +1188,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 +1402,44 @@ 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 `