Files
apes-sidebar/tests/commit-message.spec.ts
T
Menghuan1918andrainmanhhh 6615f63602 feat(git): AI 生成提交信息(Ctrl+G、多行提交框、可固定生成模型)
移植 PR #642(refs/tmp/pr642 → 当前 dev 重写,不机械解冲突)。

复用(与 dev clean 或纯逻辑无冲突面):
- src/commit-message.ts 纯逻辑模块原样取用:prompt 契约、provider/model 路由
  词法、reasoning effort 取最低档、宽容的 provider/model 目录读取、建议文本清理。
- host 半边(git.suggest-message / git.commit-model / git.models 三条路由 +
  pinned / 会话 / agent-default-model 三级路由解析)按 dev 现状重贴:ctx.llm、
  agentDefaultModel、git.status/diff、sessionId 参数名逐条对照本机 0.2.0-rc.1 核实。
- 设置缝复用 dev 已有的 SidebarSettingsRenderProps + settings.render(PR 侧写在
  同一接口上,零适配)。
- 三个新 spec 原样取用,只有 git-suggest 的 harness 改了「visible 默认 true」——
  dev 的共享 git-status store 只在可见时订阅,不可见时 snapshot 恒 null,
  commit bar 根本不渲染(PR 作者当年跑绿的是本机未提交状态)。

丢弃 / 按 dev 重写:
- PR 的 GitLens 客户端半边基于 #471 重写前的 GitLens:提交栏在 dev 已是
  primitives Input + commitBar/commitRow/commitInput,且 Ctrl/Cmd+Enter 提交
  已存在(重复实现)。现按 dev 的 .commitRow 结构重做多行 textarea + 自动增高,
  保留既有快捷键,并追加 Ctrl/Cmd+G。
- 六个 Icon*16 具名导入在 0.2.0-rc.1 一个不剩(§3.1 第 2 条:缺导出 = render 期
  Element type is invalid),改用 IconSparkleRegular size={14}。
- CSS 类名跟随 dev 的 commit* 前缀(PR 的 gitCommit*/gitSuggest*/gitError 在 dev
  已不存在;错误行改走 dev 的 Notice,不新增自绘错误样式)。
- 消息 source kind 用生产者自有值 plugin:dsh-better-sidebar:0.1.7 起
  kind:'plugin' 会被 format v4 硬拒。
- locales:dev 已删 commitError 锚点,且 19 份词典与 locales.ts 必须键集相等,
  故按各文件在 commit 行后逐份插入,不整份取 PR 侧。

验收:pnpm typecheck 0;pnpm exec vitest run --maxWorkers=3 全绿(163 files /
1836 tests);新增 spec 单独跑绿;真机挂载冒烟未跑(见报告)。

Refs #642

Co-authored-by: rainmanhhh <13862623+rainmanhhh@users.noreply.github.com>
2026-10-05 02:19:58 +08:00

185 lines
7.6 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,
formatModelRoute,
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('formatModelRoute', () => {
it('round-trips through parseModelRoute', () => {
const route = { provider: 'deepseek', model: 'deepseek-chat' }
expect(parseModelRoute(formatModelRoute(route))).toEqual(route)
})
})
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('')
})
})