Files
orca/src/shared/cross-platform-path.test.ts
T

163 lines
6.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
isPathInsideOrEqual,
isWslUncPathForCallerLinuxPath,
isWslUncPathForLinuxMountedPath,
normalizeRuntimePathForComparison,
relativePathInsideRoot
} from './cross-platform-path'
describe('isWslUncPathForCallerLinuxPath', () => {
const UBUNTU = 'Ubuntu-24.04'
// The destructive case: this feeds `worktree rm`, so a tail-only match deletes the wrong distro.
it('refuses a Linux path another distro spells identically', () => {
expect(
isWslUncPathForCallerLinuxPath(
'\\\\wsl.localhost\\Debian\\home\\neil\\qa-repo',
'/home/neil/qa-repo',
UBUNTU
)
).toBe(false)
})
it('keeps the Linux tail case-sensitive and refuses a non-WSL path', () => {
expect(
isWslUncPathForCallerLinuxPath(
'\\\\wsl.localhost\\Ubuntu-24.04\\home\\Neil\\qa-repo',
'/home/neil/qa-repo',
UBUNTU
)
).toBe(false)
expect(isWslUncPathForCallerLinuxPath('/home/neil/qa-repo', '/home/neil/qa-repo', UBUNTU)).toBe(
false
)
expect(isWslUncPathForCallerLinuxPath('C:\\repos\\qa-repo', '/home/neil/qa-repo', UBUNTU)).toBe(
false
)
})
})
describe('isWslUncPathForLinuxMountedPath', () => {
it('matches a shared /mnt drive regardless of distro', () => {
expect(
isWslUncPathForLinuxMountedPath(
'\\\\wsl.localhost\\Ubuntu\\mnt\\c\\Users\\Neil\\repo',
'/mnt/c/users/neil/repo'
)
).toBe(true)
expect(
isWslUncPathForLinuxMountedPath(
'\\\\wsl.localhost\\Debian\\mnt\\c\\Users\\Neil\\repo',
'/mnt/c/users/neil/repo'
)
).toBe(true)
})
it('keeps non-mounted Linux paths out of the distro-independent match', () => {
expect(
isWslUncPathForLinuxMountedPath(
'\\\\wsl.localhost\\Ubuntu\\home\\Neil\\repo',
'/home/neil/repo'
)
).toBe(false)
})
})
describe('cross-platform path containment', () => {
it('keeps POSIX sibling prefixes outside the root', () => {
expect(isPathInsideOrEqual('/repo/app', '/repo/app')).toBe(true)
expect(isPathInsideOrEqual('/repo/app', '/repo/app/src/index.ts')).toBe(true)
expect(isPathInsideOrEqual('/repo/app', '/repo/application/src/index.ts')).toBe(false)
expect(relativePathInsideRoot('/repo/app/', '/repo/app/src/index.ts')).toBe('src/index.ts')
})
it('keeps literal POSIX backslashes distinct from separators', () => {
expect(normalizeRuntimePathForComparison('/srv/team\\repo')).toBe('/srv/team\\repo')
expect(normalizeRuntimePathForComparison('/srv/team/repo')).toBe('/srv/team/repo')
expect(isPathInsideOrEqual('/srv/team\\repo', '/srv/team/repo/file.ts')).toBe(false)
expect(relativePathInsideRoot('/srv/repo', '/srv/repo/a\\b.txt')).toBe('a\\b.txt')
})
it('handles Windows drive roots and sibling drives case-insensitively', () => {
expect(isPathInsideOrEqual('C:\\Repo', 'c:\\repo\\src\\index.ts')).toBe(true)
expect(relativePathInsideRoot('C:\\Repo', 'c:\\repo\\src\\index.ts')).toBe('src/index.ts')
expect(isPathInsideOrEqual('C:\\Repo', 'D:\\Repo\\src\\index.ts')).toBe(false)
expect(relativePathInsideRoot('C:\\', 'c:\\repo\\src\\index.ts')).toBe('repo/src/index.ts')
})
it('handles UNC roots, trailing slashes, mixed separators, and case', () => {
expect(isPathInsideOrEqual('\\\\Server\\Share\\Repo\\', '//server/share/repo/src')).toBe(true)
expect(relativePathInsideRoot('\\\\Server\\Share\\Repo\\', '//server/share/repo/src')).toBe(
'src'
)
expect(isPathInsideOrEqual('\\\\Server\\Share\\Repo', '\\\\server\\share\\repo2')).toBe(false)
})
it('treats WSL UNC aliases as the same case-sensitive filesystem', () => {
expect(
isPathInsideOrEqual(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\Alice\\repo\\src'
)
).toBe(true)
expect(
relativePathInsideRoot(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\Alice\\repo\\Src'
)
).toBe('Src')
expect(
isPathInsideOrEqual(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\alice\\repo\\src'
)
).toBe(false)
expect(
relativePathInsideRoot(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\alice\\repo\\src'
)
).toBeNull()
expect(
relativePathInsideRoot(
'\\\\wsl$\\Ubuntu\\home\\Alice\\repo',
'\\\\wsl.localhost\\ubuntu\\home\\Alice\\repo\\line\nbreak'
)
).toBe('line\nbreak')
})
it('returns a byte-exact suffix when comparison folding changes length', () => {
// NFC/case folding changes length; slicing raw paths by folded lengths fabricates suffixes callers rejoin on disk.
const nfc = '/Users/ada/프로젝트'
const nfd = nfc.normalize('NFD')
for (const root of [nfc, nfd]) {
for (const candidate of [nfc, nfd]) {
expect(relativePathInsideRoot(root, `${candidate}/src/index.ts`)).toBe('src/index.ts')
}
}
// Pre-existing over-slice: toLowerCase expands U+0130 to two UTF-16 units.
expect(relativePathInsideRoot('C:\\İş', 'C:\\İş\\src\\a.ts')).toBe('src/a.ts')
// Kelvin U+212A folds to K; agree on Windows syntax or segment counts desynchronize.
expect(relativePathInsideRoot('\u212A:/a\\b', '\u212A:/a\\b/c')).toBe(
relativePathInsideRoot('K:/a\\b', 'K:/a\\b/c')
)
expect(relativePathInsideRoot('/repo/🚀app', '/repo/🚀app/src/🎉file.ts')).toBe('src/🎉file.ts')
// UNC-shaped paths under / must return relative suffixes without a leading slash.
expect(relativePathInsideRoot('/', '//server/share/x')).toBe('server/share/x')
// WSL suffixes stay decomposed: Linux treats NFC/NFD as distinct entries.
const decomposed = '프로젝트'.normalize('NFD')
expect(
relativePathInsideRoot(
'\\\\wsl$\\Ubuntu\\home\\ada\\repo',
`\\\\wsl.localhost\\Ubuntu\\home\\ada\\repo\\${decomposed}\\a.ts`
)
).toBe(`${decomposed}/a.ts`)
})
})