mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-07 05:39:05 +08:00
test: Add tests for expandNotesPath function
- Added comprehensive test cases for tilde expansion - Added tests for relative path expansion - Added tests for absolute path handling - Added tests for edge cases and custom base paths Co-authored-by: DeJeune <67425183+DeJeune@users.noreply.github.com>
This commit is contained in:
parent
7d6ffe472c
commit
cbd4f418f6
@ -10,6 +10,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|||||||
|
|
||||||
import { readTextFileWithAutoEncoding } from '../file'
|
import { readTextFileWithAutoEncoding } from '../file'
|
||||||
import {
|
import {
|
||||||
|
expandNotesPath,
|
||||||
getAllFiles,
|
getAllFiles,
|
||||||
getAppConfigDir,
|
getAppConfigDir,
|
||||||
getConfigDir,
|
getConfigDir,
|
||||||
@ -331,6 +332,64 @@ describe('file', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('expandNotesPath', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
// Mock path.isAbsolute
|
||||||
|
vi.mocked(path.isAbsolute).mockImplementation((p) => {
|
||||||
|
return p.startsWith('/') || /^[A-Za-z]:/.test(p)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Mock path.resolve
|
||||||
|
vi.mocked(path.resolve).mockImplementation((...args) => {
|
||||||
|
const joined = args.join('/')
|
||||||
|
return joined.startsWith('/') ? joined : `/${joined}`
|
||||||
|
})
|
||||||
|
|
||||||
|
// Mock path.normalize
|
||||||
|
vi.mocked(path.normalize).mockImplementation((p) => p.replace(/\/+/g, '/'))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should expand tilde paths to home directory', () => {
|
||||||
|
const result = expandNotesPath('~/Notes')
|
||||||
|
expect(result).toBe('/mock/home/Notes')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should expand relative paths using userData as base', () => {
|
||||||
|
const result = expandNotesPath('./Notes')
|
||||||
|
expect(result).toContain('userData')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return absolute paths unchanged', () => {
|
||||||
|
const result = expandNotesPath('/absolute/path/Notes')
|
||||||
|
expect(result).toBe('/absolute/path/Notes')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle Windows absolute paths', () => {
|
||||||
|
const result = expandNotesPath('C:\\Users\\Notes')
|
||||||
|
expect(result).toBe('C:\\Users\\Notes')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle empty string', () => {
|
||||||
|
const result = expandNotesPath('')
|
||||||
|
expect(result).toBe('')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should expand parent directory paths', () => {
|
||||||
|
const result = expandNotesPath('../Notes')
|
||||||
|
expect(result).toContain('userData')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should use custom base path when provided', () => {
|
||||||
|
const result = expandNotesPath('./Notes', '/custom/base')
|
||||||
|
expect(result).toContain('/custom/base')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle complex relative paths', () => {
|
||||||
|
const result = expandNotesPath('../../Notes')
|
||||||
|
expect(result).toContain('userData')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('isPathInside', () => {
|
describe('isPathInside', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// Mock path.resolve to simulate path resolution
|
// Mock path.resolve to simulate path resolution
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user