Files
deepseek-harness/packages/experimental/client-ui-forge/tests/parse.host.spec.ts
T
LegGasai 0c8078e196 feat(forge): name worktree branches, start them from remote branches, and show changes being read (#8536)
* feat(forge): name worktree branches, start them from remote branches, and show changes being read

The New Session worktree option's base menu now searches local branches and
each remote's branches, pins the current branch and every remote's default
branch, and names each remote group by its repository. Its last row opens a
dialog that names the new branch, starting from `worktreePrefix` and checking
Git's branch-name rules; the Host refuses an invalid or taken name. A request
records the full ref of its base and the optional name; every attempt uses the
name.

A remote-tracking base is fetched first: the worktree service gains a
`fetch` request field and a `fetch` phase, fetching only that branch without
tags. A failed fetch leaves the message waiting with Retry, Use the last
fetched version, and Continue without a worktree; a taken name offers Rename.

While the changes view reads changes it has not shown yet, it says
"Reading changes" above file card placeholders, and the files menu names no
count.

* fix(worktree): forward only the declared create_worktree arguments

The create_worktree parameter object is open, and the tool passed it to the
worktree runtime unchanged. Now that a runtime request can ask to fetch, an
undeclared `fetch: true` from the model would have fetched, or refused a local
`from`. The tool now forwards only `name` and `from`, so it never fetches.

* docs(forge): say that only generated worktree branch names come from the Session id

* fix(forge): keep the collapsed change request status on one line

In the Session panel the collapsed card's number line kept every part at
full width inside a clipping row, so a narrow panel cut the most urgent
flag off without an ellipsis, such as "有冲突" after "3 项检查失败".

The number and status now form one MarqueeText line as wide as the row.
While the line with failed checks does not fit, they take the shorter
wording without their count; a line that still does not fit ends in an
ellipsis and crawls into view with the title while the pointer rests on
the card's head. The line measures again when its width or the fonts
change. Expanded cards keep their branch names as before.

* fix(forge): hide remote credentials and read the branch a tag shares its name with

The base menu named an unrecognized remote's group by its fetch URL as
`git remote -v` prints it, so a token in the URL, such as
`https://oauth2:[email protected]/...`, reached the browser and showed
in the menu. The group now shows the URL without its user name and password.

Inspection read the checked-out branch with `symbolic-ref --short HEAD`,
which prints `heads/main` while a tag is also named `main`; the New Session
option then requested `refs/heads/heads/main` and every worktree attempt
failed. Inspection now reads the full ref and takes the name under
`refs/heads/`, so the option, the pinned current branch, and the checkout
row name `main`. HEAD that names a ref outside `refs/heads/` reads as no
branch.

* test(worktree): set up nested remotes and line endings the CI runners' Git accepts

Git 2.55 on the CI runners refuses `git remote add team` next to an
existing `team/fork` remote; the check runs only in `git remote add`, so
the longest-remote case now writes the second remote with `git config`, as
configuration written earlier or by hand holds it. The test clone also
turns off `core.autocrlf`, as `repository` does, because the Windows
runner's global setting checked the fetched file out with CRLF.
2026-11-16 16:13:00 +00:00

203 lines
9.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
parseBaseBranches, parseName, parseRemotes, parseTopLevel, parseWorktreeBases, remoteOrder, remoteRepository, upstreamBranch,
} from '../src/parse.ts'
import type { GitRemote } from '../src/parse.ts'
describe('parseTopLevel', () => {
it('reads the first line', () => {
expect(parseTopLevel('/work/repo\n')).toBe('/work/repo')
expect(parseTopLevel('C:/work/repo\r\n')).toBe('C:/work/repo')
expect(parseTopLevel('/work/repo/.git\nignored\n')).toBe('/work/repo/.git')
})
it('rejects empty output', () => {
expect(() => parseTopLevel('')).toThrow('git rev-parse printed no directory')
expect(() => parseTopLevel('\n')).toThrow('git rev-parse printed no directory')
})
})
describe('parseName', () => {
it('reads the first line', () => {
expect(parseName('main\n')).toBe('main')
expect(parseName('origin/feat/x\r\nignored\n')).toBe('origin/feat/x')
})
it('reads no name from empty output', () => {
expect(parseName('')).toBeNull()
expect(parseName('\n')).toBeNull()
})
})
describe('parseWorktreeBases', () => {
const remotes: GitRemote[] = [
{ name: 'upstream', urls: ['https://github.com/acme/demo.git'] },
{ name: 'origin', urls: ['[email protected]:me/demo.git'] },
{ name: 'team/fork', urls: ['/srv/team.git'] },
{ name: 'empty', urls: [] },
]
const output = [
'refs/remotes/upstream/HEAD\0refs/remotes/upstream/master',
'refs/remotes/origin/HEAD\0refs/remotes/origin/main',
'refs/remotes/team/fork/HEAD\0refs/remotes/team/fork/gone',
'refs/remotes/empty/HEAD\0refs/heads/main',
'refs/remotes/origin/alias\0refs/remotes/origin/main',
'refs/heads/alias\0refs/heads/main',
'refs/heads/topic\0',
'refs/remotes/upstream/master\0',
'refs/remotes/origin/main\0',
'refs/remotes/team/fork/feature\0',
'refs/remotes/upstream/release/2.4\0',
'refs/remotes/upstream/dev\0',
'refs/heads/main\0',
'refs/remotes/stray/main\0',
'refs/heads/Fix/Login\0',
'refs/tags/v1\0',
'',
].join('\r\n')
const repositories = (remote: GitRemote): string | null => remote.urls[0] ?? null
it('pins the checked-out branch and each remote default, then groups local branches and each remote in remote order', () => {
expect(parseWorktreeBases(output, 'main', remotes, '', 10, repositories)).toEqual({
current: { ref: 'refs/heads/main', name: 'main' },
defaults: [
{ ref: 'refs/remotes/upstream/master', name: 'upstream/master', remote: 'upstream' },
{ ref: 'refs/remotes/origin/main', name: 'origin/main', remote: 'origin' },
],
local: [{ ref: 'refs/heads/topic', name: 'topic' }, { ref: 'refs/heads/Fix/Login', name: 'Fix/Login' }],
remotes: [
{
name: 'upstream',
repository: 'https://github.com/acme/demo.git',
branches: [{ ref: 'refs/remotes/upstream/release/2.4', name: 'upstream/release/2.4' }, { ref: 'refs/remotes/upstream/dev', name: 'upstream/dev' }],
},
{ name: 'team/fork', repository: '/srv/team.git', branches: [{ ref: 'refs/remotes/team/fork/feature', name: 'team/fork/feature' }] },
],
})
})
it('keeps the branches whose names contain the query, ignoring case, at most `limit` in each group', () => {
expect(parseWorktreeBases(output, 'main', remotes, ' FIX ', 10, repositories)).toEqual({ current: null, defaults: [], local: [{ ref: 'refs/heads/Fix/Login', name: 'Fix/Login' }], remotes: [] })
const limited = parseWorktreeBases(output, null, remotes, '', 1, repositories)
expect(limited.current).toBeNull()
expect(limited.local).toEqual([{ ref: 'refs/heads/topic', name: 'topic' }])
expect(limited.remotes.map(remote => remote.branches.length)).toEqual([1, 1])
})
it('names no current branch on a detached HEAD or an unborn branch', () => {
expect(parseWorktreeBases(output, null, remotes, '', 10, repositories).current).toBeNull()
expect(parseWorktreeBases('', 'main', [], '', 10, repositories)).toEqual({ current: null, defaults: [], local: [], remotes: [] })
})
})
describe('remoteRepository', () => {
const recognize = (url: string) => url.startsWith('https://host.test/') ? { owner: 'acme', name: 'demo' } : undefined
it('names the first recognized repository, else the first URL, else nothing', () => {
expect(remoteRepository({ name: 'origin', urls: ['/srv/demo.git', 'https://host.test/acme/demo.git'] }, recognize)).toBe('acme/demo')
expect(remoteRepository({ name: 'fork', urls: ['/srv/fork.git'] }, recognize)).toBe('/srv/fork.git')
expect(remoteRepository({ name: 'empty', urls: [] }, recognize)).toBeNull()
})
it.each([
['a user name and token', 'https://oauth2:[email protected]/group/repo.git', 'https://gitlab.internal.test/group/repo.git'],
['a token as the user name', 'https://[email protected]/acme/demo.git', 'https://git.internal.test/acme/demo.git'],
['an `@` in the password', 'https://user:p@[email protected]:8443/repo', 'https://git.internal.test:8443/repo'],
['an SSH user', 'ssh://[email protected]:2222/acme/demo.git', 'ssh://git.internal.test:2222/acme/demo.git'],
['an `@` only in the path', 'https://git.internal.test/users/@me/repo.git', 'https://git.internal.test/users/@me/repo.git'],
['the scp-like form, which has no password', '[email protected]:acme/demo.git', '[email protected]:acme/demo.git'],
])('shows an unrecognized URL with %s without its user information', (_label, url, shown) => {
expect(remoteRepository({ name: 'selfhost', urls: [url] }, recognize)).toBe(shown)
})
})
describe('parseBaseBranches', () => {
const output = [
'refs/remotes/origin/HEAD\0refs/remotes/origin/main',
'refs/remotes/origin/main\0',
'refs/heads/feat\0',
'refs/heads/Release/2.4\0',
'refs/remotes/upstream/release/2.3\0',
'refs/heads/main\0',
'refs/tags/v1\0',
'',
].join('\r\n')
it('keeps the listed order, leaving out symbolic refs, the checked-out branch, and other ref kinds', () => {
expect(parseBaseBranches(output, 'feat', '', 10)).toEqual({
remote: [
{ ref: 'refs/remotes/origin/main', name: 'origin/main' },
{ ref: 'refs/remotes/upstream/release/2.3', name: 'upstream/release/2.3' },
],
local: [
{ ref: 'refs/heads/Release/2.4', name: 'Release/2.4' },
{ ref: 'refs/heads/main', name: 'main' },
],
})
})
it('keeps names containing the trimmed query in any case, at most limit in each group', () => {
expect(parseBaseBranches(output, null, ' RELEASE ', 1)).toEqual({
remote: [{ ref: 'refs/remotes/upstream/release/2.3', name: 'upstream/release/2.3' }],
local: [{ ref: 'refs/heads/Release/2.4', name: 'Release/2.4' }],
})
expect(parseBaseBranches(output, null, '', 1).local).toEqual([{ ref: 'refs/heads/feat', name: 'feat' }])
expect(parseBaseBranches('refs/heads/\0\n', null, '', 10)).toEqual({ remote: [], local: [] })
})
})
describe('parseRemotes', () => {
it('groups fetch and push URLs per remote in listed order', () => {
const output = [
'origin\[email protected]:octo/demo.git (push)',
'origin\thttps://github.com/octo/demo.git (fetch)',
'upstream\thttps://github.com/upstream/demo.git (fetch)',
'upstream\thttps://github.com/upstream/demo.git (push)',
'mirror\thttps://mirror.example/demo.git (push)',
'not a remote line',
'',
].join('\r\n')
expect(parseRemotes(output)).toEqual([
{ name: 'origin', urls: ['https://github.com/octo/demo.git', '[email protected]:octo/demo.git'] },
{ name: 'upstream', urls: ['https://github.com/upstream/demo.git'] },
{ name: 'mirror', urls: ['https://mirror.example/demo.git'] },
])
})
})
describe('upstreamBranch', () => {
const remote = (name: string): GitRemote => ({ name, urls: [`https://example.test/${name}.git`] })
it('splits the upstream at the longest remote name it starts with', () => {
const remotes = [remote('origin'), remote('team'), remote('team/mirror')]
expect(upstreamBranch('team/mirror/feat/x', remotes)).toEqual({ remote: remote('team/mirror'), branch: 'feat/x' })
expect(upstreamBranch('origin/main', remotes)).toEqual({ remote: remote('origin'), branch: 'main' })
})
it('names no branch without an upstream or a remote it starts with', () => {
expect(upstreamBranch(null, [remote('origin')])).toBeUndefined()
expect(upstreamBranch('elsewhere/main', [remote('origin')])).toBeUndefined()
})
})
describe('remoteOrder', () => {
const remote = (name: string): GitRemote => ({ name, urls: [`https://example.test/${name}.git`] })
it('puts the upstream remote first, preferring the longest matching name, then origin', () => {
const remotes = [remote('fork'), remote('origin'), remote('team'), remote('team/mirror')]
expect(remoteOrder('team/mirror/main', remotes).map(entry => entry.name)).toEqual(['team/mirror', 'origin', 'fork', 'team'])
expect(remoteOrder('team/main', remotes).map(entry => entry.name)).toEqual(['team', 'origin', 'fork', 'team/mirror'])
expect(remoteOrder('team/mirror/main', [remote('team/mirror'), remote('team')]).map(entry => entry.name)).toEqual(['team/mirror', 'team'])
})
it('falls back to origin without an upstream or a matching remote', () => {
const remotes = [remote('fork'), remote('origin')]
expect(remoteOrder(null, remotes).map(entry => entry.name)).toEqual(['origin', 'fork'])
expect(remoteOrder('elsewhere/main', remotes).map(entry => entry.name)).toEqual(['origin', 'fork'])
})
})