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

describe('openGenerationInBrowser', () => {
it('returns null when browser open succeeds', async () => {
const seenUrls: string[] = []
const result = await openGenerationInBrowser('gen_123', async (url) => {
seenUrls.push(url)
return undefined
})

expect(result).toBeNull()
expect(seenUrls).toEqual(['https://www.pixelmuse.studio/g/gen_123'])
})

it('returns error message when browser open fails', async () => {
const result = await openGenerationInBrowser('gen_456', async () => {
throw new Error('no browser available')
})

expect(result).toBe('no browser available')
})

it('returns fallback message for non-Error throws', async () => {
const result = await openGenerationInBrowser('gen_789', async () => {
throw { code: 'EFAIL' }
})

expect(result).toBe('Failed to open browser')
})
})

describe('openUrlSafely', () => {
it('opens an arbitrary URL unchanged', async () => {
const seenUrls: string[] = []
const result = await openUrlSafely('https://checkout.example/session/abc', async (url) => {
seenUrls.push(url)
return undefined
})

expect(result).toBeNull()
expect(seenUrls).toEqual(['https://checkout.example/session/abc'])
})
})
23 changes: 23 additions & 0 deletions src/core/open-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import open from 'open'

const OPEN_BROWSER_ERROR = 'Failed to open browser'

export async function openUrlSafely(
url: string,
openUrl: (targetUrl: string) => Promise<unknown> = open,
): Promise<string | null> {
try {
await openUrl(url)
return null
} catch (err) {
return err instanceof Error ? err.message : OPEN_BROWSER_ERROR
}
}

export async function openGenerationInBrowser(
generationId: string,
openUrl: (targetUrl: string) => Promise<unknown> = open,
): Promise<string | null> {
const url = `https://www.pixelmuse.studio/g/${generationId}`
return openUrlSafely(url, openUrl)
}
8 changes: 6 additions & 2 deletions src/screens/GalleryDetail.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useState, useEffect } from 'react'
import { Box, Text, useInput } from 'ink'
import { ConfirmInput, Spinner } from '@inkjs/ui'
import open from 'open'
import type { PixelmuseClient } from '../core/client.js'
import type { Generation } from '../core/types.js'
import { imageToBuffer, autoSave } from '../core/image.js'
import { openGenerationInBrowser } from '../core/open-browser.js'
import ImagePreview from '../components/ImagePreview.js'

interface Props {
Expand Down Expand Up @@ -49,7 +49,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(openError)
}
})
}
})

Expand Down
Loading