Files
apes-sidebar/tests/editor-line-nav-mode.spec.tsx
T
ymac 6dd0486f5c fix(editor): 换文件 / 重命名时丢掉继承来的 landing line(#826 收尾之三)
`updateTab` 的补丁只有 title/path/meta,所以两条「插件自己改路径」的路都把上一个
文件的行号留在了记录里:合并模式在页头路径框输另一个文件、以及文件树里重命名
打开的文件(`retargetPathTabs`)。后果与 reload 那条同症状——新文件跳到旧行号,
markdown 还被拉进编辑态。宿主导航那条路径本来就有清理规则
(`params.line !== existing.tab.line`),插件自己写的路径没有。

规则统一为「**改 path 就是一次新的打开**」,瞬态行号随之清掉,落在两个记录层:
`patchTab`(store 分支)与原生记录的 `update()`(`updateTab` 的 native 分支);
只改 title / meta 的补丁不动它(同一文件,行号仍属于当前这次导航)。

测试:native-tab-line.spec.ts 加两例(换 path 清掉、只改 title 保留),
editor-line-nav-mode.spec.tsx 的第四条从「模拟 updateTab 的结果」改成驱动真实
`service.updateTab` 并断言记录里 `line` 消失、编辑器留在预览态。
2026-10-11 15:56:49 +08:00

237 lines
8.3 KiB
TypeScript

/**
* A markdown reference that names a line must not own the file's mode forever
* (#826, second half — the forced edit-mode flip).
*
* The text editor flips a markdown/html file OUT of preview when a reference
* named a line (preview renders no source lines). That flip is part of the
* navigation, not a property of the file: the reader must be able to go back to
* preview afterwards. It cannot be tracked inside the viewer, because a reload
* REMOUNTS the viewer (EditorHost renders the loading row in its place) and the
* fresh instance — mode `preview`, no memory — would re-force the flip on every
* refresh, including the one the preview button itself triggers.
*
* `tests/text-editor-line-jump.spec.tsx` renders the viewer alone, so it cannot
* see any of this: the host is what decides when a navigation is spent.
*/
// @vitest-environment jsdom
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { act } from 'react-dom/test-utils'
import { renderRoot, setupReactAct } from './test-utils.ts'
import type { Context } from '../src/context-types.ts'
import { EditorHost } from '../src/client/EditorHost.tsx'
import { TextEditor } from '../src/client/TextEditor.tsx'
import { createBetterSidebarService } from '../src/client/service.ts'
import { allLeaves, createSidebarStore, type SidebarStore, type SidebarTab } from '../src/client/state.ts'
setupReactAct()
const fsRead = vi.fn()
vi.mock('../src/client/api.ts', () => ({
api: {
fsRead: (...args: unknown[]) => fsRead(...args),
mediaUrl: () => '',
},
}))
const SESSION = 'line-nav-session'
const BODY = '# title\n\nfirst\n\nsecond\n'
/** fsRead call count: the "did a reload remount the viewer" evidence. */
function reads(): number {
return fsRead.mock.calls.length
}
/** Let the initial load (and any reload it triggers) settle. */
async function settle(): Promise<void> {
for (let index = 0; index < 4; index += 1) {
await act(async () => { await Promise.resolve() })
}
}
interface Harness {
ctx: Context
store: SidebarStore
/** The editor tab for `path`, carrying `line` when the reference named one. */
tabOf: (path: string, line?: number) => SidebarTab
}
/** One sidebar service + store with the REAL text editor behind `markdown`. */
function harness(): Harness {
const store = createSidebarStore()
const service = createBetterSidebarService(store)
service.registerTab({ id: 'editor', title: 'Editor', dedupeKey: (tab) => tab.path, component: () => null })
service.registerFileViewer({
id: 'markdown',
exts: ['md'],
priority: 0,
fetchStrategy: 'fsRead',
component: TextEditor,
})
store.setSession(SESSION)
const ctx = {
betterSidebar: service,
sessions: { list: { subscribe: () => () => {}, getSnapshot: () => ({ byId: { [SESSION]: { cwd: '/tmp' } } }) } },
get: (name: string) => name === 'betterSidebar' ? service : undefined,
} as unknown as Context
return {
ctx,
store,
tabOf: (path, line): SidebarTab => {
ctx.betterSidebar.openTab({ type: 'editor', title: path.split('/').pop()!, path, id: `editor:${path}` })
const tab = allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs)
.find(item => item.path === path)!
return line === undefined ? tab : { ...tab, line }
},
}
}
function mount(ctx: Context, tab: SidebarTab, store: SidebarStore): ReturnType<typeof renderRoot> {
return renderRoot(createElement(EditorHost, {
ctx,
store,
scope: { sessionId: SESSION, cwd: '/tmp' },
tab,
visible: true,
expanded: [],
revealed: [],
onToggleDir: () => {},
onReferenceFile: () => {},
}))
}
/** The editor is on screen (edit mode) — in preview the CodeMirror host is hidden. */
function editing(container: HTMLElement): boolean {
return container.querySelector('[class*="editorCmHidden"]') === null
}
function modeButton(container: HTMLElement, label: string): HTMLButtonElement {
return Array.from(container.querySelectorAll('button'))
.find(button => button.textContent === label)!
}
function click(button: HTMLButtonElement): void {
act(() => { button.dispatchEvent(new MouseEvent('click', { bubbles: true })) })
}
beforeEach(() => {
fsRead.mockReset()
fsRead.mockResolvedValue({ kind: 'text', content: BODY, truncated: false })
})
describe('a referenced line does not own the file mode (#826)', () => {
it('a markdown reference opens in the editor, and the reader can go back to preview', async () => {
const { ctx, store, tabOf } = harness()
const view = mount(ctx, tabOf('/tmp/readme.md', 3), store)
try {
await settle()
expect(reads()).toBe(1)
// The reference asked for source, so the file lands in the editor.
expect(editing(view.container)).toBe(true)
// Back to preview — this reloads (#167 part B), which REMOUNTS the viewer.
click(modeButton(view.container, 'Preview'))
await settle()
expect(reads()).toBe(2)
// ...and the remount must not flip the file back into the editor.
expect(editing(view.container)).toBe(false)
} finally {
view.unmount()
}
})
it('the header refresh does not flip a previewed file back into the editor either', async () => {
const { ctx, store, tabOf } = harness()
const view = mount(ctx, tabOf('/tmp/readme.md', 3), store)
try {
await settle()
expect(editing(view.container)).toBe(true)
click(modeButton(view.container, 'Preview'))
await settle()
const refresh = view.container.querySelector<HTMLButtonElement>(
'button[aria-label="Refresh"], button[aria-label="刷新"]',
)!
click(refresh)
await settle()
expect(reads()).toBe(3)
expect(editing(view.container)).toBe(false)
} finally {
view.unmount()
}
})
it('a NEW line on the same open file still lands (and re-opens the editor)', async () => {
const { ctx, store, tabOf } = harness()
const view = mount(ctx, tabOf('/tmp/readme.md', 3), store)
try {
await settle()
expect(editing(view.container)).toBe(true)
click(modeButton(view.container, 'Preview'))
await settle()
expect(editing(view.container)).toBe(false)
// The same tab navigated to another line of the same file.
view.rerender(createElement(EditorHost, {
ctx,
store,
scope: { sessionId: SESSION, cwd: '/tmp' },
tab: { ...tabOf('/tmp/readme.md', 5), line: 5 },
visible: true,
expanded: [],
revealed: [],
onToggleDir: () => {},
onReferenceFile: () => {},
}))
await settle()
expect(editing(view.container)).toBe(true)
} finally {
view.unmount()
}
})
})
describe('a spent line must not ride along to the next file', () => {
it('an in-place path switch drops it (updateTab retargets the record)', async () => {
// The reader types another path into the same window (merged mode): the
// record keeps its identity and takes the new path, but the landing line
// belonged to the navigation that delivered the PREVIOUS file. Keeping it
// sent the new file to line 3 — and, being markdown, forced it into the
// editor, which is the same "stuck in edit mode" the reload bug produced.
const { ctx, store, tabOf } = harness()
const tab = tabOf('/tmp/a.md', 3)
const view = mount(ctx, tab, store)
const host = (next: SidebarTab): ReturnType<typeof createElement> => createElement(EditorHost, {
ctx,
store,
scope: { sessionId: SESSION, cwd: '/tmp' },
tab: next,
visible: true,
expanded: [],
revealed: [],
onToggleDir: () => {},
onReferenceFile: () => {},
})
try {
await settle()
expect(editing(view.container)).toBe(true)
// The real retarget path (`EditorHost.openFile` merged mode / a rename),
// then the tab the store actually holds after it.
ctx.betterSidebar.updateTab(tab.id, { path: '/tmp/b.md', title: 'b.md' }, SESSION)
const retargeted = allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs)
.find(item => item.id === tab.id)!
expect(retargeted.line).toBeUndefined()
view.rerender(host(retargeted))
await settle()
expect(editing(view.container)).toBe(false)
} finally {
view.unmount()
}
})
})