94 lines
2.6 KiB
Python
94 lines
2.6 KiB
Python
import pytest
|
|
|
|
from oms_tui.app import OmsApp, TelegramEvent
|
|
|
|
DLL = {
|
|
"type": "dll",
|
|
"id": "76348799",
|
|
"manufacturer": "KAM",
|
|
"version": "1b",
|
|
"media": "Cold water meter",
|
|
"driver": "kamwater",
|
|
"c_field": "44",
|
|
"rssi": 97,
|
|
}
|
|
JSON = {
|
|
"type": "json",
|
|
"id": "76348799",
|
|
"driver": "kamwater",
|
|
"media": "cold water",
|
|
"fields": {"total_m3": 6.408, "status": "DRY"},
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_device_appears_and_selects():
|
|
app = OmsApp(device="none", autostart=False)
|
|
async with app.run_test() as pilot:
|
|
app.post_message(TelegramEvent(DLL))
|
|
await pilot.pause()
|
|
table = app.query_one("#devices")
|
|
assert table.row_count == 1
|
|
|
|
# nothing selected yet -> global stream mode
|
|
assert app.show_all is True
|
|
|
|
# decrypted JSON updates the model
|
|
app.post_message(TelegramEvent(JSON))
|
|
await pilot.pause()
|
|
dev = app.store.get("76348799")
|
|
assert dev.decrypted is True
|
|
assert dev.fields["total_m3"] == 6.408
|
|
assert dev.count == 1 # one DLL telegram
|
|
|
|
# select the device
|
|
app.action_select()
|
|
await pilot.pause()
|
|
assert app.show_all is False
|
|
assert app.selected_id == "76348799"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ctrl_c_quits():
|
|
app = OmsApp(device="none", autostart=False)
|
|
async with app.run_test() as pilot:
|
|
await pilot.press("ctrl+c")
|
|
await pilot.pause()
|
|
assert app._exit is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_set_key_via_modal():
|
|
app = OmsApp(device="none", autostart=False)
|
|
async with app.run_test() as pilot:
|
|
app.post_message(TelegramEvent(DLL))
|
|
await pilot.pause()
|
|
app.action_select()
|
|
await pilot.pause()
|
|
|
|
app.action_set_key() # opens the modal prompt
|
|
await pilot.pause()
|
|
await pilot.press(*"28F64A24988064A079AA2C807D6102AE")
|
|
await pilot.press("enter") # submit
|
|
await pilot.pause()
|
|
|
|
dev = app.store.get("76348799")
|
|
assert dev.key == "28F64A24988064A079AA2C807D6102AE"
|
|
assert dev.status == "key set"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_import_preloads_key(tmp_path):
|
|
path = str(tmp_path / "keys.csv")
|
|
with open(path, "w") as f:
|
|
f.write("id,aes_key\n76348799,28F64A24988064A079AA2C807D6102AE\n")
|
|
|
|
app = OmsApp(device="none", autostart=False)
|
|
async with app.run_test() as pilot:
|
|
app._do_import(path, announce=False)
|
|
await pilot.pause()
|
|
dev = app.store.get("76348799")
|
|
assert dev is not None
|
|
assert dev.key == "28F64A24988064A079AA2C807D6102AE"
|
|
assert dev.status == "key set"
|