gabriel / musehub public
test_webhook_events_304.py python
75 lines 3.5 KB
Raw
sha256:b6c0bf4d4a047b432e15f1e74dd72a51b3a2b0c7bf11996680a5a91a0068ffe9 feat(webhooks): dispatch all eight events; deactivate a hoo… Opus 5.5 patch 1 day ago
1 """musehub#304: every registrable webhook event is dispatched, and a hook that keeps failing is deactivated."""
2 from __future__ import annotations
3
4 import asyncio
5 from unittest.mock import AsyncMock, patch
6
7 import pytest
8 from sqlalchemy.ext.asyncio import AsyncSession
9
10 from musehub.models.musehub import WEBHOOK_EVENT_TYPES
11 from musehub.services import musehub_webhook_dispatcher as d
12
13
14 def test_disable_threshold_is_ten() -> None:
15 assert d.DISABLE_AFTER_CONSECUTIVE_FAILURES == 10
16
17
18 @pytest.mark.asyncio
19 async def test_emit_event_schedules_a_background_dispatch() -> None:
20 seen: list[tuple[str, str]] = []
21
22 async def fake(repo_id: str, event_type: str, payload: object) -> None:
23 seen.append((repo_id, event_type))
24
25 with patch.object(d, "dispatch_event_background", new=fake):
26 d.emit_event("r1", "push", {"repoId": "r1", "branch": "main", "headCommitId": "c", "pushedBy": "a", "commitCount": 1})
27 await asyncio.sleep(0)
28 await asyncio.sleep(0)
29 assert seen == [("r1", "push")]
30
31
32 def test_emit_event_without_a_loop_never_raises() -> None:
33 d.emit_event("r1", "push", {"repoId": "r1", "branch": "main", "headCommitId": "c", "pushedBy": "a", "commitCount": 1})
34
35
36 @pytest.mark.asyncio
37 async def test_a_hook_is_deactivated_after_ten_consecutive_failed_events(db_session: AsyncSession) -> None:
38 from tests.factories import create_repo
39
40 repo = await create_repo(db_session, slug="hook-disable")
41 from musehub.db.musehub_webhook_models import MusehubWebhook
42 from musehub.core.genesis import compute_webhook_id
43 hook = MusehubWebhook(webhook_id=compute_webhook_id(repo.repo_id, "https://hooks.example/x", "t"), repo_id=repo.repo_id,
44 url="https://hooks.example/x", events=list(WEBHOOK_EVENT_TYPES), secret="", active=True)
45 db_session.add(hook)
46 await db_session.commit()
47 payload = {"repoId": repo.repo_id, "branch": "main", "headCommitId": "c", "pushedBy": "a", "commitCount": 1}
48 with patch.object(d, "_attempt_delivery", new=AsyncMock(return_value=(False, 500, "down"))), patch.object(d, "_BACKOFF_BASE", 0.0):
49 for n in range(1, 11):
50 await d.dispatch_event(db_session, repo_id=repo.repo_id, event_type="push", payload=payload)
51 await db_session.commit()
52 await db_session.refresh(hook)
53 assert hook.active is (n < 10), f"after {n} failed events"
54
55
56 @pytest.mark.asyncio
57 async def test_one_success_resets_the_failure_streak(db_session: AsyncSession) -> None:
58 from tests.factories import create_repo
59 from musehub.db.musehub_webhook_models import MusehubWebhook
60 from musehub.core.genesis import compute_webhook_id
61
62 repo = await create_repo(db_session, slug="hook-streak")
63 hook = MusehubWebhook(webhook_id=compute_webhook_id(repo.repo_id, "https://hooks.example/y", "t"), repo_id=repo.repo_id,
64 url="https://hooks.example/y", events=["push"], secret="", active=True)
65 db_session.add(hook)
66 await db_session.commit()
67 payload = {"repoId": repo.repo_id, "branch": "main", "headCommitId": "c", "pushedBy": "a", "commitCount": 1}
68 outcomes = [False] * 9 + [True] + [False] * 9
69 with patch.object(d, "_BACKOFF_BASE", 0.0):
70 for ok in outcomes:
71 with patch.object(d, "_attempt_delivery", new=AsyncMock(return_value=(ok, 200 if ok else 500, ""))):
72 await d.dispatch_event(db_session, repo_id=repo.repo_id, event_type="push", payload=payload)
73 await db_session.commit()
74 await db_session.refresh(hook)
75 assert hook.active is True
File History 1 commit
sha256:b6c0bf4d4a047b432e15f1e74dd72a51b3a2b0c7bf11996680a5a91a0068ffe9 feat(webhooks): dispatch all eight events; deactivate a hoo… Opus 5.5 patch 1 day ago