| 1 | """Tests for the validated runtime turn state machine.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from loader.runtime.phases import ( |
| 8 | TurnPhase, |
| 9 | TurnStateMachine, |
| 10 | TurnTransitionKind, |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | def test_turn_state_machine_accepts_valid_transitions() -> None: |
| 15 | machine = TurnStateMachine() |
| 16 | |
| 17 | prepare = machine.transition( |
| 18 | TurnPhase.PREPARE, |
| 19 | reason_code="prepare_runtime", |
| 20 | reason_summary="Preparing runtime state", |
| 21 | ) |
| 22 | assistant = machine.transition( |
| 23 | TurnPhase.ASSISTANT, |
| 24 | reason_code="request_assistant_response", |
| 25 | reason_summary="Requesting assistant response", |
| 26 | ) |
| 27 | tools = machine.transition( |
| 28 | TurnPhase.TOOLS, |
| 29 | reason_code="execute_tool_batch", |
| 30 | reason_summary="Executing tool batch", |
| 31 | ) |
| 32 | finalize = machine.transition( |
| 33 | TurnPhase.FINALIZE, |
| 34 | reason_code="turn_complete", |
| 35 | reason_summary="Finalizing completed turn", |
| 36 | kind=TurnTransitionKind.TERMINAL, |
| 37 | ) |
| 38 | |
| 39 | assert prepare is not None |
| 40 | assert prepare.from_phase is None |
| 41 | assert prepare.to_phase == "prepare" |
| 42 | assert assistant is not None |
| 43 | assert assistant.from_phase == "prepare" |
| 44 | assert tools is not None |
| 45 | assert tools.from_phase == "assistant" |
| 46 | assert finalize is not None |
| 47 | assert finalize.kind is TurnTransitionKind.TERMINAL |
| 48 | assert machine.current_phase == "finalize" |
| 49 | assert machine.last_transition == finalize |
| 50 | assert finalize.summary == "tools -> finalize [terminal] Finalizing completed turn" |
| 51 | |
| 52 | |
| 53 | def test_turn_state_machine_rejects_invalid_transitions() -> None: |
| 54 | machine = TurnStateMachine() |
| 55 | machine.transition( |
| 56 | TurnPhase.PREPARE, |
| 57 | reason_code="prepare_runtime", |
| 58 | reason_summary="Preparing runtime state", |
| 59 | ) |
| 60 | |
| 61 | with pytest.raises(ValueError, match="prepare -> tools"): |
| 62 | machine.transition( |
| 63 | TurnPhase.TOOLS, |
| 64 | reason_code="execute_tool_batch", |
| 65 | reason_summary="Executing tool batch", |
| 66 | ) |