diff --git a/src/slack_annotations/core.py b/src/slack_annotations/core.py index 9176688..7799182 100644 --- a/src/slack_annotations/core.py +++ b/src/slack_annotations/core.py @@ -14,7 +14,10 @@ def notify( # values are needed for the algorithm below to work. search_params["sort"] = "created" search_params["order"] = "asc" - search_params["search_after"] = (datetime.now(UTC) - timedelta(hours=1)).isoformat() + if not search_params.get("search_after"): + search_params["search_after"] = ( + datetime.now(UTC) - timedelta(hours=1) + ).isoformat() if cache_path: try: diff --git a/tests/data/search_annotations.json b/tests/data/search_annotations.json new file mode 100644 index 0000000..e873381 --- /dev/null +++ b/tests/data/search_annotations.json @@ -0,0 +1,116 @@ +{ + "total": 1000, + "rows": [ + { + "id": "gtu_pDTHEe-6-NN-lcCgBQ", + "created": "2024-06-27T20:54:56.605900+00:00", + "updated": "2024-11-29T15:12:47.777369+00:00", + "user": "acct:Janrae.Valencia@hypothes.is", + "uri": "https://example.com/", + "text": " test", + "tags": [], + "group": "__world__", + "permissions": { + "read": [ + "group:__world__" + ], + "admin": [ + "acct:Janrae.Valencia@hypothes.is" + ], + "update": [ + "acct:Janrae.Valencia@hypothes.is" + ], + "delete": [ + "acct:Janrae.Valencia@hypothes.is" + ] + }, + "target": [ + { + "source": "https://example.com/", + "selector": [ + { + "type": "RangeSelector", + "endOffset": 4, + "startOffset": 0, + "endContainer": "/div[1]/h1[1]/hypothesis-highlight[2]/hypothesis-highlight[1]/hypothesis-highlight[1]/hypothesis-highlight[1]/hypothesis-highlight[1]", + "startContainer": "/div[1]/h1[1]/hypothesis-highlight[1]/hypothesis-highlight[1]" + }, + { + "end": 20, + "type": "TextPositionSelector", + "start": 6 + }, + { + "type": "TextQuoteSelector", + "exact": "Example Domain", + "prefix": "\n\n ", + "suffix": "\n This domain is for use in i" + } + ] + } + ], + "document": { + "title": [ + "Annotating the law | Hypothes.is" + ] + }, + "links": { + "html": "https://hypothes.is/a/gtu_pDTHEe-6-NN-lcCgBQ", + "incontext": "https://hyp.is/gtu_pDTHEe-6-NN-lcCgBQ/example.com/", + "json": "https://hypothes.is/api/annotations/gtu_pDTHEe-6-NN-lcCgBQ" + }, + "user_info": { + "display_name": null + }, + "flagged": false, + "hidden": false + }, + { + "id": "wU0NQqjzEe-eIPcuiGN1Hg", + "created": "2024-11-22T17:03:54.248643+00:00", + "updated": "2024-11-22T17:03:54.248643+00:00", + "user": "acct:4egson@hypothes.is", + "uri": "https://example.com", + "text": "This is a test annotation.", + "tags": [ + "Test", + "API" + ], + "group": "__world__", + "permissions": { + "read": [ + "group:__world__" + ], + "admin": [ + "acct:4egson@hypothes.is" + ], + "update": [ + "acct:4egson@hypothes.is" + ], + "delete": [ + "acct:4egson@hypothes.is" + ] + }, + "target": [ + { + "source": "https://example.com" + } + ], + "document": { + "title": [ + "Annotating the law | Hypothes.is" + ] + }, + "links": { + "html": "https://hypothes.is/a/wU0NQqjzEe-eIPcuiGN1Hg", + "incontext": "https://hyp.is/wU0NQqjzEe-eIPcuiGN1Hg/example.com", + "json": "https://hypothes.is/api/annotations/wU0NQqjzEe-eIPcuiGN1Hg" + }, + "user_info": { + "display_name": null + }, + "flagged": false, + "hidden": false + } + ] +} diff --git a/tests/data/slack_annotations.json b/tests/data/slack_annotations.json new file mode 100644 index 0000000..b85ddb9 --- /dev/null +++ b/tests/data/slack_annotations.json @@ -0,0 +1,70 @@ +{ + "text": "2 new annotations", + "blocks": [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "`Janrae.Valencia` (None) annotated :" + }, + "fields": [ + { + "type": "mrkdwn", + "text": "*Quote:*" + }, + { + "type": "mrkdwn", + "text": "*Annotation* ():" + }, + { + "type": "plain_text", + "text": "Example Domain" + }, + { + "type": "plain_text", + "text": " test" + } + ] + }, + { + "type": "divider" + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "`4egson` (None) annotated :" + }, + "fields": [ + { + "type": "mrkdwn", + "text": "*Quote:*" + }, + { + "type": "mrkdwn", + "text": "*Annotation* ():" + }, + { + "type": "plain_text", + "text": "(None)" + }, + { + "type": "plain_text", + "text": "This is a test annotation." + } + ] + }, + { + "type": "divider" + }, + { + "type": "context", + "elements": [ + { + "type": "mrkdwn", + "text": "These annotations are posted to Slack by a " + } + ] + } + ] +} diff --git a/tests/functional/core_test.py b/tests/functional/core_test.py new file mode 100644 index 0000000..ba1ce45 --- /dev/null +++ b/tests/functional/core_test.py @@ -0,0 +1,38 @@ +import json +import pytest + +from slack_annotations.core import notify + + +@pytest.fixture +def slack_annotations(): + with open("tests/data/slack_annotations.json") as f: + return json.load(f) + + +@pytest.fixture +def search_annotations(): + with open("tests/data/search_annotations.json") as f: + return json.load(f) + + +def test_notify_search_after(httpx_mock, search_annotations, slack_annotations): + url = "https://hypothes.is/api/search?search_after=1733007600000.0&sort=created&order=asc" + httpx_mock.add_response(url=url, content=json.dumps(search_annotations)) + + search_params = {"search_after": "1733007600000.0"} + assert notify(search_params=search_params) == json.dumps(slack_annotations) + + +def test_notify_cache_file(httpx_mock, tmp_path, search_annotations, slack_annotations): + url = "https://hypothes.is/api/search?search_after=1733007600000.0&sort=created&order=asc" + httpx_mock.add_response(url=url, content=json.dumps(search_annotations)) + + search_params = {"search_after": "1733007600000.0"} + cache_path = tmp_path / "cache.json" + assert notify(search_params=search_params, cache_path=cache_path) == json.dumps( + slack_annotations + ) + assert json.loads(cache_path.read_text()) == { + "search_after": "2024-11-22T17:03:54.248643+00:00" + }