forked from ChelseaKR/mrf-honest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cohort.py
More file actions
700 lines (589 loc) · 26.5 KB
/
Copy pathtest_cohort.py
File metadata and controls
700 lines (589 loc) · 26.5 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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
"""Cohort comparison and presentation-grade policy tests.
Every grade band, every fail-closed path, and every refusal in ``mrf_honest.cohort`` is pinned
here with synthetic assessments composed through the real phase-3 composition path.
"""
from __future__ import annotations
import hashlib
import json
from datetime import date
from pathlib import Path
from typing import cast
import pytest
from mrf_honest.cohort import (
COMPARISON_VERSION,
GRADE_POLICY_FINGERPRINT,
NOT_GRADED,
CohortError,
FileGrade,
build_comparison,
grade_assessment,
)
from mrf_honest.fetch import FetchOutcome, FetchPolicy, FetchStatus
from mrf_honest.inspect import inspect_hospital_file
from mrf_honest.scorecard import (
AssessmentSubject,
PublisherType,
RetrievalPolicyEvidence,
URLProvenance,
compose_file_assessment,
)
from mrf_honest.types import PublisherRef
OBSERVED_AT = "2026-05-01T12:00:00Z"
AS_OF = date(2026, 5, 1)
GENERATED_AT = "2026-05-01T13:00:00Z"
def _policy() -> FetchPolicy:
return FetchPolicy(contact="maintainer@example.test", retries=0)
def _subject(
publisher_id: str = "example-health",
location_id: str = "main",
*,
provenance: URLProvenance = URLProvenance.CMS_HPT,
) -> AssessmentSubject:
url = f"https://files.example.test/{publisher_id}/{location_id}/standardcharges.json"
return AssessmentSubject(
publisher=PublisherRef(identifier=publisher_id, name="Example Hospital", source_url=url),
publisher_type=PublisherType.HOSPITAL,
location_id=location_id,
requested_url=url,
url_provenance=provenance,
)
def _document() -> dict[str, object]:
return {
"hospital_name": "Example Hospital",
"last_updated_on": "2026-04-01",
"version": "3.0.0",
"location_name": ["Example Main"],
"hospital_address": ["1 Main St, Testville, CA 90000"],
"type_2_npi": ["1234567890"],
"license_information": {"state": "CA", "license_number": "A1"},
"attestation": {
"attestation": "CMS-required statement represented in the fixture",
"confirm_attestation": True,
"attester_name": "A. Executive",
},
"standard_charge_information": [
{
"description": "Example service",
"code_information": [{"code": "12345", "type": "CPT"}],
"standard_charges": [
{
"gross_charge": 120,
"discounted_cash": 90,
"minimum": 70,
"maximum": 100,
"setting": "outpatient",
"payers_information": [
{
"payer_name": "Alpha",
"plan_name": "PPO",
"methodology": "fee schedule",
"standard_charge_dollar": 80,
}
],
}
],
}
],
}
def _success_record(
tmp_path: Path,
document: dict[str, object] | None = None,
*,
raw: bytes | None = None,
subject: AssessmentSubject | None = None,
content_type: str | None = None,
) -> dict[str, object]:
subject = subject or _subject()
body = raw if raw is not None else json.dumps(document or _document()).encode()
tmp_path.mkdir(parents=True, exist_ok=True)
path = tmp_path / f"{subject.publisher.identifier}-{subject.location_id}.json"
path.write_bytes(body)
inspection = inspect_hospital_file(path, subject.publisher, as_of=AS_OF)
fetch = FetchOutcome(
url=subject.requested_url,
status=FetchStatus.FETCHED,
attempted_at=OBSERVED_AT,
attempts=1,
path=path,
content_sha256=hashlib.sha256(body).hexdigest(),
size_bytes=len(body),
wire_size_bytes=len(body),
http_status=200,
final_url=subject.requested_url,
content_type=content_type,
)
assessment = compose_file_assessment(
subject,
fetch,
retrieval_policy=RetrievalPolicyEvidence.from_policy(_policy()),
inspection=inspection,
)
return assessment.to_dict()
def _failure_record(
status: FetchStatus,
*,
http_status: int | None = None,
subject: AssessmentSubject | None = None,
error: str | None = None,
) -> dict[str, object]:
subject = subject or _subject()
fetch = FetchOutcome(
url=subject.requested_url,
status=status,
attempted_at=OBSERVED_AT,
attempts=0 if status is FetchStatus.INVALID_URL else 1,
http_status=http_status,
final_url=subject.requested_url,
error=error or f"fixture {status.value}",
)
assessment = compose_file_assessment(
subject,
fetch,
retrieval_policy=RetrievalPolicyEvidence.from_policy(_policy()),
)
return assessment.to_dict()
def _manifest(utc_date: str = "2026-05-01") -> dict[str, object]:
return {
"policy": "test cohorts attest their collection run",
"cohort_id": "test-cohort",
"collection": {"operator_controlled_single_run": True, "utc_date": utc_date},
"exclusions": [{"id": "held-out", "reason": "out of profile"}],
}
# --- grade bands ---------------------------------------------------------------------------
def test_clean_file_grades_a(tmp_path: Path) -> None:
grade = grade_assessment(_success_record(tmp_path))
assert grade.grade == "A"
assert grade.error_dimensions == ()
assert grade.warning_findings == 0
def test_info_findings_never_lower_a_grade(tmp_path: Path) -> None:
body = b"\xef\xbb\xbf" + json.dumps(_document()).encode()
grade = grade_assessment(_success_record(tmp_path, raw=body))
assert grade.grade == "A"
assert grade.info_findings >= 1
def test_warning_only_file_grades_b(tmp_path: Path) -> None:
document = _document()
document["last_updated_on"] = "2024-01-01" # more than a year before AS_OF
grade = grade_assessment(_success_record(tmp_path, document))
assert grade.grade == "B"
assert grade.warning_findings >= 1
assert "warning" in grade.reason
def test_errors_in_one_dimension_grade_c(tmp_path: Path) -> None:
document = _document()
charges = cast(list[dict[str, object]], document["standard_charge_information"])
del charges[0]["description"] # completeness ERROR only
grade = grade_assessment(_success_record(tmp_path, document))
assert grade.grade == "C"
assert grade.error_dimensions == ("completeness",)
def test_errors_in_two_dimensions_grade_d(tmp_path: Path) -> None:
document = _document()
charges = cast(list[dict[str, object]], document["standard_charge_information"])
del charges[0]["description"]
groups = cast(list[dict[str, object]], charges[0]["standard_charges"])
groups[0]["setting"] = "clinic" # conformance ERROR: outside the accepted set
grade = grade_assessment(_success_record(tmp_path, document))
assert grade.grade == "D"
assert set(grade.error_dimensions) == {"conformance", "completeness"}
def test_errors_in_three_dimensions_grade_f(tmp_path: Path) -> None:
document = _document()
del document["last_updated_on"] # conformance ERROR + freshness ERROR
charges = cast(list[dict[str, object]], document["standard_charge_information"])
del charges[0]["description"] # completeness ERROR
grade = grade_assessment(_success_record(tmp_path, document))
assert grade.grade == "F"
assert len(grade.error_dimensions) >= 3
def test_incomplete_stream_is_f_not_a_pass(tmp_path: Path) -> None:
truncated = b'{"standard_charge_information": [ {"description": "x"'
grade = grade_assessment(_success_record(tmp_path, raw=truncated))
assert grade.grade == "F"
assert "could not be streamed to completion" in grade.reason
def test_a_download_that_stopped_early_and_a_malformed_file_read_differently(
tmp_path: Path,
) -> None:
"""Both are F. They must not be the same F, because they say different things.
One says a hospital published a document that cannot be read; the other says this project
did not finish downloading it. Both grades are published beside the hospital's name, so the
sentences have to stay distinguishable.
"""
malformed = grade_assessment(
_success_record(tmp_path, raw=b'{"standard_charge_information": [ {"description": "x"')
)
stopped_early = grade_assessment(
_failure_record(
FetchStatus.NETWORK_ERROR,
http_status=200,
error="the response body ended after 41 of the 883973507 bytes the server declared "
"in Content-Length",
)
)
assert malformed.grade == stopped_early.grade == "F"
assert "could not be streamed to completion" in malformed.reason
assert "could not be streamed to completion" not in stopped_early.reason
assert "did not produce a verified file" in stopped_early.reason
assert "883973507 bytes the server declared" in stopped_early.reason
_LANDING_PAGE = (
b'<!doctype html>\n<html lang="en"><head><title>Price Transparency</title></head>\n'
b"<body><h1>Standard Charges</h1><p>Select a file to download.</p></body></html>\n"
)
def test_a_web_page_and_a_malformed_file_do_not_read_alike(tmp_path: Path) -> None:
"""Both are F and both are the publisher's. They are still not the same event.
One URL answered with a web page where a document was requested; the other answered with the
document, and the document is broken. Before Content-Type was recorded the tool had no way
to tell those apart, so it published the second sentence for both.
"""
web_page = grade_assessment(
_success_record(
tmp_path / "page",
raw=_LANDING_PAGE,
content_type="text/html;charset=UTF-8",
)
)
malformed = grade_assessment(
_success_record(
tmp_path / "broken",
raw=b'{"standard_charge_information": [ {"description": "x"',
content_type="application/json",
)
)
assert web_page.grade == malformed.grade == "F"
assert web_page.reason != malformed.reason
assert "text/html" in web_page.reason
assert "a web page, not the requested file" in web_page.reason
assert "web page" not in malformed.reason
assert "application/json" in malformed.reason
# Neither sentence may drop the rule that unread content is failed, not passed.
assert "failed, not passed" in web_page.reason
assert "failed, not passed" in malformed.reason
def test_a_declared_media_type_is_not_a_grading_input(tmp_path: Path) -> None:
"""A conforming MRF served as ``text/html`` is a conforming MRF.
This is the decision the fix must not regress: Content-Type explains a failure that already
happened, and never causes one.
"""
served_as_html = grade_assessment(_success_record(tmp_path / "a", content_type="text/html"))
served_as_json = grade_assessment(
_success_record(tmp_path / "b", content_type="application/json")
)
unrecorded = grade_assessment(_success_record(tmp_path / "c"))
assert served_as_html.grade == served_as_json.grade == unrecorded.grade == "A"
assert served_as_html.reason == served_as_json.reason == unrecorded.reason
assert "html" not in served_as_html.reason
def test_an_unrecorded_media_type_claims_nothing_about_what_was_served(tmp_path: Path) -> None:
"""Assessments made before Content-Type was recorded must not gain a claim retroactively.
An absent header and an absent recording are different facts, and neither is "the server
served the wrong thing". The historical sentence is reproduced exactly.
"""
grade = grade_assessment(
_success_record(tmp_path, raw=b'{"standard_charge_information": [ {"description": "x"')
)
assert grade.grade == "F"
assert grade.reason == (
"the standard_charge_information array could not be streamed to completion; "
"content that could not be read is treated as failed, not passed"
)
def test_a_stopped_download_and_a_served_web_page_do_not_read_alike(tmp_path: Path) -> None:
"""The third sentence in the family, so all three events stay distinguishable."""
web_page = grade_assessment(
_success_record(tmp_path, raw=_LANDING_PAGE, content_type="text/html")
)
stopped_early = grade_assessment(
_failure_record(
FetchStatus.NETWORK_ERROR,
http_status=200,
error="the response body ended after 41 of the 883973507 bytes the server declared "
"in Content-Length",
)
)
assert web_page.grade == stopped_early.grade == "F"
assert web_page.reason != stopped_early.reason
assert "could not be streamed to completion" in web_page.reason
assert "could not be streamed to completion" not in stopped_early.reason
def test_failed_download_is_a_stated_f() -> None:
grade = grade_assessment(_failure_record(FetchStatus.HTTP_ERROR, http_status=500))
assert grade.grade == "F"
assert "did not produce a verified file" in grade.reason
def test_project_size_ceiling_is_not_graded_not_f() -> None:
grade = grade_assessment(_failure_record(FetchStatus.TOO_LARGE))
assert grade.grade == NOT_GRADED
assert "ceiling" in grade.reason
def test_invalid_url_is_not_graded() -> None:
record = _failure_record(FetchStatus.INVALID_URL)
grade = grade_assessment(record)
assert grade.grade == NOT_GRADED
def test_unrecognized_retrievability_status_is_refused(tmp_path: Path) -> None:
record = _success_record(tmp_path)
scorecard = cast(dict[str, object], record["scorecard"])
retrievability = dict(cast(dict[str, object], scorecard["retrievability"]))
retrievability["status"] = "MYSTERY"
scorecard["retrievability"] = retrievability
with pytest.raises(CohortError, match="unrecognized retrievability status"):
grade_assessment(record)
def test_observed_without_inspection_evidence_is_refused(tmp_path: Path) -> None:
record = _success_record(tmp_path)
record["inspection"] = None
with pytest.raises(CohortError, match="no inspection evidence"):
grade_assessment(record)
def test_missing_scorecard_field_is_refused() -> None:
with pytest.raises(CohortError, match="scorecard"):
grade_assessment({})
def test_grade_serialization_carries_the_policy_identity() -> None:
payload = FileGrade("A", "reason").to_dict()
assert payload["policy_fingerprint"] == GRADE_POLICY_FINGERPRINT
assert len(GRADE_POLICY_FINGERPRINT) == 64
# --- cohort building -----------------------------------------------------------------------
def _two_records(tmp_path: Path) -> list[dict[str, object]]:
stale = _document()
stale["last_updated_on"] = "2024-01-01"
return [
_success_record(tmp_path, subject=_subject("alpha-health", "main")),
_success_record(tmp_path, stale, subject=_subject("beta-health", "north")),
]
def test_build_comparison_grades_sorts_and_counts(tmp_path: Path) -> None:
records = _two_records(tmp_path)
content = cast(dict[str, object], records[0]["retrieval"])["content_sha256"]
ingest = {
"run_id": "r1",
"source_file_id": content,
"publisher_id": "alpha-health",
"status": "success",
"reused": False,
"counts": {"items": 1},
"database_path": "/local/warehouse/catalog.duckdb",
}
comparison = build_comparison(
records,
_manifest(),
ingest_results=[ingest],
generated_at=GENERATED_AT,
)
files = cast(list[dict[str, object]], comparison["files"])
assert [row["slug"] for row in files] == ["alpha-health/main", "beta-health/north"]
summary = cast(dict[str, object], comparison["summary"])
assert summary["targeted"] == 2
assert summary["graded"] == 2
distribution = cast(dict[str, int], summary["grade_distribution"])
assert distribution["A"] == 1
assert distribution["B"] == 1
lakehouse = cast(dict[str, object], files[0]["lakehouse"])
assert lakehouse["run_id"] == "r1"
assert "database_path" not in lakehouse
assert files[1]["lakehouse"] is None
matrix = cast(list[dict[str, object]], comparison["finding_matrix"])
overdue = [entry for entry in matrix if entry["code"] == "FRESHNESS_ANNUAL_UPDATE_OVERDUE"]
assert overdue and overdue[0]["files"] == ["beta-health/north"]
header = cast(dict[str, object], comparison["cohort"])
grade_policy = cast(dict[str, object], header["grade_policy"])
assert grade_policy["fingerprint"] == GRADE_POLICY_FINGERPRINT
assert comparison["generated_at"] == GENERATED_AT
def test_failed_target_stays_a_row_in_the_comparison(tmp_path: Path) -> None:
records = [
_success_record(tmp_path, subject=_subject("alpha-health", "main")),
_failure_record(
FetchStatus.HTTP_ERROR,
http_status=403,
subject=_subject("gamma-health", "main"),
),
]
comparison = build_comparison(records, _manifest(), generated_at=GENERATED_AT)
files = cast(list[dict[str, object]], comparison["files"])
graded = {
cast(str, row["slug"]): cast(dict[str, object], row["grade"])["grade"] for row in files
}
assert graded["gamma-health/main"] == "F"
summary = cast(dict[str, object], comparison["summary"])
assert summary["verified_body_available"] == 1
assert summary["targeted"] == 2
def test_comparison_requires_the_single_run_attestation(tmp_path: Path) -> None:
manifest = _manifest()
collection = cast(dict[str, object], manifest["collection"])
collection["operator_controlled_single_run"] = False
with pytest.raises(CohortError, match="operator-controlled collection run"):
build_comparison(_two_records(tmp_path), manifest, generated_at=GENERATED_AT)
def test_comparison_requires_at_least_two_rows(tmp_path: Path) -> None:
records = [_success_record(tmp_path)]
with pytest.raises(CohortError, match="at least two"):
build_comparison(records, _manifest(), generated_at=GENERATED_AT)
def test_comparison_refuses_duplicate_subjects(tmp_path: Path) -> None:
records = [_success_record(tmp_path), _success_record(tmp_path)]
with pytest.raises(CohortError, match="duplicate assessment subject"):
build_comparison(records, _manifest(), generated_at=GENERATED_AT)
def test_comparison_refuses_as_of_outside_the_attested_run(tmp_path: Path) -> None:
with pytest.raises(CohortError, match="utc_date"):
build_comparison(_two_records(tmp_path), _manifest("2026-05-02"), generated_at=GENERATED_AT)
def test_comparison_refuses_foreign_or_duplicate_ingest_evidence(tmp_path: Path) -> None:
records = _two_records(tmp_path)
foreign = {
"run_id": "r9",
"source_file_id": "0" * 64,
"publisher_id": "alpha-health",
"status": "success",
}
with pytest.raises(CohortError, match="does not match any cohort assessment"):
build_comparison(records, _manifest(), ingest_results=[foreign], generated_at=GENERATED_AT)
content = cast(dict[str, object], records[0]["retrieval"])["content_sha256"]
duplicate = {
"run_id": "r1",
"source_file_id": content,
"publisher_id": "alpha-health",
"status": "success",
}
with pytest.raises(CohortError, match="duplicate ingest evidence"):
build_comparison(
records,
_manifest(),
ingest_results=[duplicate, duplicate],
generated_at=GENERATED_AT,
)
def _refusal(content: object, publisher_id: str = "beta-health") -> dict[str, object]:
return {
"status": "refused",
"source_file_id": content,
"publisher_id": publisher_id,
"reason": "unsupported hospital JSON template version: '2.0.0'",
"implemented_scope": "CMS hospital JSON template version 3.0.0",
"observed_scope": "CMS hospital JSON template version 2.0.0",
}
def test_a_refused_ingest_carries_its_reason_into_the_row(tmp_path: Path) -> None:
"""A refused load and a load nobody attempted must not produce the same row.
Both used to serialize as ``"lakehouse": null``, so the site could only say that no
contract evidence existed and could not say why. docs/how-we-compare.md requires the reason
for a project limit to be stated; dropping it here is what made stating it impossible.
"""
records = _two_records(tmp_path)
content = cast(dict[str, object], records[1]["retrieval"])["content_sha256"]
comparison = build_comparison(
records,
_manifest(),
ingest_results=[_refusal(content)],
generated_at=GENERATED_AT,
)
files = cast(list[dict[str, object]], comparison["files"])
assert files[0]["lakehouse"] is None # nothing was recorded for this one, and it says so
refused = cast(dict[str, object], files[1]["lakehouse"])
assert refused["status"] == "refused"
assert refused["reason"] == "unsupported hospital JSON template version: '2.0.0'"
assert refused["implemented_scope"] == "CMS hospital JSON template version 3.0.0"
assert refused["observed_scope"] == "CMS hospital JSON template version 2.0.0"
# Warehouse evidence is not a grading input, in either direction.
assert cast(dict[str, object], files[1]["grade"])["grade"] == "B"
assert cast(dict[str, object], files[1]["grade"])["policy_fingerprint"] == (
GRADE_POLICY_FINGERPRINT
)
def test_a_refusal_without_its_reason_is_itself_refused(tmp_path: Path) -> None:
"""Half a refusal record would publish the same unexplained absence it exists to remove."""
records = _two_records(tmp_path)
content = cast(dict[str, object], records[1]["retrieval"])["content_sha256"]
for omitted in ("reason", "implemented_scope", "observed_scope", "publisher_id"):
incomplete = _refusal(content)
del incomplete[omitted]
with pytest.raises(CohortError, match=omitted):
build_comparison(
records,
_manifest(),
ingest_results=[incomplete],
generated_at=GENERATED_AT,
)
def test_refusal_evidence_obeys_the_same_cohort_binding(tmp_path: Path) -> None:
records = _two_records(tmp_path)
with pytest.raises(CohortError, match="does not match any cohort assessment"):
build_comparison(
records,
_manifest(),
ingest_results=[_refusal("0" * 64)],
generated_at=GENERATED_AT,
)
content = cast(dict[str, object], records[1]["retrieval"])["content_sha256"]
with pytest.raises(CohortError, match="duplicate ingest evidence"):
build_comparison(
records,
_manifest(),
ingest_results=[_refusal(content), _refusal(content)],
generated_at=GENERATED_AT,
)
def test_comparison_version_announces_the_document_shape(tmp_path: Path) -> None:
"""The refused branch changed what ``files[].lakehouse`` can be, so the schema moved.
The grade policy did not: the rule table is untouched and every grade is unchanged, so the
fingerprint must stay put rather than announce a regrade that did not happen (ADR 0005).
"""
comparison = build_comparison(_two_records(tmp_path), _manifest(), generated_at=GENERATED_AT)
assert comparison["comparison_version"] == COMPARISON_VERSION == 2
cohort = cast(dict[str, object], comparison["cohort"])
policy = cast(dict[str, object], cohort["grade_policy"])
assert policy["fingerprint"] == GRADE_POLICY_FINGERPRINT
assert policy["version"] == "cms-hospital-json-v3-file-grade-v1"
def test_comparison_propagates_the_scope_boundary(tmp_path: Path) -> None:
records = [
_success_record(tmp_path, subject=_subject("alpha-health", "main")),
_success_record(
tmp_path,
subject=_subject("beta-health", "north", provenance=URLProvenance.OPERATOR),
),
]
with pytest.raises(ValueError, match="not directly comparable"):
build_comparison(records, _manifest(), generated_at=GENERATED_AT)
def test_manifest_requires_cohort_id(tmp_path: Path) -> None:
manifest = _manifest()
del manifest["cohort_id"]
with pytest.raises(CohortError, match="cohort_id"):
build_comparison(_two_records(tmp_path), manifest, generated_at=GENERATED_AT)
# --- CLI -----------------------------------------------------------------------------------
def _write_cohort_inputs(tmp_path: Path) -> tuple[Path, Path]:
records = _two_records(tmp_path / "bodies")
assessments = tmp_path / "assessments.jsonl"
lines = [
json.dumps(record, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
for record in records
]
assessments.write_text("\n".join(lines) + "\n", encoding="utf-8")
manifest = tmp_path / "manifest.json"
manifest.write_text(json.dumps(_manifest()), encoding="utf-8")
return assessments, manifest
def test_cli_compare_emits_canonical_json(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
from mrf_honest.cli import main
assessments, manifest = _write_cohort_inputs(tmp_path)
status = main(
[
"compare",
"--assessments",
str(assessments),
"--manifest",
str(manifest),
"--generated-at",
GENERATED_AT,
]
)
assert status == 0
payload = json.loads(capsys.readouterr().out)
assert payload["generated_at"] == GENERATED_AT
assert payload["summary"]["targeted"] == 2
def test_cli_compare_human_format_names_each_grade(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
from mrf_honest.cli import main
assessments, manifest = _write_cohort_inputs(tmp_path)
status = main(
[
"compare",
"--assessments",
str(assessments),
"--manifest",
str(manifest),
"--format",
"human",
]
)
assert status == 0
out = capsys.readouterr().out
assert "alpha-health/main: A" in out
assert "beta-health/north: B" in out
def test_cli_compare_reports_refusals_without_a_traceback(
tmp_path: Path, capsys: pytest.CaptureFixture[str]
) -> None:
from mrf_honest.cli import main
assessments, manifest = _write_cohort_inputs(tmp_path)
unattested = json.loads(manifest.read_text(encoding="utf-8"))
unattested["collection"]["operator_controlled_single_run"] = False
manifest.write_text(json.dumps(unattested), encoding="utf-8")
status = main(["compare", "--assessments", str(assessments), "--manifest", str(manifest)])
assert status == 1
assert "operator-controlled collection run" in capsys.readouterr().err