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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Tests for special ability trigger system from the design document
 * Tests all the different trigger events and their implementations
 */

import { describe, it, expect, beforeEach } from 'vitest';
import { BattleEngine } from './BattleEngine';
import { PicletDefinition, Move, SpecialAbility } from './types';
import { PicletType, AttackType } from './types';

const STANDARD_STATS = { hp: 100, attack: 80, defense: 70, speed: 60 };

describe('Special Ability Trigger System - TDD Implementation', () => {
  describe('Damage Triggers', () => {
    it('should handle onDamageTaken triggers', () => {
      const berserkAbility: SpecialAbility = {
        name: "Berserker",
        description: "Attack increases when taking damage",
        triggers: [
          {
            event: 'onDamageTaken',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { attack: 'increase' }
              }
            ]
          }
        ]
      };

      const berserkerPiclet: PicletDefinition = {
        name: "Rage Beast",
        description: "Gets stronger when hurt",
        tier: 'medium',
        primaryType: PicletType.BEAST,
        baseStats: STANDARD_STATS,
        nature: "Brave",
        specialAbility: berserkAbility,
        movepool: [{
          name: "Tackle", type: AttackType.NORMAL, power: 40, accuracy: 100, pp: 35,
          priority: 0, flags: [], effects: [{ type: 'damage', target: 'opponent', amount: 'normal' }]
        }]
      };

      expect(berserkAbility.triggers![0].event).toBe('onDamageTaken');
    });

    it('should handle onDamageDealt triggers', () => {
      const lifeStealAbility: SpecialAbility = {
        name: "Life Steal",
        description: "Heals when dealing damage",
        triggers: [
          {
            event: 'onDamageDealt',
            effects: [
              {
                type: 'heal',
                target: 'self',
                amount: 'small'
              }
            ]
          }
        ]
      };

      expect(lifeStealAbility.triggers![0].event).toBe('onDamageDealt');
    });

    it('should handle onContactDamage triggers', () => {
      const toxicSkin: SpecialAbility = {
        name: "Toxic Skin",
        description: "Physical contact poisons the attacker",
        triggers: [
          {
            event: 'onContactDamage',
            effects: [
              {
                type: 'applyStatus',
                target: 'attacker',
                status: 'poison',
                chance: 50
              }
            ]
          }
        ]
      };

      expect(toxicSkin.triggers![0].event).toBe('onContactDamage');
      expect(toxicSkin.triggers![0].effects[0].target).toBe('attacker');
    });
  });

  describe('Status Triggers', () => {
    it('should handle onStatusInflicted triggers', () => {
      const burnBoost: SpecialAbility = {
        name: "Burn Boost",
        description: "Fire damage energizes this Piclet, increasing attack power",
        triggers: [
          {
            event: 'onStatusInflicted',
            condition: 'ifStatus:burn',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { attack: 'greatly_increase' }
              }
            ]
          }
        ]
      };

      expect(burnBoost.triggers![0].event).toBe('onStatusInflicted');
      expect(burnBoost.triggers![0].condition).toBe('ifStatus:burn');
    });

    it('should handle onStatusMove triggers', () => {
      const statusReflect: SpecialAbility = {
        name: "Status Shield",
        description: "Reflects status moves back to user",
        triggers: [
          {
            event: 'onStatusMove',
            effects: [
              {
                type: 'mechanicOverride',
                mechanic: 'targetRedirection',
                value: 'reflect'
              }
            ]
          }
        ]
      };

      expect(statusReflect.triggers![0].event).toBe('onStatusMove');
    });

    it('should handle onStatusMoveTargeted triggers', () => {
      const statusCounter: SpecialAbility = {
        name: "Status Counter",
        description: "When targeted by status moves, counter with damage",
        triggers: [
          {
            event: 'onStatusMoveTargeted',
            effects: [
              {
                type: 'damage',
                target: 'attacker',
                formula: 'fixed',
                value: 30
              }
            ]
          }
        ]
      };

      expect(statusCounter.triggers![0].event).toBe('onStatusMoveTargeted');
    });
  });

  describe('Critical Hit Triggers', () => {
    it('should handle onCriticalHit triggers', () => {
      const criticalMomentum: SpecialAbility = {
        name: "Critical Momentum",
        description: "Critical hits increase speed",
        triggers: [
          {
            event: 'onCriticalHit',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { speed: 'increase' }
              }
            ]
          }
        ]
      };

      expect(criticalMomentum.triggers![0].event).toBe('onCriticalHit');
    });
  });

  describe('HP Drain Triggers', () => {
    it('should handle onHPDrained triggers', () => {
      const drainPunish: SpecialAbility = {
        name: "Drain Punishment",
        description: "Damages opponents who try to drain HP",
        triggers: [
          {
            event: 'onHPDrained',
            effects: [
              {
                type: 'damage',
                target: 'attacker',
                formula: 'fixed',
                value: 25
              }
            ]
          }
        ]
      };

      expect(drainPunish.triggers![0].event).toBe('onHPDrained');
    });
  });

  describe('KO Triggers', () => {
    it('should handle onKO triggers', () => {
      const koBoost: SpecialAbility = {
        name: "Victory Rush",
        description: "Gets stronger after knocking out an opponent",
        triggers: [
          {
            event: 'onKO',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { attack: 'greatly_increase', speed: 'increase' }
              }
            ]
          }
        ]
      };

      expect(koBoost.triggers![0].event).toBe('onKO');
    });
  });

  describe('Switch Triggers', () => {
    it('should handle onSwitchIn triggers', () => {
      const intimidate: SpecialAbility = {
        name: "Intimidate",
        description: "Lowers opponent's attack when entering battle",
        triggers: [
          {
            event: 'onSwitchIn',
            effects: [
              {
                type: 'modifyStats',
                target: 'opponent',
                stats: { attack: 'decrease' }
              }
            ]
          }
        ]
      };

      expect(intimidate.triggers![0].event).toBe('onSwitchIn');
      expect(intimidate.triggers![0].effects[0].target).toBe('opponent');
    });

    it('should handle onSwitchOut triggers', () => {
      const regenerator: SpecialAbility = {
        name: "Regenerator",
        description: "Restores HP when switching out",
        triggers: [
          {
            event: 'onSwitchOut',
            effects: [
              {
                type: 'heal',
                target: 'self',
                amount: 'small'
              }
            ]
          }
        ]
      };

      expect(regenerator.triggers![0].event).toBe('onSwitchOut');
    });

    it('should handle conditional switch-in triggers', () => {
      const stormCaller: SpecialAbility = {
        name: "Storm Caller",
        description: "Boosts attack when entering during storm weather",
        triggers: [
          {
            event: 'onSwitchIn',
            condition: 'ifWeather:storm',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { attack: 'increase' }
              }
            ]
          }
        ]
      };

      expect(stormCaller.triggers![0].condition).toBe('ifWeather:storm');
    });
  });

  describe('Weather Triggers', () => {
    it('should handle onWeatherChange triggers', () => {
      const weatherAdapt: SpecialAbility = {
        name: "Weather Adaptation",
        description: "Adapts stats based on weather changes",
        triggers: [
          {
            event: 'onWeatherChange',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { speed: 'increase' }
              }
            ]
          }
        ]
      };

      expect(weatherAdapt.triggers![0].event).toBe('onWeatherChange');
    });
  });

  describe('Move Use Triggers', () => {
    it('should handle beforeMoveUse triggers', () => {
      const movePrep: SpecialAbility = {
        name: "Move Preparation",
        description: "Boosts accuracy before using moves",
        triggers: [
          {
            event: 'beforeMoveUse',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { accuracy: 'increase' }
              }
            ]
          }
        ]
      };

      expect(movePrep.triggers![0].event).toBe('beforeMoveUse');
    });

    it('should handle afterMoveUse triggers', () => {
      const moveRecovery: SpecialAbility = {
        name: "Move Recovery",
        description: "Heals slightly after using any move",
        triggers: [
          {
            event: 'afterMoveUse',
            effects: [
              {
                type: 'heal',
                target: 'self',
                formula: 'fixed',
                value: 5
              }
            ]
          }
        ]
      };

      expect(moveRecovery.triggers![0].event).toBe('afterMoveUse');
    });
  });

  describe('HP Threshold Triggers', () => {
    it('should handle onLowHP triggers', () => {
      const emergencyMode: SpecialAbility = {
        name: "Emergency Mode",
        description: "Activates emergency protocols when HP is low",
        triggers: [
          {
            event: 'onLowHP',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { speed: 'greatly_increase', attack: 'increase' }
              },
              {
                type: 'mechanicOverride',
                mechanic: 'statusImmunity',
                value: ['burn', 'poison', 'paralyze']
              }
            ]
          }
        ]
      };

      expect(emergencyMode.triggers![0].event).toBe('onLowHP');
      expect(emergencyMode.triggers![0].effects).toHaveLength(2);
    });

    it('should handle onFullHP triggers', () => {
      const fullPower: SpecialAbility = {
        name: "Full Power",
        description: "Maximum power when at full health",
        triggers: [
          {
            event: 'onFullHP',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { attack: 'greatly_increase' }
              }
            ]
          }
        ]
      };

      expect(fullPower.triggers![0].event).toBe('onFullHP');
    });
  });

  describe('Turn-Based Triggers', () => {
    it('should handle endOfTurn triggers', () => {
      const turnRegeneration: SpecialAbility = {
        name: "Slow Regeneration",
        description: "Heals at the end of each turn",
        triggers: [
          {
            event: 'endOfTurn',
            effects: [
              {
                type: 'heal',
                target: 'self',
                formula: 'percentage',
                value: 10
              }
            ]
          }
        ]
      };

      expect(turnRegeneration.triggers![0].event).toBe('endOfTurn');
    });

    it('should handle conditional turn triggers', () => {
      const sleepHeal: SpecialAbility = {
        name: "Slumber Heal",
        description: "Restores HP while sleeping instead of being unable to act",
        triggers: [
          {
            event: 'endOfTurn',
            condition: 'ifStatus:sleep',
            effects: [
              {
                type: 'heal',
                target: 'self',
                formula: 'percentage',
                value: 15
              }
            ]
          }
        ]
      };

      expect(sleepHeal.triggers![0].condition).toBe('ifStatus:sleep');
    });
  });

  describe('Opponent Move Triggers', () => {
    it('should handle onOpponentContactMove triggers', () => {
      const contactPunish: SpecialAbility = {
        name: "Contact Punishment",
        description: "Damages opponents who use contact moves",
        triggers: [
          {
            event: 'onOpponentContactMove',
            effects: [
              {
                type: 'damage',
                target: 'attacker',
                formula: 'fixed',
                value: 15
              }
            ]
          }
        ]
      };

      expect(contactPunish.triggers![0].event).toBe('onOpponentContactMove');
    });

    it('should handle wind currents ability from design doc', () => {
      const windCurrents: SpecialAbility = {
        name: "Wind Currents",
        description: "Gains +25% speed when opponent uses a contact move",
        triggers: [
          {
            event: 'onOpponentContactMove',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { speed: 'increase' }
              }
            ]
          }
        ]
      };

      const zephyrSprite: PicletDefinition = {
        name: "Zephyr Sprite",
        description: "A mysterious floating creature that manipulates wind currents",
        tier: 'medium',
        primaryType: PicletType.SPACE,
        baseStats: { hp: 65, attack: 85, defense: 40, speed: 90 },
        nature: "hasty",
        specialAbility: windCurrents,
        movepool: [{
          name: "Tackle", type: AttackType.NORMAL, power: 40, accuracy: 100, pp: 35,
          priority: 0, flags: [], effects: [{ type: 'damage', target: 'opponent', amount: 'normal' }]
        }]
      };

      expect(windCurrents.triggers![0].event).toBe('onOpponentContactMove');
      expect(zephyrSprite.specialAbility.name).toBe('Wind Currents');
    });
  });

  describe('Complex Multi-Trigger Abilities', () => {
    it('should handle abilities with multiple triggers', () => {
      const complexAbility: SpecialAbility = {
        name: "Adaptive Guardian",
        description: "Complex ability with multiple trigger conditions",
        triggers: [
          {
            event: 'onSwitchIn',
            effects: [
              {
                type: 'modifyStats',
                target: 'self',
                stats: { defense: 'increase' }
              }
            ]
          },
          {
            event: 'onDamageTaken',
            condition: 'ifLowHp',
            effects: [
              {
                type: 'mechanicOverride',
                mechanic: 'damageReflection',
                value: 0.3
              }
            ]
          },
          {
            event: 'endOfTurn',
            condition: 'ifStatus:burn',
            effects: [
              {
                type: 'removeStatus',
                target: 'self',
                status: 'burn'
              },
              {
                type: 'modifyStats',
                target: 'self',
                stats: { attack: 'increase' }
              }
            ]
          }
        ]
      };

      expect(complexAbility.triggers).toHaveLength(3);
      expect(complexAbility.triggers![1].condition).toBe('ifLowHp');
      expect(complexAbility.triggers![2].effects).toHaveLength(2);
    });
  });

  describe('Status-Specific Ability Examples', () => {
    it('should handle Glacial Birth - starts battle frozen', () => {
      const glacialBirth: SpecialAbility = {
        name: "Glacial Birth",
        description: "Enters battle in a frozen state but gains defensive bonuses",
        triggers: [
          {
            event: 'onSwitchIn',
            effects: [
              {
                type: 'applyStatus',
                target: 'self',
                status: 'freeze',
                chance: 100
              },
              {
                type: 'modifyStats',
                target: 'self',
                stats: { defense: 'greatly_increase' },
                condition: 'whileFrozen'
              }
            ]
          }
        ]
      };

      expect(glacialBirth.triggers![0].effects[0].status).toBe('freeze');
      expect(glacialBirth.triggers![0].effects[1].condition).toBe('whileFrozen');
    });

    it('should handle Cryogenic Touch - freezes on contact', () => {
      const cryogenicTouch: SpecialAbility = {
        name: "Cryogenic Touch",
        description: "Contact moves have a chance to freeze the attacker",
        triggers: [
          {
            event: 'onContactDamage',
            effects: [
              {
                type: 'applyStatus',
                target: 'attacker',
                status: 'freeze',
                chance: 30
              }
            ]
          }
        ]
      };

      expect(cryogenicTouch.triggers![0].effects[0].chance).toBe(30);
    });

    it('should handle Paralytic Aura - paralyzes on entry', () => {
      const paralyticAura: SpecialAbility = {
        name: "Paralytic Aura",
        description: "Intimidating presence paralyzes the opponent upon entry",
        triggers: [
          {
            event: 'onSwitchIn',
            effects: [
              {
                type: 'applyStatus',
                target: 'opponent',
                status: 'paralyze',
                chance: 75
              }
            ]
          }
        ]
      };

      expect(paralyticAura.triggers![0].effects[0].target).toBe('opponent');
      expect(paralyticAura.triggers![0].effects[0].chance).toBe(75);
    });

    it('should handle Confusion Clarity - team status removal', () => {
      const confusionClarity: SpecialAbility = {
        name: "Confusion Clarity",
        description: "Clear mind prevents confusion and helps allies focus",
        effects: [
          {
            type: 'mechanicOverride',
            mechanic: 'statusImmunity',
            value: ['confuse']
          }
        ],
        triggers: [
          {
            event: 'onSwitchIn',
            effects: [
              {
                type: 'removeStatus',
                target: 'allies',
                status: 'confuse'
              }
            ]
          }
        ]
      };

      expect(confusionClarity.effects![0].value).toContain('confuse');
      expect(confusionClarity.triggers![0].effects[0].target).toBe('allies');
    });
  });
});