import importlib.machinery
import importlib.util
from pathlib import Path

BIN = Path(__file__).resolve().parents[1] / "bin" / "mem-pressure-notify"

spec = importlib.util.spec_from_loader(
    "mem_pressure_notify",
    importlib.machinery.SourceFileLoader("mem_pressure_notify", str(BIN)),
)
notifier = importlib.util.module_from_spec(spec)
spec.loader.exec_module(notifier)


def _seed(now):
    notifier.hist.clear()
    notifier.alerted.clear()
    notifier.hist["stale.scope"].append((now - notifier.GROW_WINDOW - 1, 1000))
    notifier.hist["live.scope"].append((now - 1, 2000))
    notifier.alerted.add("stale.scope")
    notifier.alerted.add("live.scope")


def test_groups_that_stopped_reporting_are_forgotten():
    now = 10_000.0
    _seed(now)
    notifier.expire_history(now)
    assert set(notifier.hist) == {"live.scope"}
    assert notifier.alerted == {"live.scope"}


def test_live_group_samples_inside_the_window_survive():
    now = 10_000.0
    _seed(now)
    notifier.expire_history(now)
    assert list(notifier.hist["live.scope"]) == [(now - 1, 2000)]
