-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_nyc_dot_bot.py
More file actions
533 lines (393 loc) · 16.7 KB
/
Copy pathtest_nyc_dot_bot.py
File metadata and controls
533 lines (393 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import io
import random
from unittest.mock import MagicMock, patch
from urllib.parse import urljoin
import pytest
from bs4 import BeautifulSoup, Tag
from click.testing import CliRunner
from nyc_dot_bot import (
BlueskyPoster,
CacheData,
LocalCache,
MastodonPoster,
S3Cache,
TooManyNewPDFsException,
TwitterPoster,
_default_s3_path,
_make_poster,
cli,
find_new_links,
format_link_for_post,
get_html,
get_pdf_links,
is_non_english,
make_cache,
parse_s3_path,
post_new_links,
run,
)
def make_link(href: str, text: str) -> Tag:
"""Build a bs4 <a> Tag for testing."""
html = f'<a href="{href}">{text}</a>'
tag = BeautifulSoup(html, "html.parser").a
assert tag is not None
return tag
# --- parse_s3_path ---
def test_parse_s3_path_basic():
assert parse_s3_path("s3://my-bucket/my-key.json") == ("my-bucket", "my-key.json")
def test_parse_s3_path_nested_key():
assert parse_s3_path("s3://bucket/path/to/key.json") == ("bucket", "path/to/key.json")
def test_parse_s3_path_no_key():
assert parse_s3_path("s3://bucket/") == ("bucket", "")
# --- make_cache ---
def test_make_cache_returns_local_for_file_path():
cache = make_cache("/tmp/cache.json")
assert isinstance(cache, LocalCache)
assert cache.path == "/tmp/cache.json"
@patch("nyc_dot_bot.boto3")
def test_make_cache_returns_s3_for_s3_url(mock_boto3):
cache = make_cache("s3://my-bucket/cache.json")
assert isinstance(cache, S3Cache)
assert cache.bucket == "my-bucket"
assert cache.key == "cache.json"
# --- LocalCache ---
def test_local_cache_read_write(tmp_path):
path = str(tmp_path / "cache.json")
data = CacheData(links={"https://example.com/a.pdf": "Project A"})
cache = LocalCache(path)
cache.write(data)
result = cache.read()
assert result == data
def test_local_cache_read_missing_file(tmp_path):
cache = LocalCache(str(tmp_path / "missing.json"))
with pytest.raises(FileNotFoundError):
cache.read()
# --- get_pdf_links ---
def test_get_pdf_links_filters_pdfs():
html = """
<html><body>
<div class="view-content">
<a href="/doc/a.pdf">PDF A</a>
<a href="/doc/b.html">HTML B</a>
<a href="/doc/c.pdf">PDF C</a>
</div>
</body></html>
"""
response = MagicMock()
response.text = html
links = get_pdf_links(response)
assert len(links) == 2
assert links[0].text == "PDF A"
assert links[1].text == "PDF C"
def test_get_pdf_links_empty():
html = '<html><body><div class="view-content"></div></body></html>'
response = MagicMock()
response.text = html
assert get_pdf_links(response) == []
# --- find_new_links ---
def test_find_new_links_returns_only_new():
cached = CacheData(links={"https://www1.nyc.gov/doc/a.pdf": "A"})
current = [make_link("/doc/a.pdf", "A"), make_link("/doc/b.pdf", "B")]
result = find_new_links(cached, current)
assert len(result) == 1
assert result[0]["href"] == "https://www1.nyc.gov/doc/b.pdf"
def test_find_new_links_all_cached():
cached = CacheData(
links={
"https://www1.nyc.gov/doc/a.pdf": "A",
"https://www1.nyc.gov/doc/b.pdf": "B",
}
)
current = [make_link("/doc/a.pdf", "A"), make_link("/doc/b.pdf", "B")]
assert find_new_links(cached, current) == []
def test_find_new_links_resolves_relative_urls():
cached = CacheData()
current = [make_link("/doc/relative.pdf", "Relative")]
result = find_new_links(cached, current)
assert result[0]["href"] == "https://www1.nyc.gov/doc/relative.pdf"
def test_find_new_links_too_many_raises():
cached = CacheData()
current = [make_link(f"/doc/{i}.pdf", f"Link {i}") for i in range(16)]
with pytest.raises(TooManyNewPDFsException):
find_new_links(cached, current)
# --- is_non_english ---
@pytest.mark.parametrize(
"title",
[
"Project - presented in June 2021 (Spanish pdf)",
"Project - presented in June 2021 (Spanish)",
"Project - presented in June 2021 (Simplified Chinese pdf)",
"Project - presented in June 2021 (Traditional Chinese)",
"Project - presented in June 2021 (Chinese Simplified)",
"Project - presented in June 2021 (French)",
"Project - presented in June 2021 (Greek pdf)",
"Project - presented in June 2021 (Hebrew)",
"Project - presented in June 2021 (Korean)",
"Project - presented in April 2023 (Arabic)",
"Project - presented in April 2023 (Chinese)",
],
)
def test_is_non_english_positive(title):
assert is_non_english(title) is True
@pytest.mark.parametrize(
"title",
[
"Flatbush Avenue - presented to Brooklyn Community Board 14 in January 2023",
"31st Avenue - Street Design Checklist",
"Meeker Avenue - What's Happening Here Flyer",
"Brooklyn Waterfront Greenway Presentation",
"Flyer in English, Simplified Chinese, Spanish & Traditional Chinese",
],
)
def test_is_non_english_negative(title):
assert is_non_english(title) is False
# --- format_link_for_post ---
def test_format_link_for_post_short():
link = make_link("https://example.com/a.pdf", "Project A (pdf)")
result = format_link_for_post(link)
assert result == "Project A https://example.com/a.pdf"
def test_format_link_for_post_normalizes_whitespace():
link = make_link(
"https://example.com/a.pdf",
"Courtlandt\r\n Avenue, Park Avenue, and Morris Avenue"
" - presented to Bronx Community Board 1\r\n in December 2025 (pdf)",
)
result = format_link_for_post(link)
assert result == (
"Courtlandt Avenue, Park Avenue, and Morris Avenue"
" - presented to Bronx Community Board 1 in December 2025 https://example.com/a.pdf"
)
def test_format_link_for_post_truncates_long_text():
long_text = "A" * 300
link = make_link("https://example.com/a.pdf", long_text)
result = format_link_for_post(link)
text_part = result.split(" https://")[0]
# 280 - 23 (link) - 1 (space) = 256 max, truncated with ...
assert len(text_part) == 256
assert text_part.endswith("...")
# --- _default_s3_path ---
def test_default_s3_path_uses_env(monkeypatch):
monkeypatch.setenv("BUCKET_NAME", "my-bucket")
assert _default_s3_path() == "s3://my-bucket/cache.json"
def test_default_s3_path_fallback(monkeypatch):
monkeypatch.delenv("BUCKET_NAME", raising=False)
assert _default_s3_path() == "s3://nyc-dot-current-projects-bot-mastodon-staging/cache.json"
# --- _make_poster ---
@patch("nyc_dot_bot.tweepy")
def test_make_poster_twitter(mock_tweepy, monkeypatch):
monkeypatch.setenv("TWITTER_CONSUMER_KEY", "key")
monkeypatch.delenv("BLUESKY_USERNAME", raising=False)
assert isinstance(_make_poster(), TwitterPoster)
@patch("nyc_dot_bot.Client")
def test_make_poster_bluesky(mock_client, monkeypatch):
monkeypatch.delenv("TWITTER_CONSUMER_KEY", raising=False)
monkeypatch.setenv("BLUESKY_USERNAME", "user")
monkeypatch.setenv("BLUESKY_APP_PASSWORD", "pass")
assert isinstance(_make_poster(), BlueskyPoster)
@patch("nyc_dot_bot.Mastodon")
def test_make_poster_mastodon(mock_mastodon, monkeypatch):
monkeypatch.delenv("TWITTER_CONSUMER_KEY", raising=False)
monkeypatch.delenv("BLUESKY_USERNAME", raising=False)
monkeypatch.setenv("MASTODON_ACCESS_TOKEN", "token")
assert isinstance(_make_poster(), MastodonPoster)
def test_make_poster_no_credentials_raises(monkeypatch):
monkeypatch.delenv("TWITTER_CONSUMER_KEY", raising=False)
monkeypatch.delenv("BLUESKY_USERNAME", raising=False)
monkeypatch.delenv("MASTODON_ACCESS_TOKEN", raising=False)
with pytest.raises(ValueError, match="No platform credentials configured"):
_make_poster()
# --- post_new_links ---
@patch("nyc_dot_bot.convert_pdf_to_image")
@patch("nyc_dot_bot.get_pdf")
def test_post_new_links_no_poster_prints(mock_get_pdf, mock_convert, capsys):
link = make_link("https://example.com/a.pdf", "Project A (pdf)")
mock_get_pdf.return_value = b"pdf"
mock_convert.return_value = io.BytesIO(b"jpegdata")
result = post_new_links([link])
assert result == {"https://example.com/a.pdf": "Project A (pdf)"}
assert 'Would have posted: "Project A https://example.com/a.pdf"' in capsys.readouterr().out
@patch("nyc_dot_bot.convert_pdf_to_image")
@patch("nyc_dot_bot.get_pdf")
def test_post_new_links_with_poster_calls_post(mock_get_pdf, mock_convert):
link = make_link("https://example.com/a.pdf", "Project A")
mock_get_pdf.return_value = b"pdf"
mock_convert.return_value = io.BytesIO(b"jpegdata")
poster = MagicMock()
result = post_new_links([link], poster)
assert result == {"https://example.com/a.pdf": "Project A"}
poster.post.assert_called_once()
posted_link, posted_title, posted_image = poster.post.call_args[0]
assert posted_link["href"] == "https://example.com/a.pdf"
assert posted_title == "Project A"
assert posted_image == b"jpegdata"
@patch("nyc_dot_bot.convert_pdf_to_image")
@patch("nyc_dot_bot.get_pdf")
def test_post_new_links_skips_non_english(mock_get_pdf, mock_convert):
english = make_link("https://example.com/a.pdf", "Project A (pdf)")
spanish = make_link("https://example.com/a-spanish.pdf", "Project A (Spanish pdf)")
mock_get_pdf.return_value = b"pdf"
mock_convert.return_value = io.BytesIO(b"jpegdata")
poster = MagicMock()
result = post_new_links([english, spanish], poster)
# Both should be in the result (cached)
assert "https://example.com/a.pdf" in result
assert "https://example.com/a-spanish.pdf" in result
# But only the English one should have been posted
poster.post.assert_called_once()
posted_link = poster.post.call_args[0][0]
assert posted_link["href"] == "https://example.com/a.pdf"
@patch("nyc_dot_bot.convert_pdf_to_image")
@patch("nyc_dot_bot.get_pdf")
def test_post_new_links_continues_after_failure(mock_get_pdf, mock_convert):
link1 = make_link("https://example.com/a.pdf", "A")
link2 = make_link("https://example.com/b.pdf", "B")
mock_get_pdf.return_value = b"pdf"
mock_convert.return_value = io.BytesIO(b"jpegdata")
poster = MagicMock()
poster.post.side_effect = [RuntimeError("fail"), None]
result = post_new_links([link1, link2], poster)
assert result == {"https://example.com/b.pdf": "B"}
assert poster.post.call_count == 2
# --- run ---
@patch("nyc_dot_bot.get_html")
@patch("nyc_dot_bot.get_pdf_links")
def test_run_no_new_links(mock_get_pdf_links, mock_get_html, tmp_path):
cache_path = str(tmp_path / "cache.json")
initial = CacheData(links={"https://www1.nyc.gov/doc/a.pdf": "A"})
LocalCache(cache_path).write(initial)
link = make_link("/doc/a.pdf", "A")
mock_get_pdf_links.return_value = [link]
run(cache_path, dry_run=True)
# Cache should be unchanged
assert LocalCache(cache_path).read() == initial
@patch("nyc_dot_bot.post_new_links")
@patch("nyc_dot_bot.get_html")
@patch("nyc_dot_bot.get_pdf_links")
def test_run_dry_run_does_not_write_cache(mock_get_pdf_links, mock_get_html, mock_post, tmp_path):
cache_path = str(tmp_path / "cache.json")
initial = CacheData()
LocalCache(cache_path).write(initial)
link = make_link("/doc/new.pdf", "New")
mock_get_pdf_links.return_value = [link]
mock_post.return_value = {"https://www1.nyc.gov/doc/new.pdf": "New"}
run(cache_path, dry_run=True)
assert LocalCache(cache_path).read() == initial
@patch("nyc_dot_bot._make_poster")
@patch("nyc_dot_bot.post_new_links")
@patch("nyc_dot_bot.get_html")
@patch("nyc_dot_bot.get_pdf_links")
def test_run_writes_successes_to_cache(mock_get_pdf_links, mock_get_html, mock_post, mock_make_poster, tmp_path):
cache_path = str(tmp_path / "cache.json")
LocalCache(cache_path).write(CacheData())
link = make_link("/doc/new.pdf", "New")
mock_get_pdf_links.return_value = [link]
mock_post.return_value = {"https://www1.nyc.gov/doc/new.pdf": "New"}
run(cache_path)
result = LocalCache(cache_path).read()
assert result == CacheData(links={"https://www1.nyc.gov/doc/new.pdf": "New"})
@patch("nyc_dot_bot._make_poster")
@patch("nyc_dot_bot.post_new_links")
@patch("nyc_dot_bot.get_html")
@patch("nyc_dot_bot.get_pdf_links")
def test_run_merges_new_links_with_existing_cache(mock_get_pdf_links, mock_get_html, mock_post, mock_make_poster, tmp_path):
cache_path = str(tmp_path / "cache.json")
LocalCache(cache_path).write(CacheData(links={"https://www1.nyc.gov/doc/old.pdf": "Old"}))
mock_get_pdf_links.return_value = [make_link("/doc/old.pdf", "Old"), make_link("/doc/new.pdf", "New")]
mock_post.return_value = {"https://www1.nyc.gov/doc/new.pdf": "New"}
run(cache_path)
result = LocalCache(cache_path).read()
assert result == CacheData(
links={
"https://www1.nyc.gov/doc/old.pdf": "Old",
"https://www1.nyc.gov/doc/new.pdf": "New",
}
)
@patch("nyc_dot_bot.post_new_links")
@patch("nyc_dot_bot.get_html")
@patch("nyc_dot_bot.get_pdf_links")
def test_run_no_post_writes_cache(mock_get_pdf_links, mock_get_html, mock_post, tmp_path):
cache_path = str(tmp_path / "cache.json")
LocalCache(cache_path).write(CacheData())
mock_get_pdf_links.return_value = [make_link("/doc/new.pdf", "New")]
mock_post.return_value = {"https://www1.nyc.gov/doc/new.pdf": "New"}
run(cache_path, no_post=True)
result = LocalCache(cache_path).read()
assert result == CacheData(links={"https://www1.nyc.gov/doc/new.pdf": "New"})
# --- cli ---
@patch("nyc_dot_bot.load_dotenv")
@patch("nyc_dot_bot.run")
def test_cli_post_with_cache_flag(mock_run, _mock_dotenv):
runner = CliRunner()
result = runner.invoke(cli, ["post", "--cache", "/tmp/test.json", "--dry-run"])
assert result.exit_code == 0
mock_run.assert_called_once_with("/tmp/test.json", dry_run=True, no_post=False)
@patch("nyc_dot_bot.load_dotenv")
@patch("nyc_dot_bot.run")
def test_cli_post_defaults_to_s3(mock_run, _mock_dotenv, monkeypatch):
monkeypatch.delenv("BUCKET_NAME", raising=False)
runner = CliRunner()
result = runner.invoke(cli, ["post", "--dry-run"])
assert result.exit_code == 0
mock_run.assert_called_once_with(
"s3://nyc-dot-current-projects-bot-mastodon-staging/cache.json",
dry_run=True,
no_post=False,
)
@patch("nyc_dot_bot.get_html")
@patch("nyc_dot_bot.get_pdf_links")
def test_cli_prune_dry_run(mock_get_pdf_links, mock_get_html, tmp_path):
cache_path = str(tmp_path / "cache.json")
initial = CacheData(
links={
"https://www1.nyc.gov/doc/a.pdf": "A",
"https://www1.nyc.gov/doc/b.pdf": "B",
}
)
LocalCache(cache_path).write(initial)
# Only "a.pdf" is still on the page
mock_get_pdf_links.return_value = [make_link("/doc/a.pdf", "A")]
runner = CliRunner()
result = runner.invoke(cli, ["prune", "--cache", cache_path, "--dry-run"])
assert result.exit_code == 0
assert "Removing: B" in result.output
# Cache should be unchanged in dry-run
assert LocalCache(cache_path).read() == initial
@patch("nyc_dot_bot.get_html")
@patch("nyc_dot_bot.get_pdf_links")
def test_cli_prune_removes_stale(mock_get_pdf_links, mock_get_html, tmp_path):
cache_path = str(tmp_path / "cache.json")
LocalCache(cache_path).write(
CacheData(
links={
"https://www1.nyc.gov/doc/a.pdf": "A",
"https://www1.nyc.gov/doc/b.pdf": "B",
}
)
)
mock_get_pdf_links.return_value = [make_link("/doc/a.pdf", "A")]
runner = CliRunner()
result = runner.invoke(cli, ["prune", "--cache", cache_path])
assert result.exit_code == 0
result_cache = LocalCache(cache_path).read()
assert result_cache == CacheData(links={"https://www1.nyc.gov/doc/a.pdf": "A"})
# --- integration ---
@pytest.mark.integration
def test_detects_removed_link_from_cache(tmp_path):
html = get_html()
all_links = get_pdf_links(html)
assert len(all_links) > 0, "Expected at least one PDF link on the page"
# Build a full cache from all current links
cached = CacheData()
for link in all_links:
resolved = urljoin("https://www1.nyc.gov/html/dot/html/about/current-projects.shtml", str(link["href"]))
cached.links[resolved] = link.text
# Remove one link at random
removed_url = random.choice(list(cached.links.keys()))
cached.links.pop(removed_url)
# Re-fetch and find new links against the modified cache
html = get_html()
new_links = find_new_links(cached, get_pdf_links(html))
new_urls = [link["href"] for link in new_links]
assert removed_url in new_urls
# All new links should point to the one URL we removed
assert all(url == removed_url for url in new_urls)