File size: 23,166 Bytes
e3e5444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
"""
Unit tests for the DataAnalyst Agent project.
Covers: security utils, rate limiting, cleanup, profiler, models, and API endpoints.
"""
import os
import sys
import time
import json
import uuid
import tempfile
import pytest
import pandas as pd
from unittest.mock import patch, MagicMock

# Force tests to use a temporary file-based database so that connections across the test client share the data
_test_db_fd, _test_db_path = tempfile.mkstemp(suffix=".db")
os.environ["DATABASE_URL"] = f"sqlite:///{_test_db_path}"

from datetime import datetime, timezone

# ============================================================================
# SECURITY UTILS TESTS
# ============================================================================


class TestRedactSensitiveData:
    """Tests for app.utils.security.redact_sensitive_data"""

    def test_redacts_api_key(self):
        from app.utils.security import redact_sensitive_data
        text = "api_key=sk-abc123secret"
        result = redact_sensitive_data(text)
        assert "sk-abc123secret" not in result
        assert "[REDACTED]" in result

    def test_redacts_bearer_token(self):
        from app.utils.security import redact_sensitive_data
        text = "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.longtoken"
        result = redact_sensitive_data(text)
        assert "eyJhbGciOiJIUzI1NiJ9" not in result

    def test_redacts_database_url(self):
        from app.utils.security import redact_sensitive_data
        text = "connecting to postgres://user:pass@host:5432/db"
        result = redact_sensitive_data(text)
        assert "user:pass" not in result
        assert "[DATABASE_URL_REDACTED]" in result

    def test_redacts_password_field(self):
        from app.utils.security import redact_sensitive_data
        text = "password: mysecretpassword123"
        result = redact_sensitive_data(text)
        assert "mysecretpassword123" not in result

    def test_preserves_normal_text(self):
        from app.utils.security import redact_sensitive_data
        text = "This is a normal log message with no secrets."
        result = redact_sensitive_data(text)
        assert result == text


class TestValidateTableName:
    """Tests for app.utils.security.validate_table_name"""

    def test_valid_simple_name(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("users") is True

    def test_valid_with_underscore(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("user_profiles") is True

    def test_valid_starts_with_underscore(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("_temp_table") is True

    def test_invalid_starts_with_number(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("123table") is False

    def test_invalid_contains_dash(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("user-table") is False

    def test_invalid_contains_space(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("user table") is False

    def test_invalid_empty_string(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("") is False

    def test_invalid_too_long(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("a" * 64) is False

    def test_valid_at_max_length(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("a" * 63) is True

    def test_invalid_sql_injection_attempt(self):
        from app.utils.security import validate_table_name
        assert validate_table_name("users; DROP TABLE users;--") is False


class TestSanitizeHtml:
    """Tests for app.utils.security.sanitize_html"""

    def test_allows_safe_tags(self):
        from app.utils.security import sanitize_html
        html = "<p>Hello <strong>world</strong></p>"
        result = sanitize_html(html)
        assert "<p>" in result
        assert "<strong>" in result

    def test_removes_script_tags(self):
        from app.utils.security import sanitize_html
        html = "<p>Hello</p><script>alert('xss')</script>"
        result = sanitize_html(html)
        assert "<script>" not in result
        assert "alert" not in result

    def test_removes_event_handlers(self):
        from app.utils.security import sanitize_html
        html = '<div onmouseover="alert(1)">hover</div>'
        result = sanitize_html(html)
        assert "onmouseover" not in result


class TestSanitizeMarkdownOutput:
    """Tests for app.utils.security.sanitize_markdown_output"""

    def test_strips_script_tags(self):
        from app.utils.security import sanitize_markdown_output
        md = "# Title\n<script>alert('xss')</script>\nHello"
        result = sanitize_markdown_output(md)
        assert "<script>" not in result
        assert "# Title" in result

    def test_strips_event_handlers(self):
        from app.utils.security import sanitize_markdown_output
        md = "Click <div onclick=alert(1)>here</div>"
        result = sanitize_markdown_output(md)
        assert "onclick" not in result

    def test_preserves_normal_markdown(self):
        from app.utils.security import sanitize_markdown_output
        md = "# Title\n\n- Item 1\n- Item 2\n\n**Bold text**"
        result = sanitize_markdown_output(md)
        assert result == md


class TestValidateFileSize:
    """Tests for app.utils.security.validate_file_size"""

    def test_valid_file_size(self):
        from app.utils.security import validate_file_size
        valid, msg = validate_file_size(1024, 1048576)
        assert valid is True
        assert msg == ""

    def test_zero_size_file(self):
        from app.utils.security import validate_file_size
        valid, msg = validate_file_size(0, 1048576)
        assert valid is False
        assert "greater than 0" in msg

    def test_over_max_size(self):
        from app.utils.security import validate_file_size
        valid, msg = validate_file_size(2000000, 1048576)
        assert valid is False
        assert "too large" in msg.lower()


# ============================================================================
# RATE LIMIT TESTS
# ============================================================================


class TestRateLimit:
    """Tests for app.utils.rate_limit"""

    def setup_method(self):
        """Reset rate limit stores before each test."""
        from app.utils.rate_limit import _rate_limit_store, _concurrent_jobs
        _rate_limit_store.clear()
        _concurrent_jobs.clear()

    def test_allows_under_limit(self):
        from app.utils.rate_limit import check_rate_limit
        allowed, msg = check_rate_limit("test_client", limit=5)
        assert allowed is True
        assert msg == ""

    def test_blocks_over_limit(self):
        from app.utils.rate_limit import check_rate_limit
        for _ in range(5):
            check_rate_limit("test_client", limit=5)
        allowed, msg = check_rate_limit("test_client", limit=5)
        assert allowed is False
        assert "Rate limit exceeded" in msg

    def test_different_clients_independent(self):
        from app.utils.rate_limit import check_rate_limit
        for _ in range(5):
            check_rate_limit("client_a", limit=5)
        # client_a is blocked
        allowed_a, _ = check_rate_limit("client_a", limit=5)
        assert allowed_a is False
        # client_b is still free
        allowed_b, _ = check_rate_limit("client_b", limit=5)
        assert allowed_b is True

    def test_concurrent_job_tracking(self):
        from app.utils.rate_limit import (
            can_start_analysis, increment_concurrent_job,
            decrement_concurrent_job
        )
        # Under limit
        allowed, _ = can_start_analysis("client_x")
        assert allowed is True

        # Fill up slots
        for _ in range(5):
            increment_concurrent_job("client_x")
        allowed, msg = can_start_analysis("client_x")
        assert allowed is False
        assert "Too many" in msg

        # Free one slot
        decrement_concurrent_job("client_x")
        allowed, _ = can_start_analysis("client_x")
        assert allowed is True

    def test_decrement_never_goes_negative(self):
        from app.utils.rate_limit import (
            _concurrent_jobs, decrement_concurrent_job
        )
        decrement_concurrent_job("nonexistent_client")
        assert _concurrent_jobs["nonexistent_client"] == 0

    def test_extract_client_id_forwarded_trusted(self):
        from app.utils.rate_limit import extract_client_id
        import os
        os.environ["TRUST_FORWARDED_IP"] = "true"
        mock_request = MagicMock()
        mock_request.headers.get.return_value = "1.2.3.4, 5.6.7.8"
        result = extract_client_id(mock_request)
        assert result == "1.2.3.4"
        
    def test_extract_client_id_forwarded_untrusted(self):
        from app.utils.rate_limit import extract_client_id
        import os
        os.environ["TRUST_FORWARDED_IP"] = "false"
        mock_request = MagicMock()
        mock_request.headers.get.return_value = "1.2.3.4, 5.6.7.8"
        mock_request.client.host = "10.0.0.1"
        result = extract_client_id(mock_request)
        assert result == "10.0.0.1"

    def test_extract_client_id_direct(self):
        from app.utils.rate_limit import extract_client_id
        mock_request = MagicMock()
        mock_request.headers.get.return_value = None
        mock_request.client.host = "127.0.0.1"
        result = extract_client_id(mock_request)
        assert result == "127.0.0.1"


# ============================================================================
# PROFILER TESTS
# ============================================================================


class TestProfiler:
    """Tests for app.services.profiler.profile_dataframe"""

    def test_basic_numeric_profiling(self):
        from app.services.profiler import profile_dataframe
        df = pd.DataFrame({"age": [25, 30, 35], "salary": [50000, 60000, 70000]})
        profile = profile_dataframe(df)

        assert profile["num_rows"] == 3
        assert profile["num_cols"] == 2
        assert "age" in profile["numeric_columns"]
        assert "salary" in profile["numeric_columns"]
        assert profile["columns"]["age"]["mean"] == 30.0

    def test_categorical_profiling(self):
        from app.services.profiler import profile_dataframe
        df = pd.DataFrame({"color": ["red", "blue", "red", "green"]})
        profile = profile_dataframe(df)

        assert "color" in profile["categorical_columns"]
        assert "top_values" in profile["columns"]["color"]

    def test_datetime_detection(self):
        from app.services.profiler import profile_dataframe
        df = pd.DataFrame({
            "date": pd.to_datetime(["2024-01-01", "2024-06-15", "2024-12-31"])
        })
        profile = profile_dataframe(df)
        assert "date" in profile["datetime_columns"]

    def test_datetime_string_detection(self):
        from app.services.profiler import profile_dataframe
        df = pd.DataFrame({
            "date_str": ["2024-01-01", "2024-06-15", "2024-12-31"]
        })
        profile = profile_dataframe(df)
        # Should detect as datetime_like since >80% parse
        assert "date_str" in profile["datetime_columns"]

    def test_missing_values_counted(self):
        from app.services.profiler import profile_dataframe
        df = pd.DataFrame({"val": [1, None, 3, None, 5]})
        profile = profile_dataframe(df)
        assert profile["columns"]["val"]["num_missing"] == 2

    def test_empty_dataframe(self):
        from app.services.profiler import profile_dataframe
        df = pd.DataFrame({"a": pd.Series(dtype="float64")})
        profile = profile_dataframe(df)
        assert profile["num_rows"] == 0

    def test_mixed_types(self):
        from app.services.profiler import profile_dataframe
        df = pd.DataFrame({
            "id": [1, 2, 3],
            "name": ["Alice", "Bob", "Charlie"],
            "score": [90.5, 85.0, 92.3]
        })
        profile = profile_dataframe(df)
        assert "id" in profile["numeric_columns"]
        assert "name" in profile["categorical_columns"]
        assert "score" in profile["numeric_columns"]


# ============================================================================
# CLEANUP TESTS
# ============================================================================


class TestCleanup:
    """Tests for app.utils.cleanup"""

    def test_cleanup_old_files(self):
        from app.utils.cleanup import cleanup_old_files
        with tempfile.TemporaryDirectory() as tmpdir:
            # Create a file and backdate it
            filepath = os.path.join(tmpdir, "old_file.csv")
            with open(filepath, "w") as f:
                f.write("test data")
            # Set mtime to 100 hours ago
            old_time = time.time() - (100 * 3600)
            os.utime(filepath, (old_time, old_time))

            deleted, freed = cleanup_old_files(tmpdir, cutoff_hours=72)
            assert deleted == 1
            assert freed > 0
            assert not os.path.exists(filepath)

    def test_cleanup_preserves_recent_files(self):
        from app.utils.cleanup import cleanup_old_files
        with tempfile.TemporaryDirectory() as tmpdir:
            filepath = os.path.join(tmpdir, "recent.csv")
            with open(filepath, "w") as f:
                f.write("recent data")

            deleted, freed = cleanup_old_files(tmpdir, cutoff_hours=72)
            assert deleted == 0
            assert os.path.exists(filepath)

    def test_cleanup_dry_run(self):
        from app.utils.cleanup import cleanup_old_files
        with tempfile.TemporaryDirectory() as tmpdir:
            filepath = os.path.join(tmpdir, "old_file.csv")
            with open(filepath, "w") as f:
                f.write("test data")
            old_time = time.time() - (100 * 3600)
            os.utime(filepath, (old_time, old_time))

            deleted, freed = cleanup_old_files(tmpdir, cutoff_hours=72, dry_run=True)
            assert deleted == 1
            assert os.path.exists(filepath)  # File NOT deleted in dry run

    def test_cleanup_nonexistent_directory(self):
        from app.utils.cleanup import cleanup_old_files
        deleted, freed = cleanup_old_files("/nonexistent/path", cutoff_hours=72)
        assert deleted == 0
        assert freed == 0

    def test_get_directory_size(self):
        from app.utils.cleanup import get_directory_size
        with tempfile.TemporaryDirectory() as tmpdir:
            filepath = os.path.join(tmpdir, "data.txt")
            with open(filepath, "w") as f:
                f.write("A" * 1000)
            size = get_directory_size(tmpdir)
            assert size >= 1000


# ============================================================================
# LLM UTILS TESTS
# ============================================================================


class TestLLMUtils:
    """Tests for app.utils.llm_utils"""

    def test_estimate_tokens(self):
        from app.utils.llm_utils import estimate_prompt_tokens
        assert estimate_prompt_tokens("1234") == 1
        assert estimate_prompt_tokens("12345678") == 2

    def test_enforce_token_budget_under(self):
        from app.utils.llm_utils import enforce_token_budget
        text = "Short text"
        result = enforce_token_budget(text, max_tokens=2000)
        assert result == text

    def test_enforce_token_budget_over(self):
        from app.utils.llm_utils import enforce_token_budget
        text = "A" * 10000  # ~2500 tokens
        result = enforce_token_budget(text, max_tokens=500)
        assert len(result) == 2000  # 500 * 4


# ============================================================================
# DATABASE MODELS TESTS
# ============================================================================


class TestModels:
    """Tests for app.db.models"""

    def test_analysis_job_defaults(self):
        from app.db.models import AnalysisJob
        # SQLAlchemy defaults apply on flush, so we test the declarative column defaults
        assert AnalysisJob.status.default.arg == "uploaded"
        assert AnalysisJob.progress_logs.default.arg == ""
        assert AnalysisJob.error_message.default.arg == ""

    def test_analysis_memory_fields(self):
        from app.db.models import AnalysisMemory
        mem = AnalysisMemory(
            job_id="test-123",
            schema_fingerprint="abc123",
            insights_summary="Key finding: sales up 20%"
        )
        assert mem.job_id == "test-123"
        assert mem.schema_fingerprint == "abc123"


# ============================================================================
# FASTAPI ENDPOINT TESTS
# ============================================================================


class TestAPIEndpoints:
    """Tests for FastAPI endpoints using TestClient."""

    @pytest.fixture(autouse=True)
    def setup_client(self):
        """Create test client and ensure upload directory exists."""
        from fastapi.testclient import TestClient
        from app.main import app
        from app.db.database import Base, engine
        # Ensure fresh tables in our in-memory DB
        Base.metadata.drop_all(bind=engine)
        Base.metadata.create_all(bind=engine)
        
        self.client = TestClient(app)
        os.makedirs("data/uploads", exist_ok=True)

    def test_health_endpoint(self):
        res = self.client.get("/health")
        assert res.status_code == 200
        data = res.json()
        assert data["status"] == "ok"
        assert "version" in data

    def test_upload_csv_success(self):
        csv_content = b"name,age,score\nAlice,30,95\nBob,25,88\nCharlie,35,92\n"
        res = self.client.post(
            "/upload_dataset",
            files={"file": ("test_data.csv", csv_content, "text/csv")}
        )
        assert res.status_code == 200
        data = res.json()
        assert "job_id" in data
        assert data["message"] == "File uploaded successfully"

    def test_upload_non_csv_rejected(self):
        res = self.client.post(
            "/upload_dataset",
            files={"file": ("test.txt", b"hello world", "text/plain")}
        )
        assert res.status_code == 400
        assert "CSV" in res.json()["detail"]

    def test_upload_empty_csv_rejected(self):
        csv_content = b""
        res = self.client.post(
            "/upload_dataset",
            files={"file": ("empty.csv", csv_content, "text/csv")}
        )
        assert res.status_code == 400

    def test_start_analysis_missing_job(self):
        res = self.client.post(
            "/start_analysis",
            json={"job_id": "nonexistent-id-12345"}
        )
        assert res.status_code == 404

    def test_analysis_status_missing_job(self):
        res = self.client.get("/analysis_status/nonexistent-id-12345")
        assert res.status_code == 404

    def test_download_report_missing_job(self):
        res = self.client.get("/download_report/nonexistent-id-12345?format=json")
        assert res.status_code == 404

    def test_job_cleaning_missing_job(self):
        res = self.client.get("/job_cleaning/nonexistent-id-12345")
        assert res.status_code == 404

    def test_job_plan_missing_job(self):
        res = self.client.get("/job_plan/nonexistent-id-12345")
        assert res.status_code == 404

    def test_sql_upload_invalid_table_name(self):
        res = self.client.post(
            "/upload_sql_table",
            json={
                "database_url": "sqlite:///test.db",
                "table_name": "123-invalid;DROP",
                "limit": 1000
            }
        )
        assert res.status_code == 400
        assert "Invalid table name" in res.json()["detail"]

    def test_sql_upload_invalid_limit(self):
        res = self.client.post(
            "/upload_sql_table",
            json={
                "database_url": "sqlite:///test.db",
                "table_name": "valid_table",
                "limit": 50000
            }
        )
        assert res.status_code == 400
        assert "Limit" in res.json()["detail"]

    def test_full_upload_and_status_flow(self):
        """Integration: upload CSV → check status → verify uploaded state."""
        csv_content = b"x,y\n1,2\n3,4\n5,6\n"
        upload_res = self.client.post(
            "/upload_dataset",
            files={"file": ("flow_test.csv", csv_content, "text/csv")}
        )
        assert upload_res.status_code == 200
        job_id = upload_res.json()["job_id"]

        status_res = self.client.get(f"/analysis_status/{job_id}")
        assert status_res.status_code == 200
        assert status_res.json()["status"] == "uploaded"


# ============================================================================
# AGENT NODES VALIDATION TESTS
# ============================================================================


class TestNodeValidation:
    """Tests for the plan validation logic in agent nodes."""

    def test_validate_plan_filters_invalid_operations(self):
        from app.agent.nodes import _validate_plan
        raw_plan = [
            {"task": "Check missing", "operation": "missing_values", "params": {}},
            {"task": "Hack system", "operation": "exec_shell", "params": {}},
        ]
        result = _validate_plan(raw_plan)
        assert len(result) == 1
        assert result[0]["operation"] == "missing_values"

    def test_validate_plan_fixes_bad_freq(self):
        from app.agent.nodes import _validate_plan
        raw_plan = [
            {
                "task": "Trend analysis",
                "operation": "time_series_trend",
                "params": {"date_col": "date", "value_col": "sales", "freq": "Monthly"}
            },
        ]
        result = _validate_plan(raw_plan)
        assert len(result) == 1
        assert result[0]["params"]["freq"] == "M"  # Auto-corrected

    def test_validate_plan_preserves_valid_freq(self):
        from app.agent.nodes import _validate_plan
        raw_plan = [
            {
                "task": "Trend analysis",
                "operation": "time_series_trend",
                "params": {"date_col": "date", "value_col": "sales", "freq": "W"}
            },
        ]
        result = _validate_plan(raw_plan)
        assert result[0]["params"]["freq"] == "W"  # Kept as-is

    def test_validate_plan_empty_input(self):
        from app.agent.nodes import _validate_plan
        result = _validate_plan([])
        assert result == []

    def test_validate_plan_skips_bad_params(self):
        from app.agent.nodes import _validate_plan
        raw_plan = [
            {"task": "Bad step", "operation": "describe_numeric", "params": "not_a_dict"},
        ]
        with pytest.raises(ValueError, match="No valid analysis steps"):
            _validate_plan(raw_plan)