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('plans/ 后一段去掉 .plan.md', () => { expect(deriveSlug('.claude/plans/app-entity.plan.md')).toBe('app-entity') }) it('prds/ 后一段去掉 .prd.md', () => { expect(deriveSlug('.claude/prds/app-entity.prd.md')).toBe('app-entity') }) it('目录形式取 plans/ 后的目录名', () => { expect(deriveSlug('docs/superpowers/plans/x/plan.md')).toBe('x') }) it('没有 plans/prds/specs 锚点时退回父目录名', () => { expect(deriveSlug('foo/bar/notes.md')).toBe('bar') }) it('没有父目录时退回路径 hash 前 8 位', () => { expect(deriveSlug('notes.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') }) })