"""Tests for JavaScript/TypeScript import resolution.

Tests the _resolve_import function to ensure it correctly detects export styles
from source code and generates appropriate import statements.
"""

import pytest

from core.languages.js_ts.testgen import _resolve_import


class TestResolveImport:
    """Tests for _resolve_import function."""

    def test_standalone_function_uses_named_import(self) -> None:
        """Test that standalone functions use named import style."""
        function_name = "execMongoEval"
        module_path = "../utils/mongo"
        source_code = """
export function execMongoEval(code: string) {
    return eval(code);
}
"""

        import_style, import_name, function_accessor = _resolve_import(function_name, module_path, source_code)

        assert import_style == "named"
        assert import_name == "execMongoEval"
        assert function_accessor == "execMongoEval"

    def test_class_method_with_named_export_uses_named_import(self) -> None:
        """Test that class methods with 'export class' use named import style."""
        function_name = "ModulesContainer.getById"
        module_path = "../../injector/modules-container"
        source_code = """
export class ModulesContainer {
    public getById(id: string): Module | undefined {
        return Array.from(this.values()).find(m => m.id === id);
    }
}
"""

        import_style, import_name, function_accessor = _resolve_import(function_name, module_path, source_code)

        # Should use NAMED import for named export
        assert import_style == "named", f"Expected 'named' import for 'export class', got '{import_style}'"
        assert import_name == "ModulesContainer"
        assert function_accessor == "ModulesContainer.getById"

    def test_class_method_with_default_export_uses_default_import(self) -> None:
        """Test that class methods with 'export default class' use default import style."""
        function_name = "Validator.validateRequest"
        module_path = "../validators/validator"
        source_code = """
export default class Validator {
    validateRequest(req: Request): boolean {
        return req.method === 'POST';
    }
}
"""

        import_style, import_name, function_accessor = _resolve_import(function_name, module_path, source_code)

        # Should use DEFAULT import for default export
        assert import_style == "default"
        assert import_name == "Validator"
        assert function_accessor == "Validator.validateRequest"

    def test_class_method_without_export_uses_named_import(self) -> None:
        """Test that non-exported class methods default to named import (will fail, surfacing the issue)."""
        function_name = "PartialGraphHost.toJSON"
        module_path = "../inspector/partial-graph"
        source_code = """
class PartialGraphHost {
    static toJSON() {
        return this.partialGraph?.toJSON();
    }
}
"""

        import_style, import_name, function_accessor = _resolve_import(function_name, module_path, source_code)

        # Should use NAMED import (even though not exported - this will fail at runtime,
        # which surfaces the issue that the class needs to be exported)
        assert import_style == "named", f"Expected 'named' import for non-exported class, got '{import_style}'"
        assert import_name == "PartialGraphHost"
        assert function_accessor == "PartialGraphHost.toJSON"

    def test_static_method_with_named_export(self) -> None:
        """Test that static methods on named exports use named import."""
        function_name = "ServerFactory.create"
        module_path = "../factories/server-factory"
        source_code = """
export class ServerFactory {
    public static create<T>(server: T): ServerHost<T> {
        return { server };
    }
}
"""

        import_style, import_name, function_accessor = _resolve_import(function_name, module_path, source_code)

        assert import_style == "named"
        assert import_name == "ServerFactory"
        assert function_accessor == "ServerFactory.create"

    def test_namespace_fallback_for_complex_patterns(self) -> None:
        """Test that complex patterns fall back to namespace import."""
        function_name = "Constructor.prototype.method"
        module_path = "../utils/complex"
        source_code = """
function Constructor() {}
Constructor.prototype.method = function() {
    return 42;
};
"""

        import_style, import_name, function_accessor = _resolve_import(function_name, module_path, source_code)

        assert import_style == "namespace"
        assert function_accessor == "Constructor.prototype.method"
