CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 12s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 2m2s
CI & Build / integration (push) Successful in 2m10s
CI & Build / Build & push image (push) Successful in 34s
A delivery to a session that opened the rule within the open window is stamped opened as it is written, and migration 0135 stamps the rows already recorded the same way. The adherence readout, the ordering open rate and the weighing pass read one definition: a rule read early in a long session is not counted unread each time it is delivered later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
8.6 KiB
Python
192 lines
8.6 KiB
Python
"""Real-Postgres tests for `rule_deliveries`: a delivery recorded against a
|
|
session, the open stamped on the one it followed, and the history read back."""
|
|
import asyncio
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
from sqlalchemy import delete, select, update
|
|
|
|
from grimoire.models import async_session
|
|
from grimoire.models.rule_delivery import RuleDelivery
|
|
from grimoire.services import background, rule_deliveries
|
|
from tests.helpers import ensure_user
|
|
|
|
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
|
|
|
|
RULE = 9_100_001
|
|
OTHER = 9_100_002
|
|
# A plugin version, as the `X-Grimoire-Plugin` header carries it. Deliveries
|
|
# count toward an open rate only when their plugin sent one.
|
|
PLUGIN = "2026.11.23.2100"
|
|
|
|
|
|
async def _user() -> int:
|
|
async with async_session() as s:
|
|
user = await ensure_user(s, "rule_delivery_owner")
|
|
uid = user.id
|
|
await s.commit()
|
|
await s.execute(delete(RuleDelivery).where(RuleDelivery.user_id == uid))
|
|
await s.commit()
|
|
return uid
|
|
|
|
|
|
async def _rows(uid: int) -> list[RuleDelivery]:
|
|
async with async_session() as s:
|
|
return list((await s.execute(
|
|
select(RuleDelivery).where(RuleDelivery.user_id == uid).order_by(RuleDelivery.id)
|
|
)).scalars().all())
|
|
|
|
|
|
async def _record(uid, sid, source, lines, plugin=PLUGIN):
|
|
# A bound request's rows wait for the door's payload; flushing all of
|
|
# them is what a door does when every line it rendered was kept. A named
|
|
# session is recorded as a request from `plugin` would be.
|
|
if sid is not None:
|
|
rule_deliveries.bind_session("", plugin)
|
|
try:
|
|
rule_deliveries.record(uid, sid, source, lines)
|
|
rule_deliveries.flush()
|
|
await background.drain()
|
|
finally:
|
|
if sid is not None:
|
|
rule_deliveries.bind_session("")
|
|
|
|
|
|
async def test_an_open_is_stamped_on_the_sessions_latest_delivery_only():
|
|
uid = await _user()
|
|
await _record(uid, "s-1", "moment_rule", [(RULE, "work.finish", "full")])
|
|
await asyncio.sleep(0.01)
|
|
await _record(uid, "s-1", "moment_rule", [(RULE, "work.finish", "repeat")])
|
|
await _record(uid, "s-2", "moment_rule", [(RULE, "work.finish", "full")])
|
|
|
|
assert await rule_deliveries.mark_opened(uid, "s-1", RULE) is True
|
|
rows = await _rows(uid)
|
|
opened = [(r.session_id, r.form) for r in rows if r.opened_at is not None]
|
|
assert opened == [("s-1", "repeat")]
|
|
# A second open with nothing left unopened in the session stamps the earlier one.
|
|
assert await rule_deliveries.mark_opened(uid, "s-1", RULE) is True
|
|
assert await rule_deliveries.mark_opened(uid, "s-1", RULE) is False
|
|
|
|
|
|
async def test_a_line_for_a_rule_the_session_already_read_is_recorded_opened():
|
|
uid = await _user()
|
|
await _record(uid, "s-1", "moment_rule", [(RULE, "work.start", "full"), (OTHER, "work.start", "full")])
|
|
assert await rule_deliveries.mark_opened(uid, "s-1", RULE) is True
|
|
await _record(uid, "s-1", "pre_tool_rule", [(RULE, "pre_tool_rule", "repeat"), (OTHER, "pre_tool_rule", "full")])
|
|
await _record(uid, "s-2", "pre_tool_rule", [(RULE, "pre_tool_rule", "full")])
|
|
later = {(r.session_id, r.rule_id): r.opened_at is not None
|
|
for r in await _rows(uid) if r.at == "pre_tool_rule"}
|
|
# Read in s-1, so it reached a reader who had it; OTHER was never read,
|
|
# and another session's read is not this one's.
|
|
assert later == {("s-1", RULE): True, ("s-1", OTHER): False, ("s-2", RULE): False}
|
|
|
|
|
|
async def test_a_read_older_than_the_open_window_does_not_carry():
|
|
uid = await _user()
|
|
await _record(uid, "s-1", "moment_rule", [(RULE, "work.start", "full")])
|
|
async with async_session() as s:
|
|
await s.execute(update(RuleDelivery).where(RuleDelivery.user_id == uid).values(
|
|
opened_at=datetime.now(timezone.utc) - rule_deliveries.OPEN_WINDOW - timedelta(minutes=1)))
|
|
await s.commit()
|
|
await _record(uid, "s-1", "moment_rule", [(RULE, "work.deliver", "full")])
|
|
latest = (await _rows(uid))[-1]
|
|
assert (latest.at, latest.opened_at) == ("work.deliver", None)
|
|
|
|
|
|
async def test_an_open_with_no_delivery_behind_it_stamps_nothing():
|
|
uid = await _user()
|
|
assert await rule_deliveries.mark_opened(uid, "s-1", OTHER) is False
|
|
assert await rule_deliveries.mark_opened(uid, "", RULE) is False
|
|
|
|
|
|
async def test_without_a_session_nothing_is_recorded():
|
|
uid = await _user()
|
|
rule_deliveries.bind_session("")
|
|
await _record(uid, None, "prompt_rule", [(RULE, "prompt_rule", "full")])
|
|
assert await _rows(uid) == []
|
|
|
|
|
|
async def test_a_bound_session_is_the_one_recorded():
|
|
uid = await _user()
|
|
rule_deliveries.bind_session("bound s/3")
|
|
await _record(uid, None, "prompt_rule", [(RULE, "prompt_rule", "full")])
|
|
# The plugin's filename-safe form, so the raw and safe ids agree.
|
|
assert [r.session_id for r in await _rows(uid)] == ["bound_s_3"]
|
|
rule_deliveries.bind_session("")
|
|
|
|
|
|
async def test_history_is_the_smoothed_open_rate_at_each_place():
|
|
uid = await _user()
|
|
await _record(uid, "s-1", "moment_rule", [(RULE, "work.finish", "full")] * 3
|
|
+ [(RULE, "work.deliver", "full"), (OTHER, "work.finish", "full")])
|
|
await rule_deliveries.mark_opened(uid, "s-1", RULE) # one of the five RULE rows
|
|
|
|
history = await rule_deliveries.history(uid, [(RULE, "work.finish"), (OTHER, "work.finish")])
|
|
rows = await _rows(uid)
|
|
stamped_at = next(r.at for r in rows if r.opened_at is not None)
|
|
finish_opened = 1 if stamped_at == "work.finish" else 0
|
|
assert history[RULE] == pytest.approx((finish_opened + 1) / (3 + 2))
|
|
assert history[OTHER] == pytest.approx(1 / 3)
|
|
# A place with no delivery is unknown, not zero.
|
|
assert await rule_deliveries.history(uid, [(RULE, "reply.report")]) == {}
|
|
|
|
|
|
async def test_a_delivery_keeps_the_plugin_version_that_asked_for_it():
|
|
uid = await _user()
|
|
rule_deliveries.bind_session("s-p", PLUGIN)
|
|
try:
|
|
await _record(uid, None, "prompt_rule", [(RULE, "prompt_rule", "full")])
|
|
finally:
|
|
rule_deliveries.bind_session("")
|
|
await _record(uid, "s-old", "prompt_rule", [(RULE, "prompt_rule", "full")], plugin=None)
|
|
assert [(r.session_id, r.plugin) for r in await _rows(uid)] == [("s-p", PLUGIN), ("s-old", None)]
|
|
|
|
|
|
async def test_history_counts_only_deliveries_whose_plugin_reports_opens():
|
|
uid = await _user()
|
|
# An older plugin never reported these opens, so they read as unopened
|
|
# however often they were read; the rate is taken over the others.
|
|
await _record(uid, "s-old", "moment_rule", [(RULE, "work.finish", "full")] * 4, plugin=None)
|
|
await _record(uid, "s-old", "moment_rule", [(OTHER, "work.finish", "full")], plugin=None)
|
|
await _record(uid, "s-new", "moment_rule", [(RULE, "work.finish", "full")])
|
|
await rule_deliveries.mark_opened(uid, "s-new", RULE)
|
|
|
|
history = await rule_deliveries.history(uid, [(RULE, "work.finish"), (OTHER, "work.finish")])
|
|
assert history[RULE] == pytest.approx((1 + 1) / (1 + 2))
|
|
# Delivered only to a plugin that does not report opens: unknown, not zero.
|
|
assert OTHER not in history
|
|
|
|
|
|
async def test_old_deliveries_are_pruned():
|
|
uid = await _user()
|
|
await _record(uid, "s-1", "moment_rule", [(RULE, "work.finish", "full"), (OTHER, "work.finish", "full")])
|
|
async with async_session() as s:
|
|
await s.execute(
|
|
update(RuleDelivery)
|
|
.where(RuleDelivery.user_id == uid, RuleDelivery.rule_id == RULE)
|
|
.values(delivered_at=datetime.now(timezone.utc) - timedelta(days=rule_deliveries.RETAIN_DAYS + 1))
|
|
)
|
|
await s.commit()
|
|
assert await rule_deliveries.prune() >= 1
|
|
assert [r.rule_id for r in await _rows(uid)] == [OTHER]
|
|
|
|
|
|
async def test_a_rule_retires_after_its_reminder_and_a_full_line_re_arms_it():
|
|
uid = await _user()
|
|
rule_deliveries.bind_session("s-life")
|
|
try:
|
|
await _record(uid, None, "moment_rule", [(RULE, "work.finish", "full")])
|
|
assert await rule_deliveries.retired(uid, [RULE]) == set()
|
|
await asyncio.sleep(0.01)
|
|
await _record(uid, None, "moment_rule", [(RULE, "work.finish", "repeat")])
|
|
assert await rule_deliveries.retired(uid, [RULE, OTHER]) == {RULE}
|
|
await asyncio.sleep(0.01)
|
|
# The ledger forgot it (a compaction): the next line is full, and re-arms it.
|
|
await _record(uid, None, "moment_rule", [(RULE, "work.finish", "full")])
|
|
assert await rule_deliveries.retired(uid, [RULE]) == set()
|
|
# Another session's reminder retires nothing here.
|
|
await _record(uid, "s-other", "moment_rule", [(OTHER, "work.finish", "repeat")])
|
|
assert await rule_deliveries.retired(uid, [OTHER]) == set()
|
|
finally:
|
|
rule_deliveries.bind_session("")
|