feat: Implement atomic design system core structure and recursive Figma import

This commit is contained in:
DSS
2025-12-11 06:28:21 -03:00
parent edb991093a
commit bcd1a86ae4
12 changed files with 893 additions and 1213 deletions

99
tests/test_atomic_dss.py Normal file
View File

@@ -0,0 +1,99 @@
import asyncio
from pathlib import Path
import json
from unittest.mock import patch, MagicMock
import pytest
from httpx import Response
from dss.project.manager import ProjectManager, DSSProject, ProjectRegistry
from dss.models.component import AtomicType, Component
@pytest.fixture
def project_manager(tmp_path: Path) -> ProjectManager:
"""
Fixture for the ProjectManager.
"""
registry_path = tmp_path / "registry.json"
registry = ProjectRegistry(registry_path=registry_path)
return ProjectManager(registry=registry)
@pytest.fixture
def dss_project(project_manager: ProjectManager, tmp_path: Path) -> DSSProject:
"""
Fixture for a DSSProject.
"""
project_path = tmp_path / "test_project"
project = project_manager.init(project_path, "test_project")
project.config.figma = MagicMock()
project.config.figma.files = [MagicMock(key="fake_key", name="fake_name")]
return project
@patch("httpx.AsyncClient")
def test_recursive_figma_import(MockAsyncClient, dss_project: DSSProject, project_manager: ProjectManager):
"""
Test that the Figma import is recursive and that the components are
classified correctly.
"""
# Mock the httpx.AsyncClient to return a sample Figma file
mock_client_instance = MockAsyncClient.return_value
mock_client_instance.get.return_value = Response(
200,
json={
"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"}
],
},
],
}
],
}
},
)
# Run the sync
dss_project = asyncio.run(project_manager.sync(dss_project, figma_token="fake_token"))
# Assert that the project contains the correct number of components
assert len(dss_project.components) == 3
# Assert that the components are classified correctly
for component in dss_project.components:
if component.name == "Icon":
assert component.classification == AtomicType.ATOM
elif component.name == "Button":
assert component.classification == AtomicType.ATOM
elif component.name == "Card":
assert component.classification == AtomicType.MOLECULE

View File

@@ -0,0 +1,91 @@
"""
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:
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):
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):
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.
"""
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."