Files
apes-sidebar/tests/native-tab-line.spec.ts
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

104 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The landing line a file reference carries (`a/b.c#L131`, `a/b.c:131`).
*
* `fileParamsOf` already split the `path:line` form off the address (#826
* first half, `src/client/path-line.ts`); what is left — the second half — is
* that nothing CONSUMED the line it parsed. `NativeTabParams.line` was
* declared, the host's own `#L131` fragment already arrives as
* `navigation.params.line`, and the record simply dropped both on the floor,
* so `[path](path:131)` opened the file and left the reader on line 1.
*
* These assertions are on the RECORD's event surface, not on the editor's
* pixels: the record is what carries the line from the address to the viewer,
* and it is where a stale line would survive (the host keeps a visited tab
* body mounted and re-delivers its navigation, so "just put it in the record"
* is only half of it — the record must also let it go again).
*/
import { describe, expect, it } from 'vitest'
import { createNativeTabRecords } from '../src/client/native/tab-adapter.tsx'
describe('the landing line on a native tab record (#826)', () => {
const records = createNativeTabRecords()
const scope = { sessionId: 's1', cwd: '/work' }
const ensure = (params: { path?: string; title?: string; line?: number } | undefined, revision?: number) =>
records.ensure({
sessionId: 'seat-1',
id: 'tab-1',
kind: 'editor',
title: 'CMakeLists.txt',
params,
scope,
revision,
}).tab
it('seeds the line a `path:line` address named', () => {
const tab = ensure({ path: 'omlx/CMakeLists.txt', title: 'CMakeLists.txt', line: 131 })
expect(tab.path).toBe('omlx/CMakeLists.txt')
expect(tab.line).toBe(131)
})
it('carries the host’s own `#L131` fragment the same way', () => {
// The host parses `path#L131` and hands it down as a navigation param;
// both spellings must land on the same row, so both take this one field.
expect(ensure({ path: 'omlx/CMakeLists.txt', line: 131 }).line).toBe(131)
})
it('seeds no line on an ordinary open', () => {
expect(ensure({ path: 'src/main.ts' }).line).toBeUndefined()
})
it('re-seeds the line when a new navigation moves it', () => {
records.ensure({
sessionId: 'seat-1', id: 'tab-2', kind: 'editor', title: 'a.ts',
params: { path: 'a.ts', line: 10 }, scope, revision: 1,
})
const moved = records.ensure({
sessionId: 'seat-1', id: 'tab-2', kind: 'editor', title: 'a.ts',
params: { path: 'a.ts', line: 90 }, scope, revision: 2,
})
expect(moved.tab.line).toBe(90)
})
it('CLEARS the line when the same tab is navigated to a bare path', () => {
// An in-place switch from `a.ts:131` to `a.ts` must not leave 131 behind:
// the reader asked for the file, not for that line of it.
records.ensure({
sessionId: 'seat-1', id: 'tab-3', kind: 'editor', title: 'a.ts',
params: { path: 'a.ts', line: 131 }, scope, revision: 1,
})
const bare = records.ensure({
sessionId: 'seat-1', id: 'tab-3', kind: 'editor', title: 'a.ts',
params: { path: 'a.ts' }, scope, revision: 2,
})
expect(bare.tab.line).toBeUndefined()
})
it('DROPS the line when an updateTab retarget moves the record to another file', () => {
// The plugin's own writers (`updateTab`: an in-place path switch, a rename)
// are not host navigations, so they never passed through the clearing rule
// above. A path change is a new open all the same: the reader asked for
// that other file, not for line 131 of it.
const seeded = ensure({ path: 'a.ts', line: 131 }, 1)
records.update('seat-1', 'tab-1', { path: 'b.ts', title: 'b.ts' })
const retargeted = records.get('seat-1', 'tab-1')!.tab
expect(retargeted.path).toBe('b.ts')
expect(retargeted.line).toBeUndefined()
expect(seeded.line).toBe(131)
})
it('keeps the line when updateTab patches only the title (same file)', () => {
ensure({ path: 'a.ts', line: 7 }, 1)
records.update('seat-1', 'tab-1', { title: 'renamed.ts' })
expect(records.get('seat-1', 'tab-1')!.tab.line).toBe(7)
})
it('keeps the record identity when the navigation repeats unchanged', () => {
// The host re-delivers the last navigation on every store notification;
// an unchanged line must not mint a new record (a new object identity is
// what re-renders every kept-mounted body).
const first = ensure({ path: 'a.ts', line: 5 }, 1)
const again = ensure({ path: 'a.ts', line: 5 }, 1)
expect(again).toBe(first)
})
})