/**
 * File Browser 路由单元测试
 */
import { describe, it, expect } from 'vitest';
import multer from 'multer';
import { createWorkspaceUploadLimitResponse, parseWorkspaceUploadRelativePaths, WORKSPACE_UPLOAD_FILE_LIMIT } from './file-browser.js';
describe('file-browser route validation', () => {
    it('returns root drives on Windows when path is empty', () => {
        const getRootForWindows = () => {
            const drives = [];
            for (let i = 67; i <= 90; i++)
                drives.push(String.fromCharCode(i) + ':\\');
            return drives;
        };
        const drives = getRootForWindows();
        expect(drives.length).toBe(24);
        expect(drives.includes('C:\\')).toBe(true);
    });
    it('validates workspacePath for workspace/list', () => {
        const validateWorkspaceListRequest = (body) => {
            if (!body.workspacePath)
                return { valid: false, error: 'workspacePath is required' };
            return { valid: true };
        };
        expect(validateWorkspaceListRequest({}).valid).toBe(false);
        expect(validateWorkspaceListRequest({ workspacePath: '/project' }).valid).toBe(true);
    });
    it('validates filePath for workspace/read', () => {
        const validateReadRequest = (body) => {
            if (!body.workspacePath || !body.filePath)
                return { valid: false, error: 'workspacePath and filePath are required' };
            return { valid: true };
        };
        expect(validateReadRequest({}).valid).toBe(false);
        expect(validateReadRequest({ workspacePath: '/project', filePath: 'file.txt' }).valid).toBe(true);
    });
    it('validates create request', () => {
        const validateCreateRequest = (body) => {
            if (!body.workspacePath)
                return { valid: false, error: 'workspacePath is required' };
            if (!body.entryName)
                return { valid: false, error: 'entryName is required' };
            return { valid: true };
        };
        expect(validateCreateRequest({}).valid).toBe(false);
        expect(validateCreateRequest({ workspacePath: '/p', entryName: 'new-file' }).valid).toBe(true);
    });
    it('validates rename request', () => {
        const validateRenameRequest = (body) => {
            if (!body.workspacePath || !body.targetPath || !body.newName)
                return { valid: false, error: 'workspacePath, targetPath, and newName are required' };
            return { valid: true };
        };
        expect(validateRenameRequest({}).valid).toBe(false);
        expect(validateRenameRequest({ workspacePath: '/p', targetPath: '/p/old', newName: 'new' }).valid).toBe(true);
    });
    it('validates delete request', () => {
        const validateDeleteRequest = (body) => {
            if (!body.workspacePath || !body.targetPath)
                return { valid: false, error: 'workspacePath and targetPath are required' };
            return { valid: true };
        };
        expect(validateDeleteRequest({}).valid).toBe(false);
        expect(validateDeleteRequest({ workspacePath: '/p', targetPath: '/p/file' }).valid).toBe(true);
    });
    it('validates move request', () => {
        const validateMoveRequest = (body) => {
            if (!body.workspacePath || !body.targetPath || !body.destinationPath)
                return { valid: false, error: 'workspacePath, targetPath, and destinationPath are required' };
            return { valid: true };
        };
        expect(validateMoveRequest({}).valid).toBe(false);
        expect(validateMoveRequest({ workspacePath: '/p', targetPath: '/p/file', destinationPath: '/p/dest' }).valid).toBe(true);
    });
});
describe('file-browser response building', () => {
    it('builds correct list response', () => {
        const response = {
            isWindows: true,
            currentPath: 'C:\\Users',
            items: [
                { name: 'Documents', path: 'C:\\Users\\Documents', isDirectory: true },
                { name: 'Downloads', path: 'C:\\Users\\Downloads', isDirectory: true },
            ],
        };
        expect(response.isWindows).toBe(true);
        expect(response.items.length).toBe(2);
    });
    it('categorizes entries by type', () => {
        const entries = [
            { name: 'src', isDir: true },
            { name: 'package.json', isDir: false },
        ];
        const directories = entries.filter(e => e.isDir).map(e => e.name);
        const files = entries.filter(e => !e.isDir).map(e => e.name);
        expect(directories).toEqual(['src']);
        expect(files).toEqual(['package.json']);
    });
    it('returns JSON guidance when folder upload exceeds file count limit', () => {
        const response = createWorkspaceUploadLimitResponse(new multer.MulterError('LIMIT_FILE_COUNT'));
        expect(response.status).toBe(400);
        expect(response.body).toEqual({
            error: `一次最多上传 ${WORKSPACE_UPLOAD_FILE_LIMIT} 个文件，请拆分文件夹后重试。`,
            code: 'LIMIT_FILE_COUNT'
        });
    });
    it('parses relative paths from a JSON manifest to preserve folder hierarchy', () => {
        const relativePaths = parseWorkspaceUploadRelativePaths({
            relativePathsJson: JSON.stringify(['a/b/c.txt', 'a/d/e.txt'])
        });
        expect(relativePaths).toEqual(['a/b/c.txt', 'a/d/e.txt']);
    });
    it('keeps backward compatibility with repeated relativePaths fields', () => {
        const relativePaths = parseWorkspaceUploadRelativePaths({
            relativePaths: ['a/b/c.txt', 'a/d/e.txt']
        });
        expect(relativePaths).toEqual(['a/b/c.txt', 'a/d/e.txt']);
    });
});
