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
27 changes: 27 additions & 0 deletions src/screens/GalleryDetail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, it, expect, vi } from 'vitest'
import { openGenerationInBrowser } from './GalleryDetail.js'

describe('openGenerationInBrowser', () => {
it('opens the generation URL', async () => {
const setError = vi.fn<(value: string | null) => void>()
const openBrowser = vi.fn<(target: string) => Promise<unknown>>().mockResolvedValue(undefined)

openGenerationInBrowser('gen_123', setError, openBrowser)
await Promise.resolve()

expect(openBrowser).toHaveBeenCalledWith('https://www.pixelmuse.studio/g/gen_123')
expect(setError).not.toHaveBeenCalled()
})

it('handles browser open failures without throwing', async () => {
const setError = vi.fn<(value: string | null) => void>()
const openBrowser = vi
.fn<(target: string) => Promise<unknown>>()
.mockRejectedValue(new Error('No browser available'))

expect(() => openGenerationInBrowser('gen_456', setError, openBrowser)).not.toThrow()
await Promise.resolve()

expect(setError).toHaveBeenCalledWith('No browser available')
})
})
13 changes: 12 additions & 1 deletion src/screens/GalleryDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ interface Props {
back: () => void
}

export function openGenerationInBrowser(
generationId: string,
setError: (value: string | null) => void,
openBrowser: (target: string) => Promise<unknown> = open,
): void {
const url = `https://www.pixelmuse.studio/g/${generationId}`
void openBrowser(url).catch((err: unknown) => {
setError(err instanceof Error ? err.message : 'Failed to open in browser')
})
}

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 +60,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}`)
openGenerationInBrowser(generation.id, setError)
}
})

Expand Down
Loading