Files
apes-sidebar/tests/commit-model-settings.spec.tsx
T
Menghuan1918 0425c52c98 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
2026-10-05 02:19:58 +08:00

186 lines
6.7 KiB
TypeScript

/**
* The Git card's pinned-model panel: the follow switch gates the route box,
* and every candidate is displayed as the SAME string the host dispatches on
* (`provider/model`) — a catalog entry must not read as a decorated
* "<provider> · <model>" while the default reads as a route.
*/
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { CommitModelSettings } from '../src/client/changes/CommitModelSettings.tsx'
import { createSidebarStore } from '../src/client/state.ts'
import { api, type GitModelCatalog } from '../src/client/api.ts'
import type { BetterSidebarService } from '../src/client/service.ts'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
const CATALOG: GitModelCatalog = {
llm: true,
providers: [{
provider: 'deepseek',
name: 'DeepSeek',
models: [{ id: 'deepseek-chat', name: 'DeepSeek Chat' }],
}],
recent: [{ provider: 'openai', model: 'gpt-4o' }],
default: { provider: 'deepseek', model: 'deepseek-chat' },
}
/** Mount the panel with one session attached and the catalog already loaded. */
async function mount(pluginSettings: Record<string, unknown>): Promise<{
container: HTMLElement
writes: unknown[]
unmount: () => void
}> {
vi.spyOn(api, 'gitModels').mockResolvedValue(CATALOG)
const writes: unknown[] = []
const store = createSidebarStore()
store.setSession('session')
const container = document.createElement('div')
document.body.append(container)
const root: Root = createRoot(container)
await act(async () => {
root.render(createElement(CommitModelSettings, {
store,
service: {} as unknown as BetterSidebarService,
prefs: store.getPrefs(),
pluginSettings,
updatePluginSetting: (_key: string, value: unknown) => { writes.push(value) },
close: () => {},
}))
})
await act(async () => { await Promise.resolve() })
await act(async () => { await Promise.resolve() })
return {
container,
writes,
unmount: () => {
act(() => { root.unmount() })
container.remove()
},
}
}
function routeInput(container: HTMLElement): HTMLInputElement {
const input = container.querySelector<HTMLInputElement>('input[type="text"]')
if (input === null) throw new Error('the route input is missing')
return input
}
function followSwitch(container: HTMLElement): HTMLInputElement {
const box = container.querySelector<HTMLInputElement>('input[type="checkbox"]')
if (box === null) throw new Error('the follow switch is missing')
return box
}
afterEach(() => { vi.restoreAllMocks() })
describe('CommitModelSettings', () => {
it('follows the conversation by default and disables the route box while it does', async () => {
const { container, unmount } = await mount({})
try {
expect(followSwitch(container).checked).toBe(true)
expect(routeInput(container).disabled).toBe(true)
} finally {
unmount()
}
})
it('enables the box when following is switched off', async () => {
const { container, unmount } = await mount({})
try {
const box = followSwitch(container)
await act(async () => { box.click() })
expect(box.checked).toBe(false)
expect(routeInput(container).disabled).toBe(false)
} finally {
unmount()
}
})
it('lists every candidate as the route it would dispatch on', async () => {
const { container, unmount } = await mount({})
try {
await act(async () => { followSwitch(container).click() })
const input = routeInput(container)
await act(async () => {
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!
setter.call(input, 'deepseek')
input.dispatchEvent(new Event('input', { bubbles: true }))
})
await act(async () => { await Promise.resolve() })
// The portalled menu renders outside this container.
const menuText = document.body.textContent ?? ''
expect(menuText).toContain('deepseek/deepseek-chat')
// The decorated catalog rendering would show the provider label instead.
expect(menuText).not.toContain('DeepSeek · DeepSeek Chat')
} finally {
unmount()
}
})
it('shows the stored route on its own line and keeps the search box empty', async () => {
const { container, unmount } = await mount({ commitModel: 'openai/gpt-4o' })
try {
// A stored route means the switch starts OFF; the route is displayed,
// but it is NOT projected into the search box (which would then filter
// the menu down to the already-selected model).
expect(followSwitch(container).checked).toBe(false)
expect(container.textContent).toContain('Current: openai/gpt-4o')
expect(routeInput(container).value).toBe('')
} finally {
unmount()
}
})
it('reopens the menu over every candidate while a model is already pinned', async () => {
const { container, unmount } = await mount({ commitModel: 'deepseek/deepseek-chat' })
try {
const input = routeInput(container)
await act(async () => { input.focus() })
await act(async () => { await Promise.resolve() })
const menuText = document.body.textContent ?? ''
expect(menuText).toContain('deepseek/deepseek-chat')
// The other candidate proves the list is not filtered by the selection.
expect(menuText).toContain('openai/gpt-4o')
} finally {
unmount()
}
})
it('commits a hand-typed route and clears the filter', async () => {
const { container, writes, unmount } = await mount({})
try {
await act(async () => { followSwitch(container).click() })
const input = routeInput(container)
await act(async () => {
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!
setter.call(input, 'vendor/model-x')
input.dispatchEvent(new Event('input', { bubbles: true }))
})
await act(async () => {
input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }))
})
expect(writes.at(-1)).toBe('vendor/model-x')
expect(input.value).toBe('')
} finally {
unmount()
}
})
it('clears the pinned route when following is restored', async () => {
const { container, writes, unmount } = await mount({ commitModel: 'openai/gpt-4o' })
try {
await act(async () => { followSwitch(container).click() })
expect(writes.at(-1)).toBe('')
} finally {
unmount()
}
})
})