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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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""
Mobile API Workflow Execution Tests

Tests for mobile API workflow execution endpoints:
- Workflow creation
- Workflow skill addition
- Workflow execution
- Workflow DAG validation
- Workflow execution history

All tests use API-first approach with TestClient (no browser).
Response structure matches web API for consistency.
"""

import pytest
from fastapi.testclient import TestClient


class TestMobileWorkflowCreate:
    """Test mobile workflow creation endpoint"""

    def test_mobile_workflow_create(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test workflow creation"""
        response = mobile_api_client.post(
            "/api/v1/workflows",
            headers=mobile_auth_headers,
            json={
                "name": "Mobile Test Workflow",
                "description": "Test workflow from mobile API",
                "category": "testing"
            }
        )

        # Verify response (may be 201 or 200)
        if response.status_code in [200, 201]:
            data = response.json()

            # Verify workflow_id present
            assert "workflow_id" in data or "id" in data

            # Verify workflow details
            workflow_id = data.get("workflow_id") or data.get("id")
            assert workflow_id is not None

            # Verify name and description in response
            assert "name" in data or data.get("workflow", {}).get("name")
        elif response.status_code == 404:
            pytest.skip("Workflow creation endpoint not implemented")

    def test_mobile_workflow_create_invalid(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test workflow creation with invalid data"""
        response = mobile_api_client.post(
            "/api/v1/workflows",
            headers=mobile_auth_headers,
            json={
                "name": "",  # Invalid: empty name
                "description": "Test"
            }
        )

        # Verify validation error
        assert response.status_code in [400, 422]

    def test_mobile_workflow_create_unauthorized(self, mobile_api_client: TestClient):
        """Test workflow creation without authentication"""
        response = mobile_api_client.post(
            "/api/v1/workflows",
            json={
                "name": "Unauthorized Workflow",
                "description": "Should fail"
            }
        )

        # Verify unauthorized response
        assert response.status_code == 401


class TestMobileWorkflowAddSkill:
    """Test mobile workflow skill management"""

    def test_mobile_workflow_add_skill(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test adding skill to workflow"""
        # First, create a workflow
        create_response = mobile_api_client.post(
            "/api/v1/workflows",
            headers=mobile_auth_headers,
            json={
                "name": "Test Workflow for Skills",
                "description": "Testing skill addition"
            }
        )

        if create_response.status_code not in [200, 201]:
            pytest.skip("Workflow creation not available")

        create_data = create_response.json()
        workflow_id = create_data.get("workflow_id") or create_data.get("id")

        if not workflow_id:
            pytest.skip("Workflow ID not returned")

        # Add skill to workflow
        response = mobile_api_client.post(
            f"/api/v1/workflows/{workflow_id}/skills",
            headers=mobile_auth_headers,
            json={
                "skill_id": "test_skill",
                "order": 1,
                "parameters": {"test": "value"}
            }
        )

        # Verify response
        if response.status_code == 200:
            data = response.json()
            assert "success" in data or "skill_id" in data

            # Verify skill added by fetching workflow
            get_response = mobile_api_client.get(
                f"/api/v1/workflows/{workflow_id}",
                headers=mobile_auth_headers
            )

            if get_response.status_code == 200:
                workflow_data = get_response.json()
                # Verify skill present in workflow
                assert "skills" in workflow_data or "nodes" in workflow_data
        elif response.status_code == 404:
            pytest.skip("Workflow skill endpoint not implemented")

    def test_mobile_workflow_list_skills(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test listing workflow skills"""
        # Try to list skills for a workflow
        response = mobile_api_client.get(
            "/api/v1/workflows/test_workflow_id/skills",
            headers=mobile_auth_headers
        )

        # May be 404 if workflow doesn't exist, or endpoint not implemented
        if response.status_code == 200:
            data = response.json()
            assert "skills" in data or isinstance(data, list)
        elif response.status_code == 404:
            # Expected - workflow doesn't exist or endpoint not implemented
            pass


class TestMobileWorkflowExecute:
    """Test mobile workflow execution endpoint"""

    def test_mobile_workflow_execute(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test workflow execution"""
        # First, create a simple workflow
        create_response = mobile_api_client.post(
            "/api/v1/workflows",
            headers=mobile_auth_headers,
            json={
                "name": "Executable Test Workflow",
                "description": "Testing workflow execution"
            }
        )

        if create_response.status_code not in [200, 201]:
            pytest.skip("Workflow creation not available")

        create_data = create_response.json()
        workflow_id = create_data.get("workflow_id") or create_data.get("id")

        if not workflow_id:
            pytest.skip("Workflow ID not returned")

        # Execute workflow
        response = mobile_api_client.post(
            f"/api/v1/workflows/{workflow_id}/execute",
            headers=mobile_auth_headers,
            json={
                "input": "test input data"
            }
        )

        # Verify response
        if response.status_code == 200:
            data = response.json()

            # Verify execution_id present
            assert "execution_id" in data or "id" in data

            # Verify status
            assert "status" in data or data.get("success") is True
        elif response.status_code == 404:
            pytest.skip("Workflow execution endpoint not implemented")

    def test_mobile_workflow_execute_with_parameters(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test workflow execution with custom parameters"""
        # Create workflow
        create_response = mobile_api_client.post(
            "/api/v1/workflows",
            headers=mobile_auth_headers,
            json={
                "name": "Parameterized Workflow",
                "description": "Testing parameter passing"
            }
        )

        if create_response.status_code not in [200, 201]:
            pytest.skip("Workflow creation not available")

        create_data = create_response.json()
        workflow_id = create_data.get("workflow_id") or create_data.get("id")

        if not workflow_id:
            pytest.skip("Workflow ID not returned")

        # Execute with parameters
        response = mobile_api_client.post(
            f"/api/v1/workflows/{workflow_id}/execute",
            headers=mobile_auth_headers,
            json={
                "input": "test",
                "parameters": {
                    "param1": "value1",
                    "param2": 123,
                    "param3": True
                }
            }
        )

        # Verify execution started
        if response.status_code == 200:
            data = response.json()
            assert "execution_id" in data or "success" in data
        elif response.status_code == 404:
            pytest.skip("Workflow execution endpoint not implemented")

    def test_mobile_workflow_execute_not_found(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test executing non-existent workflow"""
        response = mobile_api_client.post(
            "/api/v1/workflows/nonexistent_workflow_id/execute",
            headers=mobile_auth_headers,
            json={"input": "test"}
        )

        # Verify error response
        assert response.status_code in [404, 400]


class TestMobileWorkflowDAGValidation:
    """Test mobile workflow DAG validation"""

    def test_mobile_workflow_dag_validation(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test DAG validation detects cycles"""
        # Try to validate a workflow with cyclic dependency
        response = mobile_api_client.post(
            "/api/v1/workflows/validate",
            headers=mobile_auth_headers,
            json={
                "name": "Cyclic Workflow",
                "nodes": [
                    {"id": "A", "type": "skill"},
                    {"id": "B", "type": "skill"},
                    {"id": "C", "type": "skill"}
                ],
                "edges": [
                    {"from": "A", "to": "B"},
                    {"from": "B", "to": "C"},
                    {"from": "C", "to": "A"}  # Cycle!
                ]
            }
        )

        # Verify validation fails
        if response.status_code == 200:
            data = response.json()

            # If validation endpoint exists, it should detect cycle
            if "valid" in data:
                assert data["valid"] is False
                assert "cycle" in str(data.get("error", "")).lower()
            elif "errors" in data:
                # Validation returned list of errors
                assert len(data["errors"]) > 0
        elif response.status_code == 404:
            pytest.skip("Workflow validation endpoint not implemented")

    def test_mobile_workflow_dag_valid(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test DAG validation passes for valid DAG"""
        # Create a valid DAG (no cycles)
        response = mobile_api_client.post(
            "/api/v1/workflows/validate",
            headers=mobile_auth_headers,
            json={
                "name": "Valid DAG Workflow",
                "nodes": [
                    {"id": "A", "type": "skill"},
                    {"id": "B", "type": "skill"},
                    {"id": "C", "type": "skill"}
                ],
                "edges": [
                    {"from": "A", "to": "B"},
                    {"from": "B", "to": "C"}
                    # No edge back to A - valid DAG
                ]
            }
        )

        # Verify validation passes
        if response.status_code == 200:
            data = response.json()

            if "valid" in data:
                assert data["valid"] is True
            elif "errors" in data:
                # Should have no errors
                assert len(data["errors"]) == 0
        elif response.status_code == 404:
            pytest.skip("Workflow validation endpoint not implemented")


class TestMobileWorkflowExecutionHistory:
    """Test mobile workflow execution history"""

    def test_mobile_workflow_execution_history(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test listing workflow execution history"""
        response = mobile_api_client.get(
            "/api/v1/workflows/executions",
            headers=mobile_auth_headers
        )

        # Verify response
        if response.status_code == 200:
            data = response.json()

            # Verify history structure
            assert "executions" in data or "items" in data or isinstance(data, list)

            # If executions present, verify fields
            if isinstance(data, dict) and "executions" in data:
                executions = data["executions"]
                if executions:
                    # Check first execution has required fields
                    execution = executions[0]
                    assert "status" in execution or "workflow_id" in execution
        elif response.status_code == 404:
            pytest.skip("Workflow executions history endpoint not implemented")

    def test_mobile_workflow_execution_history_fields(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test execution history has required fields"""
        response = mobile_api_client.get(
            "/api/v1/workflows/executions?limit=5",
            headers=mobile_auth_headers
        )

        if response.status_code == 200:
            data = response.json()
            executions = data.get("executions", data.get("items", data))

            if isinstance(executions, list) and executions:
                execution = executions[0]

                # Verify expected fields present
                expected_fields = ["status", "workflow_id"]
                for field in expected_fields:
                    if field in execution:
                        assert execution[field] is not None

                # Verify timestamp fields if present
                if "started_at" in execution or "start_time" in execution:
                    timestamp = execution.get("started_at") or execution.get("start_time")
                    assert timestamp is not None

                if "completed_at" in execution or "end_time" in execution:
                    timestamp = execution.get("completed_at") or execution.get("end_time")
                    # completed_at may be None for running executions
                    assert timestamp is None or timestamp is not None
        elif response.status_code == 404:
            pytest.skip("Workflow executions history endpoint not implemented")

    def test_mobile_workflow_execution_history_pagination(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test execution history pagination"""
        response = mobile_api_client.get(
            "/api/v1/workflows/executions?limit=10&offset=0",
            headers=mobile_auth_headers
        )

        if response.status_code == 200:
            data = response.json()
            executions = data.get("executions", data.get("items", []))

            if isinstance(executions, list):
                # Verify limit respected
                assert len(executions) <= 10
        elif response.status_code == 404:
            pytest.skip("Workflow executions history endpoint not implemented")


class TestMobileWorkflowList:
    """Test mobile workflow listing"""

    def test_mobile_workflow_list(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test listing available workflows"""
        response = mobile_api_client.get(
            "/api/v1/workflows",
            headers=mobile_auth_headers
        )

        # Verify response
        if response.status_code == 200:
            data = response.json()

            # Verify workflows structure
            assert "workflows" in data or isinstance(data, list)

            # Get workflows list
            workflows = data.get("workflows", data)

            if isinstance(workflows, list) and workflows:
                # Verify workflow structure
                workflow = workflows[0]
                assert "id" in workflow or "workflow_id" in workflow
                assert "name" in workflow
        elif response.status_code == 404:
            pytest.skip("Workflow list endpoint not implemented")

    def test_mobile_workflow_list_filtering(self, mobile_api_client: TestClient, mobile_auth_headers: dict):
        """Test workflow list filtering"""
        # Try filtering by category
        response = mobile_api_client.get(
            "/api/v1/workflows?category=testing",
            headers=mobile_auth_headers
        )

        if response.status_code == 200:
            data = response.json()
            workflows = data.get("workflows", data)

            if isinstance(workflows, list):
                # If category filtering works, verify results
                # (This is a basic test - actual filtering depends on implementation)
                assert True  # Endpoint available
        elif response.status_code == 404:
            pytest.skip("Workflow list endpoint not implemented")