Files
apes-sidebar/tests/text-editor-conflict.spec.tsx
T
Menghuan1918 2c8d11a8fc feat(editor): 编辑器未提交变更提示(行号着色 + 左侧色条 + hover blame)(#212)
code 视图(可编辑 CodeMirror)显示未提交变更的行级提示,一个布尔设置
editorGitGutter(默认开)总开关:

- 行号槽着色 + 行号左侧 2px 细色条(新增/修改/删除),取色复用文件树
  既有 tone 口径(--dsw-alias-state-*);
- hover 某行时按需取该行 blame,纯文本 tooltip(作者 · 时间 + 摘要),
  按 sessionId+path+line 记忆化,不预取整文件、滚动不重复请求;
- 行映射复用既有 useGitStatus 共享 store 与既有统一 diff 引擎
  (parseUnifiedDiff/unifiedSegments),不新增第二个 git status 轮询器;
  未跟踪文件按状态 store 整篇标新增;
- 新增宿主路由 git.diff-head(git diff HEAD,部分暂存时给出已暂存+未暂存
  的整份未提交变更,未出生 HEAD 回退 --cached)与 git.blame
  (git blame --porcelain -L,四条失败路径一律返回空,不抛);
- 只给 viewerId === 'code' 挂 decoration/gutter/tooltip,markdown/html 与
  关闭设置后不安装任何扩展;开关经 Compartment 原地重配。

不做行尾常驻作者 widget、不做徽章、不做动画、不做渐变、不做四个独立开关。
2026-10-05 22:25:51 +08:00

127 lines
4.6 KiB
TypeScript

/**
* The text editor's save-conflict surface: a save whose baseline mtime no
* longer matches the file on disk is refused by the host (`fs-conflict`) and
* the editor must (a) keep the draft, (b) explain why, (c) offer the host's
* reload. Rendered with the REAL CodeMirror viewer (the dirty draft only
* exists there), against a mocked api module.
*/
// @vitest-environment jsdom
import { afterEach, 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 { SidebarApiError } from '../src/client/api.ts'
import { TextEditor } from '../src/client/TextEditor.tsx'
import { createSidebarStore } from '../src/client/state.ts'
import type { FileViewerProps } from '../src/client/service.ts'
setupReactAct()
const fsWrite = vi.fn()
vi.mock('../src/client/api.ts', async (importOriginal) => {
// The real error class: TextEditor narrows the failure with instanceof.
const actual = await importOriginal<typeof import('../src/client/api.ts')>()
return {
...actual,
// The real api object with only the save call replaced: TextEditor also
// reads the shared git status store in the code viewer.
api: { ...actual.api, fsWrite: (...args: unknown[]) => fsWrite(...args) },
}
})
function viewerProps(overrides: Partial<FileViewerProps> = {}): FileViewerProps {
return {
ctx: {} as FileViewerProps['ctx'],
store: createSidebarStore(),
scope: { sessionId: 's1', cwd: '/p' },
path: '/p/a.ts',
title: 'a.ts',
viewerId: 'code',
content: 'const a = 1\n',
mtimeMs: 123,
...overrides,
}
}
/** The save button of the viewer's own toolbar (no host hoisting here). */
function saveButton(container: HTMLDivElement): HTMLButtonElement {
const button = container.querySelector('button[aria-label="Save"], button[aria-label="保存"]')
if (button === null) throw new Error('save button not found')
return button as HTMLButtonElement
}
function click(element: HTMLElement): void {
act(() => { element.dispatchEvent(new MouseEvent('click', { bubbles: true })) })
}
beforeEach(() => {
fsWrite.mockReset()
})
afterEach(() => {
document.body.innerHTML = ''
})
describe('TextEditor save conflict', () => {
it('sends the loaded mtime as the baseline', async () => {
fsWrite.mockResolvedValue({ ok: true, mtimeMs: 456 })
const mounted = renderRoot(createElement(TextEditor, viewerProps()))
try {
click(saveButton(mounted.container))
await act(async () => { await Promise.resolve() })
expect(fsWrite).toHaveBeenCalledTimes(1)
// scope, path, content, expectedMtimeMs
expect(fsWrite.mock.calls[0]![3]).toBe(123)
} finally {
mounted.unmount()
}
})
it('keeps the draft and offers a reload when the host refuses with fs-conflict', async () => {
fsWrite.mockRejectedValue(new SidebarApiError('fs-conflict', 'changed on disk'))
const onReload = vi.fn()
const mounted = renderRoot(createElement(TextEditor, viewerProps({ onReload })))
try {
click(saveButton(mounted.container))
await act(async () => { await Promise.resolve() })
const text = mounted.container.textContent ?? ''
expect(text).toContain('changed on disk since it was loaded')
const reload = Array.from(mounted.container.querySelectorAll('button'))
.find(button => button.textContent === 'Reload from disk')
expect(reload).toBeDefined()
click(reload!)
expect(onReload).toHaveBeenCalledTimes(1)
} finally {
mounted.unmount()
}
})
it('does not show the conflict banner for an ordinary save failure', async () => {
fsWrite.mockRejectedValue(new SidebarApiError('fs-error', 'disk full'))
const mounted = renderRoot(createElement(TextEditor, viewerProps()))
try {
click(saveButton(mounted.container))
await act(async () => { await Promise.resolve() })
expect(mounted.container.textContent ?? '').not.toContain('changed on disk')
} finally {
mounted.unmount()
}
})
it('adopts the baseline the host reports after a successful save', async () => {
fsWrite.mockResolvedValue({ ok: true, mtimeMs: 456 })
const mounted = renderRoot(createElement(TextEditor, viewerProps()))
try {
click(saveButton(mounted.container))
await act(async () => { await Promise.resolve() })
click(saveButton(mounted.container))
await act(async () => { await Promise.resolve() })
expect(fsWrite).toHaveBeenCalledTimes(2)
expect(fsWrite.mock.calls[0]![3]).toBe(123)
expect(fsWrite.mock.calls[1]![3]).toBe(456)
} finally {
mounted.unmount()
}
})
})