Files
roux/tests/test_convert_to_cooklang.py
T

92 lines
2.9 KiB
Python

"""Tests for scripts/convert-to-cooklang."""
import importlib.machinery
import importlib.util
import os
import tempfile
# Load the script as a module (no .py extension, so use SourceFileLoader)
SCRIPT_PATH = os.path.join(
os.path.dirname(__file__), "..", "scripts", "convert-to-cooklang"
)
_loader = importlib.machinery.SourceFileLoader("convert_to_cooklang", SCRIPT_PATH)
_spec = importlib.util.spec_from_loader(
"convert_to_cooklang", _loader, origin=SCRIPT_PATH
)
mod = importlib.util.module_from_spec(_spec)
_loader.exec_module(mod)
def test_load_config_valid():
"""Test loading a valid config file."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
f.write("anthropic:\n api_key: sk-test-123\n")
config_path = f.name
try:
config = mod.load_config(config_path)
assert config["api_key"] == "sk-test-123"
finally:
os.unlink(config_path)
def test_load_config_missing_api_key():
"""Test that missing api_key exits with error."""
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
f.write("anthropic:\n model: foo\n")
config_path = f.name
try:
try:
mod.load_config(config_path)
assert False, "Expected sys.exit"
except SystemExit as e:
assert e.code == 1
finally:
os.unlink(config_path)
def test_build_text_content():
"""Test building a text content block."""
result = mod.build_text_content("Hello")
assert result == [{"type": "text", "text": "Hello"}]
def test_build_file_content_pdf():
"""Test building file content for a PDF."""
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f:
f.write(b"%PDF- test")
filepath = f.name
try:
result = mod.build_file_content(filepath)
assert result[0]["type"] == "document"
assert result[0]["source"]["media_type"] == "application/pdf"
finally:
os.unlink(filepath)
def test_build_file_content_jpeg():
"""Test building file content for a JPEG image."""
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
f.write(b"\xff\xd8\xff\xe0 test jpeg")
filepath = f.name
try:
result = mod.build_file_content(filepath)
assert result[0]["type"] == "image"
assert result[0]["source"]["media_type"] == "image/jpeg"
finally:
os.unlink(filepath)
def test_unsupported_file_type():
"""Test that unsupported file types are rejected."""
with tempfile.NamedTemporaryFile(suffix=".tiff", delete=False) as f:
f.write(b"test")
filepath = f.name
try:
try:
mod.build_file_content(filepath)
assert False, "Expected sys.exit for unsupported type"
except SystemExit as e:
assert e.code == 1
finally:
os.unlink(filepath)