Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(settings): fail stale CLI sources and keep HUD stop/save in sync

超超超超超级喜欢你的达妮娅1df32feda8b9
diff --git a/src/cli/CliRecordRunner.test.tsx b/src/cli/CliRecordRunner.test.tsx
new file mode 100644
index 000000000..3e01a0d47
--- /dev/null
+++ b/src/cli/CliRecordRunner.test.tsx
@@ -0,0 +1,84 @@
+// @vitest-environment jsdom
+import { render, waitFor } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { CliRecordRunner } from "./CliRecordRunner";
+
+const recorder = vi.hoisted(() => ({
+ recording: false,
+ saving: false,
+ startRecordingImmediately: vi.fn(async () => undefined),
+ toggleRecording: vi.fn(),
+ setMicrophoneEnabled: vi.fn(),
+ setMicrophoneDeviceId: vi.fn(),
+ setMicrophoneDeviceName: vi.fn(),
+ setSystemAudioEnabled: vi.fn(),
+ setCursorCaptureMode: vi.fn(),
+ setWebcamEnabled: vi.fn(async () => false),
+ setWebcamDeviceId: vi.fn(),
+ setWebcamDeviceName: vi.fn(),
+ recordingPrefsLoaded: true,
+ microphoneEnabled: false,
+ webcamEnabled: false,
+ systemAudioEnabled: false,
+ cursorCaptureMode: "editable-overlay" as const,
+}));
+
+vi.mock("@/hooks/useScreenRecorder", () => ({
+ useScreenRecorder: () => recorder,
+}));
+
+const screenSource = {
+ id: "screen:gone",
+ name: "Display 1",
+ display_id: "1",
+ thumbnail: null,
+ appIcon: null,
+} satisfies ProcessedDesktopSource;
+
+const request = {
+ kind: "record" as const,
+ displayIndex: 0,
+ windowTitle: null,
+ mic: false,
+ micDevice: null,
+ systemAudio: false,
+ cursorMode: "editable-overlay" as const,
+ durationMs: null,
+ projectOut: null,
+};
+
+describe("CliRecordRunner", () => {
+ beforeEach(() => {
+ recorder.startRecordingImmediately.mockClear();
+ recorder.toggleRecording.mockClear();
+ window.electronAPI = {
+ cliGetRequest: vi.fn(async () => request),
+ cliLog: vi.fn(),
+ cliDone: vi.fn(async () => undefined),
+ onCliStopRecording: vi.fn(() => () => undefined),
+ getCurrentRecordingSession: vi.fn(async () => ({ success: false, session: null })),
+ getSources: vi.fn(async () => [screenSource]),
+ selectSource: vi.fn(async () => screenSource),
+ } as unknown as typeof window.electronAPI;
+ });
+
+ it("fails immediately when selectSource returns null", async () => {
+ vi.mocked(window.electronAPI.selectSource).mockResolvedValueOnce(null);
+ render(<CliRecordRunner />);
+ await waitFor(() => expect(window.electronAPI.cliDone).toHaveBeenCalled());
+ expect(window.electronAPI.cliDone).toHaveBeenCalledWith(
+ expect.objectContaining({
+ success: false,
+ error: expect.stringContaining("openscreen sources"),
+ }),
+ );
+ expect(recorder.startRecordingImmediately).not.toHaveBeenCalled();
+ expect(recorder.toggleRecording).not.toHaveBeenCalled();
+ });
+
+ it("starts recording when the selected source is still available", async () => {
+ render(<CliRecordRunner />);
+ await waitFor(() => expect(recorder.startRecordingImmediately).toHaveBeenCalledTimes(1));
+ expect(window.electronAPI.cliDone).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/cli/CliRecordRunner.tsx b/src/cli/CliRecordRunner.tsx
index a3c232472..be04ab043 100644
--- a/src/cli/CliRecordRunner.tsx
+++ b/src/cli/CliRecordRunner.tsx
@@ -163,8 +163,14 @@ export function CliRecordRunner() {
requestRef.current = request;
const source = await pickSource(request);
- await window.electronAPI.selectSource(source, { persist: false });
- window.electronAPI.cliLog("info", `Recording source: ${source.name}`);
+ const selected = await window.electronAPI.selectSource(source, { persist: false });
+ if (!selected) {
+ throw new Error(
+ `Recording source "${source.name}" (${source.id}) is no longer available. ` +
+ "Re-list sources with `openscreen sources` and pick one that is currently shared.",
+ );
+ }
+ window.electronAPI.cliLog("info", `Recording source: ${selected.name}`);
setMicrophoneEnabled(Boolean(request.mic));
if (request.mic) {
diff --git a/src/components/ai-edition/AppSettings.test.tsx b/src/components/ai-edition/AppSettings.test.tsx
index 505a8afb1..63c80fbdb 100644
--- a/src/components/ai-edition/AppSettings.test.tsx
+++ b/src/components/ai-edition/AppSettings.test.tsx
@@ -15,14 +15,18 @@ vi.mock("@/lib/ai-edition/store/projectStore", () => ({
}));
const microphoneHook = vi.hoisted(() => ({
enabled: [] as boolean[],
+ selectedDeviceId: "mic-live",
+ devices: [{ deviceId: "mic-live", label: "Live microphone", groupId: "g" }],
+ isReady: true,
}));
vi.mock("@/hooks/useMicrophoneDevices", () => ({
useMicrophoneDevices: (enabled: boolean) => {
microphoneHook.enabled.push(enabled);
return {
- devices: [{ deviceId: "mic-live", label: "Live microphone", groupId: "g" }],
- selectedDeviceId: "mic-live",
+ devices: microphoneHook.devices,
+ selectedDeviceId: microphoneHook.selectedDeviceId,
setSelectedDeviceId: vi.fn(),
+ isReady: microphoneHook.isReady,
};
},
}));
@@ -31,6 +35,7 @@ vi.mock("@/hooks/useCameraDevices", () => ({
devices: [{ deviceId: "cam-live", label: "Live camera", groupId: "g" }],
selectedDeviceId: "cam-live",
setSelectedDeviceId: vi.fn(),
+ isReady: true,
}),
}));
vi.mock("@/contexts/I18nContext", () => ({
@@ -81,6 +86,9 @@ function renderSettings() {
describe("AppSettings", () => {
beforeEach(() => {
microphoneHook.enabled = [];
+ microphoneHook.selectedDeviceId = "mic-live";
+ microphoneHook.devices = [{ deviceId: "mic-live", label: "Live microphone", groupId: "g" }];
+ microphoneHook.isReady = true;
project.document = createEmptyDocument({ projectId: "proj_settings", title: "Settings" });
window.electronAPI = {
getAppSettings: vi.fn(async () => snapshot),
@@ -167,6 +175,43 @@ describe("AppSettings", () => {
expect(screen.queryByText("appSettings.saveRecording")).not.toBeInTheDocument();
});
+ it("saves the resolved microphone when the stored device has fallen back", async () => {
+ vi.mocked(window.electronAPI.getAppSettings).mockResolvedValue({
+ ...snapshot,
+ recording: {
+ ...recording,
+ micEnabled: true,
+ micDeviceId: "mic-stale",
+ micDeviceName: "Unplugged microphone",
+ },
+ });
+ renderSettings();
+ await screen.findByTestId("app-settings-dialog");
+ fireEvent.click(screen.getByText("appSettings.saveRecording"));
+ await waitFor(() =>
+ expect(window.electronAPI.setRecordingPrefs).toHaveBeenCalledWith(
+ expect.objectContaining({
+ micEnabled: true,
+ micDeviceId: "mic-live",
+ micDeviceName: "Live microphone",
+ }),
+ ),
+ );
+ });
+
+ it("does not save while an enabled microphone is still resolving", async () => {
+ microphoneHook.isReady = false;
+ vi.mocked(window.electronAPI.getAppSettings).mockResolvedValue({
+ ...snapshot,
+ recording: { ...recording, micEnabled: true },
+ });
+ renderSettings();
+ await screen.findByTestId("app-settings-dialog");
+ expect(screen.getByText("appSettings.saveRecording").closest("button")).toBeDisabled();
+ fireEvent.click(screen.getByText("appSettings.saveRecording"));
+ expect(window.electronAPI.setRecordingPrefs).not.toHaveBeenCalled();
+ });
+
it("copies the current look and exposes both reset actions", async () => {
renderSettings();
await screen.findByTestId("app-settings-dialog");
diff --git a/src/components/ai-edition/AppSettings.tsx b/src/components/ai-edition/AppSettings.tsx
index 6cc9121b6..46a016c83 100644
--- a/src/components/ai-edition/AppSettings.tsx
+++ b/src/components/ai-edition/AppSettings.tsx
@@ -75,10 +75,31 @@ function AppSettings({ open, onClose }: { open: boolean; onClose: () => void })
}
};
+ const recordingDevicesReady = recording
+ ? (!recording.micEnabled || microphones.isReady) && (!recording.camEnabled || cameras.isReady)
+ : false;
+
const saveRecording = () => {
- if (!recording) return;
+ if (!recording || !recordingDevicesReady) return;
+ const payload = { ...recording };
+ if (recording.micEnabled) {
+ const mic = microphones.devices.find(
+ (device) => device.deviceId === microphones.selectedDeviceId,
+ );
+ if (mic) {
+ payload.micDeviceId = mic.deviceId;
+ payload.micDeviceName = mic.label;
+ }
+ }
+ if (recording.camEnabled) {
+ const cam = cameras.devices.find((device) => device.deviceId === cameras.selectedDeviceId);
+ if (cam) {
+ payload.camDeviceId = cam.deviceId;
+ payload.camDeviceName = cam.label;
+ }
+ }
void run(async () => {
- const saved = await window.electronAPI.setRecordingPrefs(recording);
+ const saved = await window.electronAPI.setRecordingPrefs(payload);
const next = await window.electronAPI.getAppSettings();
return { ...next, recording: saved };
});
@@ -193,7 +214,7 @@ function AppSettings({ open, onClose }: { open: boolean; onClose: () => void })
type="button"
className={`${styles.btn} ${styles.btnPrimary}`}
onClick={saveRecording}
- disabled={status === "saving"}
+ disabled={status === "saving" || !recordingDevicesReady}
>
<Save size={14} /> {t("appSettings.saveRecording")}
</button>
diff --git a/src/components/launch/LaunchWindow.test.tsx b/src/components/launch/LaunchWindow.test.tsx
index 856c608b6..ebf319e65 100644
--- a/src/components/launch/LaunchWindow.test.tsx
+++ b/src/components/launch/LaunchWindow.test.tsx
@@ -515,6 +515,48 @@ describe("LaunchWindow record button", () => {
expect(window.electronAPI.openSourceSelector).not.toHaveBeenCalled();
});
+ it("stops immediately without waiting for device readiness", async () => {
+ recorderState.value.recording = true;
+ recorderState.value.recordingPrefsLoaded = false;
+ stubElectronAPI(vi.fn(async () => displayOneSource));
+
+ renderLaunchWindow();
+ const recordButton = await screen.findByTestId("launch-record-button");
+ fireEvent.click(recordButton);
+
+ expect(recorderState.value.toggleRecording).toHaveBeenCalledTimes(1);
+ });
+
+ it("can start again after stop when devices were already ready", async () => {
+ stubElectronAPI(vi.fn(async () => displayOneSource));
+ const view = renderLaunchWindow();
+ const recordButton = await screen.findByTestId("launch-record-button");
+ await waitFor(() => {
+ expect(recordButton).toHaveAttribute("title", "Display 1");
+ });
+
+ fireEvent.click(recordButton);
+ expect(recorderState.value.toggleRecording).toHaveBeenCalledTimes(1);
+
+ recorderState.value.recording = true;
+ view.rerender(
+ <TooltipProvider>
+ <LaunchWindow />
+ </TooltipProvider>,
+ );
+ fireEvent.click(recordButton);
+ expect(recorderState.value.toggleRecording).toHaveBeenCalledTimes(2);
+
+ recorderState.value.recording = false;
+ view.rerender(
+ <TooltipProvider>
+ <LaunchWindow />
+ </TooltipProvider>,
+ );
+ fireEvent.click(recordButton);
+ expect(recorderState.value.toggleRecording).toHaveBeenCalledTimes(3);
+ });
+
it("does not wait for the camera list when the webcam is off", async () => {
cameraDevicesState.isReady = false;
recorderState.value.webcamEnabled = false;
diff --git a/src/components/launch/LaunchWindow.tsx b/src/components/launch/LaunchWindow.tsx
index e5a2e5ff7..f8fe67052 100644
--- a/src/components/launch/LaunchWindow.tsx
+++ b/src/components/launch/LaunchWindow.tsx
@@ -650,7 +650,12 @@ export function LaunchWindow() {
if (startWhenDevicesReadyInFlight.current) {
return startWhenDevicesReadyInFlight.current;
}
- const pending = (async () => {
+ let settle!: () => void;
+ const pending = new Promise<void>((resolve) => {
+ settle = resolve;
+ });
+ startWhenDevicesReadyInFlight.current = pending;
+ void (async () => {
try {
for (let attempt = 0; attempt < 100; attempt++) {
const ready = deviceReadinessRef.current;
@@ -666,10 +671,12 @@ export function LaunchWindow() {
}
console.warn("Recording did not start because device preferences could not be resolved.");
} finally {
- startWhenDevicesReadyInFlight.current = null;
+ if (startWhenDevicesReadyInFlight.current === pending) {
+ startWhenDevicesReadyInFlight.current = null;
+ }
+ settle();
}
})();
- startWhenDevicesReadyInFlight.current = pending;
return pending;
}, []);
@@ -740,6 +747,10 @@ export function LaunchWindow() {
if (saving) {
return;
}
+ if (recording) {
+ toggleRecording();
+ return;
+ }
// Linux never detours through the in-app picker: there is nothing for
// it to select, and waiting for a selection that can never arrive left
// the record button opening a modal instead of recording.
@@ -778,6 +789,7 @@ export function LaunchWindow() {
recording,
saving,
startWhenDevicesReady,
+ toggleRecording,
],
);
const handleRecordClick = useCallback(() => handleRecordButtonClick(), [handleRecordButtonClick]);
diff --git a/src/native/browserShim.test.ts b/src/native/browserShim.test.ts
index c2d849baf..9ce3a647a 100644
--- a/src/native/browserShim.test.ts
+++ b/src/native/browserShim.test.ts
@@ -136,4 +136,20 @@ describe("browserShim app settings", () => {
expect(localStorage.getItem("browser-shim-selected-source")).toBeNull();
expect((await window.electronAPI.getAppSettings()).lastSource).toBeNull();
});
+
+ it("getAppSettings after selectSource does not need Node process", async () => {
+ const source = (await window.electronAPI.getSources({ types: ["screen"] }))[0];
+ await window.electronAPI.selectSource(source);
+ const original = (globalThis as { process?: unknown }).process;
+ try {
+ delete (globalThis as { process?: unknown }).process;
+ await expect(window.electronAPI.getAppSettings()).resolves.toMatchObject({
+ lastSource: { id: source.id, name: source.name },
+ });
+ } finally {
+ if (original !== undefined) {
+ (globalThis as { process?: unknown }).process = original;
+ }
+ }
+ });
});
diff --git a/src/native/browserShim.ts b/src/native/browserShim.ts
index 15a806935..c297ed98f 100644
--- a/src/native/browserShim.ts
+++ b/src/native/browserShim.ts
@@ -16,6 +16,7 @@ import {
type ProjectAppearanceDefaults,
parseProjectAppearanceDefaults,
} from "../lib/projectDefaults";
+import { getPlatform } from "../utils/platformUtils";
import { nativeBridgeClient as realClient } from "./client";
function detectBrowserMode(): boolean {
@@ -126,7 +127,7 @@ function shimAppSettingsSnapshot() {
return {
recording: shimRecordingPrefs,
lastSource: shimSelectedSource
- ? describeRecordingSource(process.platform, {
+ ? describeRecordingSource(getPlatform(), {
id: shimSelectedSource.id,
name: shimSelectedSource.name,
display_id: shimSelectedSource.display_id,