Files
smartpath/test/test.iso.both.ts

145 lines
6.9 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartpath from '../ts_iso/index.js';
tap.test('pathJoin - should join path segments correctly', async () => {
// Basic path joining
expect(smartpath.join('path', 'to', 'file.txt')).toEqual('path/to/file.txt');
expect(smartpath.join('/path', 'to', 'file.txt')).toEqual('/path/to/file.txt');
// Windows paths
expect(smartpath.join('C:', 'Users', 'test')).toEqual('C:\\Users\\test');
expect(smartpath.join('C:\\Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
// Empty segments
expect(smartpath.join('path', '', 'file.txt')).toEqual('path/file.txt');
expect(smartpath.join('', 'path', 'file.txt')).toEqual('path/file.txt');
// No segments
expect(smartpath.join()).toEqual('');
expect(smartpath.join('')).toEqual('');
// Single segment
expect(smartpath.join('path')).toEqual('path');
expect(smartpath.join('/path')).toEqual('/path');
// Multiple separators
expect(smartpath.join('path/', '/to/', '/file.txt')).toEqual('path/to/file.txt');
expect(smartpath.join('path\\\\', '\\\\to\\\\', '\\\\file.txt')).toEqual('path\\to\\file.txt');
// Root paths
expect(smartpath.join('/')).toEqual('/');
expect(smartpath.join('/', 'path')).toEqual('/path');
});
tap.test('pathJoin - should handle file:// URLs', async () => {
// Unix file URLs
expect(smartpath.join('file:///home/user', 'documents', 'file.txt')).toEqual('/home/user/documents/file.txt');
expect(smartpath.join('file:///home/user/', 'documents')).toEqual('/home/user/documents');
// Windows file URLs
expect(smartpath.join('file:///C:/Users', 'test', 'file.txt')).toEqual('C:\\Users\\test\\file.txt');
expect(smartpath.join('file:///D:/Projects/', 'app')).toEqual('D:\\Projects\\app');
// Mixed file URL and path
expect(smartpath.join('file:///home/user', '../test')).toEqual('/home/user/../test');
expect(smartpath.join('documents', 'file:///home/user/file.txt')).toEqual('documents/home/user/file.txt');
});
tap.test('fileUrlToPath - should convert file URLs to paths', async () => {
// Unix file URLs
expect(smartpath.fileUrlToPath('file:///home/user/file.txt')).toEqual('/home/user/file.txt');
expect(smartpath.fileUrlToPath('file:///home/user%20name/file.txt')).toEqual('/home/user name/file.txt');
// Windows file URLs
expect(smartpath.fileUrlToPath('file:///C:/Users/test/file.txt')).toEqual('C:\\Users\\test\\file.txt');
expect(smartpath.fileUrlToPath('file:///D:/My%20Documents/file.txt')).toEqual('D:\\My Documents\\file.txt');
// Not a file URL
expect(smartpath.fileUrlToPath('/home/user/file.txt')).toEqual('/home/user/file.txt');
expect(smartpath.fileUrlToPath('C:\\Users\\test\\file.txt')).toEqual('C:\\Users\\test\\file.txt');
expect(smartpath.fileUrlToPath('https://example.com')).toEqual('https://example.com');
});
tap.test('pathToFileUrl - should convert paths to file URLs', async () => {
// Unix paths
expect(smartpath.pathToFileUrl('/home/user/file.txt')).toEqual('file:///home/user/file.txt');
expect(smartpath.pathToFileUrl('/home/user name/file.txt')).toEqual('file:///home/user%20name/file.txt');
// Windows paths
expect(smartpath.pathToFileUrl('C:\\Users\\test\\file.txt')).toEqual('file:///C:/Users/test/file.txt');
expect(smartpath.pathToFileUrl('D:\\My Documents\\file.txt')).toEqual('file:///D:/My%20Documents/file.txt');
expect(smartpath.pathToFileUrl('C:/Users/test/file.txt')).toEqual('file:///C:/Users/test/file.txt');
// Already a file URL
expect(smartpath.pathToFileUrl('file:///home/user/file.txt')).toEqual('file:///home/user/file.txt');
// Relative paths (can't make file URLs from these)
expect(smartpath.pathToFileUrl('relative/path/file.txt')).toEqual('relative/path/file.txt');
expect(smartpath.pathToFileUrl('./file.txt')).toEqual('./file.txt');
// Special characters
expect(smartpath.pathToFileUrl('/path/with?query')).toEqual('file:///path/with%3Fquery');
expect(smartpath.pathToFileUrl('/path/with#hash')).toEqual('file:///path/with%23hash');
});
tap.test('dirname - should extract directory from paths and URLs', async () => {
// Unix paths
expect(smartpath.dirname('/home/user/file.txt')).toEqual('/home/user');
expect(smartpath.dirname('/home/user/')).toEqual('/home');
expect(smartpath.dirname('/file.txt')).toEqual('/');
expect(smartpath.dirname('/')).toEqual('/');
// Windows paths
expect(smartpath.dirname('C:\\Users\\test\\file.txt')).toEqual('C:\\Users\\test');
expect(smartpath.dirname('C:\\file.txt')).toEqual('C:\\');
expect(smartpath.dirname('C:\\')).toEqual('C:\\');
// File URLs
expect(smartpath.dirname('file:///home/user/file.txt')).toEqual('/home/user');
expect(smartpath.dirname('file:///C:/Users/test/file.txt')).toEqual('C:\\Users\\test');
// Relative paths
expect(smartpath.dirname('relative/path/file.txt')).toEqual('relative/path');
expect(smartpath.dirname('file.txt')).toEqual('.');
expect(smartpath.dirname('')).toEqual('.');
// Mixed separators
expect(smartpath.dirname('path\\to/file.txt')).toEqual('path\\to');
expect(smartpath.dirname('path/to\\file.txt')).toEqual('path/to');
});
tap.test('edge cases - should handle edge cases correctly', async () => {
// Non-string values filtered out
expect(smartpath.join('path', null as any, 'file.txt')).toEqual('path/file.txt');
expect(smartpath.join('path', undefined as any, 'file.txt')).toEqual('path/file.txt');
expect(smartpath.join('path', 123 as any, 'file.txt')).toEqual('path/file.txt');
// Very long paths
const longSegment = 'a'.repeat(100);
const result = smartpath.join(longSegment, longSegment, 'file.txt');
expect(result).toEqual(`${longSegment}/${longSegment}/file.txt`);
// Unicode characters
expect(smartpath.join('path', '文件夹', 'файл.txt')).toEqual('path/文件夹/файл.txt');
expect(smartpath.fileUrlToPath('file:///home/用户/文件.txt')).toEqual('/home/用户/文件.txt');
expect(smartpath.pathToFileUrl('/home/用户/文件.txt')).toEqual('file:///home/%E7%94%A8%E6%88%B7/%E6%96%87%E4%BB%B6.txt');
// Backslashes in Unix-style paths (should be preserved)
expect(smartpath.join('/home/user', 'path\\with\\backslashes')).toEqual('/home/user/path\\with\\backslashes');
});
tap.test('cross-platform behavior - should detect separators correctly', async () => {
// Should detect Windows paths and use backslashes
expect(smartpath.join('C:\\Users', 'test')).toEqual('C:\\Users\\test');
expect(smartpath.join('D:', 'Projects', 'app')).toEqual('D:\\Projects\\app');
// Should detect Unix paths and use forward slashes
expect(smartpath.join('/home', 'user')).toEqual('/home/user');
expect(smartpath.join('/var', 'log', 'app.log')).toEqual('/var/log/app.log');
// Mixed paths - first segment determines separator
expect(smartpath.join('C:\\Users', 'test/file.txt')).toEqual('C:\\Users\\test\\file.txt');
expect(smartpath.join('/home', 'user\\documents')).toEqual('/home/user\\documents');
});
await tap.start();