2025-10-24 01:28:04 -04:00
|
|
|
import typing as T
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2026-03-03 21:58:47 -07:00
|
|
|
from wpilib import Alert
|
|
|
|
|
from wpilib.simulation import AlertSim
|
2025-10-24 01:28:04 -04:00
|
|
|
|
2026-03-03 21:58:47 -07:00
|
|
|
Level = Alert.Level
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2026-03-03 21:58:47 -07:00
|
|
|
def group_name(request):
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
group_name = f"AlertTest_{request.node.name}"
|
|
|
|
|
yield group_name
|
|
|
|
|
|
2026-03-03 21:58:47 -07:00
|
|
|
AlertSim.resetData()
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
2026-03-09 23:03:00 -07:00
|
|
|
def get_active_alerts(level: Alert.Level) -> T.List[str]:
|
2026-03-03 21:58:47 -07:00
|
|
|
return [
|
|
|
|
|
a.text
|
|
|
|
|
for a in AlertSim.getAll()
|
2026-03-09 23:03:00 -07:00
|
|
|
if a.level == level and a.isActive()
|
2026-03-03 21:58:47 -07:00
|
|
|
]
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
2026-03-09 23:03:00 -07:00
|
|
|
def is_alert_active(text: str, level: Alert.Level):
|
2026-03-03 21:58:47 -07:00
|
|
|
matches = [
|
|
|
|
|
a
|
|
|
|
|
for a in AlertSim.getAll()
|
2026-03-09 23:03:00 -07:00
|
|
|
if a.level == level and a.text == text and a.isActive()
|
2026-03-03 21:58:47 -07:00
|
|
|
]
|
|
|
|
|
return len(matches) > 0
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_state(
|
2026-03-03 21:58:47 -07:00
|
|
|
level: Alert.Level,
|
2025-10-24 01:28:04 -04:00
|
|
|
expected_state: T.List[str],
|
|
|
|
|
):
|
2026-03-09 23:03:00 -07:00
|
|
|
assert expected_state == get_active_alerts(level)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_no_alerts_initially(group_name):
|
|
|
|
|
assert AlertSim.getCount() == 0
|
|
|
|
|
assert not AlertSim.getAll()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_no_alerts_after_reset(group_name):
|
|
|
|
|
with Alert(group_name, "alert", Alert.Level.HIGH) as alert:
|
|
|
|
|
|
|
|
|
|
alert.set(True)
|
|
|
|
|
assert is_alert_active("alert", Alert.Level.HIGH)
|
|
|
|
|
|
|
|
|
|
AlertSim.resetData()
|
|
|
|
|
assert AlertSim.getCount() == 0
|
|
|
|
|
assert not AlertSim.getAll()
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
2026-03-03 21:58:47 -07:00
|
|
|
def test_set_unset_single(group_name):
|
|
|
|
|
with Alert(group_name, "one", Alert.Level.HIGH) as one:
|
2025-10-24 01:28:04 -04:00
|
|
|
|
2026-03-09 23:03:00 -07:00
|
|
|
assert not is_alert_active("one", Alert.Level.HIGH)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
one.set(True)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert is_alert_active("one", Alert.Level.HIGH)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
one.set(False)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert not is_alert_active("one", Alert.Level.HIGH)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
2026-03-03 21:58:47 -07:00
|
|
|
def test_set_unset_multiple(group_name):
|
2025-10-24 01:28:04 -04:00
|
|
|
with (
|
2026-03-03 21:58:47 -07:00
|
|
|
Alert(group_name, "one", Alert.Level.HIGH) as one,
|
|
|
|
|
Alert(group_name, "two", Alert.Level.LOW) as two,
|
2025-10-24 01:28:04 -04:00
|
|
|
):
|
|
|
|
|
|
2026-03-09 23:03:00 -07:00
|
|
|
assert not is_alert_active("one", Alert.Level.HIGH)
|
|
|
|
|
assert not is_alert_active("two", Alert.Level.LOW)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
one.set(True)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert is_alert_active("one", Alert.Level.HIGH)
|
|
|
|
|
assert not is_alert_active("two", Alert.Level.LOW)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
one.set(True)
|
|
|
|
|
two.set(True)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert is_alert_active("one", Alert.Level.HIGH)
|
|
|
|
|
assert is_alert_active("two", Alert.Level.LOW)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
one.set(False)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert not is_alert_active("one", Alert.Level.HIGH)
|
|
|
|
|
assert is_alert_active("two", Alert.Level.LOW)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
2026-03-03 21:58:47 -07:00
|
|
|
def test_set_is_idempotent(group_name):
|
2025-10-24 01:28:04 -04:00
|
|
|
with (
|
2026-03-03 21:58:47 -07:00
|
|
|
Alert(group_name, "A", Alert.Level.LOW) as a,
|
|
|
|
|
Alert(group_name, "B", Alert.Level.LOW) as b,
|
|
|
|
|
Alert(group_name, "C", Alert.Level.LOW) as c,
|
2025-10-24 01:28:04 -04:00
|
|
|
):
|
|
|
|
|
|
|
|
|
|
a.set(True)
|
|
|
|
|
b.set(True)
|
|
|
|
|
c.set(True)
|
|
|
|
|
|
2026-03-09 23:03:00 -07:00
|
|
|
start_state = get_active_alerts(Alert.Level.LOW)
|
2025-10-24 01:28:04 -04:00
|
|
|
assert set(start_state) == {"A", "B", "C"}
|
|
|
|
|
|
|
|
|
|
b.set(True)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert_state(Alert.Level.LOW, start_state)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
a.set(True)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert_state(Alert.Level.LOW, start_state)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
2026-03-03 21:58:47 -07:00
|
|
|
def test_close_unsets_alert(group_name):
|
|
|
|
|
with Alert(group_name, "alert", Alert.Level.MEDIUM) as alert:
|
2025-10-24 01:28:04 -04:00
|
|
|
alert.set(True)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert is_alert_active("alert", Alert.Level.MEDIUM)
|
|
|
|
|
assert not is_alert_active("alert", Alert.Level.MEDIUM)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
2026-03-03 21:58:47 -07:00
|
|
|
def test_set_text_while_unset(group_name):
|
|
|
|
|
with Alert(group_name, "BEFORE", Alert.Level.LOW) as alert:
|
2025-10-24 01:28:04 -04:00
|
|
|
assert alert.getText() == "BEFORE"
|
|
|
|
|
alert.set(True)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert is_alert_active("BEFORE", Alert.Level.LOW)
|
2025-10-24 01:28:04 -04:00
|
|
|
alert.set(False)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert not is_alert_active("BEFORE", Alert.Level.LOW)
|
2025-10-24 01:28:04 -04:00
|
|
|
alert.setText("AFTER")
|
|
|
|
|
assert alert.getText() == "AFTER"
|
|
|
|
|
alert.set(True)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert not is_alert_active("BEFORE", Alert.Level.LOW)
|
|
|
|
|
assert is_alert_active("AFTER", Alert.Level.LOW)
|
2025-10-24 01:28:04 -04:00
|
|
|
|
|
|
|
|
|
2026-03-03 21:58:47 -07:00
|
|
|
def test_set_text_while_set(group_name):
|
|
|
|
|
with Alert(group_name, "BEFORE", Alert.Level.LOW) as alert:
|
2025-10-24 01:28:04 -04:00
|
|
|
assert alert.getText() == "BEFORE"
|
|
|
|
|
alert.set(True)
|
2026-03-09 23:03:00 -07:00
|
|
|
assert is_alert_active("BEFORE", Alert.Level.LOW)
|
2025-10-24 01:28:04 -04:00
|
|
|
alert.setText("AFTER")
|
|
|
|
|
assert alert.getText() == "AFTER"
|
2026-03-09 23:03:00 -07:00
|
|
|
assert not is_alert_active("BEFORE", Alert.Level.LOW)
|
|
|
|
|
assert is_alert_active("AFTER", Alert.Level.LOW)
|
2026-04-10 00:25:26 -05:00
|
|
|
|
|
|
|
|
def test_get_active(group_name):
|
|
|
|
|
with (
|
|
|
|
|
Alert(group_name, "A", Alert.Level.HIGH) as a,
|
|
|
|
|
Alert(group_name, "B", Alert.Level.HIGH) as b,
|
|
|
|
|
Alert(group_name, "C", Alert.Level.HIGH) as c,
|
|
|
|
|
):
|
|
|
|
|
|
|
|
|
|
a.set(True)
|
|
|
|
|
b.set(True)
|
|
|
|
|
c.set(False)
|
|
|
|
|
|
|
|
|
|
active = AlertSim.getActive()
|
|
|
|
|
allAlerts = AlertSim.getAll()
|
|
|
|
|
|
|
|
|
|
assert len(active) == 2
|
|
|
|
|
assert len(allAlerts) == 3
|
|
|
|
|
|
|
|
|
|
activeTexts = [a.text for a in active]
|
|
|
|
|
assert set(activeTexts) == {"A", "B"}
|
|
|
|
|
|
|
|
|
|
a.set(False)
|
|
|
|
|
active = AlertSim.getActive()
|
|
|
|
|
allAlerts = AlertSim.getAll()
|
|
|
|
|
assert len(active) == 1
|
|
|
|
|
assert len(allAlerts) == 3
|
|
|
|
|
assert active[0].text == "B"
|