Files
dss/tests/test_figma_ingest.py

103 lines
3.5 KiB
Python

"""
Tests for the Figma ingestion source.
"""
import asyncio
from unittest.mock import patch, AsyncMock, MagicMock
import pytest
from dss.ingest.sources.figma import FigmaTokenSource
from dss.models.component import AtomicType
# Mock Figma client with async context manager and async methods
class MockAsyncClient:
"""
Mocks the IntelligentFigmaClient for testing purposes.
Simulates an async context manager and provides mock async methods
for Figma API calls.
"""
def __init__(self, *args, **kwargs):
pass
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
pass
async def get_file(self, file_key: str):
"""
Mocks the async get_file method to return a predefined Figma document structure.
"""
return {
"document": {
"id": "0:0",
"name": "Document",
"type": "DOCUMENT",
"children": [
{
"id": "1:0",
"name": "Page 1",
"type": "CANVAS",
"children": [
{
"id": "1:1",
"name": "Icon",
"type": "COMPONENT",
},
{
"id": "1:2",
"name": "Button",
"type": "COMPONENT",
"children": [
{"id": "1:1", "name": "Icon", "type": "COMPONENT"}
],
},
{
"id": "1:3",
"name": "Card",
"type": "COMPONENT_SET",
"children": [
{"id": "1:2", "name": "Button", "type": "COMPONENT"}
],
},
],
}
],
}
}
async def get_file_variables(self, file_key: str):
"""
Mocks the async get_file_variables method to return empty variables.
"""
return {"meta": {"variables": {}, "variableCollections": {}}}
@patch("dss.ingest.sources.figma.IntelligentFigmaClient", new=MockAsyncClient)
def test_figma_component_extraction():
"""
Test that the Figma ingestion source correctly extracts and classifies
components from a mock Figma file structure. It verifies that the recursive
component discovery works and assigns correct AtomicType classifications.
"""
source = FigmaTokenSource(figma_token="fake_token")
token_collection, components = asyncio.run(source.extract("fake_file_key"))
# Assert that the correct number of components were extracted
assert len(components) == 1
# Assert that the components are classified correctly
card_component_found = False
for component in components:
if component.name == "Card":
card_component_found = True
assert component.classification == AtomicType.MOLECULE
assert component.sub_components # should not be empty
assert len(component.sub_components) == 1 # Card has one child
assert component.figma_node_id == "1:3"
assert card_component_found, "Card component not found in extracted components."