Testing Procedures
Testing Procedures
To ensure that the Udility Diffuser is correctly installed and functioning, follow these testing procedures. These tests validate the connection to the Llama-3.1/3.5 models via OpenRouter, the integrity of the generated SVG code, and the successful rendering of the final image.
1. Environmental Readiness Test
Before running functional tests, verify that your environment variables and dependencies are correctly configured.
Connectivity Check:
Create a file named test_env.py:
import os
from Udility import diffuser
def test_api_config():
api_key = os.getenv("OPENROUTER_API_KEY")
if api_key:
print(f"✅ API Key detected: {api_key[:4]}...{api_key[-4:]}")
else:
print("❌ Error: OPENROUTER_API_KEY environment variable is missing.")
if __name__ == "__main__":
test_api_config()
2. End-to-End Functional Test
The primary way to test the package is to run the full generation pipeline. This validates the text-to-SVG logic and the SVG-to-PNG conversion.
from Udility import diffuser
import os
def test_full_pipeline():
test_prompt = "A simple diagram of a water molecule."
output_file = "test_molecule.png"
try:
print("Starting image generation...")
diffuser.generate_image_from_text(test_prompt, output_filename=output_file)
# Validate output
if os.path.exists(output_file):
print(f"✅ Success: Image saved to {output_file}")
else:
print("❌ Failure: Image file was not created.")
except Exception as e:
print(f"❌ Test failed with error: {e}")
if __name__ == "__main__":
test_full_pipeline()
3. SVG Integrity Validation
Udility Diffuser relies on the LLM's ability to generate valid XML-based SVG code. You can test the integrity of the generated SVG script independently of the image rendering.
from Udility import diffuser
def test_svg_syntax():
prompt = "A red circle on a blue background."
# Step 1: Get instructions
instructions = diffuser.get_detailed_instructions(prompt)
# Step 2: Generate SVG code
svg_code = diffuser.generate_svg_from_instructions(instructions)
# Integrity Checks
if "<svg" in svg_code and "</svg>" in svg_code:
print("✅ SVG Integrity: Valid tags detected.")
else:
print("❌ SVG Integrity: Invalid SVG structure generated.")
print(f"Generated Snippet: {svg_code[:100]}...")
if __name__ == "__main__":
test_svg_syntax()
4. Rendering Engine Test
This test ensures that cairosvg and matplotlib are correctly handling the conversion from vector to raster graphics.
from Udility import diffuser
import os
def test_rendering():
# Minimal valid SVG for testing
sample_svg = '<svg height="100" width="100"><circle cx="50" cy="50" r="40" fill="red" /></svg>'
output_path = "render_test.png"
try:
diffuser.svg_to_png(sample_svg, output_filename=output_path)
print("✅ Rendering: SVG converted to PNG successfully.")
except Exception as e:
print(f"❌ Rendering: Conversion failed. Check cairosvg/cairocffi dependencies. Error: {e}")
if __name__ == "__main__":
test_rendering()
Troubleshooting Common Test Failures
| Issue | Likely Cause | Resolution |
| :--- | :--- | :--- |
| EnvironmentError | API Key not set. | Run export OPENROUTER_API_KEY='your_key' in your terminal. |
| RuntimeError (SVG to PNG) | Missing system C libraries. | Ensure libcairo2 (Linux) or cairo (macOS/Homebrew) is installed on your system. |
| OpenAI.AuthenticationError | Invalid API Key. | Verify your key status at OpenRouter. |
| Empty Image / Black Box | LLM generated invalid SVG. | Re-run the command; LLM output can occasionally vary in syntax accuracy. |