File size: 14,024 Bytes
915746c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Server and Integration Tests
============================
Tests for server module, retrieval helpers, and integration scenarios.
"""

import pytest
from unittest.mock import patch, MagicMock
from datetime import datetime
import tempfile
from pathlib import Path
import json
import os


# ============================================================================
# SERVER MODULE TESTS
# ============================================================================

class TestServerModule:
    """Tests for eurus.server module."""
    
    def test_server_class_exists(self):
        """Test Server class can be imported."""
        from eurus.server import Server
        assert Server is not None
        
    def test_server_instance_exists(self):
        """Test server instance can be imported."""
        from eurus.server import server
        assert server is not None


# ============================================================================
# RETRIEVAL HELPERS TESTS
# ============================================================================

class TestRetrievalHelpers:
    """Tests for retrieval helper functions."""
    
    def test_format_coord_positive(self):
        """Test coordinate formatting for positive values."""
        from eurus.retrieval import _format_coord
        assert _format_coord(25.5) == "25.50"
        
    def test_format_coord_negative(self):
        """Test coordinate formatting for negative values."""
        from eurus.retrieval import _format_coord
        assert _format_coord(-10.333) == "-10.33"
        
    def test_format_coord_zero(self):
        """Test coordinate formatting for near-zero values."""
        from eurus.retrieval import _format_coord
        # Values very close to zero should be formatted as 0.00
        result = _format_coord(0.001)
        assert "0.00" in result or "0.01" in result
        
    def test_format_file_size_bytes(self):
        """Test file size formatting for bytes."""
        from eurus.retrieval import format_file_size
        assert "B" in format_file_size(500)
        
    def test_format_file_size_kb(self):
        """Test file size formatting for kilobytes."""
        from eurus.retrieval import format_file_size
        assert "KB" in format_file_size(2048)
        
    def test_format_file_size_mb(self):
        """Test file size formatting for megabytes."""
        from eurus.retrieval import format_file_size
        assert "MB" in format_file_size(5 * 1024 * 1024)
        
    def test_format_file_size_gb(self):
        """Test file size formatting for gigabytes."""
        from eurus.retrieval import format_file_size
        assert "GB" in format_file_size(5 * 1024 * 1024 * 1024)

    def test_ensure_aws_region_sets_env_from_repo_metadata(self, monkeypatch):
        """Auto-populate AWS vars when metadata includes region_name."""
        import eurus.retrieval as _retrieval
        from eurus.retrieval import _ensure_aws_region

        # Reset one-shot flag so the function actually runs
        _retrieval._aws_region_set = False

        for key in ("AWS_REGION", "AWS_DEFAULT_REGION", "AWS_ENDPOINT_URL", "AWS_S3_ENDPOINT"):
            monkeypatch.delenv(key, raising=False)

        response = MagicMock()
        response.read.return_value = json.dumps(
            {"bucket": {"extra_config": {"region_name": "eu-north-1"}}}
        ).encode("utf-8")
        context_manager = MagicMock()
        context_manager.__enter__.return_value = response

        with patch("eurus.retrieval.urlopen", return_value=context_manager) as mock_urlopen:
            _ensure_aws_region("token", "earthmover-public/era5-surface-aws")

        assert os.environ["AWS_REGION"] == "eu-north-1"
        assert os.environ["AWS_DEFAULT_REGION"] == "eu-north-1"
        assert os.environ["AWS_ENDPOINT_URL"] == "https://s3.eu-north-1.amazonaws.com"
        assert os.environ["AWS_S3_ENDPOINT"] == "https://s3.eu-north-1.amazonaws.com"

        req = mock_urlopen.call_args.args[0]
        assert req.full_url == "https://api.earthmover.io/repos/earthmover-public/era5-surface-aws"

    def test_ensure_aws_region_does_not_override_existing_env(self, monkeypatch):
        """Keep explicit user-provided AWS endpoint config untouched."""
        import eurus.retrieval as _retrieval
        from eurus.retrieval import _ensure_aws_region

        # Reset one-shot flag so the function actually runs
        _retrieval._aws_region_set = False

        monkeypatch.setenv("AWS_REGION", "custom-region")
        monkeypatch.setenv("AWS_DEFAULT_REGION", "custom-default")
        monkeypatch.setenv("AWS_ENDPOINT_URL", "https://custom.endpoint")
        monkeypatch.setenv("AWS_S3_ENDPOINT", "https://custom.s3.endpoint")

        response = MagicMock()
        response.read.return_value = json.dumps(
            {"bucket": {"extra_config": {"region_name": "us-west-2"}}}
        ).encode("utf-8")
        context_manager = MagicMock()
        context_manager.__enter__.return_value = response

        with patch("eurus.retrieval.urlopen", return_value=context_manager):
            _ensure_aws_region("token")

        assert os.environ["AWS_REGION"] == "custom-region"
        assert os.environ["AWS_DEFAULT_REGION"] == "custom-default"
        assert os.environ["AWS_ENDPOINT_URL"] == "https://custom.endpoint"
        assert os.environ["AWS_S3_ENDPOINT"] == "https://custom.s3.endpoint"




# ============================================================================
# ANALYSIS GUIDE TESTS
# ============================================================================

class TestAnalysisGuide:
    """Tests for analysis guide module."""
    
    def test_analysis_guide_tool_exists(self):
        """Test analysis guide tool can be imported."""
        from eurus.tools.analysis_guide import analysis_guide_tool
        assert analysis_guide_tool is not None
        
    def test_analysis_guide_returns_content(self):
        """Test analysis guide returns useful content."""
        from eurus.tools.analysis_guide import get_analysis_guide
        result = get_analysis_guide("timeseries")
        assert len(result) > 100  # Should have substantial content


# ============================================================================
# ERA5 TOOL EXTENDED TESTS
# ============================================================================

class TestERA5ToolValidation:
    """Tests for ERA5 tool validation and edge cases."""
    
    def test_era5_args_date_validation(self):
        """Test date format validation works."""
        from eurus.tools.era5 import ERA5RetrievalArgs
        # Valid dates should work
        args = ERA5RetrievalArgs(
            variable_id="sst",
            start_date="2023-01-01",
            end_date="2023-12-31",
            min_latitude=20.0,
            max_latitude=30.0,
            min_longitude=260.0,
            max_longitude=280.0
        )
        assert args.start_date == "2023-01-01"
        
    def test_era5_args_latitude_range(self):
        """Test latitude range parameters."""
        from eurus.tools.era5 import ERA5RetrievalArgs
        args = ERA5RetrievalArgs(
            variable_id="t2",
            start_date="2023-01-01",
            end_date="2023-01-31",
            min_latitude=-90.0,
            max_latitude=90.0,
            min_longitude=0.0,
            max_longitude=360.0
        )
        assert args.min_latitude == -90.0
        assert args.max_latitude == 90.0
        
    def test_era5_args_query_type_field(self):
        """Test that ERA5 args handles optional query_type correctly."""
        from eurus.tools.era5 import ERA5RetrievalArgs
        args = ERA5RetrievalArgs(
            variable_id="sst",
            start_date="2023-01-01",
            end_date="2023-12-31",
            min_latitude=20.0,
            max_latitude=30.0,
            min_longitude=260.0,
            max_longitude=280.0
        )
        # Just verify args created successfully
        assert args.variable_id == "sst"


# ============================================================================
# CONFIG EXTENDED TESTS
# ============================================================================

class TestConfigRegions:
    """Tests for region configuration."""
    
    def test_get_region_valid(self):
        """Test getting valid predefined region."""
        from eurus.config import get_region
        region = get_region("gulf_of_mexico")
        assert region is not None
        assert hasattr(region, 'min_lat')
        assert hasattr(region, 'max_lat')
        
    def test_get_region_case_insensitive(self):
        """Test region lookup is case insensitive."""
        from eurus.config import get_region
        region = get_region("GULF_OF_MEXICO")
        assert region is not None
        
    def test_list_regions_output(self):
        """Test list_regions returns formatted string."""
        from eurus.config import list_regions
        output = list_regions()
        assert "gulf" in output.lower() or "region" in output.lower()


# ============================================================================
# MEMORY MODULE INTEGRATION
# ============================================================================

class TestMemoryIntegration:
    """Integration tests for memory management."""
    
    def test_memory_manager_create(self):
        """Test MemoryManager can be created."""
        from eurus.memory import MemoryManager, reset_memory
        reset_memory()
        mm = MemoryManager()
        assert mm is not None
        
    def test_memory_add_conversation(self):
        """Test adding to conversation history."""
        from eurus.memory import MemoryManager, reset_memory
        reset_memory()
        mm = MemoryManager()
        mm.add_message("user", "Hello")
        history = mm.get_conversation_history()
        assert len(history) >= 1
        
    def test_memory_dataset_registration(self):
        """Test dataset registration."""
        from eurus.memory import MemoryManager, reset_memory
        reset_memory()
        mm = MemoryManager()
        mm.register_dataset(
            path="/tmp/test.zarr",
            variable="sst",
            query_type="temporal",
            start_date="2023-01-01",
            end_date="2023-12-31",
            lat_bounds=(20.0, 30.0),
            lon_bounds=(260.0, 280.0),
            file_size_bytes=1024
        )
        datasets = mm.list_datasets()
        assert len(datasets) >= 1


# ============================================================================
# ROUTING TOOL EXTENDED TESTS
# ============================================================================

class TestRoutingTool:
    """Extended tests for routing functionality."""
    
    def test_routing_tool_exists(self):
        """Test routing tool can be imported."""
        from eurus.tools.routing import routing_tool
        assert routing_tool is not None
        assert routing_tool.name == "calculate_maritime_route"
        
    def test_has_routing_deps_flag(self):
        """Test HAS_ROUTING_DEPS flag exists."""
        from eurus.tools.routing import HAS_ROUTING_DEPS
        assert isinstance(HAS_ROUTING_DEPS, bool)


# ============================================================================
# REPL TOOL COMPREHENSIVE SECURITY TESTS
# ============================================================================

class TestREPLSecurityComprehensive:
    """REPL tests — Docker is the sandbox, all imports allowed."""
    
    def test_repl_allows_sys(self):
        """Test REPL allows sys module (Docker sandbox)."""
        from eurus.tools.repl import PythonREPLTool
        repl = PythonREPLTool()
        result = repl._run("import sys; print(sys.version_info.major)")
        assert result is not None
        assert "Error" not in result
        repl.close()
        
    def test_repl_allows_os(self):
        """Test REPL allows os module (Docker sandbox)."""
        from eurus.tools.repl import PythonREPLTool
        repl = PythonREPLTool()
        result = repl._run("import os; print(os.getcwd())")
        assert result is not None
        assert "Error" not in result
        repl.close()
        
    def test_repl_allows_xarray(self):
        """Test REPL allows xarray operations."""
        from eurus.tools.repl import PythonREPLTool
        repl = PythonREPLTool()
        result = repl._run("import xarray as xr; print(type(xr))")
        assert "module" in result.lower() or "xarray" in result.lower()
        repl.close()
        
    def test_repl_allows_pandas(self):
        """Test REPL allows pandas operations."""
        from eurus.tools.repl import PythonREPLTool
        repl = PythonREPLTool()
        result = repl._run("import pandas as pd; print(pd.DataFrame({'a': [1, 2]}))")
        assert "Error" not in result
        repl.close()



# ============================================================================
# EDGE CASES AND ERROR HANDLING
# ============================================================================

class TestEdgeCases:
    """Tests for edge cases and error handling."""
    
    def test_get_short_name_unknown(self):
        """Test get_short_name with unknown variable returns input."""
        from eurus.config import get_short_name
        result = get_short_name("completely_unknown_variable_xyz")
        # Should return the input as-is for unknown variables
        assert "completely_unknown_variable_xyz" in result or result is not None
        
    def test_variable_info_none_for_unknown(self):
        """Test get_variable_info returns None for unknown."""
        from eurus.config import get_variable_info
        result = get_variable_info("unknown_var_xyz")
        assert result is None
        
    def test_era5_tool_has_description(self):
        """Test ERA5 tool has comprehensive description."""
        from eurus.tools.era5 import era5_tool
        assert len(era5_tool.description) > 100