fix: preserve screenshot fallback encoding errors

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo-H
2026-05-30 09:47:42 -07:00
co-authored by Orca
parent 517a13d351
commit 0ec4f335ed
2 changed files with 29 additions and 1 deletions
+22
View File
@@ -190,6 +190,28 @@ describe('captureScreenshot', () => {
)
})
it('reports the original timeout when fallback encoding fails', async () => {
vi.useFakeTimers()
const webContents = createMockWebContents()
webContents.debugger.sendCommand.mockImplementation(() => new Promise(() => {}))
webContents.capturePage.mockResolvedValueOnce({
isEmpty: () => {
throw new Error('native image unavailable')
}
})
const onResult = vi.fn()
const onError = vi.fn()
captureScreenshot(webContents as never, { format: 'png' }, onResult, onError)
await vi.advanceTimersByTimeAsync(8000)
expect(onResult).not.toHaveBeenCalled()
expect(onError).toHaveBeenCalledWith(
'Screenshot timed out — the browser tab may not be visible or the window may not have focus.'
)
})
it('reports the timeout when both CDP and fallback capture stall', async () => {
vi.useFakeTimers()
+7 -1
View File
@@ -268,7 +268,13 @@ export function captureScreenshot(
clearTimeout(fallbackTimer)
fallbackTimer = null
}
const fallback = encodeNativeImageScreenshot(image, params)
let fallback: { data: string } | null = null
try {
fallback = encodeNativeImageScreenshot(image, params)
} catch {
settleError(SCREENSHOT_TIMEOUT_MESSAGE)
return
}
if (fallback) {
settleResult(fallback)
return