File size: 11,011 Bytes
56c063a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""

Validation script for Streamlit dashboard setup.

Checks that all components can be imported and basic functionality works.

"""

import sys
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

def test_imports():
    """Test that all Streamlit components can be imported."""
    
    print("πŸ” Testing component imports...")
    
    try:
        from streamlit_app.components.file_upload import FileUploadComponent
        print("βœ… FileUploadComponent imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import FileUploadComponent: {e}")
        return False
    
    try:
        from streamlit_app.components.business_context import BusinessContextForm
        print("βœ… BusinessContextForm imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import BusinessContextForm: {e}")
        return False
    
    try:
        from streamlit_app.components.progress_monitor import ProgressMonitor
        print("βœ… ProgressMonitor imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import ProgressMonitor: {e}")
        return False
    
    try:
        from streamlit_app.components.results_viewer import ResultsViewer
        print("βœ… ResultsViewer imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import ResultsViewer: {e}")
        return False
    
    try:
        from streamlit_app.components.session_manager import SessionManagerComponent
        print("βœ… SessionManagerComponent imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import SessionManagerComponent: {e}")
        return False
    
    try:
        from streamlit_app.components.auth import AuthManager
        print("βœ… AuthManager imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import AuthManager: {e}")
        return False
    
    try:
        from streamlit_app.utils.api_client import APIClient
        print("βœ… APIClient imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import APIClient: {e}")
        return False
    
    try:
        from streamlit_app.utils.sse_client import SSEClient
        print("βœ… SSEClient imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import SSEClient: {e}")
        return False
    
    return True


def test_component_initialization():
    """Test that components can be initialized."""
    
    print("\nπŸ”§ Testing component initialization...")
    
    try:
        from streamlit_app.components.file_upload import FileUploadComponent
        component = FileUploadComponent()
        assert hasattr(component, 'supported_formats')
        assert hasattr(component, 'max_size_mb')
        print("βœ… FileUploadComponent initializes correctly")
    except Exception as e:
        print(f"❌ FileUploadComponent initialization failed: {e}")
        return False
    
    try:
        from streamlit_app.components.business_context import BusinessContextForm
        component = BusinessContextForm()
        assert hasattr(component, 'context_templates')
        assert len(component.context_templates) > 0
        print("βœ… BusinessContextForm initializes correctly")
    except Exception as e:
        print(f"❌ BusinessContextForm initialization failed: {e}")
        return False
    
    try:
        from streamlit_app.components.results_viewer import ResultsViewer
        component = ResultsViewer()
        assert hasattr(component, 'chart_colors')
        print("βœ… ResultsViewer initializes correctly")
    except Exception as e:
        print(f"❌ ResultsViewer initialization failed: {e}")
        return False
    
    try:
        from streamlit_app.utils.api_client import APIClient
        client = APIClient("http://test.com")
        assert client.base_url == "http://test.com"
        assert hasattr(client, 'session')
        print("βœ… APIClient initializes correctly")
    except Exception as e:
        print(f"❌ APIClient initialization failed: {e}")
        return False
    
    return True


def test_component_methods():
    """Test that key component methods work."""
    
    print("\nβš™οΈ Testing component methods...")
    
    try:
        from streamlit_app.components.file_upload import FileUploadComponent
        from unittest.mock import Mock
        
        component = FileUploadComponent()
        
        # Test file validation with mock file
        mock_file = Mock()
        mock_file.name = "test.txt"  # Unsupported format
        mock_file.getvalue.return_value = b"small file"
        
        result = component._validate_file(mock_file)
        assert not result["valid"]
        assert "Unsupported file format" in result["error"]
        print("βœ… FileUploadComponent validation works")
    except Exception as e:
        print(f"❌ FileUploadComponent method test failed: {e}")
        return False
    
    try:
        from streamlit_app.components.business_context import BusinessContextForm
        
        component = BusinessContextForm()
        
        # Test response validation
        insufficient_responses = {"question_1": "short", "question_2": ""}
        result = component._validate_responses(insufficient_responses)
        assert not result
        
        sufficient_responses = {
            "question_1": "This is a long enough response for validation",
            "question_2": "Another sufficiently long response for testing"
        }
        result = component._validate_responses(sufficient_responses)
        assert result
        print("βœ… BusinessContextForm validation works")
    except Exception as e:
        print(f"❌ BusinessContextForm method test failed: {e}")
        return False
    
    try:
        from streamlit_app.utils.api_client import APIClient
        from unittest.mock import patch
        
        client = APIClient("http://test.com")
        
        # Test header generation (without Streamlit context)
        headers = client._get_headers()
        assert "Content-Type" in headers
        assert headers["Content-Type"] == "application/json"
        
        print("βœ… APIClient methods work")
    except Exception as e:
        print(f"❌ APIClient method test failed: {e}")
        return False
    
    return True


def test_requirements_compliance():
    """Test that implementation meets the specified requirements."""
    
    print("\nπŸ“‹ Testing requirements compliance...")
    
    # Requirement 10.1: Intuitive Streamlit-based dashboard
    try:
        from streamlit_app.main import main, render_main_dashboard
        print("βœ… Main dashboard functions exist")
    except ImportError:
        print("❌ Main dashboard functions missing")
        return False
    
    # Requirement 10.2: Upload progress and validation in real-time
    try:
        from streamlit_app.components.file_upload import FileUploadComponent
        component = FileUploadComponent()
        assert hasattr(component, '_validate_file')
        assert hasattr(component, '_display_file_info')
        print("βœ… File upload with validation implemented")
    except Exception as e:
        print(f"❌ File upload validation missing: {e}")
        return False
    
    # Requirement 10.3: Progress indicators and intermediate results
    try:
        from streamlit_app.components.progress_monitor import ProgressMonitor
        from unittest.mock import Mock
        
        monitor = ProgressMonitor(Mock(), Mock())
        assert hasattr(monitor, 'stages')
        assert len(monitor.stages) > 0
        print("βœ… Progress monitoring implemented")
    except Exception as e:
        print(f"❌ Progress monitoring missing: {e}")
        return False
    
    # Requirement 10.4: Clear visualization of insights and recommendations
    try:
        from streamlit_app.components.results_viewer import ResultsViewer
        viewer = ResultsViewer()
        assert hasattr(viewer, '_render_business_insights')
        assert hasattr(viewer, '_render_model_performance')
        print("βœ… Results visualization implemented")
    except Exception as e:
        print(f"❌ Results visualization missing: {e}")
        return False
    
    # Requirement 10.5: Interactive controls for customization
    try:
        # Check that enhanced parameter controls exist in main.py
        with open(project_root / "streamlit_app" / "main.py", "r", encoding="utf-8") as f:
            content = f.read()
            
        interactive_controls = [
            "st.slider",
            "st.selectbox", 
            "st.multiselect",
            "st.checkbox",
            "st.number_input"
        ]
        
        controls_found = sum(1 for control in interactive_controls if control in content)
        assert controls_found >= 3, f"Only {controls_found} interactive controls found"
        print("βœ… Interactive parameter controls implemented")
    except Exception as e:
        print(f"❌ Interactive controls missing: {e}")
        return False
    
    return True


def main():
    """Run all validation tests."""
    
    print("πŸ§ͺ Streamlit Dashboard Validation")
    print("=" * 50)
    
    tests = [
        ("Component Imports", test_imports),
        ("Component Initialization", test_component_initialization), 
        ("Component Methods", test_component_methods),
        ("Requirements Compliance", test_requirements_compliance)
    ]
    
    results = []
    
    for test_name, test_func in tests:
        print(f"\nπŸ” Running {test_name}...")
        try:
            success = test_func()
            results.append((test_name, success))
        except Exception as e:
            print(f"❌ {test_name} failed with exception: {e}")
            results.append((test_name, False))
    
    # Summary
    print("\n" + "=" * 50)
    print("πŸ“Š Validation Results:")
    
    all_passed = True
    for test_name, success in results:
        status = "βœ… PASSED" if success else "❌ FAILED"
        print(f"  {test_name}: {status}")
        if not success:
            all_passed = False
    
    print(f"\n🎯 Overall Result: {'βœ… ALL TESTS PASSED' if all_passed else '❌ SOME TESTS FAILED'}")
    
    if all_passed:
        print("\nπŸŽ‰ Streamlit dashboard is ready for use!")
        print("πŸ“ To run the dashboard: streamlit run streamlit_app/main.py")
    else:
        print("\n⚠️ Please fix the failing tests before using the dashboard.")
    
    return all_passed


if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)