File size: 4,970 Bytes
d02cfdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from calender_en.models import CalenderEnAction
from calender_en.server.calender_en_environment import CalenderEnEnvironment


def _advance_to_proposal(env: CalenderEnEnvironment) -> None:
    env.step(
        CalenderEnAction(
            stage="understand_request",
            final_note="Identify participants, objective, and deadline.",
        )
    )
    env.step(
        CalenderEnAction(
            stage="evaluate_availability",
            final_note="Intersect shared availability and choose the earliest feasible option.",
        )
    )


def test_reset_returns_valid_initial_observation() -> None:
    env = CalenderEnEnvironment()

    observation = env.reset()

    assert observation.request
    assert observation.availability
    assert observation.constraints
    assert observation.step_count == 0
    assert observation.reward == 0.0
    assert observation.done is False
    assert observation.next_expected_stage == "understand_request"
    assert env.state.step_count == 0
    assert env.history == []
    assert env.solved is False


def test_step_follows_expected_multi_stage_flow() -> None:
    env = CalenderEnEnvironment()
    env.reset()

    understand = env.step(
        CalenderEnAction(
            stage="understand_request",
            final_note="Identify participants, objective, and deadline.",
        )
    )
    availability = env.step(
        CalenderEnAction(
            stage="evaluate_availability",
            final_note="Intersect shared availability and prioritize the earliest option.",
        )
    )
    proposal = env.step(
        CalenderEnAction(
            stage="propose_slot",
            proposed_time_slot="2026-04-08 10:00-10:30 UTC",
            final_note="Pick the earliest shared slot before the deadline.",
        )
    )
    confirmation = env.step(
        CalenderEnAction(
            stage="confirm_schedule",
            proposed_time_slot="2026-04-08 10:00-10:30 UTC",
            confirm_schedule=True,
            final_note="Confirmed and invite is ready.",
        )
    )

    assert understand.next_expected_stage == "evaluate_availability"
    assert availability.next_expected_stage == "propose_slot"
    assert proposal.next_expected_stage == "confirm_schedule"
    assert confirmation.done is True
    assert confirmation.next_expected_stage is None
    assert env.solved is True


def test_deterministic_scenario_cycling() -> None:
    env = CalenderEnEnvironment()

    requests = [env.reset().request for _ in range(4)]

    assert requests[0] != requests[1]
    assert requests[1] != requests[2]
    assert requests[0] == requests[3]


def test_correct_slot_scores_higher_than_wrong_slot() -> None:
    correct_env = CalenderEnEnvironment()
    correct_env.reset()
    _advance_to_proposal(correct_env)
    correct = correct_env.step(
        CalenderEnAction(
            stage="propose_slot",
            proposed_time_slot="2026-04-08 10:00-10:30 UTC",
            final_note="Pick the earliest shared slot before the deadline.",
        )
    )

    wrong_env = CalenderEnEnvironment()
    wrong_env.reset()
    _advance_to_proposal(wrong_env)
    wrong = wrong_env.step(
        CalenderEnAction(
            stage="propose_slot",
            proposed_time_slot="2026-04-08 14:00-14:30 UTC",
            final_note="Pick a slot even if it is not shared.",
        )
    )

    assert correct.reward > wrong.reward


def test_state_updates_episode_id_step_count_history_and_solved() -> None:
    env = CalenderEnEnvironment()

    first_reset = env.reset()
    first_episode_id = env.state.episode_id
    assert first_reset.step_count == 0
    assert env.history == []
    assert env.solved is False

    env.step(
        CalenderEnAction(
            stage="understand_request",
            final_note="Identify participants, objective, and deadline.",
        )
    )
    assert env.state.step_count == 1
    assert len(env.history) == 1
    assert env.history[0]["stage"] == "understand_request"
    assert env.solved is False

    env.step(
        CalenderEnAction(
            stage="evaluate_availability",
            final_note="Intersect shared availability and prioritize the earliest option.",
        )
    )
    env.step(
        CalenderEnAction(
            stage="propose_slot",
            proposed_time_slot="2026-04-08 10:00-10:30 UTC",
            final_note="Pick the earliest shared slot before the deadline.",
        )
    )
    env.step(
        CalenderEnAction(
            stage="confirm_schedule",
            proposed_time_slot="2026-04-08 10:00-10:30 UTC",
            confirm_schedule=True,
            final_note="Confirmed and invite is ready.",
        )
    )

    assert env.state.step_count == 4
    assert len(env.history) == 4
    assert env.solved is True

    env.reset()
    assert env.state.episode_id != first_episode_id
    assert env.state.step_count == 0
    assert env.history == []
    assert env.solved is False