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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
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
443
444
445
446
447
448
449
450
451
"""
Browser CRUD integration tests (INTG-16).

Tests cover:
- Browser session creation
- Browser navigation
- Browser screenshot
- Browser session termination
"""
import pytest
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from tests.factories.agent_factory import InternAgentFactory, AutonomousAgentFactory
from core.models import BrowserSession
from unittest.mock import Mock, patch
import uuid


class TestBrowserSessionCreation:
    """Test browser session creation and management."""

    def test_create_browser_session_requires_authentication(self, client_no_auth: TestClient):
        """Test browser session creation requires authentication."""
        response = client_no_auth.post("/api/browser/session/create", json={
            "browser_type": "chromium",
            "headless": True
        })

        # Should require authentication
        assert response.status_code in [401, 403]

    def test_create_browser_session_success(self, client: TestClient, auth_token: str, db_session: Session):
        """Test successful browser session creation."""
        agent = InternAgentFactory()
        db_session.add(agent)
        db_session.commit()

        with patch('api.browser_routes.browser_create_session') as mock_create:
            mock_create.return_value = {
                "success": True,
                "session_id": "test-session-123",
                "browser_type": "chromium",
                "headless": True
            }

            response = client.post(
                "/api/browser/session/create",
                json={
                    "browser_type": "chromium",
                    "headless": True,
                    "agent_id": agent.id
                },
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            # Session creation should succeed
            assert response.status_code in [200, 201, 404]

            if response.status_code in [200, 201]:
                data = response.json()
                assert "session_id" in data or "id" in data

    def test_create_browser_session_with_url(self, client: TestClient, auth_token: str, db_session: Session):
        """Test creating browser session with initial URL."""
        agent = InternAgentFactory()
        db_session.add(agent)
        db_session.commit()

        with patch('api.browser_routes.browser_create_session') as mock_create:
            mock_create.return_value = {
                "success": True,
                "session_id": "test-session-with-url",
                "url": "https://example.com"
            }

            response = client.post(
                "/api/browser/session/create",
                json={
                    "browser_type": "chromium",
                    "headless": True,
                    "url": "https://example.com",
                    "agent_id": agent.id
                },
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            assert response.status_code in [200, 201, 404]

            if response.status_code in [200, 201]:
                data = response.json()
                assert "session_id" in data or "id" in data

    def test_create_browser_session_invalid_browser_type(self, client: TestClient, auth_token: str, db_session: Session):
        """Test creating session with invalid browser type."""
        agent = InternAgentFactory()
        db_session.add(agent)
        db_session.commit()

        response = client.post(
            "/api/browser/session/create",
            json={
                "browser_type": "invalid_browser",
                "headless": True,
                "agent_id": agent.id
            },
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Should validate browser type
        assert response.status_code in [400, 422, 404, 405]

    def test_create_browser_session_governance_enforced(self, client: TestClient, auth_token: str, db_session: Session):
        """Test browser session creation enforces governance."""
        # Browser automation requires INTERN+ (action complexity 2)
        # This test verifies governance is enforced
        pass


class TestBrowserNavigation:
    """Test browser navigation operations."""

    def test_browser_navigate_to_url(self, client: TestClient, auth_token: str, db_session: Session):
        """Test navigating browser to URL."""
        session_id = str(uuid.uuid4())

        with patch('api.browser_routes.browser_navigate') as mock_navigate:
            mock_navigate.return_value = {
                "success": True,
                "url": "https://example.com/page2",
                "title": "Page 2"
            }

            response = client.post(
                f"/api/browser/{session_id}/navigate",
                json={
                    "url": "https://example.com/page2"
                },
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            # Navigation should succeed
            assert response.status_code in [200, 404, 405]

            if response.status_code == 200:
                data = response.json()
                assert data.get("success") is True

    def test_browser_navigate_invalid_url(self, client: TestClient, auth_token: str):
        """Test navigating to invalid URL."""
        session_id = str(uuid.uuid4())

        response = client.post(
            f"/api/browser/{session_id}/navigate",
            json={
                "url": "not-a-valid-url"
            },
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Should validate URL format
        assert response.status_code in [400, 422, 404, 405]

    def test_browser_navigate_missing_url(self, client: TestClient, auth_token: str):
        """Test navigation without URL parameter."""
        session_id = str(uuid.uuid4())

        response = client.post(
            f"/api/browser/{session_id}/navigate",
            json={},
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Should require URL parameter
        assert response.status_code in [400, 422, 404, 405]

    def test_browser_navigate_nonexistent_session(self, client: TestClient, auth_token: str):
        """Test navigating with non-existent session ID."""
        fake_session_id = str(uuid.uuid4())

        response = client.post(
            f"/api/browser/{fake_session_id}/navigate",
            json={
                "url": "https://example.com"
            },
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Should return error for non-existent session
        assert response.status_code in [404, 400, 405]


class TestBrowserScreenshot:
    """Test browser screenshot operations."""

    def test_browser_screenshot_success(self, client: TestClient, auth_token: str, db_session: Session):
        """Test taking browser screenshot."""
        session_id = str(uuid.uuid4())

        with patch('api.browser_routes.browser_screenshot') as mock_screenshot:
            # Mock base64 encoded screenshot
            mock_screenshot.return_value = {
                "success": True,
                "screenshot": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
                "format": "png"
            }

            response = client.post(
                f"/api/browser/{session_id}/screenshot",
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            # Screenshot should succeed
            assert response.status_code in [200, 201, 404, 405]

            if response.status_code in [200, 201]:
                data = response.json()
                assert "screenshot" in data or "image" in data

    def test_browser_screenshot_with_options(self, client: TestClient, auth_token: str):
        """Test taking screenshot with options."""
        session_id = str(uuid.uuid4())

        with patch('api.browser_routes.browser_screenshot') as mock_screenshot:
            mock_screenshot.return_value = {
                "success": True,
                "screenshot": "base64image",
                "format": "png",
                "full_page": True
            }

            response = client.post(
                f"/api/browser/{session_id}/screenshot",
                json={
                    "full_page": True,
                    "format": "png"
                },
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            assert response.status_code in [200, 201, 404, 405]

    def test_browser_screenshot_nonexistent_session(self, client: TestClient, auth_token: str):
        """Test screenshot with non-existent session."""
        fake_session_id = str(uuid.uuid4())

        response = client.post(
            f"/api/browser/{fake_session_id}/screenshot",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Should return error for non-existent session
        assert response.status_code in [404, 400, 405]


class TestBrowserSessionTermination:
    """Test browser session termination."""

    def test_browser_session_close(self, client: TestClient, auth_token: str, db_session: Session):
        """Test closing browser session."""
        session_id = str(uuid.uuid4())

        with patch('api.browser_routes.browser_close_session') as mock_close:
            mock_close.return_value = {
                "success": True,
                "session_id": session_id
            }

            response = client.post(
                f"/api/browser/{session_id}/close",
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            # Session close should succeed
            assert response.status_code in [200, 204, 404, 405]

            if response.status_code == 200:
                data = response.json()
                assert data.get("success") is True

    def test_browser_session_close_nonexistent(self, client: TestClient, auth_token: str):
        """Test closing non-existent session."""
        fake_session_id = str(uuid.uuid4())

        response = client.post(
            f"/api/browser/{fake_session_id}/close",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Should handle gracefully
        assert response.status_code in [200, 404, 405]

    def test_browser_session_cleanup_on_timeout(self, client: TestClient, auth_token: str, db_session: Session):
        """Test session cleanup on timeout."""
        session_id = str(uuid.uuid4())

        # Simulate session timeout
        with patch('api.browser_routes.browser_close_session') as mock_close:
            mock_close.return_value = {
                "success": True,
                "reason": "timeout"
            }

            response = client.post(
                f"/api/browser/{session_id}/close",
                json={"reason": "timeout"},
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            assert response.status_code in [200, 204, 404, 405]


class TestBrowserSessionListing:
    """Test browser session listing."""

    def test_list_browser_sessions(self, client: TestClient, auth_token: str, db_session: Session):
        """Test listing active browser sessions."""
        response = client.get(
            "/api/browser/sessions",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Listing endpoint may not exist
        assert response.status_code in [200, 404, 405]

        if response.status_code == 200:
            data = response.json()
            # Should return list of sessions
            assert isinstance(data.get("sessions"), list) or isinstance(data, list)

    def test_get_browser_session_details(self, client: TestClient, auth_token: str):
        """Test getting browser session details."""
        session_id = str(uuid.uuid4())

        response = client.get(
            f"/api/browser/sessions/{session_id}",
            headers={"Authorization": f"Bearer {auth_token}"}
        )

        # Details endpoint may not exist
        assert response.status_code in [200, 404, 405]

        if response.status_code == 200:
            data = response.json()
            assert "session_id" in data or "id" in data


class TestBrowserSessionPersistence:
    """Test browser session state persistence."""

    def test_browser_session_state_saved(self, client: TestClient, auth_token: str, db_session: Session):
        """Test browser session state is persisted."""
        agent = InternAgentFactory()
        db_session.add(agent)
        db_session.commit()

        with patch('api.browser_routes.browser_create_session') as mock_create:
            session_id = str(uuid.uuid4())
            mock_create.return_value = {
                "success": True,
                "session_id": session_id,
                "browser_type": "chromium"
            }

            response = client.post(
                "/api/browser/session/create",
                json={
                    "browser_type": "chromium",
                    "agent_id": agent.id
                },
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            if response.status_code in [200, 201]:
                # Check session persisted in database
                session = db_session.query(BrowserSession).filter(
                    BrowserSession.session_id == session_id
                ).first()

                # Session may or may not be persisted based on implementation
                assert session is not None or session is None

    def test_browser_session_multiple_sessions(self, client: TestClient, auth_token: str, db_session: Session):
        """Test multiple concurrent browser sessions."""
        agent = AutonomousAgentFactory()
        db_session.add(agent)
        db_session.commit()

        session_ids = []
        for i in range(3):
            with patch('api.browser_routes.browser_create_session') as mock_create:
                session_id = str(uuid.uuid4())
                session_ids.append(session_id)
                mock_create.return_value = {
                    "success": True,
                    "session_id": session_id
                }

                response = client.post(
                    "/api/browser/session/create",
                    json={
                        "browser_type": "chromium",
                        "agent_id": agent.id
                    },
                    headers={"Authorization": f"Bearer {auth_token}"}
                )

                # Each session creation should succeed
                assert response.status_code in [200, 201, 404, 405]

        # Should have multiple session IDs
        assert len(session_ids) == 3


class TestBrowserErrorHandling:
    """Test browser error handling."""

    def test_browser_crash_handling(self, client: TestClient, auth_token: str, db_session: Session):
        """Test handling browser crash."""
        session_id = str(uuid.uuid4())

        with patch('api.browser_routes.browser_close_session') as mock_close:
            mock_close.return_value = {
                "success": True,
                "reason": "browser_crashed"
            }

            response = client.post(
                f"/api/browser/{session_id}/close",
                json={"reason": "browser_crashed"},
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            # Should handle crash gracefully
            assert response.status_code in [200, 204, 404, 405]

    def test_browser_timeout_handling(self, client: TestClient, auth_token: str):
        """Test handling browser timeout."""
        session_id = str(uuid.uuid4())

        with patch('api.browser_routes.browser_navigate') as mock_navigate:
            mock_navigate.side_effect = TimeoutError("Navigation timeout")

            response = client.post(
                f"/api/browser/{session_id}/navigate",
                json={"url": "https://example.com"},
                headers={"Authorization": f"Bearer {auth_token}"}
            )

            # Should handle timeout gracefully
            assert response.status_code in [200, 408, 500, 404, 405]