"""Tests for the project analyzer.""" import json from pathlib import Path import pytest from dss.analyze.base import Framework from dss.analyze.project_analyzer import analyze_project, export_project_context, run_project_analysis @pytest.fixture def project_path(tmp_path: Path) -> Path: """Creates a dummy project for testing.""" project_path = tmp_path / "project" (project_path / "src").mkdir(parents=True) (project_path / "package.json").write_text( json.dumps({"dependencies": {"react": "18.0.0"}}, indent=2), encoding="utf-8" ) (project_path / "src" / "Button.jsx").write_text( "\n".join( [ 'import React from "react";', 'import "./button.css";', "", "export function Button({ label }) {", ' return ;', "}", "", ] ), encoding="utf-8", ) (project_path / "src" / "button.css").write_text( "\n".join( [ ".btn {", " color: #ff0000;", "}", "", ] ), encoding="utf-8", ) return project_path def test_analyze_project(project_path: Path): """Tests that the project analyzer can analyze a project.""" analysis = analyze_project(str(project_path)) assert analysis.project_path == str(project_path.resolve()) assert analysis.framework == Framework.REACT assert analysis.component_count >= 1 assert analysis.style_file_count == 1 def test_run_project_analysis_writes_graph(project_path: Path): """Writes analysis output to /.dss/analysis_graph.json.""" result = run_project_analysis(str(project_path)) output_path = project_path / ".dss" / "analysis_graph.json" assert output_path.exists() saved = json.loads(output_path.read_text(encoding="utf-8")) assert saved["project_path"] == str(project_path.resolve()) assert "nodes" in saved assert "edges" in saved assert "analysis" in saved assert result["project_path"] == str(project_path.resolve()) def test_export_project_context(project_path: Path): """Exports a lightweight context payload for prompt injection.""" ctx = export_project_context(str(project_path)) assert ctx["project_path"] == str(project_path.resolve()) assert ctx["framework"] == Framework.REACT.value