import { analyzeCodeTool } from '../src/tools/analyzeCode.js';
describe('analyzeCodeTool', () => {
describe('TypeScript analysis', () => {
it('should analyze TypeScript code', async () => {
const code = `
import { useState } from 'react';
interface User {
name: string;
age: number;
}
export function getUser(): User {
return { name: 'John', age: 30 };
}
export class UserService {
private users: User[] = [];
async fetchUsers(): Promise<User[]> {
return this.users;
}
}
`;
const result = await analyzeCodeTool({ code, language: 'typescript' });
expect(result.analysis.language).toBe('typescript');
expect(result.analysis.functions.length).toBeGreaterThan(0);
expect(result.analysis.classes.length).toBeGreaterThan(0);
expect(result.analysis.imports.length).toBeGreaterThan(0);
});
it('should detect async functions', async () => {
const code = `
async function fetchData() {
return await fetch('/api');
}
`;
const result = await analyzeCodeTool({ code, language: 'typescript' });
const asyncFuncs = result.analysis.functions.filter(f => f.async);
expect(asyncFuncs.length).toBeGreaterThan(0);
});
});
describe('Python analysis', () => {
it('should analyze Python code', async () => {
const code = `
from typing import List
def get_users() -> List[str]:
return ["John", "Jane"]
class UserService:
def __init__(self):
self.users = []
async def fetch_users(self):
return self.users
`;
const result = await analyzeCodeTool({ code, language: 'python' });
expect(result.analysis.language).toBe('python');
expect(result.analysis.functions.length).toBeGreaterThan(0);
});
});
describe('Go analysis', () => {
it('should analyze Go code', async () => {
const code = `
package main
import (
"fmt"
"net/http"
)
type User struct {
Name string
Age int
}
func GetUser() User {
return User{Name: "John", Age: 30}
}
`;
const result = await analyzeCodeTool({ code, language: 'go' });
expect(result.analysis.language).toBe('go');
expect(result.analysis.functions.length).toBeGreaterThan(0);
});
});
describe('complexity calculation', () => {
it('should calculate low complexity for simple code', async () => {
const code = `
function add(a, b) {
return a + b;
}
`;
const result = await analyzeCodeTool({ code });
expect(result.analysis.complexity).toBeLessThan(10);
expect(result.summary.complexity).toBe(result.analysis.complexity);
});
it('should calculate higher complexity for code with many branches', async () => {
const code = `
function process(value) {
if (value > 0) {
if (value > 10) {
return 'large';
} else {
return 'small';
}
} else if (value < 0) {
return 'negative';
} else {
return 'zero';
}
}
`;
const result = await analyzeCodeTool({ code });
expect(result.analysis.complexity).toBeGreaterThan(1);
});
});
describe('insights generation', () => {
it('should generate complexity insights', async () => {
const code = `function simple() { return 1; }`;
const result = await analyzeCodeTool({ code });
expect(result.insights.length).toBeGreaterThan(0);
expect(result.insights.some(i => i.includes('complexity'))).toBe(true);
});
it('should identify async functions in insights', async () => {
const code = `
async function fetchData() {
return await fetch('/api');
}
async function saveData() {
return await fetch('/api', { method: 'POST' });
}
`;
const result = await analyzeCodeTool({ code, language: 'javascript' });
expect(result.insights.some(i => i.includes('async'))).toBe(true);
});
});
describe('diagram generation', () => {
it('should generate diagrams when enabled', async () => {
const code = `
class User {
name: string;
getName() { return this.name; }
}
`;
const result = await analyzeCodeTool({
code,
generateDiagrams: true,
diagramTypes: ['class']
});
expect(result.diagrams).toBeDefined();
expect(result.diagrams!.length).toBeGreaterThan(0);
});
it('should not generate diagrams when disabled', async () => {
const code = `
class User {
name: string;
}
`;
const result = await analyzeCodeTool({
code,
generateDiagrams: false
});
expect(result.diagrams).toBeUndefined();
});
});
describe('summary generation', () => {
it('should include all summary fields', async () => {
const code = `
import { x } from 'y';
export function test() {}
export class TestClass {}
`;
const result = await analyzeCodeTool({ code });
expect(result.summary).toHaveProperty('totalFunctions');
expect(result.summary).toHaveProperty('totalClasses');
expect(result.summary).toHaveProperty('totalImports');
expect(result.summary).toHaveProperty('complexity');
expect(result.summary).toHaveProperty('exportedItems');
expect(result.summary).toHaveProperty('dependencies');
});
});
describe('auto language detection', () => {
it('should auto-detect TypeScript', async () => {
const code = `
const x: string = 'hello';
function greet(name: string): void {}
`;
const result = await analyzeCodeTool({ code });
expect(result.analysis.language).toBe('typescript');
});
it('should auto-detect Python', async () => {
const code = `
def hello():
print("Hello")
class MyClass:
pass
`;
const result = await analyzeCodeTool({ code });
// Python detection requires specific patterns at line start
expect(['python', 'unknown']).toContain(result.analysis.language);
});
});
describe('usedAI flag', () => {
it('should have usedAI flag set to false when AI not used', async () => {
const code = `function test() { return 1; }`;
const result = await analyzeCodeTool({ code, useAI: false });
expect(result.usedAI).toBe(false);
});
});
});