code review 挖出的六处行为问题: - 宿主只在气泡之后补空格,之前不补,所以往 hello 后插气泡会读成 hello[chip]。插入前先用同一条 span 通道占一个分隔位。 - 命令气泡(/goal)不在 occurrences 里(宿主只收引用气泡),但它在 detect 投影里同样只占一个字符。补上按 claim.token 折叠,否则 span 越界、引用插不进去;setDraft 分支也会把命令气泡碾成字面文本并丢掉 claim,所以 claimed 状态同样走 span 通道。 - 选区气泡的 ref 是围栏正文而非路径,标成 appearance:file 会让宿主 把它当可点开的文件,点一下拿整段正文去开文件。改为由调用方决定。 - 宿主可能已经应用了编辑再抛异常,此时报 false 会让调用方再插一份纯 文本。异常分支先重读 revision 再判断。 - 冻结期(adjudicating/submitting)直接拒绝,不回退。 顺带删掉 insertReferenceOrText 没人覆盖的 fallbackText 参数,修掉 insertWorkspaceReference 对同一路径重复调用 mentionFor。
167 lines
6.6 KiB
TypeScript
167 lines
6.6 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { createElement } from 'react'
|
|
import { createRoot } from 'react-dom/client'
|
|
import { act } from 'react-dom/test-utils'
|
|
import { Sidebar } from '../src/client/Sidebar.tsx'
|
|
import { createSidebarStore } from '../src/client/state.ts'
|
|
import { createBetterSidebarService } from '../src/client/service.ts'
|
|
import { setupReactAct } from './test-utils.ts'
|
|
|
|
setupReactAct()
|
|
|
|
afterEach(() => {
|
|
localStorage.clear()
|
|
vi.restoreAllMocks()
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
function mount(cwd: string, options: { root?: string; phase?: 'pending' | 'ready'; state?: 'idle' | 'loading' | 'error'; chip?: boolean; workspaceService?: boolean } = {}) {
|
|
vi.stubGlobal('WebSocket', class { close() {} })
|
|
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
const store = createSidebarStore()
|
|
store.setSession('s1')
|
|
const service = createBetterSidebarService(store)
|
|
const sessions = { current: 's1', byId: { s1: { id: 's1', cwd } } }
|
|
const locale = { active: 'en' }
|
|
let draft = 'before'
|
|
let draftRev = 1
|
|
const setDraft = vi.fn((text: string) => { draft = text; draftRev++ })
|
|
const emit = vi.fn((_event: string, _payload: unknown) => {
|
|
if (options.chip) draftRev++
|
|
})
|
|
const insertReference = vi.fn((_ref: unknown, _span: unknown) => {
|
|
if (!options.chip) return false
|
|
draftRev++
|
|
return true
|
|
})
|
|
const input = {
|
|
state: { getSnapshot: () => ({ draft, draftRev }) },
|
|
setDraft,
|
|
...options.chip === false ? {} : { insertReference },
|
|
}
|
|
const workspaces = { list: { getSnapshot: () => ({
|
|
phase: options.phase ?? 'ready',
|
|
state: options.state ?? 'idle',
|
|
items: [
|
|
{ path: '/decoy', sessionIds: ['other'] },
|
|
...(options.root === undefined ? [] : [{ path: options.root, sessionIds: ['s1'] }]),
|
|
],
|
|
}) } }
|
|
const ctx = {
|
|
locale: { subscribe: () => () => {}, getSnapshot: () => locale },
|
|
sessions: {
|
|
list: { subscribe: () => () => {}, getSnapshot: () => sessions },
|
|
scope: () => ({ emit }),
|
|
},
|
|
get: (name: string) => ({
|
|
betterSidebar: service,
|
|
conversation: { input: { for: () => input } },
|
|
workspaces: options.workspaceService === false ? undefined : workspaces,
|
|
})[name],
|
|
}
|
|
let reference: ((path: string, isDir: boolean) => void) | undefined
|
|
service.registerTab({ id: 'reference-test', title: 'Reference', component: props => {
|
|
reference = props.onReferenceFile
|
|
return null
|
|
} })
|
|
service.openTab({ type: 'reference-test', title: 'Reference' })
|
|
const container = document.createElement('div')
|
|
document.body.append(container)
|
|
const root = createRoot(container)
|
|
act(() => { root.render(createElement(Sidebar, { ctx: ctx as never, store })) })
|
|
expect(reference).toBeTypeOf('function')
|
|
return {
|
|
insert: (path: string, isDir: boolean) => { act(() => { reference!(path, isDir) }) },
|
|
draft: () => draft, emit, insertReference, setDraft, warn,
|
|
unmount: () => { act(() => { root.unmount() }); container.remove() },
|
|
}
|
|
}
|
|
|
|
describe('Sidebar workspace references', () => {
|
|
it.each(['/work', '/work/src'])('uses workspace membership with session cwd %s', cwd => {
|
|
const view = mount(cwd, { root: '/work' })
|
|
try {
|
|
view.insert('/work/my dir', true)
|
|
expect(view.draft()).toBe('before @"my dir/" ')
|
|
expect(view.emit).not.toHaveBeenCalled()
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it('uses workspace-relative file chips outside the session cwd', () => {
|
|
const view = mount('/work/src', { root: '/work', chip: true })
|
|
try {
|
|
view.insert('/work/docs/my notes.md', false)
|
|
// The draft `before` ends in a non-space, so a separator is emitted
|
|
// first: the revision and the append offset both move one place.
|
|
expect(view.insertReference).toHaveBeenCalledWith({
|
|
source: 'reference', ref: '@"docs/my notes.md"', label: 'my notes.md', appearance: 'file', clipboardText: '@"docs/my notes.md"',
|
|
}, { draftRev: 2, start: 7, end: 7 })
|
|
expect(view.setDraft).not.toHaveBeenCalled()
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it.each(['src', 'my dir', 'docs/my dir'])('separates a directory %s from the next file chip', directory => {
|
|
const view = mount('/work', { root: '/work', chip: true })
|
|
try {
|
|
view.insert(`/work/${directory}`, true)
|
|
const beforeFile = view.draft()
|
|
expect(beforeFile).toMatch(/\s$/)
|
|
view.insert('/work/notes.md', false)
|
|
expect(view.insertReference).toHaveBeenCalledWith({
|
|
source: 'reference', ref: '@notes.md', label: 'notes.md', appearance: 'file', clipboardText: '@notes.md',
|
|
}, { draftRev: 2, start: beforeFile.length, end: beforeFile.length })
|
|
expect(view.setDraft).toHaveBeenCalledTimes(1)
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it('keeps quoted file text when the chip event is not handled', () => {
|
|
const view = mount('/work', { root: '/work' })
|
|
try {
|
|
view.insert('/work/my notes.md', false)
|
|
expect(view.draft()).toBe('before @"my notes.md"')
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it('normalizes Windows drive casing and mixed separators', () => {
|
|
const view = mount('C:\\Work\\src', { root: 'C:\\Work\\' })
|
|
try {
|
|
view.insert('c:/WORK/my dir\\nested', true)
|
|
expect(view.draft()).toBe('before @"my dir/nested/" ')
|
|
view.insert('c:/work', true)
|
|
expect(view.draft()).toBe('before @"my dir/nested/" @./ ')
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it.each([
|
|
{ root: undefined },
|
|
{ root: '/work', workspaceService: false },
|
|
{ root: '/work', phase: 'pending' as const },
|
|
{ root: '/work', state: 'loading' as const },
|
|
{ root: '/work', state: 'error' as const },
|
|
])('leaves the draft intact and logs when workspace data is unavailable: %j', options => {
|
|
const view = mount('/work/src', options)
|
|
try {
|
|
view.insert('/work/src/a.ts', false)
|
|
expect(view.draft()).toBe('before')
|
|
expect(view.emit).not.toHaveBeenCalled()
|
|
expect(view.warn).toHaveBeenCalled()
|
|
// Resolve the host snapshot later: the next click must see fresh data.
|
|
Object.assign(options, { root: '/work', phase: 'ready', state: 'idle', workspaceService: true })
|
|
view.insert('/work/src', true)
|
|
expect(view.draft()).toBe('before @src/ ')
|
|
} finally { view.unmount() }
|
|
})
|
|
|
|
it.each(['/outside/file.ts', '/work/a"b', '/work/a\nb'])('never inserts an unsafe path %j as a file or directory', path => {
|
|
const view = mount('/work', { root: '/work' })
|
|
try {
|
|
view.insert(path, false)
|
|
view.insert(path, true)
|
|
expect(view.draft()).toBe('before')
|
|
expect(view.emit).not.toHaveBeenCalled()
|
|
expect(view.warn).toHaveBeenCalled()
|
|
} finally { view.unmount() }
|
|
})
|
|
})
|