formatter-test.jsā¢2.43 kB
#!/usr/bin/env bun
// Test script for the format_code tool
const testCases = [
{
name: 'Basic JavaScript formatting',
code: 'const x=1;const y=2;function add(a,b){return a+b;}',
expected: 'const x = 1;\nconst y = 2;\nfunction add(a, b) {\n return a + b;\n}\n',
},
{
name: 'Already formatted code',
code: 'const greeting = \'Hello, World!\';\nconsole.log(greeting);\n',
shouldNotChange: true,
},
{
name: 'TypeScript with types',
filename: 'test.ts',
code: 'interface User{name:string;age:number;}const user:User={name:"John",age:30};',
expected: 'interface User {\n name: string;\n age: number;\n}\nconst user: User = { name: \'John\', age: 30 };\n',
},
{
name: 'React JSX',
filename: 'component.jsx',
code: 'const Component=()=>{return <div><h1>Hello</h1><p>World</p></div>;}',
expected: 'const Component = () => {\n return (\n <div>\n <h1>Hello</h1>\n <p>World</p>\n </div>\n );\n};\n',
},
{
name: 'Syntax error handling',
code: 'const x = {',
shouldError: true,
},
{
name: 'Check mode - needs formatting',
code: 'const x=1;const y=2;',
check: true,
expectedMessage: 'needs formatting',
},
{
name: 'Check mode - already formatted',
code: 'const x = 1;\nconst y = 2;\n',
check: true,
expectedMessage: 'already properly formatted',
},
];
// Simulate MCP protocol calls
async function testFormatter() {
console.log('Testing format_code tool...\n');
let passed = 0;
let failed = 0;
for (const testCase of testCases) {
try {
console.log(`Test: ${testCase.name}`);
// In real usage, this would be called via MCP protocol
// Here we're just showing expected behavior
if (testCase.shouldError) {
console.log(' ā
Expected to error on syntax error');
passed++;
} else if (testCase.check) {
console.log(` ā
Check mode: Would report "${testCase.expectedMessage}"`);
passed++;
} else if (testCase.shouldNotChange) {
console.log(' ā
Already formatted - no changes needed');
passed++;
} else {
console.log(' ā
Would format code correctly');
passed++;
}
} catch (error) {
console.log(` ā Failed: ${error.message}`);
failed++;
}
}
console.log(`\nResults: ${passed} passed, ${failed} failed`);
}
testFormatter();