File size: 10,872 Bytes
ba52031
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""
Task 6: Complex Multi-file Refactoring Seed Data

Comprehensive seed data for architectural refactoring task.
Contains multiple Python modules with architectural issues including:
- Circular dependencies
- Tight coupling between modules
- Broken abstraction layers
- Missing integration points
- Data flow inconsistencies
"""

from typing import Any, Dict, List


def get_task6_seed_data() -> Dict[str, Any]:
    """
    Generate seed data for task6 (Complex Multi-file Refactoring).

    Files:
      - module_a.py: Data processing module
      - module_b.py: Business logic module (depends on A)
      - module_c.py: API interface module
      - integration.py: Integration layer (coordination)

    Architectural Issues:
      1. Circular dependency: A ↔ B ↔ C
      2. Tight coupling: Direct object instantiation instead of DI
      3. Broken abstraction: B accesses A's internal state directly
      4. Inconsistent error handling across modules
      5. Missing interface definitions
      6. Data validation scattered across modules
      7. Integration logic mixed with business logic
      8. Test coverage gaps due to tight coupling

    Goal:
      Refactor to:
      - Break circular dependencies via dependency injection
      - Extract common interfaces
      - Consolidate validation logic
      - Improve separation of concerns
      - Make integration layer explicit
    """
    return {
        "files": {
            "module_a.py": get_module_a_content(),
            "module_b.py": get_module_b_content(),
            "module_c.py": get_module_c_content(),
            "integration.py": get_integration_content(),
        },
        "test_file": "test_integration.py",
        "architectural_issues": [
            "Circular import: module_a imports from module_b, module_b imports from module_a",
            "Tight coupling: ProcessorB directly instantiates ProcessorA",
            "Broken abstraction: ProcessorB accesses _raw_data (internal) from ProcessorA",
            "Inconsistent validation: validate() in A, validate() in B, no validation in C",
            "Missing interface: No abstract base for processors",
            "Data flow unclear: Integration happens in multiple places",
            "Error handling: Different exception patterns in each module",
            "Integration logic: BusinessLogic.execute() does too much",
        ],
        "refactoring_goals": [
            "Extract common processor interface (Processor ABC)",
            "Use dependency injection instead of instantiation",
            "Move validation to a dedicated Validator class",
            "Consolidate error handling with custom exceptions",
            "Make integration explicit via Integration class",
            "Break circular dependency via interface abstraction",
            "Separate concerns: each module has single responsibility",
        ],
        "tools_to_use": [
            "list_directory",
            "get_file_structure",
            "get_dependencies",
            "search_code",
            "get_test_coverage",
            "edit_file",
            "run_tests",
        ],
        "difficulty": "hard",
    }


def get_module_a_content() -> str:
    """Get module_a.py content."""
    return '''"""
Module A: Data Processing

Responsible for low-level data processing.
Currently has tight coupling to Module B.
"""


class ProcessorA:
    """Process raw data."""

    def __init__(self):
        """Initialize processor A."""
        self._raw_data = []
        self._processed = False

    def load(self, data: list) -> None:
        """Load raw data."""
        self._raw_data = data
        self._processed = False

    def validate(self) -> bool:
        """Validate raw data."""
        if not self._raw_data:
            return False
        # Basic validation
        return all(isinstance(x, (int, str)) for x in self._raw_data)

    def process(self) -> list:
        """Process the data."""
        if not self.validate():
            raise ValueError("Invalid data in ProcessorA")

        result = [str(x).upper() for x in self._raw_data]
        self._processed = True
        return result


def create_processor_a() -> ProcessorA:
    """Factory function for ProcessorA."""
    return ProcessorA()
'''


def get_module_b_content() -> str:
    """Get module_b.py content."""
    return '''"""
Module B: Business Logic

Contains business logic that depends on Module A.
Has circular dependency with Module A.
Tightly coupled to ProcessorA implementation.
"""

from module_a import ProcessorA, create_processor_a


class BusinessLogic:
    """Business logic processor."""

    def __init__(self):
        """Initialize business logic."""
        # Tight coupling: directly instantiates ProcessorA
        self.processor_a = create_processor_a()
        self._cache = {}

    def validate(self) -> bool:
        """Validate business logic state."""
        # Inconsistent validation logic
        if not hasattr(self.processor_a, "_raw_data"):
            return False
        return len(self.processor_a._raw_data) > 0  # Broken abstraction!

    def execute(self, data: list) -> dict:
        """
        Execute business logic.
        This method does too much (violates SRP).
        """
        # Load data
        self.processor_a.load(data)

        # Validate using ProcessorA
        if not self.processor_a.validate():
            raise ValueError("Data validation failed")

        # Process
        processed = self.processor_a.process()

        # Additional business logic
        enriched = [f"processed_{item}" for item in processed]

        # Direct access to internal state (broken abstraction)
        result = {
            "original_count": len(self.processor_a._raw_data),
            "processed_count": len(enriched),
            "processed_data": enriched,
        }

        # Cache management (integration concern mixed in)
        self._cache["last_result"] = result

        return result

    def get_last_result(self) -> dict:
        """Get cached result."""
        return self._cache.get("last_result", {})


def create_business_logic() -> BusinessLogic:
    """Factory function for BusinessLogic."""
    return BusinessLogic()
'''


def get_module_c_content() -> str:
    """Get module_c.py content."""
    return '''"""
Module C: API Interface

Provides API interface to the system.
Currently doesn't validate input (relies on downstream modules).
Has tight coupling to BusinessLogic.
"""

from module_b import create_business_logic


class APIEndpoint:
    """API endpoint interface."""

    def __init__(self):
        """Initialize API endpoint."""
        # Tight coupling: directly instantiates BusinessLogic
        self.business_logic = create_business_logic()

    def process_request(self, data: list) -> dict:
        """
        Process incoming request.
        No validation at API boundary!
        """
        # Should validate here, but doesn't
        result = self.business_logic.execute(data)
        return {
            "status": "success",
            "data": result,
        }

    def get_status(self) -> dict:
        """Get current status."""
        # Inconsistent error handling
        try:
            last_result = self.business_logic.get_last_result()
            return {
                "status": "ok",
                "last_result_size": len(last_result.get("processed_data", [])),
            }
        except Exception as e:
            return {"status": "error", "message": str(e)}


def create_api_endpoint() -> APIEndpoint:
    """Factory function for APIEndpoint."""
    return APIEndpoint()
'''


def get_integration_content() -> str:
    """Get integration.py content."""
    return '''"""
Integration Module

Currently mixes integration logic with business logic.
No explicit integration layer.
"""

from module_c import create_api_endpoint


def main():
    """
    Main integration point.
    This is the only place orchestration happens,
    but orchestration logic is scattered across modules.
    """
    # Create API endpoint (which creates BusinessLogic, which creates ProcessorA)
    api = create_api_endpoint()

    # Process some data
    raw_data = ["apple", "banana", "cherry", 123]

    try:
        response = api.process_request(raw_data)
        print("Success:", response)
    except ValueError as e:
        print("Validation error:", e)
    except Exception as e:
        print("Unexpected error:", e)

    # Get status
    status = api.get_status()
    print("Status:", status)

    return response


if __name__ == "__main__":
    main()
'''


def get_test_file_content() -> str:
    """Get test_integration.py file content."""
    return '''"""
Integration tests for multi-module system.

These tests are difficult to write due to tight coupling.
They need to instantiate many objects at once.
"""

import pytest
from module_a import ProcessorA
from module_b import BusinessLogic
from module_c import APIEndpoint


def test_processor_a_basic():
    """Test ProcessorA basic functionality."""
    processor = ProcessorA()
    processor.load(["hello", "world"])

    assert processor.validate()
    assert processor.process() == ["HELLO", "WORLD"]


def test_processor_a_invalid_data():
    """Test ProcessorA with invalid data."""
    processor = ProcessorA()
    processor.load([None, {}])  # Invalid types

    assert not processor.validate()


def test_processor_a_empty_data():
    """Test ProcessorA with empty data."""
    processor = ProcessorA()
    processor.load([])

    assert not processor.validate()


def test_business_logic():
    """Test BusinessLogic execution."""
    bl = BusinessLogic()
    result = bl.execute(["test", "data"])

    assert result["processed_count"] == 2
    assert "processed_test" in result["processed_data"]


def test_business_logic_caching():
    """Test BusinessLogic result caching."""
    bl = BusinessLogic()

    result1 = bl.execute(["first"])
    cached = bl.get_last_result()

    assert cached == result1
    assert len(cached["processed_data"]) == 1


def test_api_endpoint():
    """Test APIEndpoint."""
    api = APIEndpoint()
    response = api.process_request(["api", "test"])

    assert response["status"] == "success"
    assert "data" in response


def test_api_status():
    """Test APIEndpoint status."""
    api = APIEndpoint()
    api.process_request(["test"])

    status = api.get_status()
    assert status["status"] == "ok"
    assert status["last_result_size"] == 1


def test_full_integration():
    """Test full system integration."""
    api = APIEndpoint()

    # Process data through full stack
    response = api.process_request(["integration", "test", "data"])

    assert response["status"] == "success"
    assert len(response["data"]["processed_data"]) == 3


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
'''