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
23 changes: 23 additions & 0 deletions src/screens/GalleryDetail.test.ts
Original file line number Diff line number Diff line change
@@ -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<void>>().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<void>>()
.mockRejectedValue(new Error('spawn xdg-open ENOENT'))

const result = await openGenerationInBrowser('gen_123', openFn)

expect(result).toBe('spawn xdg-open ENOENT')
})
})
21 changes: 20 additions & 1 deletion src/screens/GalleryDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ interface Props {
back: () => void
}

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

Expand Down
Loading