File size: 5,418 Bytes
881f9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Tests for the IoT MCP server (issue #9).

Covers:
  - list_assets: happy path and health_status filter
  - get_asset_metadata: known ID, missing ID
  - list_sensors: known transformer, missing transformer (error dict, not list)
  - get_sensor_readings: known sensor, missing transformer, missing sensor,
    time-window filtering, limit clamping
"""

import sys
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from mcp_servers.iot_server.server import (
    get_asset_metadata,
    get_sensor_readings,
    list_assets,
    list_sensors,
)

# ---------------------------------------------------------------------------
# list_assets
# ---------------------------------------------------------------------------


def test_list_assets_returns_list():
    result = list_assets()
    assert isinstance(result, list)
    assert len(result) > 0


def test_list_assets_fields():
    result = list_assets()
    row = result[0]
    for key in (
        "transformer_id",
        "name",
        "location",
        "health_status",
        "rul_days",
        "in_service",
    ):
        assert key in row, f"Missing field: {key}"


def test_list_assets_filter_health_status():
    all_assets = list_assets()
    filtered = list_assets(health_status=0)
    assert isinstance(filtered, list)
    assert all(r["health_status"] == 0 for r in filtered)
    # Filtered set must be a subset of all assets
    all_ids = {r["transformer_id"] for r in all_assets}
    assert all(r["transformer_id"] in all_ids for r in filtered)


def test_list_assets_filter_nonexistent_status_returns_empty():
    result = list_assets(health_status=999)
    assert result == []


# ---------------------------------------------------------------------------
# get_asset_metadata
# ---------------------------------------------------------------------------


def test_get_asset_metadata_known():
    result = get_asset_metadata("T-001")
    assert "error" not in result
    assert result["transformer_id"] == "T-001"
    for key in (
        "name",
        "manufacturer",
        "location",
        "voltage_class",
        "rating_kva",
        "install_date",
        "age_years",
        "health_status",
        "fdd_category",
        "rul_days",
        "in_service",
    ):
        assert key in result, f"Missing field: {key}"


def test_get_asset_metadata_missing():
    result = get_asset_metadata("T-NONEXISTENT")
    assert "error" in result
    assert "T-NONEXISTENT" in result["error"]


# ---------------------------------------------------------------------------
# list_sensors
# ---------------------------------------------------------------------------


def test_list_sensors_known():
    result = list_sensors("T-001")
    assert isinstance(result, list)
    assert len(result) > 0
    for row in result:
        assert "sensor_id" in row
        assert "unit" in row
        assert "num_readings" in row


def test_list_sensors_missing_returns_error_dict():
    # Error must be a dict, not a list — harness checks result["error"]
    result = list_sensors("T-NONEXISTENT")
    assert isinstance(
        result, dict
    ), "list_sensors error case must return a dict, not a list"
    assert "error" in result


# ---------------------------------------------------------------------------
# get_sensor_readings
# ---------------------------------------------------------------------------


def _get_first_sensor(transformer_id: str) -> str:
    sensors = list_sensors(transformer_id)
    assert isinstance(sensors, list) and len(sensors) > 0
    return sensors[0]["sensor_id"]


def test_get_sensor_readings_happy_path():
    sensor_id = _get_first_sensor("T-001")
    result = get_sensor_readings("T-001", sensor_id)
    assert isinstance(result, list)
    assert len(result) > 0
    for row in result:
        assert "timestamp" in row
        assert "value" in row
        assert "unit" in row
        # Timestamp must be a plain string, not a pandas Timestamp
        assert isinstance(row["timestamp"], str), "timestamp must be a string"


def test_get_sensor_readings_missing_transformer():
    result = get_sensor_readings("T-NONEXISTENT", "dga_h2_ppm")
    assert isinstance(result, list)
    assert len(result) == 1
    assert "error" in result[0]


def test_get_sensor_readings_missing_sensor():
    result = get_sensor_readings("T-001", "nonexistent_sensor_xyz")
    assert isinstance(result, list)
    assert len(result) == 1
    assert "error" in result[0]


def test_get_sensor_readings_limit():
    sensor_id = _get_first_sensor("T-001")
    result = get_sensor_readings("T-001", sensor_id, limit=5)
    assert len(result) <= 5


def test_get_sensor_readings_limit_capped_at_1000():
    sensor_id = _get_first_sensor("T-001")
    result = get_sensor_readings("T-001", sensor_id, limit=9999)
    assert len(result) <= 1000


def test_get_sensor_readings_time_window():
    sensor_id = _get_first_sensor("T-001")
    all_readings = get_sensor_readings("T-001", sensor_id, limit=1000)
    assert len(all_readings) >= 2
    # Use the second and third timestamps as window bounds
    start = all_readings[1]["timestamp"]
    end = all_readings[2]["timestamp"]
    windowed = get_sensor_readings("T-001", sensor_id, start_time=start, end_time=end)
    assert isinstance(windowed, list)
    assert all(start <= r["timestamp"] <= end for r in windowed)