import { describe, it, expect, vi, type Mock } from 'vitest';; import { PythonAdapter } from '../python'; vi.mock('fs', () => ({ readFileSync: vi.fn(), existsSync: vi.fn(() => true), })); import { readFileSync } from 'fs'; describe('PythonAdapter', () => { it('should implement the Adapter interface', () => { (readFileSync as Mock).mockReturnValue('def test_fn(): pass\nclass TestClass:'); const adapter = new PythonAdapter('test.py'); expect(adapter).toHaveProperty('detectLanguage'); expect(adapter).toHaveProperty('parseAST'); expect(adapter).toHaveProperty('extractFunctions'); expect(adapter).toHaveProperty('extractClasses'); expect(adapter).toHaveProperty('countLines'); }); it('should detect language as python for .py files', () => { (readFileSync as Mock).mockReturnValue('def test_fn(): pass'); const adapter = new PythonAdapter('test.py'); const detected = adapter.detectLanguage(); expect(detected).toBe('python'); }); it('should parse Python file AST correctly', () => { (readFileSync as Mock).mockReturnValue('def test_fn(): pass\nclass TestClass:'); const adapter = new PythonAdapter('test.py'); const ast = adapter.parseAST(); expect(ast).toHaveProperty('content'); expect(ast).toHaveProperty('language'); expect(ast).toHaveProperty('filePath'); expect((ast as { language: unknown }).language).toBe('python'); }); it('should extract functions from Python AST', () => { (readFileSync as Mock).mockReturnValue('def test_fn(): pass\nclass TestClass:'); const adapter = new PythonAdapter('test.py'); const functions = adapter.extractFunctions(); expect(Array.isArray(functions)).toBe(true); expect(functions.some(fn => (fn as {name: string}).name === 'test_fn')).toBe(true); }); it('should extract classes from Python AST', () => { (readFileSync as Mock).mockReturnValue('def test_fn(): pass\nclass TestClass:'); const adapter = new PythonAdapter('test.py'); const classes = adapter.extractClasses(); expect(Array.isArray(classes)).toBe(true); expect(classes.some(cls => (cls as {name: string}).name === 'TestClass')).toBe(true); }); it('should count Python file physical lines', () => { (readFileSync as Mock).mockReturnValue('def test_fn(): pass\ndef test_fn2(): pass'); const adapter = new PythonAdapter('test.py'); const lineCount = adapter.countLines(); expect(lineCount).toBe(2); }); it('should handle async functions in Python', () => { (readFileSync as Mock).mockReturnValue('async def async_fn(): pass'); const adapter = new PythonAdapter('test.py'); const functions = adapter.extractFunctions(); expect(Array.isArray(functions)).toBe(true); }); it('should fall back to super.detectLanguage for non-py extensions', () => { (readFileSync as Mock).mockReturnValue('content'); const adapter = new PythonAdapter('test.ts'); expect(adapter.detectLanguage()).toBe('typescript'); }); it('should extract code block with indented body', () => { (readFileSync as Mock).mockReturnValue('def outer():\n inner()\n return 0'); const adapter = new PythonAdapter('test.py'); const functions = adapter.extractFunctions(); expect(Array.isArray(functions)).toBe(true); expect(functions.length).toBe(1); expect((functions[0] as {name: string}).name).toBe('outer'); }); });