#802 把 tool call 里的候选路径按 pathKeyOf()(小写 + 分隔符归一)当 map key 记账, 随后又把这个 key 本身交给 git rev-parse --show-toplevel 去探测。Linux 区分大小写, …/iHpOqm 折叠成 …/ihpoqm 后 stat 与 git 全部失败,候选被静默丢弃,函数回退到会话 cwd——跨仓 linked worktree 永不跟随。macOS / Windows 的文件系统折叠大小写,所以本地 全绿;dev 上 b2599b3、9b97e6f、3bd5e19 三次 ci 全红在 "follows a linked worktree of a DIFFERENT repository named by the tool calls"。 现在候选以 { path, score } 记账:key 仍只用于身份比较,探测一律用 path。回归用例断言 探测入参里出现原始大小写的路径(旧代码是小写变体),因此在 macOS 上同样是红的, 不依赖宿主文件系统是否折叠大小写。 Refs #802
121 lines
4.9 KiB
TypeScript
121 lines
4.9 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { spawnSync } from 'node:child_process'
|
|
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { activeWorktreeRootOf } from '../src/active-worktree.ts'
|
|
import type { Context } from '../src/context-types.ts'
|
|
|
|
/** Every cwd handed to the plugin's own git probe, in call order. */
|
|
const probed = vi.hoisted(() => [] as string[])
|
|
|
|
// Records (and delegates to) the real spawn so the case-folding regression is
|
|
// visible on case-insensitive hosts too.
|
|
vi.mock('../src/git.ts', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../src/git.ts')>()
|
|
return {
|
|
...actual,
|
|
runGitRaw: (cwd: string, args: string[], timeoutMs?: number) => {
|
|
probed.push(cwd)
|
|
return actual.runGitRaw(cwd, args, timeoutMs)
|
|
},
|
|
}
|
|
})
|
|
|
|
const 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',
|
|
}
|
|
|
|
function git(cwd: string, args: string[]): string {
|
|
// `core.hooksPath=` keeps a developer's global commit-msg hook out of the scratch repos.
|
|
const result = spawnSync('git', ['-c', 'core.hooksPath=', '-C', cwd, ...args], {
|
|
encoding: 'utf8',
|
|
env: { ...process.env, ...IDENTITY },
|
|
})
|
|
if (result.status !== 0) throw new Error(result.stderr || `git ${args.join(' ')} failed`)
|
|
return result.stdout
|
|
}
|
|
|
|
function initRepo(path: string, file: string): void {
|
|
mkdirSync(path, { recursive: true })
|
|
git(path, ['init', '-q'])
|
|
git(path, ['checkout', '-q', '-b', 'main'])
|
|
writeFileSync(join(path, file), 'base\n')
|
|
git(path, ['add', '-A'])
|
|
git(path, ['commit', '-q', '-m', 'base'])
|
|
}
|
|
|
|
/** A minimal live-session face: header cwd plus the tool/call events to scan. */
|
|
function sessionCtx(cwd: string, calls: Array<Record<string, unknown>>): Context {
|
|
return {
|
|
sessions: {
|
|
get: () => ({
|
|
header: { cwd },
|
|
snapshotEvents: () => calls.map((data, index) => ({ type: 'tool/call', seq: index, time: index, data })),
|
|
}),
|
|
},
|
|
get: () => undefined,
|
|
} as never
|
|
}
|
|
|
|
describe('active worktree root', () => {
|
|
it('follows a linked worktree of a DIFFERENT repository named by the tool calls', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repoA = join(root, 'repo-a')
|
|
const repoB = join(root, 'repo-b')
|
|
const worktreeB = join(root, 'worktree-b')
|
|
try {
|
|
initRepo(repoA, 'a.txt')
|
|
initRepo(repoB, 'b.txt')
|
|
git(repoB, ['worktree', 'add', '-q', '-b', 'agent', worktreeB])
|
|
// The session's header cwd is repo A, but its newest tool call works in
|
|
// a linked worktree of repo B the sidebar must follow that worktree.
|
|
const ctx = sessionCtx(repoA, [{ cwd: worktreeB, path: join(worktreeB, 'b.txt') }])
|
|
// The roots come back from `git rev-parse --show-toplevel`, i.e. already
|
|
// canonical: compare against the real path (macOS tmpdirs live under the
|
|
// `/var` → `/private/var` symlink; same convention as git-worktree.spec).
|
|
await expect(activeWorktreeRootOf(ctx, 's-cross-repo', repoA)).resolves.toBe(realpathSync.native(worktreeB))
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps the session cwd when no tool call points at a linked worktree', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repo = join(root, 'repo')
|
|
try {
|
|
initRepo(repo, 'a.txt')
|
|
const ctx = sessionCtx(repo, [{ cwd: join(repo, 'src') }])
|
|
await expect(activeWorktreeRootOf(ctx, 's-no-worktree', repo)).resolves.toBe(realpathSync.native(repo))
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('probes the REAL path, never its case-folded identity (Linux is case-sensitive)', async () => {
|
|
// `addActivePath` keys candidates by a lowercased identity; probing that
|
|
// KEY instead of the path silently drops every candidate whose path has an
|
|
// uppercase letter. macOS and Windows fold case and never noticed it —
|
|
// `Repo-A` / `WorkTree-B` below make the difference visible on every host.
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repoA = join(root, 'Repo-A')
|
|
const repoB = join(root, 'Repo-B')
|
|
const worktreeB = join(root, 'WorkTree-B')
|
|
try {
|
|
initRepo(repoA, 'a.txt')
|
|
initRepo(repoB, 'b.txt')
|
|
git(repoB, ['worktree', 'add', '-q', '-b', 'agent', worktreeB])
|
|
probed.length = 0
|
|
const ctx = sessionCtx(repoA, [{ cwd: worktreeB, path: join(worktreeB, 'b.txt') }])
|
|
await expect(activeWorktreeRootOf(ctx, 's-case', repoA)).resolves.toBe(realpathSync.native(worktreeB))
|
|
expect(probed).toContain(worktreeB)
|
|
expect(probed).not.toContain(worktreeB.toLowerCase())
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|