返回 last30days-skill
test_patents_selection.py
根目录 / tests / test_patents_selection.py
1 """What the patent lane makes of the lists: groups, counts, entries, index."""
2
3 from __future__ import annotations
4
5 import copy
6 from datetime import date
7
8 from lib import render
9 from tests.test_patents_client import (
10 DETAILS,
11 PDF,
12 FakeUSPTO,
13 guid_of,
14 make_row,
15 recorded,
16 run,
17 )
18 from tests.test_render_patents import _report
19
20 ARTIFACT_KEYS = {
21 "company", "company_via_person", "window", "groups", "other_names", "counts",
22 "entries", "index", "partial", "status",
23 }
24 COUNT_KEYS = {
25 "applications", "grants", "name_matched_applications", "name_matched_grants",
26 "first_publications", "name_matched_first_publications", "continuing",
27 "previously_published_grants", "designs", "other_kinds", "floor",
28 }
29 ENTRY_KEYS = [
30 "guid", "number", "title", "url", "kind", "kind_code", "published", "filed",
31 "earliest_priority", "cpc", "filed_as", "group_kind", "applicant_location",
32 "abstract", "first_claim",
33 ]
34 INDEX_KEYS = ["title", "url", "kind", "published", "cpc", "filed_as", "group_kind"]
35
36 # Filed 2026-01-15 (make_row's default) with a parent more than 13 months older.
37 CONTINUING = {"related": ["2023-02-01"]}
38
39
40 def numbers(items: list[dict]) -> list[str]:
41 return [guid_of(item["url"]) for item in items]
42
43
44 # --- Apple, as recorded -----------------------------------------------------
45
46
47 def apple_lists() -> dict[str, list[dict]]:
48 """The recorded Apple lists plus the stranger the full live list held."""
49 stranger = make_row(
50 "US-20260300001-A1", "2026-10-20", name="Deep Apple Therapeutics, Inc.", cpc="A61K31/00",
51 )
52 return {
53 "US-PGPUB": recorded("Apple", "US-PGPUB") + [stranger],
54 "USPAT": recorded("Apple", "USPAT"),
55 }
56
57
58 def test_apple_artifact_has_the_agreed_shape(monkeypatch):
59 FakeUSPTO(lists=apple_lists()).install(monkeypatch)
60
61 artifact = run({"company": "Apple Inc.", "company_via_person": 1})["artifact"]
62
63 assert set(artifact) == ARTIFACT_KEYS
64 assert artifact["company"] == "Apple Inc."
65 assert artifact["company_via_person"] is True
66 assert artifact["window"] == {"from": "2026-10-14", "to": "2026-11-13"}
67 assert set(artifact["counts"]) == COUNT_KEYS
68 assert artifact["partial"] is None
69 assert artifact["status"] == "digest"
70 assert len(artifact["entries"]) == 12
71 assert len(artifact["index"]) == 30
72 for entry in artifact["entries"]:
73 assert list(entry) == ENTRY_KEYS
74 for item in artifact["index"]:
75 assert list(item) == INDEX_KEYS
76 assert isinstance(item["cpc"], str)
77
78
79 def test_apple_spellings_are_one_confirmed_group_and_the_stranger_is_another_name(monkeypatch):
80 FakeUSPTO(lists=apple_lists()).install(monkeypatch)
81
82 artifact = run()["artifact"]
83
84 assert artifact["groups"] == [
85 {"name": "Apple Inc.", "kind": "confirmed", "applications": 88, "grants": 88},
86 ]
87 assert artifact["other_names"] == [{"name": "Deep Apple Therapeutics, Inc.", "count": 1}]
88 assert artifact["counts"]["applications"] == 88
89 assert artifact["counts"]["grants"] == 88
90 assert artifact["counts"]["name_matched_applications"] == 0
91 shown = artifact["entries"] + artifact["index"]
92 assert {item["filed_as"] for item in shown} == {"Apple Inc."}
93 assert "20260300001" not in numbers(shown)
94
95
96 def test_apple_continuing_applications_are_counted_and_never_shown(monkeypatch):
97 lists = apple_lists()
98 FakeUSPTO(lists=lists).install(monkeypatch)
99
100 artifact = run()["artifact"]
101
102 counts = artifact["counts"]
103 # 88 applications: 61 first publications and 27 continuing. 88 grants:
104 # 68 B2 and one continuing B1, 13 designs, 6 B1 with no earlier publication.
105 assert counts["continuing"] == 27
106 assert counts["previously_published_grants"] == 69
107 assert counts["designs"] == 13
108 assert counts["other_kinds"] == 0
109 assert counts["first_publications"] == 67
110 assert counts["floor"] is False
111 continuing = {
112 row["publicationReferenceDocumentNumber"]
113 for row in lists["US-PGPUB"]
114 if row["relatedApplFilingDate"]
115 and (
116 date.fromisoformat(row["applicationFilingDate"][0][:10])
117 - date.fromisoformat(min(row["relatedApplFilingDate"])[:10])
118 ).days > 396
119 }
120 assert len(continuing) == 27
121 shown = set(numbers(artifact["entries"] + artifact["index"]))
122 assert not shown & continuing
123 # Recorded documents USPTO's own continuity data marks as continuations.
124 assert not shown & {"20260349259", "20260338957", "20260342141", "20260322621"}
125
126
127 def test_an_entry_carries_the_recorded_text(monkeypatch):
128 FakeUSPTO(lists=apple_lists()).install(monkeypatch)
129
130 entry = run()["artifact"]["entries"][0]
131
132 assert entry["guid"] == "US-20260359001-A1"
133 assert entry["number"] == "20260359001"
134 assert entry["title"] == "SPEECH PREDICTION BASED ON MOTION"
135 assert entry["url"] == PDF + "20260359001"
136 assert (entry["kind"], entry["kind_code"]) == ("application", "A1")
137 assert entry["published"] == "2026-11-12"
138 assert entry["filed"] == "2026-04-23"
139 assert entry["earliest_priority"] == "2025-05-12"
140 assert entry["cpc"] == ["G06F3/165", "G06F3/012", "G06F3/013", "G06F3/017", "G06F3/167"]
141 assert (entry["filed_as"], entry["group_kind"]) == ("Apple Inc.", "confirmed")
142 assert entry["applicant_location"] == "Cupertino, CA, US"
143 assert entry["abstract"].startswith("Some examples of the disclosure are directed to")
144 assert len(entry["abstract"]) <= 1000
145 assert entry["first_claim"].startswith("1. A method comprising: at a first computer system")
146 assert "an intention of the user to speak" in entry["first_claim"]
147 assert len(entry["first_claim"]) <= 1200
148 assert "<" not in entry["abstract"] + entry["first_claim"]
149
150
151 def test_text_is_capped_and_a_grant_reads_the_assignee_location(monkeypatch):
152 row = make_row("US-12790001-B1", "2026-11-10", kind="B1")
153 detail = {
154 "abstractHtml": "<p>An&nbsp;&amp; abstract</p>\n " + "word " * 280,
155 "claimsHtml": "<b>1</b>. A device " + "that works " * 200 + "<br /> <b>2</b>. Other.",
156 "applicantName": ["Somebody Else LLC", "Apple Inc."],
157 "applicantCity": ["Austin", "Cupertino"],
158 "assigneeName": ["Somebody Else LLC", "APPLE INC."],
159 "assigneeCity": ["Seoul", "Cupertino"],
160 "assigneeState": ["N/A", "CA"],
161 "assigneeCountry": ["KR", "US"],
162 "cpcInventive": [f"G06F3/0{n} 20130101" for n in range(8)],
163 "continuityData": ["us-provisional-application US 63000001 20250101\n<br />"],
164 }
165 FakeUSPTO(lists={"USPAT": [row]}, details={"US-12790001-B1": detail}).install(monkeypatch)
166
167 [entry] = run()["artifact"]["entries"]
168
169 assert entry["kind"] == "grant"
170 assert entry["abstract"].startswith("An & abstract word word")
171 # A 1,400-character abstract prints 1,000; a 2,000-character claim prints 1,200.
172 assert 995 <= len(entry["abstract"]) <= 1000
173 assert entry["first_claim"].startswith("1. A device that works")
174 assert 1190 <= len(entry["first_claim"]) <= 1200
175 assert entry["applicant_location"] == "Cupertino, CA, US"
176 assert len(entry["cpc"]) == 5
177 assert entry["cpc"][0] == "G06F3/00"
178
179
180 def test_a_row_without_its_own_number_takes_it_from_the_guid(monkeypatch):
181 rows = [
182 make_row("US-20260310001-A1", "2026-11-05"),
183 make_row("US-12790002-B1", "2026-11-03", kind="B1"),
184 ]
185 for row in rows:
186 row["publicationReferenceDocumentNumber"] = None
187 FakeUSPTO(lists={"US-PGPUB": rows[:1], "USPAT": rows[1:]}).install(monkeypatch)
188
189 entries = run()["artifact"]["entries"]
190
191 assert [(entry["number"], entry["url"]) for entry in entries] == [
192 ("20260310001", PDF + "20260310001"),
193 ("12790002", PDF + "12790002"),
194 ]
195
196
197 def test_earliest_priority_is_the_earliest_claimed_or_related_date(monkeypatch):
198 row = make_row("US-20260310002-A1", "2026-11-05", related=["2025-09-01"])
199 row["priorityClaimsDate"] = ["2025-06-30T00:00:00Z", "2025-08-01T00:00:00Z"]
200 bare = make_row("US-20260310003-A1", "2026-11-04")
201 bare["applicationFilingDate"] = None
202 FakeUSPTO(lists={"US-PGPUB": [row, bare]}).install(monkeypatch)
203
204 first, second = run()["artifact"]["entries"]
205
206 assert first["earliest_priority"] == "2025-06-30"
207 assert (second["filed"], second["earliest_priority"]) == (None, None)
208
209
210 # --- kinds and counts -------------------------------------------------------
211
212
213 def test_a_company_whose_only_filing_is_a_continuation_gets_counts_only(monkeypatch):
214 # Filed more than three years after its parent application.
215 [row] = [
216 row for row in recorded("Figure AI", "US-PGPUB") if row["guid"] == "US-20260326306-A1"
217 ]
218 fake = FakeUSPTO(lists={"US-PGPUB": [row]}).install(monkeypatch)
219
220 artifact = run({"company": "Figure AI"})["artifact"]
221
222 assert artifact["status"] == "counts-only"
223 assert artifact["entries"] == []
224 assert artifact["index"] == []
225 assert artifact["partial"] is None
226 assert artifact["counts"]["continuing"] == 1
227 assert artifact["counts"]["first_publications"] == 0
228 assert artifact["groups"] == [
229 {"name": "Figure AI Inc.", "kind": "confirmed", "applications": 1, "grants": 0},
230 ]
231 # Nothing to order and nothing to read: no baseline search, no detail call.
232 assert [call["kind"] for call in fake.calls] == ["session", "search", "search"]
233
234
235 def test_recorded_figure_ai_grants_are_all_previously_published(monkeypatch):
236 FakeUSPTO(lists={"USPAT": recorded("Figure AI", "USPAT")}).install(monkeypatch)
237
238 artifact = run({"company": "Figure AI"})["artifact"]
239
240 assert artifact["status"] == "counts-only"
241 assert artifact["counts"]["grants"] == 2
242 assert artifact["counts"]["previously_published_grants"] == 2
243
244
245 def test_each_kind_lands_in_its_bucket(monkeypatch):
246 applications = [
247 make_row("US-20260320001-A1", "2026-11-05"),
248 make_row("US-20260320002-A1", "2026-11-05", **CONTINUING),
249 make_row("US-20260320003-A9", "2026-11-05", kind="A9"),
250 # A parent 396 days before filing is not yet "more than 13 months".
251 make_row("US-20260320004-A1", "2026-11-04", related=["2024-12-15"]),
252 make_row("US-20260320005-A1", "2026-11-04", related=["2024-12-14"]),
253 ]
254 grants = [
255 make_row("US-12790011-B1", "2026-11-03", kind="B1"),
256 make_row("US-12790012-B1", "2026-11-03", kind="B1", **CONTINUING),
257 make_row("US-12790013-B2", "2026-11-03", kind="B2"),
258 make_row("US-D1150001-S", "2026-11-03", kind="S"),
259 make_row("US-RE50001-E", "2026-11-03", kind="E"),
260 ]
261 FakeUSPTO(lists={"US-PGPUB": applications, "USPAT": grants}).install(monkeypatch)
262
263 artifact = run()["artifact"]
264
265 counts = artifact["counts"]
266 assert counts["applications"] == 5
267 assert counts["grants"] == 5
268 assert counts["first_publications"] == 3
269 assert counts["continuing"] == 2
270 assert counts["previously_published_grants"] == 2
271 assert counts["designs"] == 1
272 assert counts["other_kinds"] == 2
273 assert [entry["guid"] for entry in artifact["entries"]] == [
274 "US-20260320001-A1", "US-20260320004-A1", "US-12790011-B1",
275 ]
276 assert artifact["index"] == []
277
278
279 def test_a_b1_grant_with_a_continuation_in_its_continuity_data_is_replaced(monkeypatch):
280 lists = apple_lists()
281 grant = next(row for row in lists["USPAT"] if row["guid"] == "US-12792808-B1")
282 grant["datePublished"] = "2026-11-13T00:00:00Z" # newest, so it is read first
283 detail = {
284 **copy.deepcopy(DETAILS["US-12786790-B2"]),
285 "guid": "US-12792808-B1",
286 "continuityData": [
287 "continuation parent-doc US 16904342 20200617 US 11500001 child-doc US 18000001\n<br />"
288 ],
289 }
290 fake = FakeUSPTO(lists=lists, details={"US-12792808-B1": detail}).install(monkeypatch)
291
292 artifact = run()["artifact"]
293
294 fetched = fake.detail_guids()
295 assert fetched[0] == "US-12792808-B1"
296 assert len(fetched) == 13
297 entries = [entry["guid"] for entry in artifact["entries"]]
298 assert entries == fetched[1:]
299 assert "12792808" not in numbers(artifact["index"])
300 assert artifact["partial"] is None
301 counts = artifact["counts"]
302 assert counts["first_publications"] == 66
303 # A continuing B1 is a grant of something already published, as at list level.
304 assert counts["previously_published_grants"] == 70
305 assert counts["continuing"] == 27
306
307
308 def test_every_continuity_form_demotes_and_a_provisional_does_not(monkeypatch):
309 forms = {
310 "US-20260330001-A1": ["parent US continuation 17838218 20220612 GRANTED \n child US 1\n<br />"],
311 "US-20260330002-A1": ["parent WO continuation PCT/JP2024/042963 20241204 PENDING \n child US 2"],
312 "US-20260330003-A1": ["parent US continuation-in-part 17000001 20220101 PENDING"],
313 "US-20260330004-A1": ["parent US division 17000002 20220101 GRANTED"],
314 "US-20260330005-A1": ["divisional parent-doc US 17000003 20220101"],
315 "US-20260330006-A1": ["us-provisional-application US 63804555 20250512\n<br />"],
316 "US-20260330007-A1": [],
317 "US-20260330008-A1": None,
318 }
319 rows = [make_row(guid, "2026-11-05") for guid in forms]
320 details = {guid: {"abstractHtml": "x", "continuityData": data} for guid, data in forms.items()}
321 FakeUSPTO(lists={"US-PGPUB": rows}, details=details).install(monkeypatch)
322
323 artifact = run()["artifact"]
324
325 assert [entry["guid"] for entry in artifact["entries"]] == [
326 "US-20260330006-A1", "US-20260330007-A1", "US-20260330008-A1",
327 ]
328 assert artifact["counts"]["continuing"] == 5
329 assert artifact["counts"]["first_publications"] == 3
330 assert artifact["partial"] is None
331
332
333 def test_refills_stop_after_six_extra_requests(monkeypatch):
334 rows = [make_row(f"US-202603400{n:02d}-A1", f"2026-11-{12 - n // 4:02d}") for n in range(30)]
335 continuing = {"abstractHtml": "x", "continuityData": ["parent US continuation 1 20200101"]}
336 details = {row["guid"]: continuing for row in rows[:10]}
337 fake = FakeUSPTO(lists={"US-PGPUB": rows}, details=details).install(monkeypatch)
338
339 artifact = run()["artifact"]
340
341 assert len(fake.of("detail")) == 18
342 assert len(artifact["entries"]) == 8
343 assert artifact["counts"]["continuing"] == 10
344 assert artifact["counts"]["first_publications"] == 20
345 assert artifact["partial"] == {"missing_text": 4}
346 assert len(artifact["index"]) == 12
347
348
349 # --- groups -----------------------------------------------------------------
350
351
352 def test_microsoft_licensing_filings_are_the_company_s_own(monkeypatch):
353 FakeUSPTO().install(monkeypatch)
354
355 artifact = run({"company": "Microsoft"})["artifact"]
356
357 assert artifact["groups"] == [{
358 "name": "Microsoft Technology Licensing, LLC", "kind": "confirmed",
359 "applications": 25, "grants": 25,
360 }]
361 counts = artifact["counts"]
362 assert (counts["applications"], counts["grants"]) == (25, 25)
363 assert (counts["name_matched_applications"], counts["name_matched_grants"]) == (0, 0)
364 # The 25 applications: 18 first publications and 7 continuing. The 25
365 # grants are all B2, each the grant of an application published earlier.
366 assert counts["first_publications"] == 18
367 assert counts["name_matched_first_publications"] == 0
368 assert (counts["continuing"], counts["previously_published_grants"]) == (7, 25)
369 assert artifact["status"] == "digest"
370 assert len(artifact["entries"]) == 12
371 for item in artifact["entries"] + artifact["index"]:
372 assert item["group_kind"] == "confirmed"
373 assert item["filed_as"] == "Microsoft Technology Licensing, LLC"
374
375
376 def test_an_operating_company_s_filings_count_as_the_company_s_in_the_footer(monkeypatch):
377 opco = "OpenAI OpCo, LLC"
378 applications = [
379 *[make_row(f"US-2026043000{n}-A1", "2026-11-05", name=opco) for n in range(3)],
380 make_row("US-20260430010-A1", "2026-11-04", name=opco, **CONTINUING),
381 ]
382 grants = [make_row("US-12790041-B1", "2026-11-03", name=opco, kind="B1")]
383 FakeUSPTO(lists={"US-PGPUB": applications, "USPAT": grants}).install(monkeypatch)
384
385 artifact = run({"company": "OpenAI"})["artifact"]
386
387 assert artifact["groups"] == [
388 {"name": opco, "kind": "confirmed", "applications": 4, "grants": 1},
389 ]
390 report = _report(artifact, topic="OpenAI")
391 assert render._patents_footer_line(report) == (
392 "📜 Patents: 4 applications published │ 1 granted │ 4 first US publications"
393 )
394 block = "\n".join(render._patent_digest_block(report))
395 assert "filed as OpenAI OpCo, LLC" in block
396 assert "not verified" not in block and "confirmed" not in block.casefold()
397
398
399 BLOCK_IMAGING = "Block Imaging, Inc."
400
401
402 def test_name_matched_filings_stay_out_of_the_company_s_counts(monkeypatch):
403 applications = [
404 make_row("US-20260410001-A1", "2026-11-06", name="Block, Inc."),
405 *[make_row(f"US-2026041001{n}-A1", "2026-11-05", name=BLOCK_IMAGING) for n in range(5)],
406 make_row("US-20260410020-A1", "2026-11-04", name=BLOCK_IMAGING, **CONTINUING),
407 make_row("US-20260410021-A9", "2026-11-04", name=BLOCK_IMAGING, kind="A9"),
408 ]
409 grants = [
410 make_row("US-12790021-B2", "2026-11-03", name=BLOCK_IMAGING, kind="B2"),
411 make_row("US-D1150002-S", "2026-11-03", name=BLOCK_IMAGING, kind="S"),
412 ]
413 FakeUSPTO(lists={"US-PGPUB": applications, "USPAT": grants}).install(monkeypatch)
414
415 artifact = run({"company": "Block"})["artifact"]
416
417 assert artifact["groups"] == [
418 {"name": "Block, Inc.", "kind": "confirmed", "applications": 1, "grants": 0},
419 {"name": BLOCK_IMAGING, "kind": "name-matched", "applications": 7, "grants": 2},
420 ]
421 assert artifact["counts"] == {
422 "applications": 1, "grants": 0,
423 "name_matched_applications": 7, "name_matched_grants": 2,
424 "first_publications": 1, "name_matched_first_publications": 5,
425 "continuing": 0, "previously_published_grants": 0, "designs": 0, "other_kinds": 0,
426 "floor": False,
427 }
428 # The entries still hold the name-matched filings, labelled.
429 assert [entry["group_kind"] for entry in artifact["entries"]] == [
430 "confirmed", *["name-matched"] * 5,
431 ]
432
433
434 def test_a_demoted_candidate_leaves_the_first_publications_of_its_own_group_kind(monkeypatch):
435 applications = [
436 make_row("US-20260420001-A1", "2026-11-06", name="Block, Inc."),
437 make_row("US-20260420002-A1", "2026-11-06", name="Block, Inc."),
438 make_row("US-20260420011-A1", "2026-11-05", name=BLOCK_IMAGING),
439 make_row("US-20260420012-A1", "2026-11-05", name=BLOCK_IMAGING),
440 ]
441 grants = [make_row("US-12790031-B1", "2026-11-04", name=BLOCK_IMAGING, kind="B1")]
442 continuing = {"abstractHtml": "x", "continuityData": ["parent US continuation 1 20200101"]}
443 demoted = ["US-20260420002-A1", "US-20260420012-A1", "US-12790031-B1"]
444 FakeUSPTO(
445 lists={"US-PGPUB": applications, "USPAT": grants},
446 details={guid: continuing for guid in demoted},
447 ).install(monkeypatch)
448
449 artifact = run({"company": "Block"})["artifact"]
450
451 assert [entry["guid"] for entry in artifact["entries"]] == [
452 "US-20260420001-A1", "US-20260420011-A1",
453 ]
454 counts = artifact["counts"]
455 # Confirmed: two candidates, one found to be a continuation.
456 assert (counts["first_publications"], counts["continuing"]) == (1, 1)
457 # Name-matched: three candidates, two demoted, and neither joins a count
458 # of the company's own filings.
459 assert counts["name_matched_first_publications"] == 1
460 assert counts["previously_published_grants"] == 0
461
462
463 def test_alphabet_is_searched_and_confirmed_under_its_other_names(monkeypatch):
464 fake = FakeUSPTO().install(monkeypatch)
465 company = {"company": "Alphabet Inc.", "company_names": ["Google", "Waymo"]}
466
467 artifact = run(company)["artifact"]
468
469 assert [call["query"] for call in fake.of("search")] == [
470 '("alphabet" OR "google" OR "waymo").aanm. AND @pd>="20261014"<="20261113"',
471 '("alphabet" OR "google" OR "waymo").asnm. AND @pd>="20261014"<="20261113"',
472 ]
473 assert artifact["company"] == "Alphabet Inc."
474 assert artifact["groups"] == [
475 {"name": "Google LLC", "kind": "confirmed", "applications": 25, "grants": 25},
476 {"name": "Waymo LLC", "kind": "confirmed", "applications": 16, "grants": 23},
477 ]
478 assert artifact["counts"]["applications"] == 41
479 assert {entry["filed_as"] for entry in artifact["entries"]} <= {"Google LLC", "Waymo LLC"}
480
481
482 def test_other_names_that_only_extend_the_company_name_are_not_queried(monkeypatch):
483 fake = FakeUSPTO().install(monkeypatch)
484 company = {
485 "company": "Apple Inc.",
486 "company_names": ["Apple", "Apple Computer, Inc.", "Beats Electronics", "beats electronics llc", None]
487 + [f"Unit {n}" for n in range(20)],
488 }
489
490 run(company)
491
492 phrases = fake.of("search")[0]["query"].split(").aanm.")[0].lstrip("(").split(" OR ")
493 assert phrases[:2] == ['"apple"', '"beats electronics"']
494 assert len(phrases) == 1 + 12
495
496
497 def test_strangers_alone_are_the_none_state(monkeypatch):
498 rows = [row for row in recorded("Block", "US-PGPUB") if row["applicantName"] == ["QUICK BLOCK LIMITED"]]
499 rows += [make_row(f"US-2026035000{n}-A1", "2026-11-05", name="Chopping Block &amp; Co.") for n in range(3)]
500 fake = FakeUSPTO(lists={"US-PGPUB": rows}).install(monkeypatch)
501
502 result = run({"company": "Block"})
503
504 assert result == {
505 "state": "none",
506 "other_names": [
507 {"name": "Chopping Block & Co.", "count": 3},
508 {"name": "QUICK BLOCK LIMITED", "count": 1},
509 ],
510 }
511 assert [call["kind"] for call in fake.calls] == ["session", "search", "search"]
512
513
514 def test_a_company_with_nothing_in_the_window_is_the_none_state(monkeypatch):
515 FakeUSPTO().install(monkeypatch)
516
517 assert run({"company": "Anthropic"}) == {"state": "none", "other_names": []}
518
519
520 def test_other_names_are_the_five_largest(monkeypatch):
521 rows = [
522 make_row(f"US-20260360{size}{n:02d}-A1", "2026-11-05", name=f"Big Apple Ventures {size}")
523 for size in range(1, 8) for n in range(size)
524 ]
525 FakeUSPTO(lists={"US-PGPUB": rows}).install(monkeypatch)
526
527 other = run()["other_names"]
528
529 assert [item["count"] for item in other] == [7, 6, 5, 4, 3]
530 assert other[0]["name"] == "Big Apple Ventures 7"
531
532
533 def test_a_co_applicant_does_not_make_a_group(monkeypatch):
534 FakeUSPTO().install(monkeypatch)
535 company = {"company": "IBM", "company_names": ["International Business Machines"]}
536
537 artifact = run(company)["artifact"]
538
539 assert artifact["groups"] == [{
540 "name": "International Business Machines Corporation", "kind": "confirmed",
541 "applications": 25, "grants": 25,
542 }]
543 assert artifact["other_names"] == []
544 assert {item["filed_as"] for item in artifact["entries"] + artifact["index"]} == {
545 "International Business Machines Corporation",
546 }
547
548
549 def test_a_row_sits_in_the_best_group_among_its_names(monkeypatch):
550 rows = [
551 make_row("US-20260370001-A1", "2026-11-06", name=["Apple Retail Holdings LLC", "Apple Inc."]),
552 make_row("US-20260370002-A1", "2026-11-05", name="Apple Retail Holdings LLC"),
553 make_row("US-20260370003-A1", "2026-11-04", name=["Stranger Ltd", "APPLE RETAIL HOLDINGS, LLC"]),
554 make_row("US-20260370004-A1", "2026-11-03"),
555 ]
556 rows[3]["applicantName"] = "Apple Inc" # a string, not a list
557 rows.append(make_row("US-20260370005-A1", "2026-11-02", name=None))
558 FakeUSPTO(lists={"US-PGPUB": rows}).install(monkeypatch)
559
560 artifact = run()["artifact"]
561
562 assert artifact["groups"] == [
563 {"name": "Apple Inc.", "kind": "confirmed", "applications": 2, "grants": 0},
564 {"name": "Apple Retail Holdings LLC", "kind": "name-matched", "applications": 2, "grants": 0},
565 ]
566 assert artifact["other_names"] == []
567 assert artifact["counts"]["applications"] == 2
568 assert artifact["counts"]["name_matched_applications"] == 2
569 assert artifact["counts"]["first_publications"] == 2
570 assert artifact["counts"]["name_matched_first_publications"] == 2
571 assert [(entry["filed_as"], entry["group_kind"]) for entry in artifact["entries"]] == [
572 ("Apple Inc.", "confirmed"),
573 ("Apple Retail Holdings LLC", "name-matched"),
574 ("Apple Retail Holdings LLC", "name-matched"),
575 ("Apple Inc.", "confirmed"),
576 ]
577
578
579 def test_at_most_eight_groups_confirmed_first_then_largest(monkeypatch):
580 rows = [make_row("US-20260380000-A1", "2026-11-05")]
581 for size in range(1, 11):
582 rows += [
583 make_row(f"US-202603801{size:02d}{n}-A1", "2026-11-05", name=f"Apple Division {size} LLC")
584 for n in range(size)
585 ]
586 FakeUSPTO(lists={"US-PGPUB": rows}).install(monkeypatch)
587
588 artifact = run()["artifact"]
589
590 assert [(group["name"], group["applications"]) for group in artifact["groups"]] == [
591 ("Apple Inc.", 1),
592 *[(f"Apple Division {size} LLC", size) for size in range(10, 3, -1)],
593 ]
594 assert artifact["counts"]["name_matched_applications"] == sum(range(4, 11))
595
596
597 def test_every_confirmed_name_is_counted_however_many_there_are(monkeypatch):
598 units = [f"Orchard Unit {n}" for n in range(1, 10)]
599 rows = [make_row("US-20260381000-A1", "2026-11-05")]
600 rows += [
601 make_row(f"US-2026038110{n}-A1", "2026-11-05", name=f"{unit} LLC")
602 for n, unit in enumerate(units)
603 ]
604 rows.append(make_row("US-20260381200-A1", "2026-11-05", name="Apple Retail Holdings LLC"))
605 FakeUSPTO(lists={"US-PGPUB": rows}).install(monkeypatch)
606
607 artifact = run({"company": "Apple Inc.", "company_names": units})["artifact"]
608
609 confirmed = [group for group in artifact["groups"] if group["kind"] == "confirmed"]
610 assert len(confirmed) == 10
611 assert artifact["counts"]["applications"] == 10
612 assert artifact["counts"]["first_publications"] == 10
613 # With the confirmed names past the cap, no name-matched name is kept.
614 assert [group["name"] for group in artifact["groups"] if group["kind"] != "confirmed"] == []
615
616
617 # --- which candidates get text ----------------------------------------------
618
619
620 def window_rows() -> list[dict]:
621 """Fourteen candidates in a common class, newest first, and an older one
622 in a class the company has not filed in."""
623 rows = [
624 make_row(f"US-202603900{n:02d}-A1", f"2026-11-{12 - n // 2:02d}", cpc="G06F3/01;H04L9/32")
625 for n in range(14)
626 ]
627 rows.append(make_row("US-20260390099-A1", "2026-10-15", cpc="A61B5/02;G06F3/01"))
628 return rows
629
630
631 def baseline_rows(count: int) -> list[dict]:
632 return [
633 make_row(f"US-202503{n:05d}-A1", "2026-03-01", cpc="G06F3/01;G06F3/02;H04L9/32")
634 for n in range(count)
635 ]
636
637
638 def test_a_thin_baseline_orders_entries_newest_first(monkeypatch):
639 rows = window_rows()
640 FakeUSPTO(lists={"US-PGPUB": rows}, baseline=baseline_rows(19)).install(monkeypatch)
641
642 artifact = run()["artifact"]
643
644 assert [entry["guid"] for entry in artifact["entries"]] == [row["guid"] for row in rows[:12]]
645 assert numbers(artifact["index"]) == [row["guid"].split("-")[1] for row in rows[12:]]
646
647
648 def test_a_baseline_past_its_third_of_the_budget_still_reads_newest_first(monkeypatch):
649 rows = window_rows()
650 # A full baseline would put the unseen class first; this one takes 31 of
651 # the lane's 90 seconds, past its own share of 30.
652 fake = FakeUSPTO(
653 lists={"US-PGPUB": rows}, baseline=baseline_rows(30), seconds={"baseline": 31.0},
654 ).install(monkeypatch)
655
656 artifact = run()["artifact"]
657
658 assert len(fake.of("baseline")) == 1
659 assert [entry["guid"] for entry in artifact["entries"]] == [row["guid"] for row in rows[:12]]
660 assert all(entry["abstract"] and entry["first_claim"] for entry in artifact["entries"])
661 assert artifact["partial"] is None
662
663
664 def test_an_unseen_class_gets_a_slot_ahead_of_newer_common_filings(monkeypatch):
665 rows = window_rows()
666 FakeUSPTO(lists={"US-PGPUB": rows}, baseline=baseline_rows(30)).install(monkeypatch)
667
668 artifact = run()["artifact"]
669
670 assert [entry["guid"] for entry in artifact["entries"]] == [
671 "US-20260390099-A1", *[row["guid"] for row in rows[:11]],
672 ]
673 assert artifact["entries"][0]["published"] == "2026-10-15"
674
675
676 def test_half_the_slots_go_to_rare_classes_and_half_to_the_newest(monkeypatch):
677 common = [
678 make_row(f"US-202604000{n:02d}-A1", f"2026-11-{12 - n:02d}", cpc="G06F3/01") for n in range(10)
679 ]
680 unseen = [
681 make_row(f"US-202604001{n:02d}-A1", f"2026-10-{24 - n:02d}", cpc=f"A61B5/{n:02d}") for n in range(8)
682 ]
683 # Seen once in the baseline: rarer than the common class, less rare than unseen.
684 seldom = make_row("US-20260400200-A1", "2026-11-13", cpc="B60W30/00")
685 baseline = baseline_rows(30) + [make_row("US-20250399999-A1", "2026-02-01", cpc="B60W10/00")]
686 fake = FakeUSPTO(
687 lists={"US-PGPUB": common + unseen + [seldom]}, baseline=baseline,
688 ).install(monkeypatch)
689
690 artifact = run()["artifact"]
691
692 assert [entry["guid"] for entry in artifact["entries"]] == [
693 *[row["guid"] for row in unseen[:6]],
694 seldom["guid"],
695 *[row["guid"] for row in common[:5]],
696 ]
697 assert len(fake.of("baseline")) == 1
698
699
700 def test_baseline_rows_filed_by_strangers_do_not_make_a_class_look_common(monkeypatch):
701 rows = window_rows()
702 strangers = [
703 make_row(f"US-202503{n:05d}-A1", "2026-03-01", name="Deep Apple Therapeutics, Inc.", cpc="A61B5/02")
704 for n in range(100, 140)
705 ]
706 FakeUSPTO(lists={"US-PGPUB": rows}, baseline=baseline_rows(30) + strangers).install(monkeypatch)
707
708 assert run()["artifact"]["entries"][0]["guid"] == "US-20260390099-A1"
709
710
711 # --- a slot for each confirmed filer other than the company's own name ------------
712
713
714 NVIDIA_CO = {"company": "NVIDIA Corporation", "company_names": ["Mellanox Technologies"]}
715
716
717 def own_rows(count: int, *, name: str = "NVIDIA Corporation") -> list[dict]:
718 """The company's own filings, all newer than any subsidiary's."""
719 return [
720 make_row(f"US-2026041{n:04d}-A1", f"2026-11-{12 - n % 10:02d}", name=name, cpc="G06F3/01")
721 for n in range(count)
722 ]
723
724
725 def filer_rows(prefix: str, name: str, count: int) -> list[dict]:
726 return [
727 make_row(f"US-2026042{prefix}{n:02d}-A1", f"2026-10-{20 - n:02d}", name=name, cpc="G06F3/01")
728 for n in range(count)
729 ]
730
731
732 def filed_as(artifact: dict) -> list[str]:
733 return [entry["filed_as"] for entry in artifact["entries"]]
734
735
736 def test_with_no_other_filer_the_twelve_are_the_newest_own_filings(monkeypatch):
737 rows = own_rows(20)
738 FakeUSPTO(lists={"US-PGPUB": rows}).install(monkeypatch)
739
740 artifact = run(NVIDIA_CO)["artifact"]
741
742 newest = sorted(rows, key=lambda row: row["datePublished"], reverse=True)
743 assert [entry["guid"] for entry in artifact["entries"]] == [row["guid"] for row in newest[:12]]
744
745
746 def test_a_confirmed_subsidiary_gets_one_of_the_twelve(monkeypatch):
747 rows = own_rows(150) + filer_rows("10", "Mellanox Technologies, Ltd.", 4)
748 fake = FakeUSPTO(lists={"US-PGPUB": rows}).install(monkeypatch)
749
750 artifact = run(NVIDIA_CO)["artifact"]
751
752 assert len(artifact["entries"]) == 12
753 assert filed_as(artifact).count("Mellanox Technologies, Ltd.") == 1
754 assert len(fake.of("detail")) <= 18
755
756
757 def test_five_confirmed_subsidiaries_get_three_reserved_slots(monkeypatch):
758 names = [f"Subsidiary {letter} Inc." for letter in "ABCDE"]
759 rows = own_rows(60)
760 for n, name in enumerate(names):
761 rows += filer_rows(f"{n}0", name, 2)
762 FakeUSPTO(lists={"US-PGPUB": rows}).install(monkeypatch)
763
764 artifact = run({"company": "NVIDIA Corporation", "company_names": names})["artifact"]
765
766 others = [name for name in filed_as(artifact) if name != "NVIDIA Corporation"]
767 assert len(artifact["entries"]) == 12
768 assert len(others) == len(set(others)) == 3
769
770
771 def test_a_filer_the_engine_could_not_verify_never_takes_a_reserved_slot(monkeypatch):
772 rows = own_rows(40) + filer_rows("20", "NVIDIA Networking Solutions Inc.", 3)
773 FakeUSPTO(lists={"US-PGPUB": rows}).install(monkeypatch)
774
775 artifact = run(NVIDIA_CO)["artifact"]
776
777 assert "NVIDIA Networking Solutions Inc." not in filed_as(artifact)
778 assert set(filed_as(artifact)) == {"NVIDIA Corporation"}
779
780
781 def test_a_subsidiary_whose_first_filing_is_demoted_still_gets_its_second(monkeypatch):
782 mellanox = filer_rows("30", "Mellanox Technologies, Ltd.", 2)
783 rows = own_rows(40) + mellanox
784 continuing = {"abstractHtml": "x", "continuityData": ["parent US continuation 1 20200101"]}
785 fake = FakeUSPTO(
786 lists={"US-PGPUB": rows}, details={mellanox[0]["guid"]: continuing},
787 ).install(monkeypatch)
788
789 artifact = run(NVIDIA_CO)["artifact"]
790
791 assert [entry["guid"] for entry in artifact["entries"] if entry["filed_as"] != "NVIDIA Corporation"] == [
792 mellanox[1]["guid"],
793 ]
794 assert len(artifact["entries"]) == 12
795 assert len(fake.of("detail")) <= 18
796
797
798 def test_a_second_subsidiary_takes_a_spare_slot_from_the_first():
799 from collections import Counter
800
801 from lib import patents
802
803 def candidate(guid, published, key, cpc, other):
804 return {"guid": guid, "published": published, "group_key": key, "cpc": [cpc], "other_filer": other}
805
806 # Six rare parent filings, then six newer filings from subsidiary A fill
807 # the twelve slots; subsidiary B's two older filings sit outside them.
808 parent = [candidate(f"p{n}", f"2026-01-{n + 1:02d}", "parent", f"A0{n}B 1/00", False) for n in range(6)]
809 first = [candidate(f"a{n}", f"2026-06-{n + 1:02d}", "sub-a", "G06F 1/00", True) for n in range(6)]
810 second = [candidate(f"b{n}", f"2025-12-{n + 1:02d}", "sub-b", "G06F 1/00", True) for n in range(2)]
811 frequencies = Counter({"G06F": 50})
812
813 ordered, reserved = patents._order(parent + first + second, frequencies)
814
815 slots = [item["group_key"] for item in ordered[:patents.DETAIL_SLOTS]]
816 assert reserved == {"sub-a", "sub-b"}
817 assert slots.count("sub-b") == 1 and slots.count("sub-a") == 5 and slots.count("parent") == 6
818
818 lines PYTHON