import { describe, it, expect, vi, type Mock } from 'vitest';; import { KotlinAdapter } from '../kotlin'; vi.mock('fs', () => ({ readFileSync: vi.fn(), existsSync: vi.fn(() => true), })); import { readFileSync } from 'fs'; describe('KotlinAdapter', () => { it('should implement the Adapter interface', () => { (readFileSync as Mock).mockReturnValue('fun testFn() {}\nclass TestClass'); const adapter = new KotlinAdapter('test.kt'); 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 kotlin for .kt files', () => { (readFileSync as Mock).mockReturnValue('fun testFn() {}'); const adapter = new KotlinAdapter('test.kt'); const detected = adapter.detectLanguage(); expect(detected).toBe('kotlin'); }); it('should parse Kotlin file AST correctly', () => { (readFileSync as Mock).mockReturnValue('fun testFn() {}\nclass TestClass'); const adapter = new KotlinAdapter('test.kt'); const ast = adapter.parseAST(); expect(ast).toHaveProperty('content'); expect(ast).toHaveProperty('language'); expect(ast).toHaveProperty('filePath'); expect((ast as { language: unknown }).language).toBe('kotlin'); }); it('should extract functions from Kotlin AST', () => { (readFileSync as Mock).mockReturnValue('fun testFn() {}\nclass TestClass'); const adapter = new KotlinAdapter('test.kt'); const functions = adapter.extractFunctions(); expect(Array.isArray(functions)).toBe(true); expect(functions.some(fn => (fn as {name: string}).name === 'testFn')).toBe(true); }); it('should extract classes from Kotlin AST', () => { (readFileSync as Mock).mockReturnValue('fun testFn() {}\nclass TestClass'); const adapter = new KotlinAdapter('test.kt'); 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 Kotlin file physical lines', () => { (readFileSync as Mock).mockReturnValue('fun testFn() {}\nfun testFn2() {}'); const adapter = new KotlinAdapter('test.kt'); const lineCount = adapter.countLines(); expect(lineCount).toBe(2); }); it('should fall back to super.detectLanguage for non-kotlin extensions', () => { (readFileSync as Mock).mockReturnValue('content'); const adapter = new KotlinAdapter('test.ts'); expect(adapter.detectLanguage()).toBe('typescript'); }); it('should handle suspend functions in Kotlin', () => { (readFileSync as Mock).mockReturnValue('suspend fun fetchData(): String { return "data" }'); const adapter = new KotlinAdapter('test.kt'); const functions = adapter.extractFunctions(); const suspendFns = functions.filter(fn => (fn as {type: string}).type === 'suspend_function'); expect(suspendFns.length).toBeGreaterThan(0); }); });