File size: 7,708 Bytes
463f868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! Tests for Response phase flow, ability pausing, and resumption.
//! These tests verify that the engine correctly captures state and resumes
//! execution after player input.

use crate::core::logic::card_db::LOGIC_ID_MASK;
use crate::test_helpers::create_test_state;

use crate::core::logic::*;

fn create_test_db() -> CardDatabase {
    let mut db = CardDatabase::default();

    // Member 500 with O_RECOVER_MEMBER ability
    let m = MemberCard {
        card_id: 500,
        abilities: vec![Ability {
            trigger: TriggerType::Activated, // or whatever triggers manually
            bytecode: vec![O_RECOVER_MEMBER, 1, 0, 0, 0, O_RETURN, 0, 0, 0, 0],
            ..Default::default()
        }],
        ..Default::default()
    };
    db.members.insert(500, m.clone());
    db.members.insert(
        99,
        MemberCard {
            card_id: 99,
            ..Default::default()
        },
    );
    db.members_vec.resize(8431, None);
    db.members_vec[(500 as usize) & LOGIC_ID_MASK as usize] = Some(db.members[&500].clone());
    db.members_vec[(99 as usize) & LOGIC_ID_MASK as usize] = Some(db.members[&99].clone());

    db
}

/// Verifies that an ability requiring a target selection correctly pauses and enters Response phase.
#[test]
fn test_ability_pause_for_selection() {
    let db = create_test_db();
    let mut state = create_test_state();

    state.players[0].stage[0] = 500;
    state.players[0].discard = vec![99, 99].into(); // ID 99 is in our test DB
    println!(
        "DEBUG: Discard before recovery: {:?}",
        state.players[0].discard
    );

    // Activate ability 0 on slot 0
    state.activate_ability(&db, 0, 0).unwrap();

    // Should be in Response phase, waiting for choice
    assert_eq!(state.phase, Phase::Response);
    assert_eq!(
        state
            .interaction_stack
            .last()
            .map(|p| p.effect_opcode)
            .unwrap_or(0),
        O_RECOVER_MEMBER
    );
    assert!(!state.interaction_stack.is_empty());
    assert_eq!(state.players[0].looked_cards.len(), 2);
}

/// Verifies that providing a choice correctly resumes the ability and transitions back to Main phase.
#[test]
fn test_ability_resumption_after_choice() {
    let db = create_test_db();
    let mut state = create_test_state();

    state.players[0].stage[0] = 500;
    state.players[0].discard = vec![99, 99].into();

    // 1. Pause
    state.activate_ability(&db, 0, 0).unwrap();
    assert_eq!(state.phase, Phase::Response);

    // 2. Resume with choice (Select Card 99 at index 0)
    state.activate_ability_with_choice(&db, 0, 0, 0, 0).unwrap();

    // 3. Verify results
    assert_eq!(state.phase, Phase::Main);
    assert!(state.players[0].hand.contains(&99));
    assert_eq!(state.players[0].discard.len(), 1);
    assert!(state.interaction_stack.is_empty());
}

/// Verifies that multiple activations or nested triggers correctly manage the pending context.
#[test]
fn test_nested_trigger_flow_simple() {
    let db = create_test_db();
    let mut state = create_test_state();

    // Trigger depth acts as a recursion counter. It should return to its initial state
    // after the trigger chain finishes.
    let initial_depth = state.trigger_depth;
    let ctx = AbilityContext {
        player_id: 0,
        ..Default::default()
    };

    state.trigger_abilities(&db, TriggerType::TurnStart, &ctx);

    // Verify that depth returned to its starting value
    assert_eq!(state.trigger_depth, initial_depth);
}

#[test]
fn test_nested_suspension_preserves_original_phase() {
    let db = CardDatabase::default();
    let mut state = create_test_state();

    // 1. Initial State: Performance results are being processed
    state.phase = Phase::LiveResult;
    let ctx = AbilityContext {
        player_id: 0,
        ..Default::default()
    };

    // 2. First suspension (e.g., an optional cost prompt)
    // We'll use O_MOVE_TO_DISCARD to simulate an optional discard cost
    crate::core::logic::interpreter::suspension::suspend_interaction(
        &mut state,
        &db,
        &ctx,
        10,
        O_MOVE_TO_DISCARD,
        0,
        crate::core::enums::ChoiceType::Optional,
        "",
        0,
        -1,
        Vec::new(),
        Vec::new(),
    );

    assert_eq!(state.phase, Phase::Response);
    assert_eq!(state.interaction_stack.len(), 1);
    assert_eq!(state.interaction_stack[0].original_phase, Phase::LiveResult);

    // 3. Second suspension (nested, e.g., the effect after cost is paid)
    // While still in Phase::Response, another suspension occurs.
    crate::core::logic::interpreter::suspension::suspend_interaction(
        &mut state,
        &db,
        &ctx,
        20,
        O_LOOK_AND_CHOOSE,
        0,
        crate::core::enums::ChoiceType::LookAndChoose,
        "",
        0,
        1,
        Vec::new(),
        Vec::new(),
    );

    assert_eq!(state.phase, Phase::Response);
    assert_eq!(state.interaction_stack.len(), 2);

    // CRITICAL CHECK: The second suspension must have captured LiveResult, NOT Response.
    assert_eq!(
        state.interaction_stack[1].original_phase,
        Phase::LiveResult,
        "Nested suspension failed to propagate the true original phase (LiveResult)!"
    );
}

#[test]
fn test_nested_suspension_real_flow() {
    let mut db = create_test_db();
    // Member 501 with nested suspension ability:
    // Opcode 22 (O_TAP_MEMBER) with attr 2 (Optional)
    // Then Opcode 17 (O_RECOVER_MEMBER)
    db.members.insert(
        501,
        MemberCard {
            card_id: 501,
            abilities: vec![Ability {
                trigger: TriggerType::Activated,
                bytecode: vec![
                    O_TAP_MEMBER,
                    0,
                    2,
                    0,
                    0,
                    O_RECOVER_MEMBER,
                    1,
                    0,
                    0,
                    0,
                    O_RETURN,
                    0,
                    0,
                    0,
                    0,
                ],
                ..Default::default()
            }],
            ..Default::default()
        },
    );
    let logic_id = (501 as usize) & LOGIC_ID_MASK as usize;
    db.members_vec.resize(logic_id + 1, None);
    db.members_vec[logic_id] = Some(db.members[&501].clone());

    let mut state = create_test_state();
    state.players[0].stage[0] = 501;
    state.players[0].discard = vec![99, 99].into();
    state.phase = Phase::LiveResult;

    // 1. Initial activation -> First suspension (Optional Tap)
    state.activate_ability(&db, 0, 0).unwrap();
    assert_eq!(state.phase, Phase::Response);
    assert_eq!(state.interaction_stack.len(), 1);
    assert_eq!(state.interaction_stack[0].original_phase, Phase::LiveResult);

    // 2. Resume first suspension -> This triggers the second suspension (Recover Selection)
    // Choice 0 = "Yes" for the optional tap
    state.activate_ability_with_choice(&db, 0, 0, 0, 0).unwrap();

    // Should still be in Response phase, waiting for recover choice
    assert_eq!(state.phase, Phase::Response);
    assert_eq!(
        state.interaction_stack.len(),
        1,
        "Should have 1 pending interaction (the nested one)"
    );

    // THE CRITICAL CHECK:
    assert_eq!(
        state.interaction_stack[0].original_phase,
        Phase::LiveResult,
        "Nested suspension lost the original LiveResult phase!"
    );
}