import { describe, expect, it } from 'vitest' import { WORKTREE_ID_SEPARATOR, getRepoIdFromWorktreeId, getWorktreePathBasenameFromId, splitWorktreeId, splitWorktreeIdForFilesystem } from './worktree-id' describe('WORKTREE_ID_SEPARATOR', () => { it('is the literal "::" separator', () => { expect(WORKTREE_ID_SEPARATOR).toBe('::') }) }) describe('getRepoIdFromWorktreeId', () => { it('returns the repo id for a canonical worktree id', () => { expect(getRepoIdFromWorktreeId('repo-123::/abs/path')).toBe('repo-123') }) it('returns the whole input when there is no separator', () => { expect(getRepoIdFromWorktreeId('just-a-repo-id')).toBe('just-a-repo-id') }) it('returns the empty string for an empty input', () => { expect(getRepoIdFromWorktreeId('')).toBe('') }) it('returns an empty repo id for a bare separator', () => { expect(getRepoIdFromWorktreeId('::')).toBe('') }) it('returns an empty repo id for a leading separator', () => { expect(getRepoIdFromWorktreeId('::path')).toBe('') }) it('returns the repo id when only a trailing separator is present', () => { expect(getRepoIdFromWorktreeId('repo::')).toBe('repo') }) it('splits on the first separator when the path itself contains "::"', () => { expect(getRepoIdFromWorktreeId('repo::a::b')).toBe('repo') }) }) describe('splitWorktreeId', () => { it('splits a canonical worktree id into repo id and path', () => { expect(splitWorktreeId('repo-123::/abs/path')).toEqual({ repoId: 'repo-123', worktreePath: '/abs/path' }) }) it('returns null when there is no separator', () => { expect(splitWorktreeId('just-a-repo-id')).toBeNull() }) it('returns null for an empty input', () => { expect(splitWorktreeId('')).toBeNull() }) it('returns empty repo id and empty path for a bare separator', () => { expect(splitWorktreeId('::')).toEqual({ repoId: '', worktreePath: '' }) }) it('returns an empty repo id when the separator is leading', () => { expect(splitWorktreeId('::path')).toEqual({ repoId: '', worktreePath: 'path' }) }) it('returns an empty path when the separator is trailing', () => { expect(splitWorktreeId('repo::')).toEqual({ repoId: 'repo', worktreePath: '' }) }) it('splits on the first separator when the path itself contains "::"', () => { expect(splitWorktreeId('repo::a::b')).toEqual({ repoId: 'repo', worktreePath: 'a::b' }) }) it('preserves folder workspace instance suffixes in the literal parsed path', () => { expect( splitWorktreeId('repo::/folder::workspace:123e4567-e89b-12d3-a456-426614174000') ).toEqual({ repoId: 'repo', worktreePath: '/folder::workspace:123e4567-e89b-12d3-a456-426614174000' }) }) }) describe('splitWorktreeIdForFilesystem', () => { it('strips folder workspace instance suffixes from the parsed path', () => { expect( splitWorktreeIdForFilesystem('repo::/folder::workspace:123e4567-e89b-12d3-a456-426614174000') ).toEqual({ repoId: 'repo', worktreePath: '/folder' }) }) }) describe('getWorktreePathBasenameFromId', () => { it('returns the path basename for POSIX worktree ids', () => { expect(getWorktreePathBasenameFromId('repo-123::/abs/path/nightly-checks')).toBe( 'nightly-checks' ) }) it('returns the path basename for Windows worktree ids', () => { expect(getWorktreePathBasenameFromId('repo-123::C:\\workspaces\\nightly-checks')).toBe( 'nightly-checks' ) }) it('returns the real folder basename for folder workspace instance ids', () => { expect( getWorktreePathBasenameFromId( 'repo-123::/abs/project::workspace:123e4567-e89b-12d3-a456-426614174000' ) ).toBe('project') }) it('returns null when no worktree path is available', () => { expect(getWorktreePathBasenameFromId('repo-123')).toBeNull() expect(getWorktreePathBasenameFromId('repo-123::')).toBeNull() }) })