-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_property_invariants.py
More file actions
296 lines (261 loc) · 10.3 KB
/
Copy pathtest_property_invariants.py
File metadata and controls
296 lines (261 loc) · 10.3 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
"""Property-based tests for the merge-blocking status-algebra invariants.
These generate synthetic bundles and mutations to exercise the
machine-checkable subset of the invariants in
``docs/09-TEST-AND-EVALUATION.md`` section 3 against the iteration-1
evaluator: no pass without affirmative evidence (1), not-applicable only
from a predeclared rule (3), no cross-concept coercion (4 and 9), and
deterministic identical payloads for identical inputs (10). Invariants
that need pack lifecycle, review signatures, HTML rendering, or
signature verification (2, 5, 6, 7, 8) have no shipped component yet and
are deliberately absent here.
The same generated bundles also feed the property-layer half of the
published receipt contract (B-033): every receipt document a generated
bundle produces must validate against
``schemas/contextsafe-receipt-v0.1.schema.json``.
"""
import json
from pathlib import Path
from typing import Any
from hypothesis import given, settings
from hypothesis import strategies as st
from jsonschema import Draft202012Validator, FormatChecker
from contextsafe.canonical import sha256_json
from contextsafe.errors import ContextSafeError
from contextsafe.evaluator import Outcome, evaluate
from contextsafe.models import (
CASE_SCHEMA_VERSION,
OBSERVATION_SCHEMA_VERSION,
RULE_SET_SCHEMA_VERSION,
Checkpoint,
ConceptKind,
EvaluationBundle,
EvidencePointer,
GenderIdentity,
MappingDescriptor,
NameToUse,
Observation,
Pronouns,
RecordedSexOrGender,
Rule,
RuleSet,
SemanticValue,
SexParameterForClinicalUse,
SyntheticCase,
SyntheticIdentifier,
ValueStatus,
)
from contextsafe.receipt import build_receipt, build_receipt_document, render_receipt
from contextsafe.validation import parse_bundle
ROOT = Path(__file__).resolve().parents[1]
REFERENCE = ROOT / "fixtures" / "reference"
RECEIPT_SCHEMA = json.loads(
(ROOT / "schemas" / "contextsafe-receipt-v0.1.schema.json").read_text(
encoding="utf-8"
)
)
_VALUE_MARKER = "CSYNPROPVAL"
_TOKENS = st.text(alphabet="ABCDEFGH", min_size=1, max_size=6).map(
lambda suffix: f"{_VALUE_MARKER}-{suffix}"
)
_STATUSES = st.sampled_from(
(ValueStatus.SPECIFIED, ValueStatus.DECLINED, ValueStatus.UNKNOWN)
)
@st.composite
def _semantic_values(draw: st.DrawFn, concept: ConceptKind) -> SemanticValue:
token = draw(_TOKENS)
status = draw(_STATUSES)
value = token if status is ValueStatus.SPECIFIED else None
if concept is ConceptKind.GENDER_IDENTITY:
return GenderIdentity(
status=status, value=value, code_system="urn:contextsafe:fixture"
)
if concept is ConceptKind.RECORDED_SEX_OR_GENDER:
return RecordedSexOrGender(
value=draw(st.sampled_from(("F", "M", "X", "unknown"))),
context=token,
source="synthetic-fixture",
)
if concept is ConceptKind.SEX_PARAMETER_FOR_CLINICAL_USE:
return SexParameterForClinicalUse(
value=token,
context_id=f"ORDER-CSYN-{draw(_TOKENS)}",
supporting_observation_ids=(f"SUP-CSYN-{draw(_TOKENS)}",),
)
if concept is ConceptKind.NAME_TO_USE:
return NameToUse(
status=status,
value=None if value is None else f"CSYN-{value}",
use="usual",
)
return Pronouns(status=status, value=value)
@st.composite
def _rules(draw: st.DrawFn, index: int) -> Rule:
concept = draw(st.sampled_from(tuple(ConceptKind)))
return Rule(
rule_id=f"A-I{index:02d}",
version="0.1.0",
case_id=draw(st.sampled_from(("CTP-P01", "CTP-P02"))),
checkpoint=draw(st.sampled_from(tuple(Checkpoint))),
concept=concept,
expected=draw(_semantic_values(concept)),
required=draw(st.booleans()),
)
@st.composite
def _observations(draw: st.DrawFn, rule: Rule, index: int) -> Observation:
aligned = draw(st.booleans())
concept = rule.concept if aligned else draw(st.sampled_from(tuple(ConceptKind)))
matches_expected = draw(st.booleans())
value = (
rule.expected
if aligned and matches_expected and concept is rule.concept
else draw(_semantic_values(concept))
)
return Observation(
schema_version=OBSERVATION_SCHEMA_VERSION,
observation_id=f"OBS-P{index:02d}",
case_id=rule.case_id
if aligned
else draw(st.sampled_from(("CTP-P01", "CTP-P02"))),
checkpoint=rule.checkpoint
if aligned
else draw(st.sampled_from(tuple(Checkpoint))),
concept=concept,
value=value,
evidence=EvidencePointer(
source_sha256=sha256_json(value.to_dict()),
source_pointer="$.concepts",
),
mapping=MappingDescriptor(
source_concept=concept,
target_concept=concept,
mapping_version="0.1.0",
),
)
@st.composite
def _bundles(draw: st.DrawFn) -> EvaluationBundle:
rule_count = draw(st.integers(min_value=1, max_value=4))
rules = tuple(draw(_rules(index)) for index in range(rule_count))
observations: list[Observation] = []
observation_index = 0
for rule in rules:
for _ in range(draw(st.integers(min_value=0, max_value=3))):
observations.append(draw(_observations(rule, observation_index)))
observation_index += 1
case = SyntheticCase(
schema_version=CASE_SCHEMA_VERSION,
case_id="CTP-P01",
synthetic_identifier=SyntheticIdentifier(
system="urn:contextsafe:synthetic", value="CSYN-CTP-P01"
),
gender_identity=draw(_semantic_values(ConceptKind.GENDER_IDENTITY)),
recorded_sex_or_gender=(),
sex_parameter_for_clinical_use=(),
name_to_use=draw(_semantic_values(ConceptKind.NAME_TO_USE)),
pronouns=draw(_semantic_values(ConceptKind.PRONOUNS)),
prohibited_inferences=(
"gender_identity_to_spcu",
"recorded_sex_or_gender_to_spcu",
),
)
return EvaluationBundle(
case=case,
observations=tuple(observations),
rule_set=RuleSet(schema_version=RULE_SET_SCHEMA_VERSION, rules=rules),
)
def _outcome_for(rule: Rule, outcomes: tuple[Outcome, ...]) -> Outcome:
matched = [item for item in outcomes if item.rule_id == rule.rule_id]
assert len(matched) == 1
return matched[0]
@settings(max_examples=200, deadline=None)
@given(bundle=_bundles())
def test_pass_requires_exactly_one_affirmative_evidence_match(
bundle: EvaluationBundle,
) -> None:
"""Invariant 1: missing or ambiguous evidence can never produce pass."""
for outcome in evaluate(bundle):
if outcome.status.value == "pass":
assert len(outcome.observed_sha256s) == 1
assert outcome.observed_sha256s[0] == outcome.expected_sha256
assert outcome.reason == "affirmative_evidence_match"
if not outcome.observed_sha256s:
assert outcome.status.value in {"indeterminate", "not_applicable"}
if len(outcome.observed_sha256s) > 1:
assert outcome.status.value in {"indeterminate", "not_applicable"}
@settings(max_examples=200, deadline=None)
@given(bundle=_bundles())
def test_not_applicable_comes_only_from_a_predeclared_rule(
bundle: EvaluationBundle,
) -> None:
"""Invariant 3: not-applicable requires a pre-observation rule."""
outcomes = evaluate(bundle)
for rule in bundle.rule_set.rules:
outcome = _outcome_for(rule, outcomes)
if rule.required:
assert outcome.status.value != "not_applicable"
else:
assert outcome.status.value == "not_applicable"
assert outcome.reason == "predeclared_not_applicable"
@settings(max_examples=200, deadline=None)
@given(
bundle=_bundles(),
observation_seed=st.randoms(use_true_random=False),
)
def test_identical_inputs_yield_byte_identical_receipts(
bundle: EvaluationBundle, observation_seed: Any
) -> None:
"""Invariant 10: identical deterministic inputs, identical payloads."""
first = render_receipt(build_receipt(bundle, evaluate(bundle)))
permuted_observations = list(bundle.observations)
permuted_rules = list(bundle.rule_set.rules)
observation_seed.shuffle(permuted_observations)
observation_seed.shuffle(permuted_rules)
permuted = EvaluationBundle(
case=bundle.case,
observations=tuple(permuted_observations),
rule_set=RuleSet(
schema_version=bundle.rule_set.schema_version,
rules=tuple(permuted_rules),
),
)
second = render_receipt(build_receipt(permuted, evaluate(permuted)))
assert first == second
@settings(max_examples=200, deadline=None)
@given(bundle=_bundles())
def test_receipt_never_echoes_generated_semantic_values(
bundle: EvaluationBundle,
) -> None:
"""Receipts stay value-minimized: hashes appear, semantic values do not."""
rendered = render_receipt(build_receipt(bundle, evaluate(bundle)))
assert _VALUE_MARKER not in rendered
@settings(max_examples=200, deadline=None)
@given(bundle=_bundles())
def test_generated_receipts_match_the_published_receipt_contract(
bundle: EvaluationBundle,
) -> None:
"""B-033: every emitted document conforms to the published contract."""
validator = Draft202012Validator(RECEIPT_SCHEMA, format_checker=FormatChecker())
validator.validate(build_receipt_document(bundle, evaluate(bundle)))
@settings(max_examples=100, deadline=None)
@given(
source_index=st.integers(min_value=0, max_value=4),
target_concept=st.sampled_from(tuple(ConceptKind)),
)
def test_cross_concept_assignment_is_rejected_not_coerced(
source_index: int, target_concept: ConceptKind
) -> None:
"""Invariants 4 and 9: a value can never cross canonical concept types."""
case = json.loads((REFERENCE / "case.json").read_text(encoding="utf-8"))
observations = json.loads(
(REFERENCE / "observations.json").read_text(encoding="utf-8")
)
rules = json.loads((REFERENCE / "rules.json").read_text(encoding="utf-8"))
entry = observations["observations"][source_index]
if entry["concept"] == target_concept.value:
parse_bundle(case, observations, rules)
return
entry["concept"] = target_concept.value
try:
parse_bundle(case, observations, rules)
except ContextSafeError:
return
raise AssertionError("cross-concept observation was accepted")