Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect, it } from 'vitest'
import {
MarkdownDocumentListingCapacityError,
MARKDOWN_DOCUMENT_LISTING_ERROR_CODE,
MARKDOWN_DOCUMENT_LISTING_ERROR_MESSAGE
} from '../../../../shared/markdown-document-listing-limits'
import {
extractIpcErrorMessage,
isMarkdownDocumentCapacityError
} from './rich-markdown-ipc-error-message'

describe('Markdown document capacity errors', () => {
it.each([
new MarkdownDocumentListingCapacityError(),
new Error(
`Error invoking remote method 'fs:listMarkdownDocuments': MarkdownDocumentListingCapacityError: ${MARKDOWN_DOCUMENT_LISTING_ERROR_MESSAGE}`
),
new Error(
`Error invoking remote method 'fs:listMarkdownDocuments': Error: ${MARKDOWN_DOCUMENT_LISTING_ERROR_MESSAGE}`
),
Object.assign(new Error('Translated capacity message'), {
code: MARKDOWN_DOCUMENT_LISTING_ERROR_CODE
}),
Object.assign(new Error(MARKDOWN_DOCUMENT_LISTING_ERROR_MESSAGE), { code: 'runtime_error' }),
{ response: { error: { code: MARKDOWN_DOCUMENT_LISTING_ERROR_CODE, message: 'Translated' } } },
new Error(`Error invoking remote method 'rpc': Error: ${MARKDOWN_DOCUMENT_LISTING_ERROR_CODE}`)
])('recognizes the local, Electron and paired capacity shapes: %s', (error) => {
expect(isMarkdownDocumentCapacityError(error)).toBe(true)
})

it.each([
new Error('Permission denied'),
new Error('Disconnected from the execution host'),
new Error(`Other failure: ${MARKDOWN_DOCUMENT_LISTING_ERROR_MESSAGE}`),
Object.assign(new Error(MARKDOWN_DOCUMENT_LISTING_ERROR_MESSAGE), {
code: 'permission_denied'
}),
Object.assign(new Error(MARKDOWN_DOCUMENT_LISTING_ERROR_CODE), { code: 'permission_denied' }),
null,
MARKDOWN_DOCUMENT_LISTING_ERROR_MESSAGE
])('does not classify unrelated or conflicting failures: %s', (error) => {
expect(isMarkdownDocumentCapacityError(error)).toBe(false)
})

it('extracts standard and named IPC errors while preserving ordinary messages', () => {
expect(
extractIpcErrorMessage(
new Error("Error invoking remote method 'save': TypeError: Cannot save."),
'Fallback'
)
).toBe('Cannot save.')
expect(extractIpcErrorMessage(new Error('Cannot save.'), 'Fallback')).toBe('Cannot save.')
expect(extractIpcErrorMessage(null, 'Fallback')).toBe('Fallback')
})
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import {
MARKDOWN_DOCUMENT_LISTING_ERROR_CODE,
MARKDOWN_DOCUMENT_LISTING_ERROR_MESSAGE
} from '../../../../shared/markdown-document-listing-limits'
import { hasRuntimeRpcErrorCode } from '../../../../shared/runtime-rpc-error-code'

export function extractIpcErrorMessage(err: unknown, fallback: string): string {
if (!(err instanceof Error)) {
return fallback
}
const match = err.message.match(/Error invoking remote method '[^']*': (?:Error: )?(.+)/)
const match = err.message.match(/Error invoking remote method '[^']*': (?:\w*Error: )?(.+)/)
return match ? match[1] : err.message
}

export function isMarkdownDocumentCapacityError(err: unknown): boolean {
if (
err !== null &&
typeof err === 'object' &&
'code' in err &&
typeof err.code === 'string' &&
err.code !== 'runtime_error'
) {
return err.code === MARKDOWN_DOCUMENT_LISTING_ERROR_CODE
}
return (
hasRuntimeRpcErrorCode(err, MARKDOWN_DOCUMENT_LISTING_ERROR_CODE) ||
extractIpcErrorMessage(err, '') === MARKDOWN_DOCUMENT_LISTING_ERROR_MESSAGE
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
// @vitest-environment happy-dom
import { act, createElement, StrictMode, Suspense, use } from 'react'
import { createRoot } from 'react-dom/client'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import type { OpenFile } from '@/store/slices/editor'
import type { MarkdownDocument } from '../../../../shared/filesystem-entry-types'
import { MarkdownDocumentListingCapacityError } from '../../../../shared/markdown-document-listing-limits'
import type * as MarkdownDocumentListRequest from './markdown-document-list-request'
import { useMarkdownDocuments } from './useMarkdownDocuments'

const runtime = vi.hoisted(() => ({ list: vi.fn(), toast: vi.fn() }))
const state = {
settings: {},
worktreesByRepo: {
repo: [
{ id: 'a', path: '/owned/a' },
{ id: 'b', path: '/owned/b' }
]
},
openFile: vi.fn(),
openMarkdownPreview: vi.fn()
}
vi.mock('@/store', () => ({
useAppStore: Object.assign((selector: (value: typeof state) => unknown) => selector(state), {
getState: () => state
})
}))
vi.mock('sonner', () => ({ toast: { error: runtime.toast } }))
vi.mock('@/i18n/i18n', () => ({ translate: (_key: string, fallback: string) => fallback }))
vi.mock('@/lib/connection-context', () => ({ getConnectionId: () => null }))
vi.mock('@/runtime/runtime-file-client', () => ({ statRuntimePath: vi.fn() }))
vi.mock('@/runtime/runtime-rpc-client', () => ({ settingsForRuntimeOwner: () => ({}) }))
vi.mock('./markdown-document-list-request', async (importOriginal) => ({
...(await importOriginal<typeof MarkdownDocumentListRequest>()),
requestSharedMarkdownDocumentList: runtime.list
}))

const controllers = new Map<string, ReturnType<typeof useMarkdownDocuments>>()
let root: ReturnType<typeof createRoot>
let container: HTMLDivElement
let unmounted = false
const save = vi.fn(async () => true)

function sourceFile(owner: string): OpenFile {
return {
id: `${owner}-source`,
filePath: `/owned/${owner}/source.md`,
relativePath: 'source.md',
worktreeId: owner,
language: 'markdown',
isDirty: false,
mode: 'edit'
}
}

function Hydration({ pending }: { pending?: Promise<void> }) {
if (pending) {
use(pending)
}
return null
}

function Harness({
owner = 'a',
pane = 'first',
pending
}: {
owner?: string
pane?: string
pending?: Promise<void>
}) {
controllers.set(pane, useMarkdownDocuments(sourceFile(owner), true, 'source', save))
return createElement(Hydration, { pending })
}

function controller(pane = 'first') {
const value = controllers.get(pane)
if (!value) {
throw new Error('Missing mounted document controller')
}
return value
}

async function render(owner = 'a', pending?: Promise<void>) {
await act(async () => {
root.render(
createElement(Suspense, { fallback: null }, createElement(Harness, { owner, pending }))
)
})
}

beforeEach(() => {
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true)
vi.clearAllMocks()
controllers.clear()
runtime.list.mockReset().mockRejectedValue(new MarkdownDocumentListingCapacityError())
save.mockReset().mockResolvedValue(true)
vi.spyOn(console, 'error').mockImplementation(() => {})
container = document.createElement('div')
document.body.append(container)
root = createRoot(container)
unmounted = false
})

afterEach(() => {
if (!unmounted) {
act(() => root.unmount())
}
container.remove()
vi.restoreAllMocks()
vi.unstubAllGlobals()
})

it('shows one useful notice across repeated failed saves without changing successful save results', async () => {
await render()
expect(controller().markdownDocuments).toEqual([])
for (let index = 0; index < 5; index += 1) {
await act(async () => {
expect(await controller().mdSave(`saved-${index}`)).toBe(true)
})
}
expect(save).toHaveBeenCalledTimes(5)
expect(runtime.toast).toHaveBeenCalledTimes(1)
expect(runtime.toast).toHaveBeenCalledWith(
'Markdown links are unavailable because this workspace is too large.',
{ id: expect.stringContaining('markdown-document-capacity:') }
)
})

it('rearms after an admitted current listing and after changing the committed owner', async () => {
await render()
runtime.list.mockResolvedValueOnce([])
await act(async () => {
await controller().mdSave('saved')
})
await act(async () => {
await controller().mdSave('saved-again')
})
expect(runtime.toast).toHaveBeenCalledTimes(2)
await render('b')
expect(runtime.toast).toHaveBeenCalledTimes(3)
expect(runtime.toast.mock.calls[0][1].id).not.toBe(runtime.toast.mock.calls[2][1].id)
})

it('does not arm a capacity notice for ordinary listing errors or failed saves', async () => {
runtime.list.mockRejectedValue(new Error('Permission denied'))
await render()
expect(runtime.toast).not.toHaveBeenCalled()
save.mockResolvedValue(false)
runtime.list.mockRejectedValue(new MarkdownDocumentListingCapacityError())
await act(async () => {
expect(await controller().mdSave('not-saved')).toBe(false)
})
expect(runtime.list).toHaveBeenCalledTimes(1)
expect(runtime.toast).not.toHaveBeenCalled()
})

it.each(['owner-change', 'unmount', 'superseded'] as const)(
'ignores a late capacity failure after %s',
async (reason) => {
const pending = Promise.withResolvers<MarkdownDocument[]>()
runtime.list.mockReturnValueOnce(pending.promise).mockResolvedValue([])
await render()
if (reason === 'owner-change') {
await render('b')
} else if (reason === 'unmount') {
act(() => root.unmount())
unmounted = true
} else {
await act(async () => {
await controller().mdSave('newest')
})
}
await act(async () => {
pending.reject(new MarkdownDocumentListingCapacityError())
})
expect(runtime.toast).not.toHaveBeenCalled()
}
)

it('keeps one notice while Suspense hides and reveals the same committed editor', async () => {
await render()
const pending = Promise.withResolvers<void>()
await render('a', pending.promise)
await act(async () => {
await controller().mdSave('saved-while-hidden')
})
await act(async () => {
pending.resolve()
})
expect(runtime.toast).toHaveBeenCalledTimes(1)
await act(async () => {
await controller().mdSave('saved-after-reveal')
})
expect(runtime.toast).toHaveBeenCalledTimes(1)
})

it('does not notify for a request started from a discarded owner render', async () => {
runtime.list.mockResolvedValueOnce([])
await render()
const pending = Promise.withResolvers<void>()
await render('b', pending.promise)
await act(async () => {
await controller().mdSave('saved-uncommitted-owner')
})
expect(runtime.list).toHaveBeenCalledTimes(1)
expect(runtime.toast).not.toHaveBeenCalled()
await render()
})

it('uses one stable Sonner ID for split panes and does not dismiss another pane on close', async () => {
await act(async () => {
root.render(
createElement(
'div',
null,
createElement(Harness, { pane: 'first' }),
createElement(Harness, { pane: 'second' })
)
)
})
expect(runtime.toast).toHaveBeenCalledTimes(2)
expect(runtime.toast.mock.calls[0][1].id).toBe(runtime.toast.mock.calls[1][1].id)
await act(async () => {
root.render(createElement('div', null, createElement(Harness, { pane: 'second' })))
})
expect(runtime.toast).toHaveBeenCalledTimes(2)
})

it('shows one notice after Strict Mode replays mount effects', async () => {
await act(async () => {
root.render(createElement(StrictMode, null, createElement(Harness)))
})
expect(runtime.toast).toHaveBeenCalledTimes(1)
})
21 changes: 21 additions & 0 deletions src/renderer/src/components/editor/useMarkdownDocuments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { toast } from 'sonner'
import { translate } from '@/i18n/i18n'
import type { MarkdownDocument } from '../../../../shared/filesystem-entry-types'
import { useAppStore } from '@/store'
import { getConnectionId } from '@/lib/connection-context'
Expand All @@ -11,6 +13,7 @@ import {
resolveMarkdownDocLink
} from './markdown-doc-links'
import { selectMarkdownDocumentWorktreePath } from './markdown-document-worktree-path-selector'
import { isMarkdownDocumentCapacityError } from './rich-markdown-ipc-error-message'
import {
getMarkdownDocumentListRequestKey,
requestSharedMarkdownDocumentList
Expand Down Expand Up @@ -82,7 +85,11 @@ export function useMarkdownDocuments(
worktreePath ?? ''
)
const currentRequestKeyRef = useRef<string | null>(documentRequestKey)
const lastCapacityNoticeKeyRef = useRef<string | null>(null)
useLayoutEffect(() => {
if (currentRequestKeyRef.current !== documentRequestKey) {
lastCapacityNoticeKeyRef.current = null
}
currentRequestKeyRef.current = documentRequestKey
}, [documentRequestKey])
// Suspense hides layout effects while a current scan is still valid.
Expand Down Expand Up @@ -121,6 +128,7 @@ export function useMarkdownDocuments(
) {
return
}
lastCapacityNoticeKeyRef.current = null
setDocumentSnapshot({ requestKey: documentRequestKey, documents })
} catch (err) {
console.error('Failed to list markdown documents:', err)
Expand All @@ -129,6 +137,19 @@ export function useMarkdownDocuments(
currentRequestKeyRef.current === documentRequestKey
) {
setDocumentSnapshot({ requestKey: documentRequestKey, documents: [] })
if (
isMarkdownDocumentCapacityError(err) &&
lastCapacityNoticeKeyRef.current !== documentRequestKey
) {
lastCapacityNoticeKeyRef.current = documentRequestKey
toast.error(
translate(
'editor.markdownLinks.capacity',
'Markdown links are unavailable because this workspace is too large.'
),
{ id: `markdown-document-capacity:${documentRequestKey}` }
)
}
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@
},
"fileLoad": {
"hostUnresolved": "The host couldn't find this file's workspace. It may have been removed, or the host may not know about it yet. Retry, or close this tab from the tab strip."
},
"markdownLinks": {
"capacity": "Markdown links are unavailable because this workspace is too large."
}
},
"githubChecks": {
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/i18n/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -18607,6 +18607,9 @@
"searching": "正在搜索…",
"searchFailed": "搜索未能完成,请重试。",
"refreshFailed": "预览更新失败,正在显示上一版本。请打开源码视图查看当前内容。"
},
"markdownLinks": {
"capacity": "此工作区过大,Markdown 链接暂不可用。"
}
},
"featureTips": {
Expand Down
Loading
Loading