import pytest
from pathlib import Path
import json

from runtime.templates import save_chain_template, load_chain_template, list_templates, export_template, import_template

def test_save_and_load_template(tmp_path):
    data_dir = tmp_path
    steps = [{"model": "claude", "prompt": "test"}]
    
    save_chain_template(data_dir, "my_template", steps)
    
    template = load_chain_template(data_dir, "my_template")
    assert template["name"] == "my_template"
    assert template["type"] == "chain"
    assert template["steps"] == steps

def test_list_templates(tmp_path):
    data_dir = tmp_path
    steps = [{"model": "claude", "prompt": "test"}]
    
    save_chain_template(data_dir, "template1", steps)
    save_chain_template(data_dir, "template2", steps)
    
    templates = list_templates(data_dir)
    assert "template1" in templates
    assert "template2" in templates

def test_export_and_import(tmp_path):
    data_dir = tmp_path
    steps = [{"model": "claude", "prompt": "test"}]
    
    save_chain_template(data_dir, "to_export", steps)
    
    export_file = tmp_path / "export.json"
    assert export_template(data_dir, "to_export", str(export_file)) == True
    assert export_file.exists()
    
    import_name = import_template(data_dir, str(export_file))
    assert import_name == "to_export"
    
    template = load_chain_template(data_dir, import_name)
    assert template["steps"] == steps
