eigent/test/unit/components/Setting/utils.test.ts
Douglas Lai 38c7717f3d
Update Dahsboard UI (#1349)
Co-authored-by: Wendong-Fan <w3ndong.fan@gmail.com>
Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com>
2026-02-24 02:12:27 +08:00

264 lines
8.9 KiB
TypeScript

// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
import { describe, expect, it } from 'vitest';
import {
arrayToArgsJson,
parseArgsToArray,
} from '../../../../src/pages/Connectors/components/utils';
describe('parseArgsToArray', () => {
it('should parse JSON array string to array', () => {
const input = '["arg1", "arg2", "arg3"]';
const expected = ['arg1', 'arg2', 'arg3'];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should parse JSON array string with special characters', () => {
const input = '["-y", "@modelcontextprotocol/server-sequential-thinking"]';
const expected = ['-y', '@modelcontextprotocol/server-sequential-thinking'];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should parse JSON array string with file paths containing backslashes', () => {
const input =
'["--directory", "C:\\\\Users\\\\ASUS\\\\Desktop\\\\project", "run", "main.py"]';
const expected = [
'--directory',
'C:\\Users\\ASUS\\Desktop\\project',
'run',
'main.py',
];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should parse JSON array string with file paths containing forward slashes', () => {
const input =
'["--directory", "C:/Users/ASUS/Desktop/project", "run", "main.py"]';
const expected = [
'--directory',
'C:/Users/ASUS/Desktop/project',
'run',
'main.py',
];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should parse comma-separated string to array', () => {
const input = '-y,@modelcontextprotocol/server-filesystem,.';
const expected = ['-y', '@modelcontextprotocol/server-filesystem', '.'];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should parse comma-separated string with spaces', () => {
const input = '-y, @modelcontextprotocol/server-filesystem, .';
const expected = ['-y', '@modelcontextprotocol/server-filesystem', '.'];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should parse comma-separated string with file paths containing slashes', () => {
const input = '--directory,C:/Users/ASUS/Desktop/project,run,main.py';
const expected = [
'--directory',
'C:/Users/ASUS/Desktop/project',
'run',
'main.py',
];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should handle empty string', () => {
const input = '';
const expected: string[] = [];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should handle whitespace-only string', () => {
const input = ' ';
const expected: string[] = [];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should filter out empty args from comma-separated string', () => {
const input = '-y,,@modelcontextprotocol/server-filesystem,.';
const expected = ['-y', '@modelcontextprotocol/server-filesystem', '.'];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should handle invalid JSON gracefully by treating as comma-separated', () => {
const input = '[invalid json';
const expected: string[] = ['[invalid json'];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should handle non-array JSON by treating as comma-separated', () => {
const input = '{"key": "value"}';
//Trim the curly braces
const expected: string[] = ['"key": "value"'];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
it('should convert array elements to strings', () => {
const input = '[123, true, "string", null]';
const expected = ['123', 'true', 'string', 'null'];
const result = parseArgsToArray(input);
expect(result).toEqual(expected);
});
});
describe('arrayToArgsJson', () => {
it('should convert array to JSON string', () => {
const input = ['arg1', 'arg2', 'arg3'];
const expected = '["arg1","arg2","arg3"]';
const result = arrayToArgsJson(input);
expect(result).toBe(expected);
});
it('should convert array with special characters to JSON string', () => {
const input = ['-y', '@modelcontextprotocol/server-sequential-thinking'];
const expected =
'["-y","@modelcontextprotocol/server-sequential-thinking"]';
const result = arrayToArgsJson(input);
expect(result).toBe(expected);
});
it('should convert array with file paths containing backslashes', () => {
const input = [
'--directory',
'C:\\Users\\ASUS\\Desktop\\project',
'run',
'main.py',
];
const expected =
'["--directory","C:\\\\Users\\\\ASUS\\\\Desktop\\\\project","run","main.py"]';
const result = arrayToArgsJson(input);
expect(result).toBe(expected);
});
it('should convert array with file paths containing forward slashes', () => {
const input = [
'--directory',
'C:/Users/ASUS/Desktop/project',
'run',
'main.py',
];
const expected =
'["--directory","C:/Users/ASUS/Desktop/project","run","main.py"]';
const result = arrayToArgsJson(input);
expect(result).toBe(expected);
});
it('should handle empty array', () => {
const input: string[] = [];
const expected = '';
const result = arrayToArgsJson(input);
expect(result).toBe(expected);
});
it('should filter out empty strings and whitespace-only strings', () => {
const input = ['arg1', '', ' ', 'arg2'];
const expected = '["arg1","arg2"]';
const result = arrayToArgsJson(input);
expect(result).toBe(expected);
});
it('should return empty string for array with only empty/whitespace strings', () => {
const input = ['', ' ', '\t', '\n'];
const expected = '';
const result = arrayToArgsJson(input);
expect(result).toBe(expected);
});
it('should preserve strings with meaningful whitespace', () => {
const input = ['arg with spaces', 'another arg'];
const expected = '["arg with spaces","another arg"]';
const result = arrayToArgsJson(input);
expect(result).toBe(expected);
});
});
describe('bidirectional conversion', () => {
it('should correctly convert from comma-separated string to JSON and back', () => {
const original = '-y,@modelcontextprotocol/server-filesystem,.';
const array = parseArgsToArray(original);
const jsonString = arrayToArgsJson(array);
const finalArray = parseArgsToArray(jsonString);
expect(array).toEqual([
'-y',
'@modelcontextprotocol/server-filesystem',
'.',
]);
expect(jsonString).toBe(
'["-y","@modelcontextprotocol/server-filesystem","."]'
);
expect(finalArray).toEqual(array);
});
it('should correctly convert from JSON string to array and back', () => {
const original =
'["-y","@modelcontextprotocol/server-sequential-thinking"]';
const array = parseArgsToArray(original);
const jsonString = arrayToArgsJson(array);
expect(array).toEqual([
'-y',
'@modelcontextprotocol/server-sequential-thinking',
]);
expect(jsonString).toBe(original);
});
it('should handle file paths with various slash types bidirectionally', () => {
const windowsPath =
'["--directory","C:\\\\Users\\\\ASUS\\\\Desktop\\\\project","run"]';
const unixPath = '["--directory","/home/user/project","run"]';
// Test Windows paths
const windowsArray = parseArgsToArray(windowsPath);
const windowsJson = arrayToArgsJson(windowsArray);
expect(parseArgsToArray(windowsJson)).toEqual(windowsArray);
// Test Unix paths
const unixArray = parseArgsToArray(unixPath);
const unixJson = arrayToArgsJson(unixArray);
expect(parseArgsToArray(unixJson)).toEqual(unixArray);
});
it('should handle mixed path separators in comma-separated format', () => {
const mixed = '--directory,C:/Users/ASUS\\Desktop/project,run,main.py';
const array = parseArgsToArray(mixed);
const jsonString = arrayToArgsJson(array);
const finalArray = parseArgsToArray(jsonString);
expect(array).toEqual([
'--directory',
'C:/Users/ASUS\\Desktop/project',
'run',
'main.py',
]);
expect(finalArray).toEqual(array);
});
});