File size: 11,723 Bytes
aef804e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Shared fixtures for workflow integration tests.

Provides database sessions, mock LLM providers, mock WebSocket managers,
and sample workflow/episode data for integration testing.

Key principles:
- Real database (SQLite in-memory) for persistence testing
- Mocked external services (LLM providers, WebSocket) to prevent flakiness
- Transaction rollback after each test for isolation
"""

import os
import pytest
import tempfile
import uuid
from datetime import datetime, timedelta
from unittest.mock import AsyncMock, MagicMock
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker

# Set TESTING environment variable BEFORE any imports
os.environ["TESTING"] = "1"

# Add parent directory to path for imports
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent))

from core.database import Base
from core.models import (
    WorkflowExecution, WorkflowExecutionStatus,
    AgentEpisode, EpisodeSegment, EpisodeOutcome,
    ChatSession, CanvasAudit, AgentFeedback
)


@pytest.fixture(scope="function")
def db_session():
    """
    Create a fresh in-memory database for each test.

    Uses SQLite in-memory database with transaction rollback for test isolation.
    All tables are created before the test and cleaned up after.
    """
    # Use in-memory SQLite for tests
    engine = create_engine(
        "sqlite:///:memory:",
        connect_args={"check_same_thread": False},
        echo=False
    )

    # Create all tables
    try:
        Base.metadata.create_all(engine, checkfirst=True)
    except Exception as e:
        # If create_all fails, create tables individually
        for table in Base.metadata.tables.values():
            try:
                table.create(engine, checkfirst=True)
            except Exception:
                continue

    # Create session
    TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    session = TestingSessionLocal()

    yield session

    # Cleanup
    session.close()
    engine.dispose()


@pytest.fixture(scope="function")
def mock_llm_provider():
    """
    AsyncMock for LLM provider calls.

    Mocks both call_llm() and stream_llm() methods to prevent
    external API calls during testing.
    """
    mock_provider = AsyncMock()

    # Mock call_llm to return success response
    async def mock_call_llm(*args, **kwargs):
        return {
            "result": "success",
            "content": "Mock LLM response",
            "usage": {
                "prompt_tokens": 10,
                "completion_tokens": 20,
                "total_tokens": 30
            }
        }

    # Mock stream_llm to return async generator
    async def mock_stream_llm(*args, **kwargs):
        chunks = [
            {"choices": [{"delta": {"content": "Mock "}, "finish_reason": None}], "usage": None},
            {"choices": [{"delta": {"content": "streaming "}, "finish_reason": None}], "usage": None},
            {"choices": [{"delta": {"content": "response"}, "finish_reason": None}], "usage": None},
            {"choices": [{"delta": {}, "finish_reason": "stop"}],
             "usage": {"prompt_tokens": 10, "completion_tokens": 20, "total_tokens": 30}}
        ]
        for chunk in chunks:
            yield chunk

    mock_provider.call_llm = mock_call_llm
    mock_provider.stream_llm = mock_stream_llm

    return mock_provider


@pytest.fixture(scope="function")
def mock_websocket_manager():
    """
    MagicMock for WebSocket notifications.

    Tracks all WebSocket calls for verification in tests.
    """
    mock_ws = MagicMock()

    # Mock notification methods
    mock_ws.notify_workflow_status = AsyncMock()
    mock_ws.broadcast_message = AsyncMock()
    mock_ws.send_message = AsyncMock()

    # Track calls for verification
    mock_ws.reset_mock()

    return mock_ws


@pytest.fixture(scope="function")
def sample_workflow():
    """
    Valid workflow definitions for tests.

    Returns a dict with three workflow variations:
    - simple: 2-step linear execution
    - branching: if/else conditional logic
    - error_handling: try/catch error recovery
    """
    return {
        "simple": {
            "id": "simple_workflow",
            "nodes": [
                {
                    "id": "step1",
                    "title": "First Step",
                    "type": "action",
                    "config": {
                        "action": "collect_data",
                        "service": "data_service",
                        "parameters": {"source": "api"}
                    }
                },
                {
                    "id": "step2",
                    "title": "Second Step",
                    "type": "action",
                    "config": {
                        "action": "process_data",
                        "service": "processing_service",
                        "parameters": {"mode": "batch"}
                    }
                }
            ],
            "connections": [
                {"source": "step1", "target": "step2"}
            ]
        },
        "branching": {
            "id": "branching_workflow",
            "nodes": [
                {
                    "id": "step1",
                    "title": "Evaluate Condition",
                    "type": "action",
                    "config": {
                        "action": "evaluate",
                        "service": "logic_service",
                        "parameters": {}
                    }
                },
                {
                    "id": "true_branch",
                    "title": "True Branch",
                    "type": "action",
                    "config": {
                        "action": "handle_true",
                        "service": "logic_service",
                        "parameters": {"branch": "true"}
                    }
                },
                {
                    "id": "false_branch",
                    "title": "False Branch",
                    "type": "action",
                    "config": {
                        "action": "handle_false",
                        "service": "logic_service",
                        "parameters": {"branch": "false"}
                    }
                }
            ],
            "connections": [
                {"source": "step1", "target": "true_branch", "condition": "${step1.result} == true"},
                {"source": "step1", "target": "false_branch", "condition": "${step1.result} == false"}
            ]
        },
        "error_handling": {
            "id": "error_handling_workflow",
            "nodes": [
                {
                    "id": "step1",
                    "title": "Risky Step",
                    "type": "action",
                    "config": {
                        "action": "risky_operation",
                        "service": "risky_service",
                        "parameters": {"continue_on_error": True}
                    }
                },
                {
                    "id": "step2",
                    "title": "Recovery Step",
                    "type": "action",
                    "config": {
                        "action": "recover",
                        "service": "recovery_service",
                        "parameters": {}
                    }
                }
            ],
            "connections": [
                {"source": "step1", "target": "step2"}
            ]
        }
    }


@pytest.fixture(scope="function")
def sample_episode_context(db_session: Session):
    """
    Episode context for segmentation tests.

    Creates a realistic episode context with:
    - ChatSession with messages
    - Canvas presentation events
    - User feedback data
    """
    # Create ChatSession with messages
    chat_session = ChatSession(
        id=str(uuid.uuid4()),
        user_id="test_user",
        title="Test Episode Session",
        metadata_json={"agent_id": "test_agent"}
    )
    db_session.add(chat_session)

    # Create sample canvas presentation
    canvas_audit = CanvasAudit(
        id=str(uuid.uuid4()),
        canvas_id="test_canvas_123",
        canvas_type="line_chart",
        agent_id="test_agent",
        user_id="test_user",
        session_id=chat_session.id,
        title="Sales Performance Chart",
        config={"data": [1, 2, 3, 4, 5]},
        status="presented"
    )
    db_session.add(canvas_audit)

    # Create sample user feedback
    agent_feedback = AgentFeedback(
        id=str(uuid.uuid4()),
        agent_id="test_agent",
        user_id="test_user",
        session_id=chat_session.id,
        feedback_type="thumbs_up",
        feedback_value=1.0,
        comment="Great analysis!"
    )
    db_session.add(agent_feedback)

    db_session.commit()

    return {
        "chat_session": chat_session,
        "canvas_audit": canvas_audit,
        "agent_feedback": agent_feedback,
        "messages": [
            {
                "role": "user",
                "content": "Show me the sales data",
                "timestamp": datetime.utcnow() - timedelta(minutes=10)
            },
            {
                "role": "assistant",
                "content": "Here's the sales performance chart",
                "timestamp": datetime.utcnow() - timedelta(minutes=9),
                "canvas_id": canvas_audit.canvas_id
            },
            {
                "role": "user",
                "content": "Great analysis!",
                "timestamp": datetime.utcnow() - timedelta(minutes=8),
                "feedback_id": agent_feedback.id
            }
        ]
    }


@pytest.fixture(scope="function")
def sample_episode(db_session: Session):
    """
    Create a sample AgentEpisode with segments for testing.

    Returns an episode with 3 segments representing a simple workflow.
    """
    episode_id = str(uuid.uuid4())

    episode = AgentEpisode(
        id=episode_id,
        agent_id="test_agent",
        user_id="test_user",
        workflow_id="test_workflow",
        outcome=EpisodeOutcome.SUCCESS,
        title="Test Episode",
        summary="Test episode with multiple segments",
        start_time=datetime.utcnow() - timedelta(minutes=10),
        end_time=datetime.utcnow()
    )
    db_session.add(episode)

    # Create segments
    segment1 = EpisodeSegment(
        id=str(uuid.uuid4()),
        episode_id=episode_id,
        segment_type="action",
        title="Data Collection",
        content="Collected data from API",
        start_time=datetime.utcnow() - timedelta(minutes=10),
        end_time=datetime.utcnow() - timedelta(minutes=8),
        metadata={"action": "collect_data", "source": "api"}
    )
    db_session.add(segment1)

    segment2 = EpisodeSegment(
        id=str(uuid.uuid4()),
        episode_id=episode_id,
        segment_type="action",
        title="Data Processing",
        content="Processed data in batch mode",
        start_time=datetime.utcnow() - timedelta(minutes=8),
        end_time=datetime.utcnow() - timedelta(minutes=5),
        metadata={"action": "process_data", "mode": "batch"}
    )
    db_session.add(segment2)

    segment3 = EpisodeSegment(
        id=str(uuid.uuid4()),
        episode_id=episode_id,
        segment_type="result",
        title="Result Presentation",
        content="Presented results to user",
        start_time=datetime.utcnow() - timedelta(minutes=5),
        end_time=datetime.utcnow(),
        metadata={"action": "present_results"}
    )
    db_session.add(segment3)

    db_session.commit()

    return {
        "episode": episode,
        "segments": [segment1, segment2, segment3]
    }