"""
Tests for Advanced Features of Smart Card API
This module contains tests for the advanced features of the Smart Card API,
including layout optimization and multi-modal content support.
"""
import os
import sys
from typing import Dict, List, Optional
import pytest
# Add the parent directory to the path so we can import the modules
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
from gchat.content_mapping.layout_optimizer import LayoutOptimizer
from gchat.content_mapping.multi_modal_support import MultiModalSupport
@pytest.mark.asyncio
async def test_layout_optimizer():
"""Test the layout optimizer."""
# Initialize the layout optimizer
optimizer = LayoutOptimizer()
# Create a test card
card = {
"header": {"title": "Test Card"},
"sections": [{"widgets": [{"textParagraph": {"text": "Test text"}}]}],
}
# Test analyze_card_engagement
card_id = "card_123"
metrics = await optimizer.analyze_card_engagement(card_id)
# Verify metrics structure
assert isinstance(metrics, dict)
assert "impressions" in metrics
assert "clicks" in metrics
assert "click_through_rate" in metrics
assert "avg_time_spent" in metrics
# Test suggest_layout_improvements
improvements = await optimizer.suggest_layout_improvements(card)
# Verify improvements structure
assert isinstance(improvements, list)
assert len(improvements) > 0
assert "type" in improvements[0]
assert "description" in improvements[0]
assert "confidence" in improvements[0]
# Test create_ab_test
variations = [
{
"header": {"title": "Variation 1"},
"sections": [
{"widgets": [{"textParagraph": {"text": "Variation 1 text"}}]}
],
},
{
"header": {"title": "Variation 2"},
"sections": [
{"widgets": [{"textParagraph": {"text": "Variation 2 text"}}]}
],
},
]
test_id = await optimizer.create_ab_test(card, variations)
# Verify test_id
assert isinstance(test_id, str)
assert test_id.startswith("abtest_")
# Test get_ab_test_results
results = await optimizer.get_ab_test_results(test_id)
# Verify results structure
assert isinstance(results, dict)
assert "test_id" in results
assert "original" in results
assert "variations" in results
assert isinstance(results["variations"], list)
@pytest.mark.asyncio
async def test_multi_modal_support():
"""Test multi-modal support."""
# Initialize multi-modal support
multi_modal = MultiModalSupport()
# Test image optimization
image_url = "https://example.com/image.jpg"
optimized_url = await multi_modal.optimize_image(image_url)
# Verify optimized URL
assert isinstance(optimized_url, str)
assert optimized_url.startswith("http")
# Test with custom size
custom_size = (400, 300)
optimized_url_custom = await multi_modal.optimize_image(image_url, custom_size)
assert f"width={custom_size[0]}" in optimized_url_custom
assert f"height={custom_size[1]}" in optimized_url_custom
# Test chart generation
data = {"labels": ["Q1", "Q2", "Q3", "Q4"], "values": [10, 20, 15, 25]}
chart_url = await multi_modal.generate_chart(data)
# Verify chart URL
assert isinstance(chart_url, str)
assert chart_url.startswith("http")
# Test with different chart type
chart_url_pie = await multi_modal.generate_chart(data, "pie")
assert "type=pie" in chart_url_pie
# Test video thumbnail extraction
video_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
thumbnail_url = await multi_modal.extract_video_thumbnail(video_url)
# Verify thumbnail URL
assert isinstance(thumbnail_url, str)
assert thumbnail_url.startswith("http")
# Test data to table conversion
table_data = [
{"name": "John", "age": 30, "role": "Developer"},
{"name": "Jane", "age": 28, "role": "Designer"},
{"name": "Bob", "age": 35, "role": "Manager"},
]
table_widget = await multi_modal.convert_data_to_table(table_data)
# Verify table widget structure
assert isinstance(table_widget, dict)
assert "section" in table_widget
assert "widgets" in table_widget["section"]
# Test image grid creation
image_urls = [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg",
"https://example.com/image3.jpg",
]
grid_widget = await multi_modal.create_image_grid(image_urls)
# Verify grid widget structure
assert isinstance(grid_widget, dict)
assert "section" in grid_widget
assert "widgets" in grid_widget["section"]
@pytest.mark.asyncio
async def test_integration():
"""Test integration of advanced features with Smart Card API."""
# This test would normally import and test the smart_card_api functions
# that use the advanced features, but we'll mock them here for simplicity
# Mock the optimize_card_layout function
async def mock_optimize_card_layout(card_id: str) -> Dict:
optimizer = LayoutOptimizer()
metrics = await optimizer.analyze_card_engagement(card_id)
card = {
"header": {"title": "Test Card"},
"sections": [{"widgets": [{"textParagraph": {"text": "Test text"}}]}],
}
improvements = await optimizer.suggest_layout_improvements(card)
return {"metrics": metrics, "improvements": improvements}
# Mock the create_multi_modal_card function
async def mock_create_multi_modal_card(
user_google_email: str,
space_id: str,
content: str,
data: Dict = None,
images: List[str] = None,
video_url: str = None,
thread_key: Optional[str] = None,
webhook_url: Optional[str] = None,
) -> str:
multi_modal = MultiModalSupport()
# Process multi-modal content
processed_content = content
if data:
chart_url = await multi_modal.generate_chart(data)
processed_content += f"\nImage: {chart_url}"
if images:
optimized_images = []
for image_url in images:
optimized_url = await multi_modal.optimize_image(image_url)
optimized_images.append(optimized_url)
image_grid = await multi_modal.create_image_grid(optimized_images)
# In a real implementation, this would add the image grid to the card
if video_url:
thumbnail_url = await multi_modal.extract_video_thumbnail(video_url)
processed_content += (
f"\nImage: {thumbnail_url}\nButton: Watch Video -> {video_url}"
)
# In a real implementation, this would create and send the card
return f"Card sent successfully! Message ID: spaces/{space_id}/messages/123"
# Test the mock functions
result = await mock_optimize_card_layout("card_123")
assert "metrics" in result
assert "improvements" in result
message = await mock_create_multi_modal_card(
user_google_email="user@example.com",
space_id="spaces/123",
content="Title: Quarterly Results | Text: Here are the Q2 results",
data={"labels": ["Q1", "Q2", "Q3", "Q4"], "values": [10, 20, 15, 25]},
images=["https://example.com/image1.jpg", "https://example.com/image2.jpg"],
video_url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
)
assert "Card sent successfully" in message
assert "spaces/123/messages" in message