import { describe, it, expect } from 'vitest'; import { baseName, dirName, ensureMdExtension, normalizeRelPath, relPathFrom, stripMdExtension, } from './paths'; describe('normalizeRelPath', () => { it('collapses repeated slashes and drops `.` segments', () => { expect(normalizeRelPath('a//b')).toBe('a/b'); expect(normalizeRelPath('./a/./b')).toBe('a/b'); }); it('resolves `..`', () => { expect(normalizeRelPath('a/../b')).toBe('b'); expect(normalizeRelPath('a/b/../../c')).toBe('c'); }); it('clamps `..` at the root rather than escaping the vault', () => { expect(normalizeRelPath('../../etc/passwd')).toBe('etc/passwd'); }); it('strips a leading slash and converts backslashes', () => { expect(normalizeRelPath('/a/b')).toBe('a/b'); expect(normalizeRelPath('a\\b')).toBe('a/b'); }); }); describe('md extension helpers', () => { it('strips `.md` case-insensitively', () => { expect(stripMdExtension('Note.md')).toBe('Note'); expect(stripMdExtension('Note.MD')).toBe('Note'); expect(stripMdExtension('Note')).toBe('Note'); }); it('appends `.md` to a name containing a dot', () => { // createFile's own auto-append would skip this one and create an // extensionless file, which is why callers go through here. expect(ensureMdExtension('Release v1.2')).toBe('Release v1.2.md'); }); it('does not double up an existing extension', () => { expect(ensureMdExtension('Note.md')).toBe('Note.md'); }); }); describe('relPathFrom', () => { it('strips the root prefix', () => { expect(relPathFrom('/vault', '/vault/a/b.md')).toBe('a/b.md'); }); it('tolerates a trailing slash on the root', () => { expect(relPathFrom('/vault/', '/vault/a.md')).toBe('a.md'); }); it('normalizes a path outside the root instead of producing nonsense', () => { expect(relPathFrom('/vault', '/other/a.md')).toBe('other/a.md'); }); }); describe('baseName / dirName', () => { it('splits a nested path', () => { expect(baseName('a/b/c.md')).toBe('c.md'); expect(dirName('a/b/c.md')).toBe('a/b'); }); it('returns an empty dir at the top level', () => { expect(dirName('c.md')).toBe(''); }); });