File size: 2,284 Bytes
9a96fdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
897318a
9a96fdc
897318a
9a96fdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
897318a
 
 
 
 
 
 
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
import pytest

from spi.schemas.project_state import Constraint, ProjectState


def test_delta_bumps_version_and_logs():
    s = ProjectState(prompt="a lighthouse at dusk")
    v0 = s.state_version
    s.apply_delta("Test", "set_intent", "goal set",
                  [{"op": "set", "path": "intent.goal", "value": "lighthouse"}])
    assert s.state_version == v0 + 1
    assert s.mutations[-1].agent == "Test"
    assert s.mutations[-1].version == s.state_version


def test_nested_delta_merge():
    s = ProjectState()
    s.apply_delta("T", "a", "merge concept",
                  [{"op": "merge", "path": "concept", "value": {"theme": "memory"}}])
    s.apply_delta("T", "a", "merge more",
                  [{"op": "merge", "path": "concept", "value": {"camera": "slow push"}}])
    assert s.concept["theme"] == "memory" and s.concept["camera"] == "slow push"


def test_append_delta():
    s = ProjectState()
    s.apply_delta("T", "a", "add constraint",
                  [{"op": "append", "path": "constraints",
                    "value": Constraint(constraint_id="C_001", text="no dialogue")}])
    assert s.constraints[0].constraint_id == "C_001"
    assert s.constraints[0].hard is True


def test_unknown_op_raises():
    s = ProjectState()
    try:
        s.apply_delta("T", "a", "bad", [{"op": "delete", "path": "concept", "value": {}}])
        raise AssertionError("unknown op must raise")
    except ValueError as e:
        assert "delete" in str(e)


def test_lazy_get_context():
    s = ProjectState(prompt="x")
    s.apply_delta("T", "a", "s", [{"op": "set", "path": "intent.goal", "value": "g"}])
    ctx = s.get_context(["intent.goal", "concept.missing"])
    assert ctx["intent.goal"] == "g" and ctx["concept.missing"] is None


def test_json_roundtrip():
    s = ProjectState(prompt="roundtrip")
    s.apply_delta("T", "a", "s", [{"op": "set", "path": "intent.goal", "value": "g"}])
    s2 = ProjectState.from_json(s.to_json())
    assert s2.prompt == "roundtrip" and s2.state_version == s.state_version
    assert len(s2.mutations) == 1


def test_validate_assignment_coerces_dicts():
    s = ProjectState()
    s.plan = {"project_id": "SPI-X", "scenes": [], "shots": [], "jobs": []}
    assert hasattr(s.plan, "shots")  # dict coerced into ProductionPlan