Files
apes-sidebar/tests/commit-message.spec.ts
T
unsc9 ebc5d0b6b7 refactor: 精简 dev 相对 main 新增代码里的重复与白干(行为不变)
dev 分支的新增代码过了一轮「代码精简」审查(复用 / 简化 / 效率 / 修在哪一层四个角度,
分宿主侧、客户端外壳、客户端功能面板三片,另加测试与脚本),这里只落地行为完全不变的那批:

- `runGitRaw` 是 `runGit` 的纯转发别名(三个角度都点到):导出 `runGit`、删掉别名,
  `active-worktree.ts` 与它的 `vi.mock` 键同步改名。
- `open-external.ts` 的 `messageOf` 与 `fs-tree.ts` 的导出逐字相同:删本地实现改 import。
- `fs-operations.ts` 的单段名校验抄了三遍(注释却自称 shared rule):提成
  `assertSingleSegmentName`,rename / mkdir / 新建文件三处共用。
- `fs-tree.ts` 两个探测函数各抄一遍有界并发池:提成 `forEachRow`。
- `index.ts` 自己复刻了一遍 `request/header` 的 provider/model 读取:改调
  `collectModelRoutes(events, 1)[0]`;`lowestReasoningEffortOf` 的三态循环收成
  best/bestRank;`formatModelRoute` 零调用点,删掉(连带自测)。
- `state.ts`:`markUnread` / `clearUnread` 的当前会话路径改走已有的 `reduce`
  (「归约器返回同引用即 no-op」本来就在 reduce 里),删掉私有的 `replaceCurrent`;
  非当前会话那条仍只写内存不落盘,行为未动。
- `editor-dirty.ts`:`DirtyEntry.sessionId` 只服务一个没有生产调用者的
  `dirtyCountForSession`,会话隔离已由 `sessionId::tabId` 这个键承载,两个一起删,
  用例改断 `isEditorDirty(tabId, sessionId)`。
- `FileTree.tsx`:同一批行原先 `renderLevel` 与 `visibleRows` 各排一次,收进按
  `(data, sort)` 记忆的 `entriesFor`(照 `chainFor` 的形状)。
- `dock-fallback.tsx`:500ms 重测出的坐标没变就不换 state 对象(省掉一次无意义重渲染)。
- 其余同形收口:`DiffPane` 的死分支、`terminal-client` 的 `bottomTerminalKey`
  (与 `bottomTerminalContentId` 同值,调用点复用同一个 id)、`SideChatView` 的
  O(线程×目录) 排序扫描降为 Map、`conversation-draft` 的末段路径复用 `paths.baseName`、
  `TreePanel` 的目录命中集按 results 记忆。

验证:`tsc --noEmit` 只剩改动前那 3 条缺 `@xterm/*` 的既有报错;eslint 干净;
全套 vitest 与改动前的失败集合逐条一致(13 条,均为环境原因:缺 xterm 的构建/转换类
3 条、Windows 符号链接权限 3 条、语言字典串味 4 条、真起 git 的冒烟 1 条、文件级 1 条)。
2026-10-09 16:04:24 +08:00

177 lines
7.3 KiB
TypeScript

/**
* Commit-message suggestion contract: the prompt halves the host sends, the
* `provider/model` route vocabulary behind the Git card's pinned-model
* setting, and the cleanup the panel applies before the text lands in the
* commit box. Pure module (no cordis, no git, no LLM) — every case here is a
* wire/contract guarantee, not an implementation detail.
*/
import { describe, expect, it } from 'vitest'
import {
buildCommitPrompt,
cleanSuggestion,
collectModelRoutes,
defaultRouteOf,
lowestReasoningEffortOf,
modelEntryOf,
normalizeLanguage,
parseModelRoute,
providerEntryOf,
SUGGEST_DIFF_LIMIT,
truncateDiff,
} from '../src/commit-message.ts'
describe('parseModelRoute', () => {
it('splits on the FIRST slash (a model id may carry its own)', () => {
expect(parseModelRoute('deepseek/deepseek-chat')).toEqual({ provider: 'deepseek', model: 'deepseek-chat' })
expect(parseModelRoute('openai/vendor/gpt-4o')).toEqual({ provider: 'openai', model: 'vendor/gpt-4o' })
})
it('trims surrounding whitespace', () => {
expect(parseModelRoute(' deepseek / deepseek-chat ')).toEqual({ provider: 'deepseek', model: 'deepseek-chat' })
})
it('rejects anything that cannot form a route (the caller falls back)', () => {
for (const value of [undefined, null, '', ' ', 'deepseek', '/model', 'provider/', 42, {}]) {
expect(parseModelRoute(value), String(value)).toBeUndefined()
}
})
})
describe('normalizeLanguage', () => {
it('is zh only on the exact marker, en otherwise', () => {
expect(normalizeLanguage('zh')).toBe('zh')
for (const value of ['en', 'ja', undefined, 0, {}]) {
expect(normalizeLanguage(value), String(value)).toBe('en')
}
})
})
describe('truncateDiff', () => {
it('leaves a short diff untouched and marks a truncated one', () => {
expect(truncateDiff('abc')).toBe('abc')
const long = 'x'.repeat(SUGGEST_DIFF_LIMIT + 100)
const cut = truncateDiff(long)
// The cut keeps exactly SUGGEST_DIFF_LIMIT characters of the patch and
// appends the marker — a huge diff can no longer grow without bound.
expect(cut).toBe(`${'x'.repeat(SUGGEST_DIFF_LIMIT)}\n...(diff truncated)`)
expect(cut.length).toBeLessThan(long.length)
})
})
describe('buildCommitPrompt', () => {
it('carries the file list and the diff in the user half', () => {
const prompt = buildCommitPrompt('en', ['a.ts', 'b.ts'], '+added')
expect(prompt.user).toContain('a.ts')
expect(prompt.user).toContain('b.ts')
expect(prompt.user).toContain('+added')
expect(prompt.system).toContain('Conventional Commits')
})
it('asks for the message alone (no prose the panel would commit)', () => {
expect(buildCommitPrompt('en', [], '').system).toContain('Output only the commit message')
expect(buildCommitPrompt('zh', [], '').system).toContain('只输出提交信息本身')
})
it('differs by language', () => {
expect(buildCommitPrompt('zh', [], '').system).not.toBe(buildCommitPrompt('en', [], '').system)
})
})
describe('collectModelRoutes', () => {
/** One `request/header` event as the session log carries it. */
function header(provider: string, model: string): { type: string; data: unknown } {
return { type: 'request/header', data: { header: { config: { provider, model } } } }
}
it('collects the routes a session actually used, newest first and de-duplicated', () => {
const events = [
{ type: 'user/message', data: {} },
header('deepseek', 'deepseek-chat'),
header('openai', 'gpt-4o'),
header('deepseek', 'deepseek-chat'),
]
expect(collectModelRoutes(events)).toEqual([
{ provider: 'deepseek', model: 'deepseek-chat' },
{ provider: 'openai', model: 'gpt-4o' },
])
})
it('honors the cap and ignores headerless / malformed rows', () => {
const events = [
header('p1', 'm1'),
{ type: 'request/header', data: {} },
{ type: 'request/header', data: { header: {} } },
header('p2', 'm2'),
header('p3', 'm3'),
]
expect(collectModelRoutes(events, 2).map(route => route.model)).toEqual(['m3', 'm2'])
expect(collectModelRoutes([])).toEqual([])
})
})
describe('lowestReasoningEffortOf', () => {
const effort = (id: string): { id: string; name: string } => ({ id, name: id })
it('picks the cheapest advertised level, whatever the adapter order', () => {
expect(lowestReasoningEffortOf([effort('high'), effort('off'), effort('max')])).toBe('off')
expect(lowestReasoningEffortOf([effort('high'), effort('low')])).toBe('low')
expect(lowestReasoningEffortOf([effort('medium'), effort('minimal')])).toBe('minimal')
})
it('never lets an unknown id outrank a known one, and falls back to the first entry', () => {
expect(lowestReasoningEffortOf([effort('turbo'), effort('high')])).toBe('high')
expect(lowestReasoningEffortOf([effort('turbo'), effort('brisk')])).toBe('turbo')
})
it('reports "no effort" for a model that advertises none', () => {
for (const value of [undefined, null, [], 'off', [{}], [{ id: '' }]]) {
expect(lowestReasoningEffortOf(value), JSON.stringify(value)).toBeUndefined()
}
})
})
describe('catalog reads (unknown harness shapes)', () => {
it('reads a provider from any plausible field and never invents one', () => {
expect(providerEntryOf({ provider: 'deepseek', name: 'DeepSeek' }))
.toEqual({ provider: 'deepseek', label: 'DeepSeek' })
// A differently-named registration must not become an empty route.
expect(providerEntryOf({ id: 'openai' })).toEqual({ provider: 'openai', label: 'openai' })
for (const value of [undefined, null, '', 42, {}, { name: 'no route' }]) {
expect(providerEntryOf(value), String(value)).toBeUndefined()
}
})
it('reads a model id/name tolerantly', () => {
expect(modelEntryOf({ id: 'deepseek-chat' })).toEqual({ id: 'deepseek-chat', name: 'deepseek-chat' })
expect(modelEntryOf({ model: 'gpt-4o', displayName: 'GPT-4o' })).toEqual({ id: 'gpt-4o', name: 'GPT-4o' })
// A display-only title is never an id: sending a localized label to a
// provider would fail at generation time instead of falling back.
expect(modelEntryOf({ title: 'unnamed' })).toBeUndefined()
expect(modelEntryOf(null)).toBeUndefined()
})
it('reads the harness default selection, or nothing', () => {
expect(defaultRouteOf({ provider: 'deepseek', model: 'deepseek-chat' }))
.toEqual({ provider: 'deepseek', model: 'deepseek-chat' })
for (const value of [undefined, null, {}, { provider: 'deepseek' }, { model: 'm' }]) {
expect(defaultRouteOf(value), String(value)).toBeUndefined()
}
})
})
describe('cleanSuggestion', () => {
it('drops a code fence the model wrapped around the message', () => {
expect(cleanSuggestion('```\nfeat: add a thing\n```')).toBe('feat: add a thing')
expect(cleanSuggestion('```text\nfix: broken\n```')).toBe('fix: broken')
})
it('keeps a legitimate multi-line message (subject + body)', () => {
expect(cleanSuggestion('feat: add a thing\n\n- one\n- two')).toBe('feat: add a thing\n\n- one\n- two')
})
it('trims blank runs and carriage returns', () => {
expect(cleanSuggestion('\r\n chore: tidy \n\n')).toBe('chore: tidy')
expect(cleanSuggestion(' ')).toBe('')
})
})