genshin-calc-core 0.5.12

Genshin Impact damage calculation engine
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
use serde::{Deserialize, Serialize};

use crate::buff_types::BuffableStat;
use crate::enemy::Enemy;
use crate::error::CalcError;
use crate::lunar::{LunarInput, LunarResult, calculate_lunar};
use crate::reaction::Reaction;
use crate::team::BuffTarget;
use crate::types::{Element, ScalingStat};

/// Moonsign level determined by the number of Moonsign characters in the team.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MoonsignLevel {
    /// No Moonsign characters.
    None,
    /// 初照: 1 Moonsign character.
    NascentGleam,
    /// 満照: 2+ Moonsign characters.
    AscendantGleam,
}

/// Calculated Moonsign Benediction passive info for a character.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MoonsignBenediction {
    /// Calculated Base DMG Bonus value (0.0 to max_bonus).
    pub base_dmg_bonus: f64,
    /// Lunar reaction types this character enables.
    pub enabled_reactions: Vec<Reaction>,
}

/// Moonsign-level dependent talent enhancement.
///
/// Note: only `Serialize` is derived (not `Deserialize`) because `&'static str`
/// fields cannot implement `Deserialize` generically.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct MoonsignTalentEnhancement {
    /// Name of the character providing this enhancement.
    pub character_name: &'static str,
    /// Minimum moonsign level required to activate this enhancement.
    pub required_level: MoonsignLevel,
    /// Human-readable description of the enhancement effect.
    pub description: &'static str,
    /// The mechanical effect of this enhancement.
    pub effect: MoonsignTalentEffect,
}

/// Effect type for talent enhancements.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum MoonsignTalentEffect {
    /// Grant crit to a specific lunar reaction.
    GrantReactionCrit {
        /// The lunar reaction that receives the crit bonus.
        reaction: Reaction,
        /// Additional crit rate granted (decimal form).
        crit_rate: f64,
        /// Additional crit damage granted (decimal form).
        crit_dmg: f64,
    },
    /// Stat buff applied to the character or team.
    StatBuff {
        /// The stat being buffed.
        stat: BuffableStat,
        /// The buff value (decimal form).
        value: f64,
        /// Who receives the buff.
        target: BuffTarget,
    },
    /// Flat DMG bonus added to reaction_bonus for a specific lunar reaction.
    ReactionDmgBonus {
        /// The lunar reaction that receives the bonus.
        reaction: Reaction,
        /// Bonus value added to reaction_bonus (decimal form).
        bonus: f64,
    },
}

/// Team-level Moonsign context.
///
/// Note: only `Serialize` is derived (not `Deserialize`) because `talent_enhancements`
/// contains `MoonsignTalentEnhancement` which has `&'static str` fields.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct MoonsignContext {
    /// The resolved moonsign level for the team.
    pub level: MoonsignLevel,
    /// BaseDMGBonus per reaction type (Vec for WASM compatibility).
    pub base_dmg_bonus_by_reaction: Vec<(Reaction, f64)>,
    /// Non-moonsign character lunar DMG bonus (max 0.36, only at AscendantGleam).
    pub non_moonsign_lunar_bonus: f64,
    /// Active talent enhancements.
    pub talent_enhancements: Vec<MoonsignTalentEnhancement>,
}

impl MoonsignContext {
    /// Look up BaseDMGBonus for a specific reaction type.
    pub fn base_dmg_bonus_for(&self, reaction: Reaction) -> f64 {
        self.base_dmg_bonus_by_reaction
            .iter()
            .find(|(r, _)| *r == reaction)
            .map(|(_, v)| *v)
            .unwrap_or(0.0)
    }
}

/// Determines the moonsign level based on how many moonsign characters are in the team.
///
/// Returns [`MoonsignLevel::None`] for 0, [`MoonsignLevel::NascentGleam`] for 1, and [`MoonsignLevel::AscendantGleam`] for 2 or more moonsign characters.
pub fn determine_moonsign_level(moonsign_count: usize) -> MoonsignLevel {
    match moonsign_count {
        0 => MoonsignLevel::None,
        1 => MoonsignLevel::NascentGleam,
        _ => MoonsignLevel::AscendantGleam,
    }
}

/// Non-Moonsign character lunar buff definition.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct NonMoonsignLunarBuff {
    /// The stat this buff scales from (ATK, HP, DEF, or EM depending on element).
    pub scaling_stat: ScalingStat,
    /// Bonus rate per 1 unit of the scaling stat.
    pub rate: f64,
    /// Maximum bonus value (cap, decimal form).
    pub max_bonus: f64,
}

/// Returns the non-moonsign buff scaling for a given element.
pub fn non_moonsign_scaling(element: Element) -> NonMoonsignLunarBuff {
    match element {
        Element::Pyro | Element::Electro | Element::Cryo => NonMoonsignLunarBuff {
            scaling_stat: ScalingStat::Atk,
            rate: 0.00009,
            max_bonus: 0.36,
        },
        Element::Hydro => NonMoonsignLunarBuff {
            scaling_stat: ScalingStat::Hp,
            rate: 0.000006,
            max_bonus: 0.36,
        },
        Element::Geo => NonMoonsignLunarBuff {
            scaling_stat: ScalingStat::Def,
            rate: 0.0001,
            max_bonus: 0.36,
        },
        Element::Anemo | Element::Dendro => NonMoonsignLunarBuff {
            scaling_stat: ScalingStat::Em,
            rate: 0.000225,
            max_bonus: 0.36,
        },
    }
}

/// Calculates the bonus value from a non-moonsign lunar buff.
///
/// Multiplies the buff's rate by the given stat value, clamped to `buff.max_bonus`.
pub fn calculate_non_moonsign_bonus(buff: &NonMoonsignLunarBuff, stat_value: f64) -> f64 {
    (buff.rate * stat_value).min(buff.max_bonus)
}

/// Selects the highest non-moonsign buff value from a list of team members.
///
/// Each member is represented as `(element, stat_value)`. Returns the maximum bonus across all members, or `0.0` if the list is empty.
pub fn select_non_moonsign_buff(members: &[(Element, f64)]) -> f64 {
    members
        .iter()
        .map(|(elem, stat)| {
            let buff = non_moonsign_scaling(*elem);
            calculate_non_moonsign_bonus(&buff, *stat)
        })
        .fold(0.0_f64, f64::max)
}

const CONTRIBUTION_WEIGHTS: [f64; 4] = [1.0, 0.5, 1.0 / 12.0, 1.0 / 12.0];

/// A single character's contribution to a lunar reaction.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LunarContribution {
    /// The lunar input parameters for this contributing character.
    pub input: LunarInput,
}

/// Calculate weighted lunar reaction damage from multiple contributors.
///
/// # Errors
/// - `CalcError::InvalidTeamSize` if contributions is empty or > 4
pub fn calculate_lunar_team(
    contributions: &[LunarContribution],
    enemy: &Enemy,
) -> Result<LunarResult, CalcError> {
    if contributions.is_empty() || contributions.len() > 4 {
        return Err(CalcError::InvalidTeamSize(contributions.len()));
    }

    let mut results: Vec<LunarResult> = contributions
        .iter()
        .map(|c| calculate_lunar(&c.input, enemy))
        .collect::<Result<Vec<_>, _>>()?;

    // Sort by average descending
    results.sort_by(|a, b| {
        b.average
            .partial_cmp(&a.average)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let mut non_crit = 0.0;
    let mut crit = 0.0;
    let mut average = 0.0;

    for (i, result) in results.iter().enumerate() {
        let weight = CONTRIBUTION_WEIGHTS[i];
        non_crit += result.non_crit * weight;
        crit += result.crit * weight;
        average += result.average * weight;
    }

    // Use the damage element from the highest contributor
    let damage_element = results[0].damage_element;

    Ok(LunarResult {
        non_crit,
        crit,
        average,
        damage_element,
    })
}

/// Apply Moonsign talent enhancements to a LunarInput.
/// Matching GrantReactionCrit effects add crit_rate (capped at 1.0) and crit_dmg.
/// Matching ReactionDmgBonus effects add to reaction_bonus.
pub fn apply_moonsign_enhancements(
    input: &LunarInput,
    enhancements: &[MoonsignTalentEnhancement],
) -> LunarInput {
    let mut result = input.clone();
    for enh in enhancements {
        match &enh.effect {
            MoonsignTalentEffect::GrantReactionCrit {
                reaction,
                crit_rate,
                crit_dmg,
            } => {
                if *reaction == input.reaction {
                    result.crit_rate = (result.crit_rate + crit_rate).min(1.0);
                    result.crit_dmg += crit_dmg;
                }
            }
            MoonsignTalentEffect::ReactionDmgBonus { reaction, bonus } => {
                if *reaction == input.reaction {
                    result.reaction_bonus += bonus;
                }
            }
            MoonsignTalentEffect::StatBuff { .. } => {
                // StatBuff is applied at the stat-profile level, not directly to LunarInput
            }
        }
    }
    result
}

/// Resolve the team-level Moonsign context.
pub fn resolve_moonsign_context(
    moonsign_count: usize,
    benedictions: &[MoonsignBenediction],
    non_moonsign_bonus: f64,
    enhancements: Vec<MoonsignTalentEnhancement>,
) -> MoonsignContext {
    let level = determine_moonsign_level(moonsign_count);

    let mut by_reaction: Vec<(Reaction, f64)> = Vec::new();
    for bene in benedictions {
        for &reaction in &bene.enabled_reactions {
            if let Some(entry) = by_reaction.iter_mut().find(|(r, _)| *r == reaction) {
                entry.1 += bene.base_dmg_bonus;
            } else {
                by_reaction.push((reaction, bene.base_dmg_bonus));
            }
        }
    }

    MoonsignContext {
        level,
        base_dmg_bonus_by_reaction: by_reaction,
        non_moonsign_lunar_bonus: non_moonsign_bonus,
        talent_enhancements: enhancements,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const EPSILON: f64 = 1e-9;

    fn default_enemy() -> Enemy {
        Enemy {
            level: 90,
            resistance: 0.1,
            def_reduction: 0.0,
            def_ignore: 0.0,
        }
    }

    fn lunar_ec_input() -> LunarInput {
        LunarInput {
            character_level: 90,
            elemental_mastery: 0.0,
            reaction: Reaction::LunarElectroCharged,
            reaction_bonus: 0.0,
            crit_rate: 0.5,
            crit_dmg: 1.0,
            base_dmg_bonus: 0.0,
        }
    }

    fn lunar_bloom_input() -> LunarInput {
        LunarInput {
            character_level: 90,
            elemental_mastery: 0.0,
            reaction: Reaction::LunarBloom,
            reaction_bonus: 0.0,
            crit_rate: 0.5,
            crit_dmg: 1.0,
            base_dmg_bonus: 0.0,
        }
    }

    // =====================================================================
    // MoonsignLevel tests
    // =====================================================================

    #[test]
    fn test_moonsign_level_none() {
        assert_eq!(determine_moonsign_level(0), MoonsignLevel::None);
    }

    #[test]
    fn test_moonsign_level_nascent() {
        assert_eq!(determine_moonsign_level(1), MoonsignLevel::NascentGleam);
    }

    #[test]
    fn test_moonsign_level_ascendant() {
        assert_eq!(determine_moonsign_level(2), MoonsignLevel::AscendantGleam);
        assert_eq!(determine_moonsign_level(4), MoonsignLevel::AscendantGleam);
    }

    // =====================================================================
    // Context lookup tests
    // =====================================================================

    #[test]
    fn test_moonsign_context_lookup_found() {
        let ctx = MoonsignContext {
            level: MoonsignLevel::AscendantGleam,
            base_dmg_bonus_by_reaction: vec![(Reaction::LunarElectroCharged, 0.14)],
            non_moonsign_lunar_bonus: 0.0,
            talent_enhancements: vec![],
        };
        let bonus = ctx.base_dmg_bonus_for(Reaction::LunarElectroCharged);
        assert!((bonus - 0.14).abs() < EPSILON);
    }

    #[test]
    fn test_moonsign_context_lookup_not_found() {
        let ctx = MoonsignContext {
            level: MoonsignLevel::None,
            base_dmg_bonus_by_reaction: vec![],
            non_moonsign_lunar_bonus: 0.0,
            talent_enhancements: vec![],
        };
        let bonus = ctx.base_dmg_bonus_for(Reaction::LunarElectroCharged);
        assert!((bonus - 0.0).abs() < EPSILON);
    }

    // =====================================================================
    // Non-moonsign buff tests
    // =====================================================================

    #[test]
    fn test_non_moonsign_scaling_pyro() {
        let buff = non_moonsign_scaling(Element::Pyro);
        assert_eq!(buff.scaling_stat, ScalingStat::Atk);
        assert!((buff.max_bonus - 0.36).abs() < EPSILON);
    }

    #[test]
    fn test_non_moonsign_scaling_hydro() {
        let buff = non_moonsign_scaling(Element::Hydro);
        assert_eq!(buff.scaling_stat, ScalingStat::Hp);
    }

    #[test]
    fn test_non_moonsign_scaling_anemo() {
        let buff = non_moonsign_scaling(Element::Anemo);
        assert_eq!(buff.scaling_stat, ScalingStat::Em);
    }

    #[test]
    fn test_calculate_non_moonsign_bonus_atk_capped() {
        let buff = non_moonsign_scaling(Element::Pyro);
        // 4000 ATK * 0.00009 = 0.36, exactly at cap
        let result_4000 = calculate_non_moonsign_bonus(&buff, 4000.0);
        assert!((result_4000 - 0.36).abs() < EPSILON);
        // 5000 ATK * 0.00009 = 0.45, still capped at 0.36
        let result_5000 = calculate_non_moonsign_bonus(&buff, 5000.0);
        assert!((result_5000 - 0.36).abs() < EPSILON);
    }

    #[test]
    fn test_calculate_non_moonsign_bonus_below_cap() {
        let buff = non_moonsign_scaling(Element::Pyro);
        // 2000 ATK * 0.00009 = 0.18
        let result = calculate_non_moonsign_bonus(&buff, 2000.0);
        assert!((result - 0.18).abs() < EPSILON);
    }

    #[test]
    fn test_select_non_moonsign_buff_picks_max() {
        // Pyro 3000 ATK: 3000 * 0.00009 = 0.27
        // Dendro 800 EM: 800 * 0.000225 = 0.18
        let members = vec![(Element::Pyro, 3000.0), (Element::Dendro, 800.0)];
        let result = select_non_moonsign_buff(&members);
        assert!((result - 0.27).abs() < EPSILON);
    }

    #[test]
    fn test_select_non_moonsign_buff_empty() {
        let result = select_non_moonsign_buff(&[]);
        assert!((result - 0.0).abs() < EPSILON);
    }

    // =====================================================================
    // Contribution combination tests
    // =====================================================================

    #[test]
    fn test_lunar_team_single_contribution() {
        let input = lunar_ec_input();
        let solo_result = calculate_lunar(&input, &default_enemy()).unwrap();

        let contributions = vec![LunarContribution {
            input: input.clone(),
        }];
        let team_result = calculate_lunar_team(&contributions, &default_enemy()).unwrap();

        // Single contributor with weight 1.0 — should match solo result exactly
        assert!((team_result.non_crit - solo_result.non_crit).abs() < EPSILON);
        assert!((team_result.crit - solo_result.crit).abs() < EPSILON);
        assert!((team_result.average - solo_result.average).abs() < EPSILON);
        assert_eq!(team_result.damage_element, solo_result.damage_element);
    }

    #[test]
    fn test_lunar_team_two_contributions() {
        // Contributor A: LunarEC, 0.5 crit rate, 1.0 crit dmg
        let input_a = lunar_ec_input();
        // Contributor B: LunarEC, 0.3 crit rate, 0.8 crit dmg — lower average
        let input_b = LunarInput {
            crit_rate: 0.3,
            crit_dmg: 0.8,
            ..lunar_ec_input()
        };

        let result_a = calculate_lunar(&input_a, &default_enemy()).unwrap();
        let result_b = calculate_lunar(&input_b, &default_enemy()).unwrap();

        // Sort: whichever has higher average is weight 1.0, other is weight 0.5
        let (high, low) = if result_a.average >= result_b.average {
            (&result_a, &result_b)
        } else {
            (&result_b, &result_a)
        };

        let expected_non_crit = high.non_crit * 1.0 + low.non_crit * 0.5;
        let expected_crit = high.crit * 1.0 + low.crit * 0.5;
        let expected_average = high.average * 1.0 + low.average * 0.5;

        let contributions = vec![
            LunarContribution { input: input_a },
            LunarContribution { input: input_b },
        ];
        let team_result = calculate_lunar_team(&contributions, &default_enemy()).unwrap();

        assert!((team_result.non_crit - expected_non_crit).abs() < EPSILON);
        assert!((team_result.crit - expected_crit).abs() < EPSILON);
        assert!((team_result.average - expected_average).abs() < EPSILON);
    }

    #[test]
    fn test_lunar_team_two_contributions_reverse_order() {
        let input_a = lunar_ec_input();
        let input_b = LunarInput {
            crit_rate: 0.3,
            crit_dmg: 0.8,
            ..lunar_ec_input()
        };

        let contributions_ab = vec![
            LunarContribution {
                input: input_a.clone(),
            },
            LunarContribution {
                input: input_b.clone(),
            },
        ];
        let contributions_ba = vec![
            LunarContribution { input: input_b },
            LunarContribution { input: input_a },
        ];

        let result_ab = calculate_lunar_team(&contributions_ab, &default_enemy()).unwrap();
        let result_ba = calculate_lunar_team(&contributions_ba, &default_enemy()).unwrap();

        assert!((result_ab.non_crit - result_ba.non_crit).abs() < EPSILON);
        assert!((result_ab.crit - result_ba.crit).abs() < EPSILON);
        assert!((result_ab.average - result_ba.average).abs() < EPSILON);
    }

    #[test]
    fn test_lunar_team_empty_error() {
        let result = calculate_lunar_team(&[], &default_enemy());
        assert!(matches!(result, Err(CalcError::InvalidTeamSize(0))));
    }

    #[test]
    fn test_lunar_team_five_error() {
        let input = lunar_ec_input();
        let contributions = vec![
            LunarContribution {
                input: input.clone(),
            },
            LunarContribution {
                input: input.clone(),
            },
            LunarContribution {
                input: input.clone(),
            },
            LunarContribution {
                input: input.clone(),
            },
            LunarContribution { input },
        ];
        let result = calculate_lunar_team(&contributions, &default_enemy());
        assert!(matches!(result, Err(CalcError::InvalidTeamSize(5))));
    }

    // =====================================================================
    // Enhancement tests
    // =====================================================================

    fn make_bloom_crit_enhancement() -> MoonsignTalentEnhancement {
        MoonsignTalentEnhancement {
            character_name: "TestChar",
            required_level: MoonsignLevel::NascentGleam,
            description: "LunarBloom gets crit",
            effect: MoonsignTalentEffect::GrantReactionCrit {
                reaction: Reaction::LunarBloom,
                crit_rate: 0.20,
                crit_dmg: 0.50,
            },
        }
    }

    #[test]
    fn test_apply_moonsign_enhancement_matching_reaction() {
        let input = lunar_bloom_input();
        let enhancements = vec![make_bloom_crit_enhancement()];
        let result = apply_moonsign_enhancements(&input, &enhancements);
        assert!((result.crit_rate - (input.crit_rate + 0.20)).abs() < EPSILON);
        assert!((result.crit_dmg - (input.crit_dmg + 0.50)).abs() < EPSILON);
    }

    #[test]
    fn test_apply_moonsign_enhancement_non_matching_reaction() {
        // Input is LunarEC, enhancement targets LunarBloom — should not apply
        let input = lunar_ec_input();
        let enhancements = vec![make_bloom_crit_enhancement()];
        let result = apply_moonsign_enhancements(&input, &enhancements);
        assert!((result.crit_rate - input.crit_rate).abs() < EPSILON);
        assert!((result.crit_dmg - input.crit_dmg).abs() < EPSILON);
    }

    #[test]
    fn test_apply_moonsign_enhancement_crit_rate_capped() {
        let input = LunarInput {
            crit_rate: 0.95,
            ..lunar_bloom_input()
        };
        let enhancements = vec![make_bloom_crit_enhancement()]; // adds 0.20 → 1.15, should cap at 1.0
        let result = apply_moonsign_enhancements(&input, &enhancements);
        assert!((result.crit_rate - 1.0).abs() < EPSILON);
    }

    #[test]
    fn test_apply_moonsign_enhancement_empty() {
        let input = lunar_ec_input();
        let result = apply_moonsign_enhancements(&input, &[]);
        assert!((result.crit_rate - input.crit_rate).abs() < EPSILON);
        assert!((result.crit_dmg - input.crit_dmg).abs() < EPSILON);
        assert_eq!(result.reaction, input.reaction);
    }

    // =====================================================================
    // resolve_moonsign_context tests
    // =====================================================================

    #[test]
    fn test_resolve_context_no_moonsign() {
        let ctx = resolve_moonsign_context(0, &[], 0.0, vec![]);
        assert_eq!(ctx.level, MoonsignLevel::None);
        assert!(ctx.base_dmg_bonus_by_reaction.is_empty());
        assert!((ctx.non_moonsign_lunar_bonus - 0.0).abs() < EPSILON);
        assert!(ctx.talent_enhancements.is_empty());
    }

    #[test]
    fn test_resolve_context_single_benediction() {
        let bene = MoonsignBenediction {
            base_dmg_bonus: 0.14,
            enabled_reactions: vec![Reaction::LunarElectroCharged],
        };
        let ctx = resolve_moonsign_context(1, &[bene], 0.0, vec![]);
        assert_eq!(ctx.level, MoonsignLevel::NascentGleam);
        assert_eq!(ctx.base_dmg_bonus_by_reaction.len(), 1);
        let bonus = ctx.base_dmg_bonus_for(Reaction::LunarElectroCharged);
        assert!((bonus - 0.14).abs() < EPSILON);
    }

    #[test]
    fn test_resolve_context_stacking_same_reaction() {
        // Ineffa contributes 0.14 for LunarEC, Columbina contributes 0.07 for LunarEC
        let ineffa = MoonsignBenediction {
            base_dmg_bonus: 0.14,
            enabled_reactions: vec![Reaction::LunarElectroCharged],
        };
        let columbina = MoonsignBenediction {
            base_dmg_bonus: 0.07,
            enabled_reactions: vec![Reaction::LunarElectroCharged],
        };
        let ctx = resolve_moonsign_context(2, &[ineffa, columbina], 0.0, vec![]);
        assert_eq!(ctx.level, MoonsignLevel::AscendantGleam);
        let bonus = ctx.base_dmg_bonus_for(Reaction::LunarElectroCharged);
        assert!((bonus - 0.21).abs() < EPSILON);
    }
}