Files
apes-sidebar/tests/git-worktree.spec.ts
T
Menghuan1918 2b757cfe24 fix(test): 测试间状态串台与 macOS 临时目录路径导致的用例失败
两处既有测试基建问题(在慢 runner 的 CI 上表现为 free-window 用例
间歇性失败,阻塞 PR 合并):

- free-window/sidebar-crash/bottom-auto-terminal 三个 spec 共用 session
  id 's1':store 会按 200ms 防抖把每会话状态持久化到
  localStorage['dsh-sidebar:v1:s1'],前一个用例的迟写(drop 点 400,300
  → 浮窗 left=205)被下一用例 setSession 恢复,新浮窗断言(期望 317)
  被 querySelector 拿到的旧窗口炸掉——用例间隔是否超过 200ms 决定成
  败,本地快时常过、CI 慢必挂。修复:挂载 helper 缺省生成唯一
  session id(持久化往返用例显式传固定 id),afterEach 在 unstub 之后
  清 localStorage 兜底
- git/git-worktree 两个用例在 macOS 上必挂:tmpdir() 返回 /var 符号
  链接而 git 报告解析后的 /private/var 前缀,路径比较与
  resolveWorktree 的恒等匹配失配(CI 的 Linux 路径规范所以一直绿)。
  修复:期望值与传入 git API 的路径统一 realpathSync 规范化

本地全量 vitest 963 通过 0 失败(此前 free-window 间歇 0~2 失败 +
git 2 个环境性失败);free-window 单文件连跑 8 次稳定。
2026-08-24 14:18:18 +08:00

99 lines
4.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { spawnSync } from 'node:child_process'
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { parseWorktreeList, resolveWorktree, status, worktrees } from '../src/git.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 {
const result = spawnSync('git', ['-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
}
describe('linked Git worktrees', () => {
it('parses porcelain records with spaces in checkout paths', () => {
expect(parseWorktreeList([
'worktree C:/repo/main checkout',
'HEAD abc',
'branch refs/heads/main',
'',
'worktree C:/repo/agent checkout',
'HEAD def',
'detached',
'',
].join('\n'))).toEqual([
{ path: 'C:/repo/main checkout', branch: 'main', locked: false, prunable: false },
{ path: 'C:/repo/agent checkout', branch: 'HEAD', locked: false, prunable: false },
])
})
it('preserves locked metadata and marks stale records prunable', () => {
expect(parseWorktreeList([
'worktree C:/repo/locked',
'HEAD abc',
'branch refs/heads/locked',
'locked in use',
'',
'worktree C:/repo/missing',
'HEAD def',
'prunable gitdir file points to non-existent location',
'',
].join('\0'))).toEqual([
{ path: 'C:/repo/locked', branch: 'locked', locked: true, prunable: false },
{ path: 'C:/repo/missing', branch: 'HEAD', locked: false, prunable: true },
])
})
it('discovers dirty linked checkouts and fences selected targets', async () => {
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-worktrees-'))
const main = join(root, 'main')
const agent = join(root, 'agent worktree')
try {
git(root, ['init', '-q', main])
git(main, ['checkout', '-q', '-b', 'main'])
writeFileSync(join(main, 'tracked.txt'), 'base\n')
git(main, ['add', '-A'])
git(main, ['commit', '-q', '-m', 'base'])
git(main, ['worktree', 'add', '-q', '-b', 'agent', agent])
writeFileSync(join(agent, 'tracked.txt'), 'changed by agent\n')
// macOS tmpdir() keeps the /var symlink while git reports the resolved
// /private/var prefix — canonicalize every path handed to git APIs.
const agentPath = realpathSync(agent)
const listed = await worktrees(main)
expect(listed).toHaveLength(2)
expect(listed.find(entry => entry.current)).toMatchObject({ branch: 'main', changes: 0 })
expect(listed.find(entry => !entry.current)).toMatchObject({ branch: 'agent', changes: 1 })
expect(resolve(await resolveWorktree(main, agentPath))).toBe(resolve(agentPath))
expect((await status(await resolveWorktree(main, agentPath))).entries).toEqual([
{ path: 'tracked.txt', xy: ' M' },
])
await expect(resolveWorktree(main, root)).rejects.toThrow('unknown linked worktree')
// Git retains administrative metadata after a checkout directory is
// deleted, reporting the record as prunable. It must disappear from both
// the selector inventory and the accepted command-target allowlist.
rmSync(agent, { recursive: true, force: true })
const remaining = await worktrees(main)
expect(remaining).toHaveLength(1)
expect(resolve(remaining[0]!.path)).toBe(resolve(realpathSync(main)))
expect(remaining[0]!.current).toBe(true)
await expect(resolveWorktree(main, agentPath)).rejects.toThrow('unknown linked worktree')
} finally {
try { git(main, ['worktree', 'remove', '--force', agent]) } catch { /* fixture may not be fully initialized */ }
rmSync(root, { recursive: true, force: true })
}
})
})