冲突取舍: - 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>
216 lines
8.7 KiB
TypeScript
216 lines
8.7 KiB
TypeScript
/**
|
|
* The Git panel's remote routes over the real `/sidebar/api` surface:
|
|
* `git.push` / `git.pull` against a real bare remote (fast-forward succeeds,
|
|
* a diverged branch fails instead of opening a merge editor, a branch with no
|
|
* upstream reports git's own message).
|
|
*
|
|
* The AI commit-message suggestion that landed in the same upstream PR is NOT
|
|
* wired from here: this branch keeps the implementation merged from
|
|
* `port/pr-642` (`src/commit-message.ts` + its own specs), so the duplicate
|
|
* route contract those cases pinned (a folded `requestHeader()`, a 200-token
|
|
* budget) no longer exists.
|
|
*
|
|
* The route is mounted exactly as the plugin mounts it (see smoke.spec.ts).
|
|
*/
|
|
|
|
import { describe, expect, it } from 'vitest'
|
|
import { spawnSync } from 'node:child_process'
|
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { apply } from '../src/index.ts'
|
|
import type { SidebarWebRoute, SidebarWebUpgradeRoute } from '../src/context-types.ts'
|
|
|
|
/** Fixture commit identity, confined to this process: no git config is touched. */
|
|
const FIXTURE_IDENTITY = {
|
|
GIT_AUTHOR_NAME: 'dsh-better-sidebar-test',
|
|
GIT_AUTHOR_EMAIL: 'test@dsh.invalid',
|
|
GIT_COMMITTER_NAME: 'dsh-better-sidebar-test',
|
|
GIT_COMMITTER_EMAIL: 'test@dsh.invalid',
|
|
}
|
|
|
|
/** Run one git command (throws on a non-zero exit). */
|
|
function gitRun(cwd: string, args: string[]): string {
|
|
const result = spawnSync('git', ['-C', cwd, '--no-pager', '-c', 'color.ui=false', ...args], {
|
|
encoding: 'utf8',
|
|
env: { ...process.env, ...FIXTURE_IDENTITY },
|
|
})
|
|
if (result.status !== 0) throw new Error(result.stderr || `git ${args[0] ?? ''} exited with ${String(result.status)}`)
|
|
return result.stdout
|
|
}
|
|
|
|
/** A repo on `main` with one commit. */
|
|
function makeRepo(): string {
|
|
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-remote-'))
|
|
gitRun(dir, ['init', '-q'])
|
|
// Pin the eol policy: Git for Windows defaults to core.autocrlf=true, which
|
|
// would smudge the byte-exact diff assertions below.
|
|
gitRun(dir, ['config', 'core.autocrlf', 'false'])
|
|
gitRun(dir, ['checkout', '-q', '-b', 'main'])
|
|
writeFileSync(join(dir, 'a.txt'), 'one\ntwo\n')
|
|
gitRun(dir, ['add', '-A'])
|
|
gitRun(dir, ['commit', '-q', '-m', 'base'])
|
|
return dir
|
|
}
|
|
|
|
/** A working clone, a bare `origin` it tracks, and a peer clone of the same remote. */
|
|
interface RemoteFixture {
|
|
/** The scratch directory holding all three (the only thing to clean up). */
|
|
scratch: string
|
|
origin: string
|
|
work: string
|
|
peer: string
|
|
}
|
|
|
|
function makeRemoteFixture(): RemoteFixture {
|
|
const scratch = mkdtempSync(join(tmpdir(), 'dsh-sidebar-origin-'))
|
|
const origin = join(scratch, 'origin.git')
|
|
const work = join(scratch, 'work')
|
|
const peer = join(scratch, 'peer')
|
|
gitRun(scratch, ['init', '-q', '--bare', origin])
|
|
// A bare repo's HEAD defaults to the machine's init.defaultBranch, and a
|
|
// clone whose HEAD names a branch that does not exist yet checks out
|
|
// NOTHING. Pin it so both clones start on the branch the tests push.
|
|
gitRun(origin, ['symbolic-ref', 'HEAD', 'refs/heads/main'])
|
|
gitRun(scratch, ['clone', '-q', origin, work])
|
|
gitRun(work, ['config', 'core.autocrlf', 'false'])
|
|
writeFileSync(join(work, 'a.txt'), 'one\n')
|
|
gitRun(work, ['add', '-A'])
|
|
gitRun(work, ['commit', '-q', '-m', 'base'])
|
|
gitRun(work, ['push', '-q', '-u', 'origin', 'main'])
|
|
gitRun(scratch, ['clone', '-q', origin, peer])
|
|
gitRun(peer, ['config', 'core.autocrlf', 'false'])
|
|
return { scratch, origin, work, peer }
|
|
}
|
|
/**
|
|
* Mount the plugin's `/sidebar/api` route against a minimal fake context. The
|
|
* session carries NO header cwd on purpose: the payload's cwd must win, which
|
|
* is how the real client addresses a session's workspace.
|
|
*/
|
|
function mount(): { route: SidebarWebRoute } {
|
|
const routes: SidebarWebRoute[] = []
|
|
const ctx = {
|
|
webRuntime: { trustedHosts: [] },
|
|
webServer: {
|
|
register: (route: SidebarWebRoute) => { routes.push(route); return () => {} },
|
|
registerUpgrade: (_route: SidebarWebUpgradeRoute) => () => {},
|
|
},
|
|
sessions: { get: (_id: string) => ({ header: {}, snapshotEvents: () => [] }) },
|
|
tools: { register: () => () => {} },
|
|
// The vendored cordis runs registration effects immediately.
|
|
effect: (fn: () => void | (() => void)) => { fn() },
|
|
inject: () => () => {},
|
|
on: () => () => {},
|
|
get: () => undefined,
|
|
}
|
|
apply(ctx as never)
|
|
const route = routes.find(entry => entry.path === '/sidebar/api')
|
|
if (route === undefined) throw new Error('the /sidebar/api route was not registered')
|
|
return { route }
|
|
}
|
|
interface Invoked<T> {
|
|
ok: boolean
|
|
status: number
|
|
value?: T
|
|
error?: { code?: string; message: string }
|
|
}
|
|
|
|
async function invoke<T = unknown>(route: SidebarWebRoute, method: string, payload: unknown): Promise<Invoked<T>> {
|
|
const body = Buffer.from(JSON.stringify(payload))
|
|
const req = {
|
|
method: 'POST',
|
|
url: `/sidebar/api/${method}`,
|
|
headers: { host: '127.0.0.1:3080' },
|
|
[Symbol.asyncIterator]: async function* () { yield body },
|
|
} as never
|
|
const out = { status: 200, body: '' }
|
|
const res = {
|
|
writeHead: (status: number) => { out.status = status },
|
|
end: (chunk: unknown) => { out.body += String(chunk ?? '') },
|
|
} as never
|
|
await route.handler(req, res)
|
|
return { ...JSON.parse(out.body) as Invoked<T>, status: out.status }
|
|
}
|
|
|
|
describe('git remote routes (real repository + bare origin)', () => {
|
|
it('pushes the current branch to its upstream', async () => {
|
|
const fixture = makeRemoteFixture()
|
|
try {
|
|
writeFileSync(join(fixture.work, 'a.txt'), 'one\ntwo\n')
|
|
gitRun(fixture.work, ['add', '-A'])
|
|
gitRun(fixture.work, ['commit', '-q', '-m', 'second'])
|
|
const local = gitRun(fixture.work, ['rev-parse', 'main']).trim()
|
|
|
|
const { route } = mount()
|
|
const result = await invoke(route, 'git.push', { sessionId: 's-push', cwd: fixture.work })
|
|
|
|
expect(result).toMatchObject({ ok: true, value: { ok: true } })
|
|
// The remote actually moved — the route ran a real `git push`.
|
|
expect(gitRun(fixture.origin, ['rev-parse', 'main']).trim()).toBe(local)
|
|
} finally {
|
|
rmSync(fixture.scratch, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('reports git\'s own failure for a branch with no upstream', async () => {
|
|
const dir = makeRepo()
|
|
try {
|
|
const { route } = mount()
|
|
const result = await invoke(route, 'git.push', { sessionId: 's-noup', cwd: dir })
|
|
|
|
// git-error, never a silent success: the panel shows the message and the
|
|
// user sets the tracking in a terminal.
|
|
expect(result.ok).toBe(false)
|
|
expect(result.error?.code).toBe('git-error')
|
|
expect(result.error?.message ?? '').not.toBe('')
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('pulls a fast-forward onto the peer\'s commit', async () => {
|
|
const fixture = makeRemoteFixture()
|
|
try {
|
|
writeFileSync(join(fixture.peer, 'b.txt'), 'from peer\n')
|
|
gitRun(fixture.peer, ['add', '-A'])
|
|
gitRun(fixture.peer, ['commit', '-q', '-m', 'peer work'])
|
|
gitRun(fixture.peer, ['push', '-q', 'origin', 'main'])
|
|
|
|
const { route } = mount()
|
|
const pulled = await invoke(route, 'git.pull', { sessionId: 's-pull', cwd: fixture.work })
|
|
|
|
expect(pulled).toMatchObject({ ok: true, value: { ok: true } })
|
|
expect(readFileSync(join(fixture.work, 'b.txt'), 'utf8')).toBe('from peer\n')
|
|
} finally {
|
|
rmSync(fixture.scratch, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('refuses a diverged branch instead of merging, leaving no merge to resolve', async () => {
|
|
const fixture = makeRemoteFixture()
|
|
try {
|
|
writeFileSync(join(fixture.work, 'local.txt'), 'local\n')
|
|
gitRun(fixture.work, ['add', '-A'])
|
|
gitRun(fixture.work, ['commit', '-q', '-m', 'local work'])
|
|
writeFileSync(join(fixture.peer, 'peer.txt'), 'peer\n')
|
|
gitRun(fixture.peer, ['add', '-A'])
|
|
gitRun(fixture.peer, ['commit', '-q', '-m', 'peer work'])
|
|
gitRun(fixture.peer, ['push', '-q', 'origin', 'main'])
|
|
const headBefore = gitRun(fixture.work, ['rev-parse', 'HEAD']).trim()
|
|
|
|
const { route } = mount()
|
|
const diverged = await invoke(route, 'git.pull', { sessionId: 's-pull', cwd: fixture.work })
|
|
|
|
// --ff-only: the failure is the point. A merge here would strand a
|
|
// headless panel inside an editor nobody can answer.
|
|
expect(diverged.ok).toBe(false)
|
|
expect(diverged.error?.code).toBe('git-error')
|
|
expect(gitRun(fixture.work, ['rev-parse', 'HEAD']).trim()).toBe(headBefore)
|
|
expect(gitRun(fixture.work, ['status', '--porcelain'])).toBe('')
|
|
expect(() => readFileSync(join(fixture.work, '.git', 'MERGE_HEAD'), 'utf8')).toThrow()
|
|
} finally {
|
|
rmSync(fixture.scratch, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|