diff --git a/README.md b/README.md index 023f371..276f9f9 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,9 @@ so the reviewed checklist can be identified exactly. Validation rejects nonfinite schedule/parameter/formulation values, duplicate parameter names and invalid feed-gap limits. It checks the interval from the last feed to the endpoint as well as intervals between feeds. +It also requires exactly one endpoint, no active step after it, and an explicit +QC gate on an executable step. Gates must be lists of nonblank strings. A +narrative note, blank gate label, or empty `qc` step does not count as a gate. This checks encoded consistency, not the accuracy of a source transcription. Programmatically changing a parameter does not rewrite the narrative steps; diff --git a/src/protocolcompiler/validate.py b/src/protocolcompiler/validate.py index 687a27e..37c15ab 100644 --- a/src/protocolcompiler/validate.py +++ b/src/protocolcompiler/validate.py @@ -32,6 +32,7 @@ def validate(protocol: Protocol) -> list[str]: f"{param.name}={param.value} {param.unit} is outside {param.low}–{param.high}" ) seen = set() + has_qc_gate = False for step in protocol.steps: if step.id in seen: errors.append(f"duplicate step id {step.id}") @@ -46,16 +47,25 @@ def validate(protocol: Protocol) -> list[str]: "coat", "seed", "medium_change", "passage", "qc", "endpoint", "note", }: errors.append(f"{step.id} has unknown action {step.action}") + if not isinstance(step.gates, list) or any( + not isinstance(gate, str) or not gate.strip() for gate in step.gates): + errors.append(f"{step.id} gates must be a list of nonblank strings") + elif step.action != "note" and step.gates: + has_qc_gate = True for row in protocol.formulation: if not math.isfinite(row.amount) or row.amount <= 0: errors.append(f"{row.name} formulation amount must be finite and positive") if not errors: _exclusive_overlap(protocol, errors) _feed_gaps(protocol, errors) - if not any(step.action == "qc" or step.gates for step in protocol.steps): + if not has_qc_gate: errors.append("no QC gate") - if not any(step.action == "endpoint" for step in protocol.steps): - errors.append("no endpoint") + endpoints = [step for step in protocol.steps if step.action == "endpoint"] + if len(endpoints) != 1: + errors.append("exactly one endpoint is required") + elif any(step.action != "note" and step.start_hour > endpoints[0].start_hour + for step in protocol.steps): + errors.append("active step occurs after endpoint") _dual_smad_warning(protocol, warnings) if errors: raise ProtocolError(errors) diff --git a/tests/test_validation.py b/tests/test_validation.py index 2762f27..fb306bd 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -2,7 +2,7 @@ from protocolcompiler.compile import compile_protocol from protocolcompiler.library import giwi -from protocolcompiler.schema import ProtocolError +from protocolcompiler.schema import ProtocolError, Step from protocolcompiler.validate import validate @@ -30,3 +30,62 @@ def test_compiled_digest_is_stable_and_tracks_changes(self): self.assertEqual(digest, compile_protocol(giwi())["protocol_sha256"]) protocol.steps[0].title = "Reviewed coat step" self.assertNotEqual(digest, compile_protocol(protocol)["protocol_sha256"]) + + def test_multiple_endpoints_are_rejected(self): + protocol = giwi() + protocol.steps.append(Step("early_endpoint", 300, "Earlier endpoint", "Review.", + "endpoint", hood_minutes=0)) + with self.assertRaisesRegex(ProtocolError, "exactly one endpoint"): + validate(protocol) + + def test_active_step_after_endpoint_is_rejected(self): + protocol = giwi() + protocol.steps.append(Step("late_qc", 456, "Late QC", "Review.", "qc", + hood_minutes=0, gates=["review"])) + with self.assertRaisesRegex(ProtocolError, "after endpoint"): + validate(protocol) + + def test_note_with_gate_is_not_a_qc_action(self): + protocol = giwi() + for step in protocol.steps: + step.gates = [] + if step.action == "qc": + step.action = "note" + step.hood_minutes = 0 + step.gates = ["mentioned but not performed"] + with self.assertRaisesRegex(ProtocolError, "no QC gate"): + validate(protocol) + + def test_qc_action_without_an_explicit_gate_is_rejected(self): + protocol = giwi() + for step in protocol.steps: + step.gates = [] + with self.assertRaisesRegex(ProtocolError, "no QC gate"): + validate(protocol) + + def test_blank_or_nontext_entries_cannot_satisfy_the_qc_gate_requirement(self): + for gate in ["", " ", "\t\n", "\u2003", None, 0, True, []]: + with self.subTest(gate=gate): + protocol = giwi() + for step in protocol.steps: + step.gates = [] + protocol.steps[0].gates = [gate] + with self.assertRaisesRegex(ProtocolError, "no QC gate"): + validate(protocol) + + def test_malformed_gate_fields_are_rejected_before_compilation(self): + for gates in [None, 1, "review", {"review": True}, ["review", " "], ["review", 1]]: + with self.subTest(gates=gates): + protocol = giwi() + protocol.steps[0].gates = gates + with self.assertRaisesRegex(ProtocolError, "gates must be a list of nonblank strings"): + compile_protocol(protocol) + + def test_nonblank_gate_on_an_executable_step_is_preserved(self): + protocol = giwi() + for step in protocol.steps: + step.gates = [] + protocol.steps[0].gates = [" documented review "] + compiled = compile_protocol(protocol) + step = next(row for row in compiled["schedule"] if row["id"] == protocol.steps[0].id) + self.assertEqual(step["gates"], [" documented review "])