"""Unit tests for the machine's fixed code — no LLM needed. Run: .venv/bin/python -m pytest tests/ -q (or) .venv/bin/python tests/state.py """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from floop.dspy_modules import parse_subquestions from floop.state import (AnswerType, Budget, BudgetExhausted, EvidenceRow, EvidenceTable, OpenQuestion, QuestionStack, instantiate) def test_table_append_only_and_typed(): t = EvidenceTable() t.append(EvidenceRow(qid="Q1", answer_type=AnswerType.OPEN, payload={"value": "4"}, engine="stub:numeric", verified=False)) assert t.rows()[0].eid == "E1" try: t.append(EvidenceRow(qid="Q2", answer_type=AnswerType.OPEN, payload="prose!", engine="x")) # type: ignore[arg-type] raise AssertionError("prose payload must be refused") except ValueError: pass def test_pickable_dependency_order(): t = EvidenceTable() s = QuestionStack("goal?") a = s.add(OpenQuestion(text="a?")) b = s.add(OpenQuestion(text="b?", deps=[a.qid])) assert s.pickable(t) is a # b waits although it is first in line? no: a first t.append(EvidenceRow(qid=a.qid, answer_type=AnswerType.OPEN, payload={"value": "va"}, engine="stub")) assert s.pickable(t) is b def test_failed_dependency_cascade(): t = EvidenceTable() s = QuestionStack("goal?") a = s.add(OpenQuestion(text="a?", status="failed")) b = s.add(OpenQuestion(text="b?", deps=[a.qid])) c = s.add(OpenQuestion(text="c?", deps=[b.qid])) assert s.pickable(t) is None assert b.status == "failed" and c.status == "failed" assert s.all_terminal() def test_unknown_deps_never_pick(): t = EvidenceTable() s = QuestionStack("goal?") b = s.add(OpenQuestion(text="b?", deps=["Q7"])) # Q7 does not exist assert s.pickable(t) is None # waits forever: a dep with no row is not answered def test_budget_charge(): b = Budget(max_llm_calls=2, max_depth=2, max_retries=3, missing_extensions=1) b.charge_llm(); b.charge_llm() try: b.charge_llm() raise AssertionError("budget must refuse the 3rd call") except BudgetExhausted: pass def test_instantiate_slots(): t = EvidenceTable() t.append(EvidenceRow(qid="Q1", answer_type=AnswerType.CONSTANT, payload={"value": "Grover's algorithm"}, engine="stub")) out = instantiate("which algorithm: #E1?", t) assert out == "which algorithm: Grover's algorithm?" def test_parse_subquestions_forms(): subs = parse_subquestions( "1. How many qubits are needed? | none | FUNCTION\n" "2. What is the clock speed? | 1 | INTERVAL\n") assert subs[0]["text"].endswith("needed?") and subs[0]["deps"] == [] assert subs[1]["deps"] == [1] and subs[1]["answer_type"] == AnswerType.INTERVAL def test_parse_refs_and_garbage(): subs = parse_subquestions("depends on #E2 | #E2 | OPEN\nnot a subquestion line") assert subs and subs[0]["deps"] == ["#E2"] assert parse_subquestions("") == [] assert parse_subquestions("|||") == [] def test_child_budget_slice(): b = Budget(max_llm_calls=20, max_depth=2, max_retries=3, missing_extensions=1) b.llm_calls = 4 child = b.slice_for_child() assert 6 <= child.max_llm_calls <= 16 if __name__ == "__main__": fns = [v for k, v in sorted(globals().items()) if k.startswith("test_")] for fn in fns: fn() print(f"ok {fn.__name__}") print(f"\n{len(fns)} tests passed")