File size: 2,403 Bytes
08ef203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
server/tests/test_handoff_validator.py

Unit tests for HandoffValidator.
"""

import pytest
from server.handoff_validator import HandoffValidator


@pytest.fixture
def validator():
    return HandoffValidator()


def _valid_handoff(extra: str = "") -> str:
    return (
        "TASK: Implement a stack.\n"
        "COMPLETED:\n- push() done\n- pop() done\n"
        "REMAINING:\n- __repr__ method\n- iterable support\n"
        "KEY FUNCTIONS:\n- Stack.push(val)\n- Stack.pop() -> val\n"
        "EDGE CASES:\n- pop on empty raises IndexError\n"
        "NEXT STEPS:\n1. Add __repr__\n2. Add __iter__\n3. Run all tests\n"
        + extra
    )


class TestHandoffValidator:

    def test_valid_handoff(self, validator):
        result = validator.validate(_valid_handoff())
        assert result.valid is True

    def test_missing_section(self, validator):
        bad = _valid_handoff().replace("TASK:", "SUBJECT:")
        result = validator.validate(bad)
        assert result.valid is False
        assert "TASK:" in result.reason

    def test_all_missing_sections(self, validator):
        for section in validator.REQUIRED_SECTIONS:
            bad = _valid_handoff().replace(section, section.replace(":", ""))
            result = validator.validate(bad)
            assert result.valid is False

    def test_empty_handoff(self, validator):
        result = validator.validate("")
        assert result.valid is False

    def test_code_block_too_long(self, validator):
        code = "```python\n" + "x = 1\n" * 10 + "```\n"
        result = validator.validate(_valid_handoff(extra=code))
        assert result.valid is False
        assert "Code block" in result.reason

    def test_code_block_at_limit(self, validator):
        code = "```python\n" + "x = 1\n" * 5 + "```\n"  # exactly 5 — allowed
        result = validator.validate(_valid_handoff(extra=code))
        assert result.valid is True

    def test_token_limit_exceeded(self, validator):
        long_extra = (" wordX" * 450)   # 450 extra tokens, well over 400 limit
        result = validator.validate(_valid_handoff(extra=long_extra))
        assert result.valid is False
        assert "too long" in result.reason

    def test_token_count_at_limit(self, validator):
        # Valid handoff is ~80 tokens — well within 400
        result = validator.validate(_valid_handoff())
        assert result.valid is True