"""Tests for tool generation from SaaSModel."""

from pathlib import Path

import pytest

from foremet.ingest.model import Operation, SaaSModel
from foremet.generate.tools import generate_tools, write_generated_tools


def test_generate_tools_count_and_name() -> None:
    saas_model = SaaSModel(
        operations=[
            Operation(method="GET", path="/users", summary="List users"),
            Operation(method="POST", path="/users", summary="Create user", request_body_schema={"type": "object"}),
        ],
    )
    tools = generate_tools(saas_model, base_url="https://api.example.com", auth_env_var="API_KEY")
    assert len(tools) == 2
    names = {t.name for t in tools}
    assert "get_users" in names
    assert "post_users" in names
    assert all(t.description for t in tools)


def test_write_generated_tools_importable(tmp_path: Path) -> None:
    saas_model = SaaSModel(
        operations=[Operation(method="GET", path="/health", summary="Health check")],
    )
    path = write_generated_tools(tmp_path, saas_model)
    assert path.exists()
    content = path.read_text()
    assert "get_tools" in content
    assert "OPERATIONS" in content
    # Syntax check
    compile(content, path.name, "exec")
