Files
apes-sidebar/tests/active-worktree.spec.ts
T
Menghuan1918 c5d8b13aa7 merge #802 into dev(文件树跟随活动 worktree,按 dev 原位解冲突)
三处冲突都是 main/dev 侧后插入行与变量改名造成的上下文漂移,被改的目标代码在
dev 上逐字仍在,按 dev 原位解(不是回退到 PR 的旧上下文):

- src/index.ts 导入区:保留 dev 新增的 FS_TREES_MAX_PATHS 导入与注释,同时加
  activeWorktreeRootOf 导入——两边都要;
- src/index.ts /sidebar/file 路由:保留 dev 的路径解析注释(dev 里本来就重复了
  一份,本次不顺手清理,避免把无关改动混进合并),cwd 改走 activeWorktreeRootOf;
- src/client/Sidebar.tsx:删掉 summaryCwd(改由 useSessionRoot 提供根),保留
  dev 插入的「空白会话」注释;
- src/client/native/tab-adapter.tsx:保留 dev 的 seat/scope 双会话命名与注释,
  useSessionCwd(ctx, scopeSessionId) → useSessionRoot(ctx, scopeSessionId)。

落地方另修三处(PR 自带 spec 在本机 macOS 上红,逐条有实测证据):

1. src/active-worktree.ts 的 normalizeActivePath 把分隔符一律折成反斜杠,POSIX
   路径随即不再 isAbsolute 而被 addActivePath 丢弃——该特性在 Linux/macOS 上
   静默空转(只在 Windows 上有效)。改为折成平台分隔符 sep;Windows 上 sep 就是
   反斜杠,行为不变。
2. active-worktree.ts 的根缓存改到 git 探测之前查询,并缓存「不在仓库里」这一
   结果:探测是一次真实 spawn(实测 ~6.5ms/次),此前每个 watch 帧、每个路由
   调用都要付一遍。dev 的 tests/zip.spec.ts「第 5 个并发构建被拒」正因此从 5/5
   绿变成 5/5 红(A/B:只回退 cwdOf 包装即恢复绿),本改动后该用例恢复 5/5 绿。
3. tests/active-worktree.spec.ts 的期望值改用 realpathSync.native:根来自
   `git rev-parse --show-toplevel`,本就是规范化路径,macOS 的 /var →
   /private/var 会让 resolve() 断言恒红(与 tests/git-worktree.spec.ts 同款写法)。

Refs #802
2026-10-05 01:35:58 +08:00

82 lines
3.2 KiB
TypeScript

import { describe, expect, it } 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'
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 })
}
})
})