import * as os from 'node:os' import * as path from 'node:path' import { describe, expect, it } from 'vitest' import { DEFAULT_WORKTREE_TEMPLATE, deriveSlug, resolveWorktreeDir, slugFromBranch } from '../../src/git/worktreePath' describe('deriveSlug', () => { it('计划文件名的 stem 就是 slug', () => { expect(deriveSlug('.claude/plans/app-entity.md')).toBe('app-entity') }) it('设计文档与计划共用同一个 stem', () => { expect(deriveSlug('.claude/specs/app-entity.md')).toBe('app-entity') }) it('文件名是通用的 plan.md 时退回父目录名', () => { expect(deriveSlug('.claude/plans/x/plan.md')).toBe('x') }) it('通用文件名且没有父目录时退回路径 hash 前 8 位', () => { expect(deriveSlug('plan.md')).toMatch(/^[0-9a-f]{8}$/) }) }) describe('slugFromBranch', () => { it('去掉 feature/ 前缀', () => { expect(slugFromBranch('feature/app-entity')).toBe('app-entity') }) it('无前缀原样返回', () => { expect(slugFromBranch('hotfix-1')).toBe('hotfix-1') }) }) describe('resolveWorktreeDir', () => { it('展开三个占位符与 ~', () => { const out = resolveWorktreeDir(DEFAULT_WORKTREE_TEMPLATE, '/w/proj', 'slug') expect(out).toBe(path.join(os.homedir(), 'Sources', 'worktree', 'proj', 'slug')) }) it('$project_root 展开为工作区绝对路径', () => { expect(resolveWorktreeDir('$project_root/.wt/$feature_name', '/w/proj', 's')).toBe('/w/proj/.wt/s') }) })