mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix: apply CodeRabbit auto-fixes
This commit is contained in:
1 parent
2c00d244d9
commit
cc9965beb4
4 files changed
+70
-19
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { saveFile } from './download'
|
||||
import { saveFile, saveFileFromUrl } from './download'
|
||||
import { fetchWrapperBlob } from '@/api/fetchWrapper'
|
||||
import { showToast } from './toast'
|
||||
|
||||
vi.mock('./toast', () => ({
|
||||
@@ -9,6 +10,10 @@ vi.mock('./toast', () => ({
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/api/fetchWrapper', () => ({
|
||||
fetchWrapperBlob: vi.fn(),
|
||||
}))
|
||||
|
||||
function setIosHomeScreenApp(): void {
|
||||
Object.defineProperty(navigator, 'userAgent', { value: 'iPhone', configurable: true })
|
||||
Object.defineProperty(navigator, 'standalone', { value: true, configurable: true })
|
||||
@@ -18,6 +23,7 @@ describe('saveFile', () => {
|
||||
let clickSpy: ReturnType<typeof vi.spyOn>
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
clickSpy = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
|
||||
vi.stubGlobal('URL', Object.assign(URL, {
|
||||
createObjectURL: vi.fn(() => 'blob:mock'),
|
||||
@@ -34,8 +40,9 @@ describe('saveFile', () => {
|
||||
})
|
||||
|
||||
it('downloads via an anchor outside iOS home screen apps', async () => {
|
||||
await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
const saved = await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
|
||||
expect(saved).toBe(true)
|
||||
expect(clickSpy).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
@@ -45,13 +52,26 @@ describe('saveFile', () => {
|
||||
Object.defineProperty(navigator, 'canShare', { value: () => true, configurable: true })
|
||||
Object.defineProperty(navigator, 'share', { value: share, configurable: true })
|
||||
|
||||
await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
const saved = await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
|
||||
expect(saved).toBe(true)
|
||||
expect(clickSpy).not.toHaveBeenCalled()
|
||||
const shared = share.mock.calls[0][0].files[0] as File
|
||||
expect(shared.name).toBe('notes.md')
|
||||
})
|
||||
|
||||
it('reports a cancelled share as unsaved', async () => {
|
||||
setIosHomeScreenApp()
|
||||
const share = vi.fn().mockRejectedValue(new DOMException('cancelled', 'AbortError'))
|
||||
Object.defineProperty(navigator, 'canShare', { value: () => true, configurable: true })
|
||||
Object.defineProperty(navigator, 'share', { value: share, configurable: true })
|
||||
|
||||
const saved = await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
|
||||
expect(saved).toBe(false)
|
||||
expect(clickSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('offers a retry when the share loses user activation', async () => {
|
||||
setIosHomeScreenApp()
|
||||
const share = vi.fn()
|
||||
@@ -60,11 +80,34 @@ describe('saveFile', () => {
|
||||
Object.defineProperty(navigator, 'canShare', { value: () => true, configurable: true })
|
||||
Object.defineProperty(navigator, 'share', { value: share, configurable: true })
|
||||
|
||||
await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
const saved = await saveFile(new Blob(['x'], { type: 'text/markdown' }), 'notes.md')
|
||||
|
||||
expect(saved).toBe(false)
|
||||
const options = vi.mocked(showToast.info).mock.calls[0][1]
|
||||
options?.action?.onClick()
|
||||
await vi.waitFor(() => expect(share).toHaveBeenCalledTimes(2))
|
||||
expect(clickSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('saveFileFromUrl', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
Reflect.deleteProperty(navigator, 'standalone')
|
||||
})
|
||||
|
||||
it('reports a fetch failure and returns false in an iOS home screen app', async () => {
|
||||
setIosHomeScreenApp()
|
||||
vi.mocked(fetchWrapperBlob).mockRejectedValueOnce(new Error('network'))
|
||||
|
||||
const saved = await saveFileFromUrl('/api/files/notes.md', 'notes.md')
|
||||
|
||||
expect(saved).toBe(false)
|
||||
expect(showToast.error).toHaveBeenCalledWith('Failed to save notes.md')
|
||||
})
|
||||
})
|
||||
@@ -22,45 +22,52 @@ function saveViaAnchor(href: string, filename: string): void {
|
||||
document.body.removeChild(link)
|
||||
}
|
||||
|
||||
async function saveViaShareSheet(blob: Blob, filename: string): Promise<void> {
|
||||
async function saveViaShareSheet(blob: Blob, filename: string): Promise<boolean> {
|
||||
const file = new File([blob], filename, { type: blob.type || 'application/octet-stream' })
|
||||
|
||||
if (!navigator.canShare?.({ files: [file] })) {
|
||||
showToast.error(`${filename} cannot be saved from this device`)
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
await navigator.share({ files: [file] })
|
||||
return true
|
||||
} catch (error) {
|
||||
const name = error instanceof DOMException ? error.name : ''
|
||||
if (name === 'AbortError') return
|
||||
if (name === 'AbortError') return false
|
||||
if (name === 'NotAllowedError') {
|
||||
showToast.info(`Tap Save to finish saving ${filename}`, {
|
||||
action: { label: 'Save', onClick: () => void saveViaShareSheet(blob, filename) },
|
||||
})
|
||||
return
|
||||
return false
|
||||
}
|
||||
showToast.error(`Failed to save ${filename}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveFile(blob: Blob, filename: string): Promise<void> {
|
||||
export async function saveFile(blob: Blob, filename: string): Promise<boolean> {
|
||||
if (isIosHomeScreenApp()) {
|
||||
await saveViaShareSheet(blob, filename)
|
||||
return
|
||||
return saveViaShareSheet(blob, filename)
|
||||
}
|
||||
|
||||
const url = URL.createObjectURL(blob)
|
||||
saveViaAnchor(url, filename)
|
||||
setTimeout(() => URL.revokeObjectURL(url), 0)
|
||||
return true
|
||||
}
|
||||
|
||||
export async function saveFileFromUrl(url: string, filename: string): Promise<void> {
|
||||
export async function saveFileFromUrl(url: string, filename: string): Promise<boolean> {
|
||||
if (isIosHomeScreenApp()) {
|
||||
await saveFile(await fetchWrapperBlob(url), filename)
|
||||
return
|
||||
try {
|
||||
return await saveFile(await fetchWrapperBlob(url), filename)
|
||||
} catch {
|
||||
showToast.error(`Failed to save ${filename}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
saveViaAnchor(url, filename)
|
||||
return true
|
||||
}
|
||||
@@ -116,8 +116,8 @@ function generateSessionMarkdown(
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
export async function downloadMarkdown(content: string, filename: string): Promise<void> {
|
||||
await saveFile(new Blob([content], { type: 'text/markdown' }), filename)
|
||||
export async function downloadMarkdown(content: string, filename: string): Promise<boolean> {
|
||||
return saveFile(new Blob([content], { type: 'text/markdown' }), filename)
|
||||
}
|
||||
|
||||
export function exportSession(
|
||||
|
||||
@@ -412,7 +412,7 @@ export function SessionDetail() {
|
||||
return newValue
|
||||
}, [preferences?.expandToolCalls, updateSettings]);
|
||||
|
||||
const handleExportSession = useCallback(() => {
|
||||
const handleExportSession = useCallback(async () => {
|
||||
const data = getMessagesWithParts()
|
||||
if (!data || !session) {
|
||||
showToast.error('No session data to export')
|
||||
@@ -420,8 +420,9 @@ export function SessionDetail() {
|
||||
}
|
||||
|
||||
const { filename, content } = exportSession(data, session)
|
||||
void downloadMarkdown(content, filename)
|
||||
showToast.success(`Exported to ${filename}`)
|
||||
if (await downloadMarkdown(content, filename)) {
|
||||
showToast.success(`Exported to ${filename}`)
|
||||
}
|
||||
}, [getMessagesWithParts, session]);
|
||||
|
||||
const handleUndoMessage = useCallback((restoredPrompt: string) => {
|
||||
|
||||
Reference in new issue
Block a user