working at least

This commit is contained in:
greendevilll
2026-06-06 23:30:35 +07:00
commit 366d1d687c
4 changed files with 661 additions and 0 deletions

122
tests/test_tag_hunter.py Normal file
View File

@@ -0,0 +1,122 @@
import unittest
from pathlib import Path
from tag_hunter import FetchResult, classify_fragment, classify_telegram
class FragmentClassifierTests(unittest.TestCase):
def test_prefers_not_for_sale_over_nav_auction_markers(self) -> None:
body = """
<html>
<body>
<div class="tm-header-tabs">On auction For sale Sold</div>
<tr class="tm-row-selectable js-auction-unavail" data-username="@dsfhsjdfkfhshfkshfksj">
<td><div class="table-cell-desc">Not for sale</div></td>
</tr>
<div class="popup-text">The Telegram username is currently not for sale.</div>
</body>
</html>
""".encode("utf-8")
result = FetchResult(
url="https://fragment.com/username/dsfhsjdfkfhshfkshfksj",
status=200,
headers={},
body=body,
final_url="https://fragment.com/username/dsfhsjdfkfhshfkshfksj",
)
state, reason = classify_fragment("dsfhsjdfkfhshfkshfksj", result)
self.assertEqual(state, "free")
self.assertIn("not for sale", reason)
def test_taken_when_fragment_row_contains_sale_marker(self) -> None:
body = """
<html>
<body>
<tr data-username="@cocaine">
<td><div class="table-cell-desc">On auction</div></td>
</tr>
</body>
</html>
""".encode("utf-8")
result = FetchResult(
url="https://fragment.com/username/cocaine",
status=200,
headers={},
body=body,
final_url="https://fragment.com/username/cocaine",
)
state, reason = classify_fragment("cocaine", result)
self.assertEqual(state, "taken")
self.assertEqual(reason, "on auction")
class TelegramClassifierTests(unittest.TestCase):
def test_existing_profile_is_taken_from_view_markers(self) -> None:
body = Path(__file__).resolve().parents[1].joinpath("exist.txt").read_text(encoding="utf-8")
result = FetchResult(
url="https://t.me/dicksmack",
status=200,
headers={},
body=body.encode("utf-8"),
final_url="https://t.me/dicksmack",
)
state, reason = classify_telegram("dicksmack", result)
self.assertEqual(state, "taken")
self.assertTrue(
any(marker in reason for marker in ("view @dicksmack", "view and join @dicksmack", "preview channel"))
)
def test_nonexisting_profile_is_free_from_contact_markers(self) -> None:
body = Path(__file__).resolve().parents[1].joinpath("nonexist.txt").read_text(encoding="utf-8")
result = FetchResult(
url="https://t.me/sahdfjkhdfjsahdkf",
status=200,
headers={},
body=body.encode("utf-8"),
final_url="https://t.me/sahdfjkhdfjsahdkf",
)
state, reason = classify_telegram("sahdfjkhdfjsahdkf", result)
self.assertEqual(state, "free")
self.assertTrue(
any(marker in reason for marker in ("contact @sahdfjkhdfjsahdkf", "right away"))
)
def test_redirect_without_explicit_body_is_unknown(self) -> None:
result = FetchResult(
url="https://t.me/doesnotexist123",
status=200,
headers={"Location": "tg://resolve?domain=doesnotexist123"},
body=b"<html><head><title>Telegram</title></head><body></body></html>",
final_url="https://t.me/doesnotexist123",
)
state, reason = classify_telegram("doesnotexist123", result)
self.assertEqual(state, "unknown")
self.assertIn("ambiguous", reason)
def test_explicit_unavailable_marker_is_free(self) -> None:
result = FetchResult(
url="https://t.me/doesnotexist123",
status=200,
headers={},
body=b"<html><body>Sorry, this page isn't available.</body></html>",
final_url="https://t.me/doesnotexist123",
)
state, reason = classify_telegram("doesnotexist123", result)
self.assertEqual(state, "free")
self.assertIn("sorry, this page isn't available", reason)
if __name__ == "__main__":
unittest.main()