From 5bbf0acb5657f8f5b89bf599c10f9819e0c51953 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 22 Mar 2026 11:06:24 +0000 Subject: [PATCH] fix: prevent gallery browser-open crash in TUI Co-authored-by: Dylan Boudro --- src/screens/GalleryDetail.test.ts | 23 +++++++++++++++++++++++ src/screens/GalleryDetail.tsx | 21 ++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/screens/GalleryDetail.test.ts diff --git a/src/screens/GalleryDetail.test.ts b/src/screens/GalleryDetail.test.ts new file mode 100644 index 0000000..097ccc4 --- /dev/null +++ b/src/screens/GalleryDetail.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it, vi } from 'vitest' +import { openGenerationInBrowser } from './GalleryDetail.js' + +describe('openGenerationInBrowser', () => { + it('opens expected gallery URL', async () => { + const openFn = vi.fn<(_: string) => Promise>().mockResolvedValue(undefined) + + const result = await openGenerationInBrowser('gen_123', openFn) + + expect(result).toBeNull() + expect(openFn).toHaveBeenCalledWith('https://www.pixelmuse.studio/g/gen_123') + }) + + it('returns error message when open fails', async () => { + const openFn = vi + .fn<(_: string) => Promise>() + .mockRejectedValue(new Error('spawn xdg-open ENOENT')) + + const result = await openGenerationInBrowser('gen_123', openFn) + + expect(result).toBe('spawn xdg-open ENOENT') + }) +}) diff --git a/src/screens/GalleryDetail.tsx b/src/screens/GalleryDetail.tsx index 5953137..e62f840 100644 --- a/src/screens/GalleryDetail.tsx +++ b/src/screens/GalleryDetail.tsx @@ -13,6 +13,21 @@ interface Props { back: () => void } +export async function openGenerationInBrowser( + generationId: string, + openFn: (target: string) => Promise = open, +): Promise { + try { + await openFn(`https://www.pixelmuse.studio/g/${generationId}`) + return null + } catch (error) { + if (error instanceof Error) { + return error.message + } + return 'Unknown error' + } +} + export default function GalleryDetail({ client, generationId, back }: Props) { const [generation, setGeneration] = useState(null) const [imagePath, setImagePath] = useState(null) @@ -49,7 +64,11 @@ export default function GalleryDetail({ client, generationId, back }: Props) { if (confirming) return if (input === 'd') setConfirming(true) if (input === 'o' && generation) { - open(`https://www.pixelmuse.studio/g/${generation.id}`) + void openGenerationInBrowser(generation.id).then((openError) => { + if (openError) { + setError(`Failed to open browser: ${openError}`) + } + }) } })