Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/test_preprocessor_heuristic_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
("`", "`"),
]
)
COMPOUND_SEPARATORS = st.sampled_from([" and ", "; ", "\n", ", then "])
CANONICAL_LOOKALIKE_WORDS = st.sampled_from(
[
"and butter",
"and clear examples",
"for prohibitively expensive cases",
"for removal-policy notes",
"near reset-policy docs",
]
)


@given(st.sampled_from(CANONICAL_DIRECTIVES), st.sampled_from([".", "!"]))
Expand Down Expand Up @@ -160,3 +170,33 @@ def test_heuristic_mixed_prose_connector_forms_never_directive(
for message in messages:
result = preprocess_heuristic(message)
assert result["outcome"] != PREPROCESS_OUTCOME_DIRECTIVE


@given(
st.sampled_from(CANONICAL_DIRECTIVES),
COMPOUND_SEPARATORS,
st.sampled_from(CANONICAL_DIRECTIVES),
)
def test_heuristic_compound_directives_always_abstain(
first: str, separator: str, second: str
) -> None:
assume(first != second)
result = preprocess_heuristic(f"{first}{separator}{second}")
assert result["outcome"] == PREPROCESS_OUTCOME_UNKNOWN
assert result["directive"] is None


@given(st.sampled_from(["use docker", "prohibit peanuts"]), CANONICAL_LOOKALIKE_WORDS)
def test_heuristic_singular_payload_with_canonical_looking_words_can_still_pass(
directive_seed: str, suffix: str
) -> None:
message = f"{directive_seed} {suffix}"
result = preprocess_heuristic(message)
assert result["outcome"] == PREPROCESS_OUTCOME_DIRECTIVE
assert result["directive"] == message


@given(st.sampled_from(["misuse", "re-use", "nonuse"]), NON_EMPTY_TEXT)
def test_heuristic_lexical_boundary_prevents_embedded_use_matches(prefix: str, suffix: str) -> None:
result = preprocess_heuristic(f"{prefix} docker {suffix}")
assert result["outcome"] != PREPROCESS_OUTCOME_DIRECTIVE
42 changes: 42 additions & 0 deletions tests/test_preprocessor_validator_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
st.builds(lambda a, b: f"{a}; {b}", st.text(max_size=30), st.text(max_size=30)),
st.builds(lambda a, b: f"{a}: {b}", st.text(max_size=30), st.text(max_size=30)),
)
COMPOUND_SEPARATORS = st.sampled_from([" and ", "; ", "\n", ", then "])
STRUCTURED_NON_DIRECTIVE_CLASSIFICATIONS = st.sampled_from(["no_directive", "unknown"])


@given(
Expand Down Expand Up @@ -140,3 +142,43 @@ def test_validate_output_always_has_null_for_non_directive(raw_output: object) -
assert isinstance(validated["output"], str)
else:
assert validated["output"] is None


@given(
st.sampled_from(CANONICAL_DIRECTIVES),
COMPOUND_SEPARATORS,
st.sampled_from(CANONICAL_DIRECTIVES),
)
def test_validate_compound_candidate_output_is_always_unknown(
first: str, separator: str, second: str
) -> None:
assume(first != second)
validated = validate_preprocessor_output(f"{first}{separator}{second}")
assert validated == {"classification": "unknown", "output": None}


@given(
STRUCTURED_NON_DIRECTIVE_CLASSIFICATIONS,
st.one_of(
st.none(), st.text(max_size=80), st.integers(), st.dictionaries(st.text(), st.none())
),
)
def test_validate_structured_non_directive_contract_requires_null_output(
classification: str, output: object
) -> None:
validated = validate_preprocessor_output({"classification": classification, "output": output})
if output is None:
assert validated == {"classification": classification, "output": None}
else:
assert validated == {"classification": "unknown", "output": None}


@given(st.one_of(st.text(max_size=120), st.none(), st.integers()))
def test_parse_and_validate_agree_on_directive_round_trip(raw_output: object) -> None:
parsed = parse_preprocessor_output(raw_output)
validated = validate_preprocessor_output(raw_output)
if parsed is None:
assert validated["classification"] != "directive"
assert validated["output"] is None
else:
assert validated == {"classification": "directive", "output": parsed}