diff --git a/src/screens/GalleryDetail.test.ts b/src/screens/GalleryDetail.test.ts new file mode 100644 index 0000000..4ab5dd6 --- /dev/null +++ b/src/screens/GalleryDetail.test.ts @@ -0,0 +1,22 @@ +import { describe, it, expect, vi } from 'vitest' +import { openGenerationInBrowser } from './GalleryDetail.js' + +describe('openGenerationInBrowser', () => { + it('opens the expected generation URL', async () => { + const openInBrowser = vi.fn().mockResolvedValue(undefined) + const onError = vi.fn() + + await openGenerationInBrowser('gen_123', onError, openInBrowser) + + expect(openInBrowser).toHaveBeenCalledWith('https://www.pixelmuse.studio/g/gen_123') + expect(onError).not.toHaveBeenCalled() + }) + + it('captures open errors without rejecting', async () => { + const openInBrowser = vi.fn().mockRejectedValue(new Error('xdg-open not found')) + const onError = vi.fn() + + await expect(openGenerationInBrowser('gen_123', onError, openInBrowser)).resolves.toBeUndefined() + expect(onError).toHaveBeenCalledWith('xdg-open not found') + }) +}) diff --git a/src/screens/GalleryDetail.tsx b/src/screens/GalleryDetail.tsx index 5953137..5cc08db 100644 --- a/src/screens/GalleryDetail.tsx +++ b/src/screens/GalleryDetail.tsx @@ -13,6 +13,19 @@ interface Props { back: () => void } +export async function openGenerationInBrowser( + generationId: string, + onError: (message: string) => void, + openInBrowser: (target: string) => Promise = open, +): Promise { + try { + await openInBrowser(`https://www.pixelmuse.studio/g/${generationId}`) + } catch (error) { + const message = error instanceof Error ? error.message : 'Failed to open browser' + onError(message) + } +} + export default function GalleryDetail({ client, generationId, back }: Props) { const [generation, setGeneration] = useState(null) const [imagePath, setImagePath] = useState(null) @@ -49,7 +62,7 @@ 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, setError) } })