diff --git a/justfile b/justfile index 2d81f05..89a6a95 100644 --- a/justfile +++ b/justfile @@ -6,6 +6,9 @@ dev: tui: uv run python -m tui +test *args: + uv run pytest {{ args }} + fmt: uv format cd web && npx --yes prettier --write src/ diff --git a/pyproject.toml b/pyproject.toml index aa16c05..ef21c67 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,4 +28,8 @@ packages = ["cli", "core", "telnet", "tui"] "data/shared.json" = "core/_shared.json" [dependency-groups] -dev = [] +dev = ["pytest>=8.0"] + +[tool.pytest.ini_options] +testpaths = ["tests"] +pythonpath = ["."] diff --git a/tests/test_filters.py b/tests/test_filters.py new file mode 100644 index 0000000..81b6913 --- /dev/null +++ b/tests/test_filters.py @@ -0,0 +1,48 @@ +import pytest + +from core.filters import filter_moderated +from core.models import Record + + +def _record(did: str, rkey: str) -> Record: + uri = f"at://{did}/xyz.atbbs.post/{rkey}" + return Record(uri=uri, cid="bafy", value={}) + + +def test_filter_drops_banned_dids(): + records = [_record("did:plc:alice", "1"), _record("did:plc:eve", "2")] + out = filter_moderated(records, banned_dids={"did:plc:eve"}, hidden_posts=set()) + assert [r.uri for r in out] == ["at://did:plc:alice/xyz.atbbs.post/1"] + + +def test_filter_drops_hidden_posts(): + a = _record("did:plc:alice", "1") + b = _record("did:plc:alice", "2") + out = filter_moderated([a, b], banned_dids=set(), hidden_posts={b.uri}) + assert out == [a] + + +def test_filter_combines_both_rules(): + a = _record("did:plc:alice", "1") + b = _record("did:plc:alice", "hidden") + c = _record("did:plc:eve", "3") + out = filter_moderated( + [a, b, c], banned_dids={"did:plc:eve"}, hidden_posts={b.uri} + ) + assert out == [a] + + +def test_filter_no_rules_passes_through(): + records = [_record("did:plc:alice", "1"), _record("did:plc:bob", "2")] + assert filter_moderated(records, banned_dids=set(), hidden_posts=set()) == records + + +def test_filter_empty_records(): + assert filter_moderated([], banned_dids={"did:plc:eve"}, hidden_posts=set()) == [] + + +def test_filter_propagates_malformed_uri(): + """Malformed records are a programmer error upstream — fail loud, don't silently drop.""" + bad = Record(uri="not-an-at-uri", cid="bafy", value={}) + with pytest.raises(ValueError): + filter_moderated([bad], banned_dids=set(), hidden_posts=set()) diff --git a/tests/test_models.py b/tests/test_models.py new file mode 100644 index 0000000..7980f7a --- /dev/null +++ b/tests/test_models.py @@ -0,0 +1,103 @@ +import pytest + +from core.models import AtUri, BacklinkRef, MiniDoc, Post, make_at_uri + + +URI = "at://did:plc:abc/xyz.atbbs.post/3lkw1" + + +def test_aturi_parse_fields(): + parsed = AtUri.parse(URI) + assert parsed.did == "did:plc:abc" + assert parsed.collection == "xyz.atbbs.post" + assert parsed.rkey == "3lkw1" + + +def test_aturi_str_from_constructor(): + assert str(AtUri("did:plc:abc", "xyz.atbbs.post", "3lkw1")) == URI + + +def test_make_at_uri(): + assert make_at_uri("did:plc:abc", "xyz.atbbs.post", "3lkw1") == URI + + +def test_make_at_uri_roundtrips_through_parse(): + uri = make_at_uri("did:plc:abc", "xyz.atbbs.post", "3lkw1") + parsed = AtUri.parse(uri) + assert parsed.did == "did:plc:abc" + assert parsed.collection == "xyz.atbbs.post" + assert parsed.rkey == "3lkw1" + + +def test_aturi_str_roundtrip(): + assert str(AtUri.parse(URI)) == URI + + +def test_aturi_eq_with_string(): + assert AtUri.parse(URI) == URI + + +def test_aturi_eq_with_string_reverse(): + assert URI == AtUri.parse(URI) + + +def test_aturi_eq_with_aturi(): + assert AtUri.parse(URI) == AtUri.parse(URI) + + +@pytest.mark.parametrize("other", [42, None, [], {}, 3.14, object()]) +def test_aturi_eq_other_type_is_false(other): + assert (AtUri.parse(URI) == other) is False + + +def test_aturi_hash_consistent_with_equality(): + a = AtUri.parse(URI) + b = AtUri.parse(URI) + assert a == b + assert hash(a) == hash(b) + + +def test_aturi_hash_set_membership(): + seen = {AtUri.parse(URI)} + assert AtUri.parse(URI) in seen + + +@pytest.mark.parametrize( + "bad_uri", + [ + "https://example.com/x/y/z", + "did:plc:abc/xyz.atbbs.post/3lkw1", + "at://did:plc:abc/xyz.atbbs.post", + "at://did:plc:abc/xyz.atbbs.post/3lkw1/extra", + "at://did:plc:abc//3lkw1", + "at:///xyz.atbbs.post/3lkw1", + "", + ], +) +def test_aturi_parse_rejects_malformed(bad_uri): + with pytest.raises(ValueError): + AtUri.parse(bad_uri) + + +def test_backlinkref_uri(): + ref = BacklinkRef(did="did:plc:abc", collection="xyz.atbbs.post", rkey="3lkw1") + assert ref.uri == URI + + +def _post(root=None): + return Post( + uri=URI, + scope="board", + body="hi", + created_at="2024-01-15T12:30:00+00:00", + author=MiniDoc(did="did:plc:abc", handle="alice.test"), + root=root, + ) + + +def test_post_is_root_when_no_root(): + assert _post(root=None).is_root is True + + +def test_post_is_root_false_when_root_set(): + assert _post(root="at://did:plc:abc/xyz.atbbs.post/parent").is_root is False diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 0000000..5e3c434 --- /dev/null +++ b/tests/test_util.py @@ -0,0 +1,89 @@ +import os +import time +from datetime import datetime + +import pytest + +from core.util import ( + attachment_cid, + blob_url, + format_datetime_local, + format_datetime_utc, + now_iso, +) + + +@pytest.fixture +def tz(): + """Set the process timezone for a test, then restore.""" + original = os.environ.get("TZ") + + def _set(name: str): + os.environ["TZ"] = name + time.tzset() + + yield _set + if original is None: + os.environ.pop("TZ", None) + else: + os.environ["TZ"] = original + time.tzset() + + +def test_now_iso_is_parseable_utc(): + value = now_iso() + dt = datetime.fromisoformat(value) + assert dt.utcoffset().total_seconds() == 0 + + +def test_now_iso_uses_z_suffix(): + value = now_iso() + assert value.endswith("Z") + assert "+00:00" not in value + + +def test_format_datetime_utc_passthrough(): + assert format_datetime_utc("2024-01-15T12:30:00+00:00") == "2024-01-15 12:30 UTC" + + +def test_format_datetime_utc_converts_offset_to_utc(): + # 17:30 +05:00 == 12:30 UTC + assert format_datetime_utc("2024-01-15T17:30:00+05:00") == "2024-01-15 12:30 UTC" + + +def test_format_datetime_utc_converts_negative_offset(): + # 07:30 -05:00 == 12:30 UTC + assert format_datetime_utc("2024-01-15T07:30:00-05:00") == "2024-01-15 12:30 UTC" + + +def test_format_datetime_local_converts_to_local(tz): + tz("America/New_York") + # 17:30 UTC in January = 12:30 EST (UTC-5) + assert format_datetime_local("2024-01-15T17:30:00+00:00") == "2024-01-15 12:30" + + +def test_blob_url(): + url = blob_url("https://pds.example", "did:plc:abc", "bafy123") + assert url == "https://pds.example/xrpc/com.atproto.sync.getBlob?did=did:plc:abc&cid=bafy123" + + +def test_blob_url_strips_trailing_slash(): + url = blob_url("https://pds.example/", "did:plc:abc", "bafy123") + assert url == "https://pds.example/xrpc/com.atproto.sync.getBlob?did=did:plc:abc&cid=bafy123" + + +@pytest.mark.parametrize( + "attachment,expected", + [ + ({"file": {"ref": {"$link": "bafy123"}}}, "bafy123"), + ({}, ""), + ({"file": None}, ""), + ({"file": {}}, ""), + ({"file": {"ref": None}}, ""), + ({"file": {"ref": {}}}, ""), + ({"file": {"ref": {"$link": None}}}, ""), + ], + ids=["present", "missing_file", "null_file", "missing_ref", "null_ref", "missing_link", "null_link"], +) +def test_attachment_cid(attachment, expected): + assert attachment_cid(attachment) == expected diff --git a/uv.lock b/uv.lock index c3f7d15..f7e4364 100644 --- a/uv.lock +++ b/uv.lock @@ -100,6 +100,11 @@ dependencies = [ { name = "textual" }, ] +[package.dev-dependencies] +dev = [ + { name = "pytest" }, +] + [package.metadata] requires-dist = [ { name = "aiohttp", specifier = ">=3.13.5" }, @@ -112,7 +117,7 @@ requires-dist = [ ] [package.metadata.requires-dev] -dev = [] +dev = [{ name = "pytest", specifier = ">=8.0" }] [[package]] name = "attrs" @@ -338,6 +343,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/0e/61/66938bbb5fc52dbdf84594873d5b51fb1f7c7794e9c0f5bd885f30bc507b/idna-3.11-py3-none-any.whl", hash = "sha256:771a87f49d9defaf64091e6e6fe9c18d4833f140bd19464795bc32d966ca37ea", size = 71008, upload-time = "2025-10-12T14:55:18.883Z" }, ] +[[package]] +name = "iniconfig" +version = "2.3.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/72/34/14ca021ce8e5dfedc35312d08ba8bf51fdd999c576889fc2c24cb97f4f10/iniconfig-2.3.0.tar.gz", hash = "sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730", size = 20503, upload-time = "2025-10-18T21:55:43.219Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/cb/b1/3846dd7f199d53cb17f49cba7e651e9ce294d8497c8c150530ed11865bb8/iniconfig-2.3.0-py3-none-any.whl", hash = "sha256:f631c04d2c48c52b84d0d0549c99ff3859c98df65b3101406327ecc7d53fbf12", size = 7484, upload-time = "2025-10-18T21:55:41.639Z" }, +] + [[package]] name = "linkify-it-py" version = "2.1.0" @@ -433,6 +447,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/81/08/7036c080d7117f28a4af526d794aab6a84463126db031b007717c1a6676e/multidict-6.7.1-py3-none-any.whl", hash = "sha256:55d97cc6dae627efa6a6e548885712d4864b81110ac76fa4e534c03819fa4a56", size = 12319, upload-time = "2026-01-26T02:46:44.004Z" }, ] +[[package]] +name = "packaging" +version = "26.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d7/f1/e7a6dd94a8d4a5626c03e4e99c87f241ba9e350cd9e6d75123f992427270/packaging-26.2.tar.gz", hash = "sha256:ff452ff5a3e828ce110190feff1178bb1f2ea2281fa2075aadb987c2fb221661", size = 228134, upload-time = "2026-04-24T20:15:23.917Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/df/b2/87e62e8c3e2f4b32e5fe99e0b86d576da1312593b39f47d8ceef365e95ed/packaging-26.2-py3-none-any.whl", hash = "sha256:5fc45236b9446107ff2415ce77c807cee2862cb6fac22b8a73826d0693b0980e", size = 100195, upload-time = "2026-04-24T20:15:22.081Z" }, +] + [[package]] name = "piexif" version = "1.1.3" @@ -451,6 +474,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/63/d7/97f7e3a6abb67d8080dd406fd4df842c2be0efaf712d1c899c32a075027c/platformdirs-4.9.4-py3-none-any.whl", hash = "sha256:68a9a4619a666ea6439f2ff250c12a853cd1cbd5158d258bd824a7df6be2f868", size = 21216, upload-time = "2026-03-05T18:34:12.172Z" }, ] +[[package]] +name = "pluggy" +version = "1.6.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f9/e2/3e91f31a7d2b083fe6ef3fa267035b518369d9511ffab804f839851d2779/pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3", size = 69412, upload-time = "2025-05-15T12:30:07.975Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/54/20/4d324d65cc6d9205fabedc306948156824eb9f0ee1633355a8f7ec5c66bf/pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746", size = 20538, upload-time = "2025-05-15T12:30:06.134Z" }, +] + [[package]] name = "propcache" version = "0.4.1" @@ -508,6 +540,22 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/f4/7e/a72dd26f3b0f4f2bf1dd8923c85f7ceb43172af56d63c7383eb62b332364/pygments-2.20.0-py3-none-any.whl", hash = "sha256:81a9e26dd42fd28a23a2d169d86d7ac03b46e2f8b59ed4698fb4785f946d0176", size = 1231151, upload-time = "2026-03-29T13:29:30.038Z" }, ] +[[package]] +name = "pytest" +version = "9.0.3" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "colorama", marker = "sys_platform == 'win32'" }, + { name = "iniconfig" }, + { name = "packaging" }, + { name = "pluggy" }, + { name = "pygments" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/7d/0d/549bd94f1a0a402dc8cf64563a117c0f3765662e2e668477624baeec44d5/pytest-9.0.3.tar.gz", hash = "sha256:b86ada508af81d19edeb213c681b1d48246c1a91d304c6c81a427674c17eb91c", size = 1572165, upload-time = "2026-04-07T17:16:18.027Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d4/24/a372aaf5c9b7208e7112038812994107bc65a84cd00e0354a88c2c77a617/pytest-9.0.3-py3-none-any.whl", hash = "sha256:2c5efc453d45394fdd706ade797c0a81091eccd1d6e4bccfcd476e2b8e0ab5d9", size = 375249, upload-time = "2026-04-07T17:16:16.13Z" }, +] + [[package]] name = "rich" version = "14.3.3"