Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion electron/mcp/media-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import { RecordingCompletion } from "./recording-completion";
class FakeRunners {
readonly started: CliRequest[] = [];
recordStarts = true;
/** The `run.done` error a failed record start reports, when `recordStarts` is false. */
recordStartError: string | undefined;
/** The webcam the next take records beside the screen, if any. */
webcam: { path: string; offsetMs: number } | null = null;
results: Partial<Record<CliRequest["kind"], CliDoneResult>> = {};
Expand Down Expand Up @@ -92,7 +94,10 @@ class FakeRunners {
} else if (this.recordStarts) {
queueMicrotask(() => handlers.onLog?.("info", "Recording started"));
} else {
finish({ success: false, error: "Recording did not start within 30s" });
finish({
success: false,
error: this.recordStartError ?? "Recording did not start within 30s",
});
}
return { window, done, close };
}
Expand Down Expand Up @@ -407,6 +412,17 @@ describe("recording", () => {
expect((await call("stopRecording")).isError).toBe(true);
});

it("carries the recorder's own failure reason to the client", async () => {
runners.recordStarts = false;
const reason =
'The ScreenCast portal does not advertise METADATA cursor mode, so real cursor positions cannot be obtained on this session. Retry with "cursor": "system" to record with the system cursor baked in.';
runners.recordStartError = reason;
const started = await call("startRecording", { display: 1 });
expect(started.isError).toBe(true);
expect(textOf(started)).toContain(reason);
expect((await call("stopRecording")).isError).toBe(true);
});

it("saves a take once when two stops arrive together", async () => {
await call("startRecording", { window: "My App" });
const [first, second] = await Promise.all([call("stopRecording"), call("stopRecording")]);
Expand Down
114 changes: 113 additions & 1 deletion src/cli/CliRecordRunner.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// @vitest-environment jsdom
import { render, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { RecordingStartOutcome } from "@/hooks/useScreenRecorder";
import { CliRecordRunner } from "./CliRecordRunner";

const recorder = vi.hoisted(() => ({
recording: false,
saving: false,
startRecordingImmediately: vi.fn(async () => undefined),
startRecordingImmediately: vi.fn(
async (): Promise<RecordingStartOutcome> => ({
kind: "started",
}),
),
toggleRecording: vi.fn(),
setMicrophoneEnabled: vi.fn(),
setMicrophoneDeviceId: vi.fn(),
Expand Down Expand Up @@ -217,4 +222,111 @@ describe("CliRecordRunner", () => {
expect.stringContaining("--window do not apply"),
);
});

describe("the start outcome", () => {
const METADATA_FAILURE =
"The ScreenCast portal does not advertise METADATA cursor mode, so real cursor positions cannot be obtained on this session.";

it("fails a cursor-metadata start immediately, with the retry advice", async () => {
recorder.startRecordingImmediately.mockResolvedValueOnce({
kind: "failed",
message: METADATA_FAILURE,
});
render(<CliRecordRunner />);
await waitFor(() =>
expect(window.electronAPI.cliDone).toHaveBeenCalledWith(
expect.objectContaining({
success: false,
error: expect.stringContaining(`${METADATA_FAILURE} Retry with "cursor": "system"`),
}),
),
);
});

it("reports another failure without the cursor advice", async () => {
recorder.startRecordingImmediately.mockResolvedValueOnce({
kind: "failed",
message: "Native Linux capture failed.",
});
render(<CliRecordRunner />);
await waitFor(() =>
expect(window.electronAPI.cliDone).toHaveBeenCalledWith(
expect.objectContaining({
success: false,
error: expect.not.stringContaining("Retry with"),
}),
),
);
});

it("fails immediately when the picker was dismissed", async () => {
recorder.startRecordingImmediately.mockResolvedValueOnce({ kind: "cancelled" });
render(<CliRecordRunner />);
await waitFor(() =>
expect(window.electronAPI.cliDone).toHaveBeenCalledWith(
expect.objectContaining({
success: false,
error: expect.stringContaining("picker was dismissed"),
}),
),
);
});

it("lets the camera failure win when both it and the start fail", async () => {
vi.mocked(window.electronAPI.cliGetRequest).mockResolvedValueOnce({
...request,
camera: true,
});
recorder.setWebcamEnabled.mockImplementationOnce(async () => {
recorder.webcamEnabled = true;
return true;
});
let answerStart: (outcome: RecordingStartOutcome) => void = () => undefined;
recorder.startRecordingImmediately.mockImplementationOnce(
() =>
new Promise<RecordingStartOutcome>((resolve) => {
answerStart = resolve;
}),
);
try {
const { rerender } = render(<CliRecordRunner />);
await waitFor(() => expect(recorder.startRecordingImmediately).toHaveBeenCalledTimes(1));
// The camera turns back off before the start answers: its error wins, one cliDone.
recorder.webcamEnabled = false;
rerender(<CliRecordRunner />);
await waitFor(() =>
expect(window.electronAPI.cliDone).toHaveBeenCalledWith(
expect.objectContaining({
success: false,
error: expect.stringContaining("camera could not be opened"),
}),
),
);
answerStart({ kind: "failed", message: "Native Linux capture failed." });
await new Promise((resolve) => setTimeout(resolve, 25));
expect(window.electronAPI.cliDone).toHaveBeenCalledTimes(1);
} finally {
recorder.webcamEnabled = false;
recorder.recording = false;
}
});

it("keeps a backstop for a start that reports success but never confirms", async () => {
vi.useFakeTimers();
try {
render(<CliRecordRunner />);
await vi.waitFor(() => expect(recorder.startRecordingImmediately).toHaveBeenCalledTimes(1));
// `recording` never flips: only the backstop can say so.
await vi.advanceTimersByTimeAsync(30_000);
expect(window.electronAPI.cliDone).toHaveBeenCalledWith(
expect.objectContaining({
success: false,
error: expect.stringContaining("never confirmed"),
}),
);
} finally {
vi.useRealTimers();
}
});
});
});
28 changes: 23 additions & 5 deletions src/cli/CliRecordRunner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ function buildDefaultProject(session: {
};
}

/** Advice the caller can act on, for the one failure that names a cursor limitation. The
* matched wording is the PipeWire helper's, typed and stable, so it cannot misfire. */
function withCursorRetryHint(message: string): string {
return /advertise METADATA cursor mode/.test(message)
? `${message} Retry with "cursor": "system" to record with the system cursor baked in.`
: message;
}

export function CliRecordRunner() {
const recorder = useScreenRecorder();
const startedRef = useRef(false);
Expand Down Expand Up @@ -255,15 +263,25 @@ export function CliRecordRunner() {
phaseRef.current = "recording";
void (async () => {
try {
await startRecordingImmediately();
// The hook reports start failures via toast/console, not by
// rejecting — without a deadline a failed start would hang the
// CLI forever.
const outcome = await startRecordingImmediately();
// The recorder already knows why: report its reason now, not a guessing deadline.
if (outcome.kind !== "started") {
// The camera effect may have failed the run already (#1303); its error won.
if (phaseRef.current !== "recording") return;
await fail(
outcome.kind === "cancelled"
? new Error("The screen-share picker was dismissed")
: new Error(withCursorRetryHint(outcome.message)),
);
return;
}
// Started, but `recording` may not have reached this render yet; if it never
// does, that is a wiring defect, not a slow capture.
setTimeout(() => {
if (recordingStartedAtRef.current === null && phaseRef.current === "recording") {
void fail(
new Error(
"Recording did not start within 30s — see stderr for the underlying capture error",
"The recording start reported success but the recording state never confirmed it",
),
);
}
Expand Down
48 changes: 48 additions & 0 deletions src/hooks/useScreenRecorder.startCancel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ vi.mock("./recorderHandle", () => ({

import { toast } from "sonner";
import { createRecorderHandle } from "./recorderHandle";
import type { RecordingStartOutcome } from "./useScreenRecorder";
import { useScreenRecorder } from "./useScreenRecorder";

type ElectronAPI = Window["electronAPI"];
Expand Down Expand Up @@ -208,3 +209,50 @@ describe("a failed native start never leaves the webcam recording", () => {
expect(handle.discard).not.toHaveBeenCalled();
});
});

describe("the immediate start reports its outcome", () => {
const METADATA_FAILURE =
"The ScreenCast portal does not advertise METADATA cursor mode, so real cursor positions cannot be obtained on this session.";

async function startImmediately() {
const view = renderHook(() => useScreenRecorder());
await settle();
let outcome: RecordingStartOutcome | undefined;
await act(async () => {
outcome = await view.result.current.startRecordingImmediately();
});
await settle();
return { view, outcome };
}

it("returns the recorder's own reason when the native start fails", async () => {
stubElectronAPI("linux");
api.startNativeLinuxRecording.mockResolvedValue({ success: false, error: METADATA_FAILURE });

const { view, outcome } = await startImmediately();

expect(outcome).toEqual({ kind: "failed", message: METADATA_FAILURE });
expect(toast.error).toHaveBeenCalledWith(METADATA_FAILURE);
expect(view.result.current.recording).toBe(false);
});

it("returns cancelled, without a toast, when the picker is dismissed", async () => {
stubElectronAPI("linux");
api.startNativeLinuxRecording.mockResolvedValue({ success: false, cancelled: true });

const { view, outcome } = await startImmediately();

expect(outcome).toEqual({ kind: "cancelled" });
expect(toast.error).not.toHaveBeenCalled();
expect(view.result.current.recording).toBe(false);
});

it("returns started when the take comes up", async () => {
stubElectronAPI("linux");

const { view, outcome } = await startImmediately();

expect(outcome).toEqual({ kind: "started" });
expect(view.result.current.recording).toBe(true);
});
});
Loading
Loading