manabrew-engine 0.2.3

Magic: The Gathering rules engine — a Rust port of Forge
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
use forge_foundation::{CardTypeLine, ColorSet, ManaCost, ZoneType};
/// Integration tests for Zone Change Effects (Issue #13):
/// ChangeZone, ChangeZoneAll, Sacrifice, SacrificeAll
use manabrew_engine::agent::{PassAgent, PlayerAgent};
use manabrew_engine::card::CardInstance;
use manabrew_engine::game::GameState;
use manabrew_engine::game_loop::GameLoop;
use manabrew_engine::game_rng::GameRng;
use manabrew_engine::ids::{CardId, PlayerId};
use manabrew_engine::spellability::{SpellAbility, StackEntry};

// ── Card constructors ────────────────────────────────────────────────

fn make_grizzly_bears(owner: PlayerId) -> CardInstance {
    CardInstance::new(
        CardId(0),
        "Grizzly Bears".to_string(),
        owner,
        CardTypeLine::parse("Creature - Bear"),
        ManaCost::parse("1 G"),
        ColorSet::GREEN,
        Some(2),
        Some(2),
        vec![],
        vec![],
    )
}

fn make_forest(owner: PlayerId) -> CardInstance {
    CardInstance::new(
        CardId(0),
        "Forest".to_string(),
        owner,
        CardTypeLine::parse("Basic Land - Forest"),
        ManaCost::no_cost(),
        ColorSet::COLORLESS,
        None,
        None,
        vec![],
        vec![],
    )
}

fn make_mountain(owner: PlayerId) -> CardInstance {
    CardInstance::new(
        CardId(0),
        "Mountain".to_string(),
        owner,
        CardTypeLine::parse("Basic Land - Mountain"),
        ManaCost::no_cost(),
        ColorSet::COLORLESS,
        None,
        None,
        vec![],
        vec![],
    )
}

fn make_test_source(owner: PlayerId) -> CardInstance {
    CardInstance::new(
        CardId(0),
        "Test Source".to_string(),
        owner,
        CardTypeLine::parse("Artifact"),
        ManaCost::no_cost(),
        ColorSet::COLORLESS,
        None,
        None,
        vec![],
        vec![],
    )
}

fn effect_source(game: &mut GameState, controller: PlayerId) -> CardId {
    let source = game.create_card(make_test_source(controller));
    game.move_card(source, ZoneType::Command, controller);
    source
}

/// Build a minimal 2-agent PassAgent slice for tests that don't care about choices.
fn pass_agents() -> Vec<Box<dyn PlayerAgent>> {
    vec![Box::new(PassAgent), Box::new(PassAgent)]
}

struct ReverseShuffleRng;

impl GameRng for ReverseShuffleRng {
    fn shuffle_cards(&mut self, cards: &mut [CardId]) {
        cards.reverse();
    }

    fn next_int(&mut self, _bound: i32) -> i32 {
        0
    }
}

/// Push a fake non-permanent spell entry for testing effect resolution.
fn push_effect_entry(
    game: &mut GameState,
    controller: PlayerId,
    ability_text: &str,
    target_card: Option<CardId>,
    target_player: Option<PlayerId>,
    source: Option<CardId>,
) {
    let mut sa = SpellAbility::new_simple(source, controller, ability_text);
    sa.target_chosen.target_card = target_card;
    sa.target_chosen.target_player = target_player;
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);
}

// ── Test 1: Bounce to Hand (Battlefield → Hand) ──────────────────────

/// ChangeZone Battlefield→Hand on a targeted creature (bounce effect like Unsummon).
#[test]
fn test_bounce_to_hand() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let p1 = PlayerId(1);

    // Put a creature on Bob's battlefield
    let bears = game.create_card(make_grizzly_bears(p1));
    game.move_card(bears, ZoneType::Battlefield, p1);

    // Alice casts an Unsummon-like effect targeting Bob's creature
    let ability = "SP$ ChangeZone | Origin$ Battlefield | Destination$ Hand | ValidTgts$ Creature";
    push_effect_entry(&mut game, p0, ability, Some(bears), None, None);

    // Create a fake source card for the effect (not important, just needs to exist)
    // Actually, the effect resolves the stack and moves to graveyard, but there's no source card.
    // We need to wrap this as a triggered/activated ability so it doesn't try to move a card
    // from source. Let's use an activated ability entry instead.
    // Clear the stack and use is_activated_ability = true
    game.stack.pop();

    let mut sa = SpellAbility::new_simple(Some(effect_source(&mut game, p0)), p0, ability);
    sa.is_activated = true;
    sa.target_chosen.target_card = Some(bears);
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.game_rng = Box::new(ReverseShuffleRng);
    game_loop.resolve_stack(&mut game, &mut agents);

    // Creature should now be in Bob's hand
    assert_eq!(
        game.card(bears).zone,
        ZoneType::Hand,
        "Bounced creature should be in Bob's hand"
    );
    assert_eq!(
        game.zone(ZoneType::Battlefield, p1).len(),
        0,
        "Bob's battlefield should be empty"
    );
    assert_eq!(
        game.zone(ZoneType::Hand, p1).len(),
        1,
        "Bob should have 1 card in hand (bounced creature)"
    );
}

// ── Test 2: Exile Permanent (Battlefield → Exile) ────────────────────

/// ChangeZone Battlefield→Exile on a targeted creature (Swords to Plowshares style).
#[test]
fn test_exile_permanent() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let p1 = PlayerId(1);

    // Put a creature on Bob's battlefield
    let bears = game.create_card(make_grizzly_bears(p1));
    game.move_card(bears, ZoneType::Battlefield, p1);

    let ability = "SP$ ChangeZone | Origin$ Battlefield | Destination$ Exile | ValidTgts$ Creature";
    let mut sa = SpellAbility::new_simple(Some(effect_source(&mut game, p0)), p0, ability);
    sa.is_activated = true;
    sa.target_chosen.target_card = Some(bears);
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.resolve_stack(&mut game, &mut agents);

    assert_eq!(
        game.card(bears).zone,
        ZoneType::Exile,
        "Exiled creature should be in Exile zone"
    );
    assert_eq!(
        game.zone(ZoneType::Battlefield, p1).len(),
        0,
        "Bob's battlefield should be empty"
    );
}

// ── Test 3: Reanimate (Graveyard → Battlefield) ──────────────────────

/// ChangeZone Graveyard→Battlefield on a creature in the graveyard (Animate Dead style).
#[test]
fn test_reanimate() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let p1 = PlayerId(1);

    // Put a creature in Bob's graveyard
    let bears = game.create_card(make_grizzly_bears(p1));
    game.move_card(bears, ZoneType::Graveyard, p1);

    // Alice reanimates it onto the battlefield
    let ability = "SP$ ChangeZone | Origin$ Graveyard | Destination$ Battlefield | ValidTgts$ Creature.inZoneGraveyard";
    let mut sa = SpellAbility::new_simple(Some(effect_source(&mut game, p0)), p0, ability);
    sa.is_activated = true;
    sa.target_chosen.target_card = Some(bears);
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.resolve_stack(&mut game, &mut agents);

    // Without explicit gain-control text, the card returns under its owner's control.
    assert_eq!(
        game.card(bears).zone,
        ZoneType::Battlefield,
        "Reanimated creature should be on the battlefield"
    );
    assert_eq!(
        game.zone(ZoneType::Graveyard, p1).len(),
        0,
        "Bob's graveyard should be empty after reanimation"
    );
    // It returns under Bob's (owner's) control.
    assert_eq!(
        game.zone(ZoneType::Battlefield, p1).len(),
        1,
        "The reanimated creature should be on Bob's battlefield (owner controls it)"
    );
}

// ── Test 4: Raise Dead (Graveyard → Hand) ────────────────────────────

/// ChangeZone Graveyard→Hand on a creature in the graveyard (Raise Dead style).
#[test]
fn test_raise_dead() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let _p1 = PlayerId(1);

    // Put a creature in Alice's graveyard
    let bears = game.create_card(make_grizzly_bears(p0));
    game.move_card(bears, ZoneType::Graveyard, p0);

    // Alice casts Raise Dead targeting her own creature
    let ability = "SP$ ChangeZone | Origin$ Graveyard | Destination$ Hand | ValidTgts$ Creature.inZoneGraveyard";
    let mut sa = SpellAbility::new_simple(Some(effect_source(&mut game, p0)), p0, ability);
    sa.is_activated = true;
    sa.target_chosen.target_card = Some(bears);
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.resolve_stack(&mut game, &mut agents);

    assert_eq!(
        game.card(bears).zone,
        ZoneType::Hand,
        "Raised creature should be in hand"
    );
    assert_eq!(
        game.zone(ZoneType::Graveyard, p0).len(),
        0,
        "Alice's graveyard should be empty"
    );
    assert_eq!(
        game.zone(ZoneType::Hand, p0).len(),
        1,
        "Alice should have 1 card in hand"
    );
}

/// ChangeZone Graveyard->Library with Shuffle$ True must actually shuffle the
/// destination library, matching Java's ChangeZoneEffect behavior.
#[test]
fn test_graveyard_to_library_with_shuffle() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);

    let forest = game.create_card(make_forest(p0));
    let mountain_one = game.create_card(make_mountain(p0));
    let mountain_two = game.create_card(make_mountain(p0));
    let bears = game.create_card(make_grizzly_bears(p0));

    game.move_card(forest, ZoneType::Library, p0);
    game.move_card(mountain_one, ZoneType::Library, p0);
    game.move_card(mountain_two, ZoneType::Library, p0);
    game.move_card(bears, ZoneType::Graveyard, p0);

    let expected_without_shuffle = vec![forest, mountain_one, mountain_two, bears];

    let ability =
        "SP$ ChangeZone | Origin$ Graveyard | Destination$ Library | Defined$ Self | Shuffle$ True";
    let mut sa = SpellAbility::new_simple(Some(bears), p0, ability);
    sa.is_activated = true;
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.resolve_stack(&mut game, &mut agents);

    assert_eq!(
        game.card(bears).zone,
        ZoneType::Library,
        "The moved card should end up in the library"
    );
    assert_ne!(
        game.zone(ZoneType::Library, p0).cards,
        expected_without_shuffle,
        "Shuffle$ True should not leave the library in simple append order"
    );
}

// ── Test 5: ChangeZoneAll Board Wipe (Battlefield → Exile) ───────────

/// ChangeZoneAll Battlefield→Exile moves all creatures off the battlefield.
#[test]
fn test_change_zone_all_board_wipe() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let p1 = PlayerId(1);

    // Put creatures on both battlefields
    let alice_bears = game.create_card(make_grizzly_bears(p0));
    game.move_card(alice_bears, ZoneType::Battlefield, p0);

    let bob_bears = game.create_card(make_grizzly_bears(p1));
    game.move_card(bob_bears, ZoneType::Battlefield, p1);

    // Also put a land on Alice's battlefield — it should NOT be exiled
    let alice_forest = game.create_card(make_forest(p0));
    game.move_card(alice_forest, ZoneType::Battlefield, p0);

    // Exile all creatures (Cataclysm-style)
    let ability =
        "SP$ ChangeZoneAll | Origin$ Battlefield | Destination$ Exile | ValidCards$ Creature";
    let mut sa = SpellAbility::new_simple(Some(effect_source(&mut game, p0)), p0, ability);
    sa.is_activated = true;
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.resolve_stack(&mut game, &mut agents);

    // Both creatures should be exiled
    assert_eq!(
        game.card(alice_bears).zone,
        ZoneType::Exile,
        "Alice's creature should be in Exile"
    );
    assert_eq!(
        game.card(bob_bears).zone,
        ZoneType::Exile,
        "Bob's creature should be in Exile"
    );
    // Land should remain on battlefield
    assert_eq!(
        game.card(alice_forest).zone,
        ZoneType::Battlefield,
        "Land should remain on the battlefield"
    );
    // Both players' battlefields should only have land (Alice has 1, Bob has 0)
    assert_eq!(
        game.zone(ZoneType::Battlefield, p0).len(),
        1,
        "Alice's battlefield should have only the land"
    );
    assert_eq!(
        game.zone(ZoneType::Battlefield, p1).len(),
        0,
        "Bob's battlefield should be empty"
    );
}

// ── Test 6: Sacrifice Self (SacValid$ Self) ───────────────────────────

/// Sacrifice with SacValid$=Self sacrifices the source card.
#[test]
fn test_sacrifice_self() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let _p1 = PlayerId(1);

    // Put a creature on Alice's battlefield as the source of the ability
    let bears = game.create_card(make_grizzly_bears(p0));
    game.move_card(bears, ZoneType::Battlefield, p0);

    // The creature sacrifices itself (like a Loxodon Warhammer echo)
    let ability = "SP$ Sacrifice | SacValid$ Self";
    let mut sa = SpellAbility::new_simple(Some(bears), p0, ability); // source is the creature that sacrifices
    sa.is_activated = true;
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.resolve_stack(&mut game, &mut agents);

    assert_eq!(
        game.card(bears).zone,
        ZoneType::Graveyard,
        "Self-sacrificed creature should be in graveyard"
    );
    assert_eq!(
        game.zone(ZoneType::Battlefield, p0).len(),
        0,
        "Alice's battlefield should be empty"
    );
}

// ── Test 7: SacrificeAll Creatures ────────────────────────────────────

/// SacrificeAll moves all creatures from battlefield to graveyard.
#[test]
fn test_sacrifice_all_creatures() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let p1 = PlayerId(1);

    // Put creatures on both battlefields
    let alice_bears = game.create_card(make_grizzly_bears(p0));
    game.move_card(alice_bears, ZoneType::Battlefield, p0);

    let bob_bears = game.create_card(make_grizzly_bears(p1));
    game.move_card(bob_bears, ZoneType::Battlefield, p1);

    // Put a land on Alice's battlefield — it should survive
    let alice_forest = game.create_card(make_forest(p0));
    game.move_card(alice_forest, ZoneType::Battlefield, p0);

    // Sacrifice all creatures (Overwhelming Splendor style)
    let ability = "SP$ SacrificeAll | ValidCards$ Creature";
    let mut sa = SpellAbility::new_simple(Some(effect_source(&mut game, p0)), p0, ability);
    sa.is_activated = true;
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.resolve_stack(&mut game, &mut agents);

    // Both creatures should be in their owners' graveyards
    assert_eq!(
        game.card(alice_bears).zone,
        ZoneType::Graveyard,
        "Alice's creature should be in graveyard"
    );
    assert_eq!(
        game.card(bob_bears).zone,
        ZoneType::Graveyard,
        "Bob's creature should be in graveyard"
    );
    // Land survives
    assert_eq!(
        game.card(alice_forest).zone,
        ZoneType::Battlefield,
        "Land should still be on battlefield"
    );
    // Graveyard counts
    assert_eq!(
        game.zone(ZoneType::Graveyard, p0).len(),
        1,
        "Alice's graveyard should have 1 card"
    );
    assert_eq!(
        game.zone(ZoneType::Graveyard, p1).len(),
        1,
        "Bob's graveyard should have 1 card"
    );
}

// ── Test 8: Sacrifice Defined$ Player — each player sacrifices ────────

/// Sacrifice with Defined$ Player (Innocent Blood) makes BOTH players sacrifice a creature.
#[test]
fn test_sacrifice_each_player() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let p1 = PlayerId(1);

    // Both players have a creature on the battlefield
    let alice_bears = game.create_card(make_grizzly_bears(p0));
    game.move_card(alice_bears, ZoneType::Battlefield, p0);

    let bob_bears = game.create_card(make_grizzly_bears(p1));
    game.move_card(bob_bears, ZoneType::Battlefield, p1);

    // Innocent Blood: each player sacrifices a creature
    let ability = "SP$ Sacrifice | SacValid$ Creature | Defined$ Player";
    let mut sa = SpellAbility::new_simple(Some(effect_source(&mut game, p0)), p0, ability);
    sa.is_activated = true;
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.resolve_stack(&mut game, &mut agents);

    // Both creatures must have been sacrificed
    assert_eq!(
        game.card(alice_bears).zone,
        ZoneType::Graveyard,
        "Alice's creature should be sacrificed (Defined$ Player affects each player)"
    );
    assert_eq!(
        game.card(bob_bears).zone,
        ZoneType::Graveyard,
        "Bob's creature should be sacrificed (Defined$ Player affects each player)"
    );
    assert_eq!(
        game.zone(ZoneType::Battlefield, p0).len(),
        0,
        "Alice's battlefield should be empty"
    );
    assert_eq!(
        game.zone(ZoneType::Battlefield, p1).len(),
        0,
        "Bob's battlefield should be empty"
    );
}

// ── Test 9: Tuck to Library Bottom (LibraryPosition$ -1) ─────────────

/// ChangeZone Battlefield→Library with LibraryPosition$=-1 places the card at the bottom.
#[test]
fn test_tuck_to_library_bottom() {
    let mut game = GameState::new(&["Alice", "Bob"], 20);
    let p0 = PlayerId(0);
    let p1 = PlayerId(1);

    // Give Alice some cards in her library first
    let library_card1 = game.create_card(make_mountain(p0));
    game.move_card(library_card1, ZoneType::Library, p0);
    let library_card2 = game.create_card(make_forest(p0));
    game.move_card(library_card2, ZoneType::Library, p0);

    // Put a creature on Bob's battlefield that will get tucked
    let bears = game.create_card(make_grizzly_bears(p1));
    game.move_card(bears, ZoneType::Battlefield, p1);

    // Alice casts Condemn on Bob's attacking creature — puts it on bottom of its owner's library
    let ability = "SP$ ChangeZone | Origin$ Battlefield | Destination$ Library | LibraryPosition$ -1 | ValidTgts$ Creature";
    let mut sa = SpellAbility::new_simple(Some(effect_source(&mut game, p0)), p0, ability);
    sa.is_activated = true;
    sa.target_chosen.target_card = Some(bears);
    let entry = StackEntry {
        id: 0,
        spell_ability: sa,
        is_creature_spell: false,
        is_permanent_spell: false,
        is_pending_cast: false,
        cast_from_zone: None,
        optional_trigger_decider: None,
        optional_trigger_description: None,
        optional_trigger_source_name: None,
    };
    game.stack.push(entry);

    let mut agents = pass_agents();
    let mut game_loop = GameLoop::new(2);
    game_loop.resolve_stack(&mut game, &mut agents);

    // Creature should be in Bob's library (its owner)
    assert_eq!(
        game.card(bears).zone,
        ZoneType::Library,
        "Tucked creature should be in Bob's library"
    );
    assert_eq!(
        game.zone(ZoneType::Battlefield, p1).len(),
        0,
        "Bob's battlefield should be empty"
    );

    // The tucked card should be at the bottom (index 0 in our internal representation)
    // Bob's library only has the bears card
    let bob_library = &game.zone(ZoneType::Library, p1).cards;
    assert_eq!(bob_library.len(), 1, "Bob's library should have 1 card");
    assert_eq!(
        bob_library[0], bears,
        "Tucked creature should be at bottom of Bob's library (index 0)"
    );

    // Alice's library should be untouched
    assert_eq!(
        game.zone(ZoneType::Library, p0).len(),
        2,
        "Alice's library should still have 2 cards"
    );
}