Skip to content
Draft
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
22 changes: 22 additions & 0 deletions src/screens/GalleryDetail.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
15 changes: 14 additions & 1 deletion src/screens/GalleryDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ interface Props {
back: () => void
}

export async function openGenerationInBrowser(
generationId: string,
onError: (message: string) => void,
openInBrowser: (target: string) => Promise<unknown> = open,
): Promise<void> {
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<Generation | null>(null)
const [imagePath, setImagePath] = useState<string | null>(null)
Expand Down Expand Up @@ -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)
}
})

Expand Down
Loading