Files
apes-sidebar/tests/git-remote-actions-ui.spec.tsx
T
Menghuan1918andAhdong&Shouey 3badac1981 Merge port/pr-434:Push / Pull(建议提交信息只留 port/pr-642 的一份实现)
冲突取舍:

- src/client/locales*.ts + locales.ts(20 份词典):两侧都是在同一锚点插入新键。保留 HEAD
  (#642 的 generateCommitMessage / generatingCommitMessage / suggestCommitError /
  suggestCommitEmpty / commitModel* 等)全部译文,只并入 434 的 push / pull / pushError /
  pullError 四键;同名键只留一份(重复插入会 TS1117 直接红)。
- src/client/api.ts:保留 #642 的 gitSuggestMessage(返回 message+provider+model)与
  gitModels / gitCommitModel,并入 434 的 gitPush / gitPull;删 434 的同名 gitSuggestMessage。
- src/index.ts:git.suggest-message 取 #642 的路由体(pinned → 会话自身 → agentDefaultModel
  三级模型回退、buildCommitPrompt/truncateDiff、最低 reasoningEffort、512 token、30s 超时、
  assembler.finish 判错),删 434 的 SUGGEST_DIFF_LIMIT / sessionModelRoute / 重复的
  @deepseek-ai/dsh-llm import;git.push / git.pull 两条路由(434)保留。
- src/client/changes/GitLens.tsx:以 #642 的提交栏为基(原生 textarea + commitInputWrap +
  commitBusyText + suggestButton + Ctrl+G + 模型 tooltip),删 434 的重复 suggestMessage 与
  Input+IconButton 版建议按钮;把 Push/Pull 两个按钮并进同一条 commitRow(不新增第二行),
  失败仍落既有那一条 actionError 状态行。
- src/client/changes/changes.module.css:删 434 的 .suggestSpinner + keyframes +
  prefers-reduced-motion 块与 .commitRemoteRow(#642 自带 suggestButton / commitBusyText)。
- src/context-types.ts:整块回退到 HEAD,删 434 的 requestHeader() / SidebarSessionRoute
  (#642 的模型路由不消费它,留着就是死接线)。
- 测试:tests/git-remote-actions.spec.ts 只留 push/pull 的 4 例宿主用例(434 的
  git.suggest-message 组钉的是被替换的契约:requestHeader 折叠与 200 token 预算);
  tests/git-suggest-ui.spec.tsx 改为 tests/git-remote-actions-ui.spec.tsx,只留 Push/Pull 一例
  (jsdom,钉调用参数与共用状态行);建议实现由 #642 自带的 commit-message / git-suggest /
  commit-model-settings 三个 spec 覆盖。
- docs/plans/2026-10-05-git-commit-suggest-and-remote-actions.md:改写为 Push/Pull 的移植
  记录,写明两处集成取舍(建议以 #642 为准并逐项列出删除的接线;文件树 rename 整块丢弃)。

Co-authored-by: Ahdong&Shouey <131278957+LiuStar656@users.noreply.github.com>
2026-10-05 02:41:34 +08:00

113 lines
4.4 KiB
TypeScript

/**
* The commit bar's Push / Pull affordances (the AI commit-message suggestion
* that landed in the same upstream PR is pinned by `git-suggest.spec.tsx`,
* which keeps the implementation merged from `port/pr-642`).
*
* These pin the WIRING the host routes cannot see — which args each action
* sends (scope, selected checkout) and that its failure lands on the bar's ONE
* status line, the same channel commit / stage failures use
* (`changes-tab.spec.tsx` pins that placement).
*
* The copy assertions go through `t()`, so they follow the active locale
* rather than pinning one language's strings.
*/
// @vitest-environment jsdom
import { afterEach, beforeEach, 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 { GitLens } from '../src/client/changes/GitLens.tsx'
import { createSidebarStore } from '../src/client/state.ts'
import { api, type GitStatusResult, type GitWorktree } from '../src/client/api.ts'
import { t } from '../src/client/locales.ts'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
const MAIN = 'C:/repo/main'
/** Install one inventory whose status carries `entries`. */
function mockGit(entries: Array<{ path: string; xy: string }>): void {
vi.spyOn(api, 'gitWorktrees').mockResolvedValue([
{ path: MAIN, branch: 'main', current: true, changes: entries.length },
] as GitWorktree[])
vi.spyOn(api, 'gitStatus').mockResolvedValue({ isRepo: true, branch: 'main', entries } as GitStatusResult)
vi.spyOn(api, 'gitBranch').mockResolvedValue({ current: 'main', names: ['main'] })
vi.spyOn(api, 'gitLog').mockResolvedValue([])
}
function mountGit(root: Root): void {
act(() => {
root.render(createElement(GitLens, {
scope: { sessionId: 'session', cwd: MAIN },
store: createSidebarStore(),
onOpenFile: () => {},
onPreview: () => {},
selectedRef: null,
visible: true,
refreshTick: 0,
}))
})
}
async function flushEffects(): Promise<void> {
// The refresh chain (inventory → branches/log → state) is several promise
// hops deep, so a shallow flush asserts on rows still in flight.
for (let round = 0; round < 5; round += 1) await act(async () => { await Promise.resolve() })
}
function makeRoot(): { container: HTMLDivElement; root: Root } {
const container = document.createElement('div')
document.body.append(container)
return { container, root: createRoot(container) }
}
/** One text button of the commit bar, by its label. */
function textButton(container: HTMLElement, label: string): HTMLButtonElement {
const button = [...container.querySelectorAll<HTMLButtonElement>('button')]
.find(candidate => candidate.textContent === label)
if (button === undefined) throw new Error(`no commit-bar button labelled "${label}"`)
return button
}
/** One rendered error line of the commit bar. */
function errorLine(container: HTMLElement, text: string): HTMLElement | undefined {
return [...container.querySelectorAll<HTMLElement>('[role="alert"]')]
.find(node => (node.textContent ?? '').includes(text))
}
beforeEach(() => {
Object.defineProperty(globalThis.navigator, 'language', { value: 'zh-CN', configurable: true })
})
afterEach(() => {
vi.restoreAllMocks()
document.body.innerHTML = ''
})
describe('GitLens commit bar: remote actions', () => {
it('pushes and pulls the selected checkout, reporting failures on the same status line', async () => {
mockGit([{ path: 'src/a.ts', xy: ' M' }])
const push = vi.spyOn(api, 'gitPush').mockResolvedValue({ ok: true })
const pull = vi.spyOn(api, 'gitPull').mockRejectedValue(new Error('Not possible to fast-forward'))
const { container, root } = makeRoot()
try {
mountGit(root)
await flushEffects()
await act(async () => { textButton(container, t('push')).click() })
await flushEffects()
expect(push).toHaveBeenCalledWith(expect.objectContaining({ sessionId: 'session' }), MAIN)
await act(async () => { textButton(container, t('pull')).click() })
await flushEffects()
expect(pull).toHaveBeenCalledWith(expect.objectContaining({ sessionId: 'session' }), MAIN)
expect(errorLine(container, `${t('pullError')}: Not possible to fast-forward`)).toBeDefined()
} finally {
act(() => { root.unmount() })
container.remove()
}
})
})