Files
apes-sidebar/tests/reference-mention.spec.ts
T
Menghuan1918 9c8d5c62ed merge #574 into dev(工作区提及重放到 reference-in-chat 新载体,复用 mentionFor)
冲突是载体搬迁,不是语义作废:

- `@` 回调的载体在 dev 已是 src/client/reference-in-chat.ts(#479 eed2fee 抽出的
  两侧共享入口)。PR 新增的 `insertWorkspaceReference` 与 `directoryMention`
  落在 conversation-draft.ts(它要用同模块的 appendToDraft /
  insertFileReference / mentionFor),由该入口转发;Sidebar.tsx 只保留转发,
  native/tab-adapter.tsx 与之同步,两个调用点不再传 cwd(工作区 root 由
  `ctx.get('workspaces')` 的会话成员匹配得到,不再回退会话 cwd)。
- PR 的 `directoryMention` 改为委托 dev 的共用构造器 `mentionFor(path, 'folder')`
  (#479 把「尾斜杠进引号」的规则收进它),不再自建一套引号规则;
  `insertWorkspaceReference` 的文件分支同样换成 `mentionFor(path, 'file')`。
- tests/paths.spec.ts:`resolveSidebarPath` 的导入走已搬迁的 src/client/paths.ts
  (produced-files.ts 已不存在),并存的 preview URL 用例与 workspaceRelativePath
  用例两块都保留。
- tests/reference-in-chat.spec.ts(#479 验收 spec):fake ctx 补上它现在要读的
  workspaces 面、调用点去掉 cwd,三处草稿断言跟随本 PR 的目录尾随分隔空格;
  引号与宿主 token 可读性的断言逐字未动。

Refs #574
2026-10-05 01:55:36 +08:00

61 lines
2.3 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { directoryMention, mentionFor } from '../src/client/conversation-draft.ts'
describe('mentionFor', () => {
it('formats a plain relative file path as an @file mention', () => {
expect(mentionFor('src/client/paths.ts', 'file')).toEqual({
mention: '@src/client/paths.ts',
label: 'paths.ts',
})
})
it('quotes a path containing whitespace and keeps the full basename', () => {
expect(mentionFor('docs/plan files/design notes.md', 'file')).toEqual({
mention: '@"docs/plan files/design notes.md"',
label: 'design notes.md',
})
})
it('trims a trailing separator before deriving the basename', () => {
expect(mentionFor('src/client/', 'file')).toEqual({
mention: '@src/client',
label: 'client',
})
})
it('rejects embedded quotes and control characters for both kinds', () => {
expect(mentionFor('src/a"b.ts', 'file')).toBeUndefined()
expect(mentionFor('src/a\u0000b.ts', 'file')).toBeUndefined()
expect(mentionFor('src/a"b', 'folder')).toBeUndefined()
})
it('keeps the trailing slash on a plain folder mention', () => {
expect(mentionFor('docs', 'folder')).toEqual({ mention: '@docs/', label: 'docs' })
expect(mentionFor('docs/', 'folder')).toEqual({ mention: '@docs/', label: 'docs' })
// The cwd itself stays the relative root spelling the composer expects.
expect(mentionFor('.', 'folder')).toEqual({ mention: '@./', label: '.' })
})
it('closes the quote around the trailing slash of a folder with whitespace', () => {
expect(mentionFor('my dir', 'folder')).toEqual({ mention: '@"my dir/"', label: 'my dir' })
})
})
describe('directoryMention', () => {
it.each([
['src', '@src/'],
['my dir', '@"my dir/"'],
['docs/my dir/notes here', '@"docs/my dir/notes here/"'],
['src/', '@src/'],
['.', '@./'],
['src\\my dir\\', '@"src/my dir/"'],
['src\\my dir/nested', '@"src/my dir/nested/"'],
])('formats %s as a complete plain-text directory token', (path, mention) => {
expect(directoryMention(path)).toBe(mention)
})
it.each(['a"b', 'a\n b', 'a\t b', 'a\u0000b', 'a\u007fb', 'a\u0085b'])('rejects an unrepresentable directory %j', path => {
expect(directoryMention(path)).toBeUndefined()
})
})