-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathtest_link_metadata.py
More file actions
652 lines (583 loc) · 20.2 KB
/
Copy pathtest_link_metadata.py
File metadata and controls
652 lines (583 loc) · 20.2 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
"""Tests for compact LinkedIn reference extraction helpers."""
from urllib.parse import quote
from linkedin_mcp_server.scraping.link_metadata import (
RawReference,
build_references,
classify_link,
dedupe_references,
normalize_url,
)
class TestBuildReferences:
def test_canonicalizes_and_types_linkedin_urls(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/in/williamhgates?miniProfileUrn=123",
"text": "Bill Gates",
"heading": "Featured",
},
{
"href": "https://www.linkedin.com/company/gates-foundation/posts/",
"text": "Gates Foundation",
"heading": "Experience",
},
{
"href": "https://www.linkedin.com/pulse/phone-call-saves-lives-bill-gates-yspvc?trackingId=123",
"text": "A phone call that saves lives",
},
],
"main_profile",
)
assert references == [
{
"kind": "person",
"url": "/in/williamhgates/",
"text": "Bill Gates",
"context": "featured",
},
{
"kind": "company",
"url": "/company/gates-foundation/",
"text": "Gates Foundation",
"context": "experience",
},
{
"kind": "article",
"url": "/pulse/phone-call-saves-lives-bill-gates-yspvc/",
"text": "A phone call that saves lives",
"context": "top card",
},
]
def test_preserves_person_slug_named_details(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/in/details/",
"text": "Details Person",
}
],
"main_profile",
)
assert references == [
{
"kind": "person",
"url": "/in/details/",
"text": "Details Person",
"context": "top card",
}
]
def test_drops_person_details_subpage(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/in/williamhgates/details/experience/",
"text": "Bill Gates",
}
],
"main_profile",
)
assert references == []
def test_unwraps_redirect_and_drops_junk(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/redir/redirect/?url=https%3A%2F%2Fgatesnot.es%2Ftgn&urlhash=abc",
"text": "Gates Notes",
},
{
"href": "blob:https://www.linkedin.com/123",
"text": "Video",
},
{
"href": "#caret-small",
"text": "",
},
{
"href": "https://www.linkedin.com/help/linkedin/",
"text": "Questions?",
},
],
"posts",
)
assert references == [
{
"kind": "external",
"url": "https://gatesnot.es/tgn",
"text": "Gates Notes",
"context": "post attachment",
}
]
def test_drops_non_http_external_schemes(self):
references = build_references(
[
{
"href": "data:text/html,<p>hello</p>",
"text": "Inline payload",
},
{
"href": "ftp://example.com/report.csv",
"text": "FTP report",
},
{
"href": "https://example.com/report.csv",
"text": "HTTPS report",
},
],
"posts",
)
assert references == [
{
"kind": "external",
"url": "https://example.com/report.csv",
"text": "HTTPS report",
"context": "post attachment",
}
]
def test_dedupes_external_tracking_variants(self):
references = build_references(
[
{
"href": "https://example.com/report?utm_source=linkedin",
"text": "Report",
},
{
"href": "https://example.com/report?utm_source=share",
"text": "Detailed annual report",
},
],
"posts",
)
assert references == [
{
"kind": "external",
"url": "https://example.com/report",
"text": "Detailed annual report",
"context": "post attachment",
}
]
def test_prefers_cleaner_duplicate_label(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/newsletters/gates-notes-123/",
"text": "View my newsletter",
"aria_label": "Gates Notes",
},
{
"href": "https://www.linkedin.com/newsletters/gates-notes-123/",
"text": "Gates Notes Gates Notes",
},
],
"posts",
)
assert references == [
{
"kind": "newsletter",
"url": "/newsletters/gates-notes-123/",
"text": "Gates Notes",
"context": "post attachment",
}
]
def test_normalize_url_unwraps_nested_redirects_within_cap(self):
target = "https://example.com/report"
nested = "https://www.linkedin.com/redir/redirect/?url=" + quote(
"https://www.linkedin.com/redir/redirect/?url=" + quote(target, safe=""),
safe="",
)
assert normalize_url(nested) == target
def test_normalize_url_drops_redirect_chain_beyond_cap(self):
target = "https://example.com/report"
href = target
for _ in range(7):
href = "https://www.linkedin.com/redir/redirect/?url=" + quote(
href, safe=""
)
assert normalize_url(href) is None
def test_prefers_shorter_clean_label_over_merged_visible_text(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/pulse/test-post?trackingId=123",
"text": "Gates Notes Gates Notes A phone call that saves lives Bill Gates",
"aria_label": "Open article: A phone call that saves lives by Bill Gates • 3 min read",
}
],
"posts",
)
assert references == [
{
"kind": "article",
"url": "/pulse/test-post/",
"text": "A phone call that saves lives",
"context": "post attachment",
}
]
def test_rejects_single_character_labels(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/in/williamhgates/",
"text": "1",
"aria_label": "Bill Gates",
}
],
"main_profile",
)
assert references == [
{
"kind": "person",
"url": "/in/williamhgates/",
"text": "Bill Gates",
"context": "top card",
}
]
def test_preserves_words_starting_with_view(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/company/viewpoint-economics/",
"text": "Viewpoint Economics",
}
],
"about",
)
assert references == [
{
"kind": "company",
"url": "/company/viewpoint-economics/",
"text": "Viewpoint Economics",
"context": "top card",
}
]
def test_prefers_company_post_context_for_feed_posts(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/feed/update/urn:li:activity:123/",
"text": "Original company post",
"in_article": True,
}
],
"posts",
)
assert references == [
{
"kind": "feed_post",
"url": "/feed/update/urn:li:activity:123/",
"text": "Original company post",
"context": "company post",
}
]
def test_drops_social_proof_company_labels(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/company/gates-foundation/",
"text": "Falguni & 8 other connections follow this page",
},
{
"href": "https://www.linkedin.com/company/gates-foundation/",
"text": "Gates Foundation",
},
],
"about",
)
assert references == [
{
"kind": "company",
"url": "/company/gates-foundation/",
"text": "Gates Foundation",
"context": "top card",
}
]
def test_drops_nav_and_footer_anchors(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/in/williamhgates/",
"text": "Bill Gates",
"in_nav": True,
},
{
"href": "https://www.linkedin.com/company/gates-foundation/",
"text": "Gates Foundation",
"in_footer": True,
},
],
"main_profile",
)
assert references == []
def test_caps_results_per_section(self):
raw: list[RawReference] = [
{
"href": f"https://www.linkedin.com/company/test-{idx}/",
"text": f"Company {idx}",
}
for idx in range(20)
]
references = build_references(raw, "about")
assert len(references) == 12
assert references[0]["url"] == "/company/test-0/"
assert references[-1]["url"] == "/company/test-11/"
def test_caps_jobs_section_more_tightly(self):
raw: list[RawReference] = [
{
"href": f"https://www.linkedin.com/jobs/view/{idx}/",
"text": f"Job {idx}",
}
for idx in range(20)
]
references = build_references(raw, "jobs")
assert len(references) == 8
assert references[0]["url"] == "/jobs/view/0/"
assert references[-1]["url"] == "/jobs/view/7/"
def test_uses_default_cap_for_unknown_section(self):
raw: list[RawReference] = [
{
"href": f"https://www.linkedin.com/company/test-{idx}/",
"text": f"Company {idx}",
}
for idx in range(20)
]
references = build_references(raw, "unknown_section")
assert len(references) == 12
def test_prefers_richer_duplicate_text(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/jobs/view/12345/",
"text": "Job",
},
{
"href": "https://www.linkedin.com/jobs/view/12345/",
"text": "Senior Software Engineer",
},
],
"search_results",
)
assert references == [
{
"kind": "job",
"url": "/jobs/view/12345/",
"text": "Senior Software Engineer",
"context": "job result",
}
]
def test_uses_search_result_contexts(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/jobs/view/12345/",
"text": "Senior Engineer",
},
{
"href": "https://www.linkedin.com/in/stickerdaniel/",
"text": "Daniel Sticker",
},
],
"search_results",
)
assert references == [
{
"kind": "job",
"url": "/jobs/view/12345/",
"text": "Senior Engineer",
"context": "job result",
},
{
"kind": "person",
"url": "/in/stickerdaniel/",
"text": "Daniel Sticker",
"context": "search result",
},
]
def test_uses_job_posting_context_for_job_pages(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/company/acme/",
"text": "Acme",
}
],
"job_posting",
)
assert references == [
{
"kind": "company",
"url": "/company/acme/",
"text": "Acme",
"context": "job posting",
}
]
def test_does_not_treat_lookalike_domains_as_linkedin(self):
references = build_references(
[
{
"href": "https://www.notlinkedin.com/company/fake/about/",
"text": "Fake Company",
}
],
"about",
)
assert references == [
{
"kind": "external",
"url": "https://www.notlinkedin.com/company/fake/about/",
"text": "Fake Company",
"context": "top card",
}
]
def test_keeps_company_about_routes(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/company/legalzoom/about/",
"text": "LegalZoom",
}
],
"about",
)
assert references == [
{
"kind": "company",
"url": "/company/legalzoom/",
"text": "LegalZoom",
"context": "top card",
}
]
def test_cross_page_dedupe_keeps_better_reference(self):
references = dedupe_references(
[
{
"kind": "job",
"url": "/jobs/view/123/",
"text": "Job",
},
{
"kind": "job",
"url": "/jobs/view/123/",
"text": "Senior Software Engineer",
"context": "job result",
},
]
)
assert references == [
{
"kind": "job",
"url": "/jobs/view/123/",
"text": "Senior Software Engineer",
"context": "job result",
}
]
class TestClassifyLink:
def test_messaging_thread_url(self):
result = classify_link(
"https://www.linkedin.com/messaging/thread/2-NjAwMDAyMDEtZWVh/"
)
assert result == (
"conversation",
"/messaging/thread/2-NjAwMDAyMDEtZWVh/",
)
def test_messaging_thread_url_with_query(self):
result = classify_link(
"https://www.linkedin.com/messaging/thread/2-abc123/?focusedMsgUrn=xyz"
)
assert result == ("conversation", "/messaging/thread/2-abc123/")
def test_inbox_references_include_threads(self):
references = build_references(
[
{
"href": "https://www.linkedin.com/messaging/thread/2-abc123/",
"text": "Tony Chan",
},
{
"href": "https://www.linkedin.com/messaging/thread/2-def456/",
"text": "Paul Jasper",
},
],
"inbox",
)
assert len(references) == 2
assert references[0]["kind"] == "conversation"
assert references[0]["url"] == "/messaging/thread/2-abc123/"
assert references[0]["text"] == "Tony Chan"
assert references[0]["context"] == "inbox"
assert references[1]["kind"] == "conversation"
assert references[1]["url"] == "/messaging/thread/2-def456/"
assert references[1]["text"] == "Paul Jasper"
def test_inbox_conversation_without_text_still_captured(self):
"""Conversation references are kept even without a usable text label."""
references = build_references(
[
{
"href": "https://www.linkedin.com/messaging/thread/2-xyz/",
"text": "",
},
],
"inbox",
)
assert len(references) == 1
assert references[0]["kind"] == "conversation"
assert references[0]["url"] == "/messaging/thread/2-xyz/"
class TestClassifyShareActivityPermalink:
"""Share-style /posts/<slug>-activity-<id>-<sig>/ canonicalize to feed_post."""
_CANONICAL = "/feed/update/urn:li:activity:7000000000000000000/"
def test_share_permalink_classifies_as_feed_post(self):
result = classify_link(
"https://www.linkedin.com/posts/"
"someone-12345_topic-thoughts-activity-7000000000000000000-AbCd/"
)
assert result == ("feed_post", self._CANONICAL)
def test_share_permalink_with_query_string(self):
result = classify_link(
"https://www.linkedin.com/posts/"
"alice-bob_topic-activity-7000000000000000000-XYZ/"
"?utm_source=share"
)
assert result == ("feed_post", self._CANONICAL)
def test_share_permalink_minimal_slug(self):
"""A bare slug (no underscores or hashtags) still matches."""
result = classify_link(
"https://www.linkedin.com/posts/foo-activity-7000000000000000000-XYZ/"
)
assert result == ("feed_post", self._CANONICAL)
def test_short_id_rejected(self):
"""IDs below the 15-digit floor are not treated as activity ids."""
result = classify_link("https://www.linkedin.com/posts/foo-activity-12345-XYZ/")
assert result is None
def test_signature_required(self):
"""The trailing -<sig> segment is required to match."""
# The URL ends after the digits (no trailing -<sig>) — should not
# match the share-permalink shape.
result = classify_link(
"https://www.linkedin.com/posts/foo-activity-7000000000000000000/"
)
assert result is None
def test_unrelated_posts_url_rejected(self):
"""A /posts/<slug>/ without -activity-<id>- doesn't match."""
result = classify_link("https://www.linkedin.com/posts/some-article-headline/")
assert result is None
def test_share_and_canonical_dedupe(self):
"""Anchor with share permalink + anchor with /feed/update/ collapse to one."""
references = build_references(
[
{
"href": (
"https://www.linkedin.com/posts/"
"alice_topic-activity-7000000000000000000-XYZ/"
),
"text": "Alice's post",
},
{
"href": (
"https://www.linkedin.com/feed/update/"
"urn:li:activity:7000000000000000000/"
),
"text": "",
},
],
"posts",
)
assert len(references) == 1
assert references[0]["kind"] == "feed_post"
assert references[0]["url"] == self._CANONICAL