opy-rs 0.3.8

Standalone OverPy-compatible .opy implementation with parsing, tooling, and bounded Workshop compilation.
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
//! Integration tests for the Workshop-independent tooling API:
//! multi-file project validation through [`opy_rs::tooling::check`],
//! semantic queries on the resolved model, and stable diagnostic codes for
//! representative malformed inputs.

use std::path::Path;

use opy_rs::diag::{Position, Span};
use opy_rs::tooling::{self, SymbolKind, check};

/// The WrightKit-authored multi-file fixture: `main.opy` includes
/// `shared/defs.opy`, declares `playervar P`, and uses symbols declared in
/// the included file (globalvar, subroutine, enum, macro) plus a `#!define`.
const MULTI_MAIN: &str = concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/tests/fixtures/multi-file/main.opy"
);

#[test]
fn multi_file_project_checks_and_resolves_end_to_end() {
    let source = std::fs::read_to_string(MULTI_MAIN).expect("fixture exists");
    let root = Path::new(MULTI_MAIN).parent().expect("fixture parent");
    let outcome = check(&source, MULTI_MAIN, root);

    assert!(
        outcome.is_clean(),
        "multi-file fixture must check clean, got: {:?}",
        outcome.diagnostics
    );
    let model = outcome.model.expect("a clean project resolves");

    // File registry: main file (id 0) plus one entry per include.
    assert_eq!(outcome.files.len(), 2);
    assert_eq!(outcome.files[0].id, 0);
    assert_eq!(outcome.files[1].path, "shared/defs.opy");
    assert_eq!(model.file(1), Some("shared/defs.opy"));

    // Declarations across both files: globalvar/subroutine/macro from the
    // include, playervar from the main file (enums are not retained in the
    // Opy HIR; they are queried separately).
    assert_eq!(model.declarations().len(), 5);
    assert!(model
        .declarations()
        .iter()
        .any(|decl| matches!(decl, opy_rs::hir::types::Declaration::GlobalVariable { name, .. } if name == "total")));
    assert!(model
        .declarations()
        .iter()
        .any(|decl| matches!(decl, opy_rs::hir::types::Declaration::PlayerVariable { name, .. } if name == "P")));

    // Rule listing: the rule plus the def'd subroutine (the include splices
    // first, so the def entry precedes the rule entry).
    assert_eq!(model.rules().len(), 2);
    assert!(model.rules().iter().any(|entry| matches!(
        entry,
        opy_rs::hir::types::RuleEntry::Rule(rule) if rule.name == "collect"
    )));
    assert!(model.rules().iter().any(|entry| matches!(
        entry,
        opy_rs::hir::types::RuleEntry::SubroutineDef { name, .. } if name == "finish"
    )));

    // Macro-expansion provenance: the #!define is recorded with its site.
    assert_eq!(model.defines().len(), 1);
    assert_eq!(model.defines()[0].name, "SCALE");

    // Custom enums are queryable even though they fold in the HIR.
    assert_eq!(model.enums().len(), 1);
    assert_eq!(model.enums()[0].name, "Direction");
    assert_eq!(model.enums()[0].members.len(), 2);

    // Symbols: bindings from both files with declaration provenance.
    let total = model.symbol("total").expect("globalvar from the include");
    assert_eq!(total.kind, SymbolKind::Global);
    assert_eq!(total.declaration.path, "shared/defs.opy");
    assert_eq!(total.declaration.start.line, 1);

    let p = model.symbol("P").expect("playervar from the main file");
    assert_eq!(p.kind, SymbolKind::Player);
    assert!(p.declaration.path.ends_with("main.opy"));

    let reset = model
        .symbol("resetScore")
        .expect("subroutine from the include");
    assert_eq!(reset.kind, SymbolKind::Subroutine);
    assert_eq!(
        model
            .symbol("doubleIt")
            .expect("macro from the include")
            .kind,
        SymbolKind::Macro
    );
    let finish_symbols: Vec<_> = model
        .symbols()
        .iter()
        .filter(|symbol| symbol.name == "finish")
        .collect();
    assert_eq!(finish_symbols.len(), 2);
    assert!(
        finish_symbols
            .iter()
            .any(|symbol| symbol.kind == SymbolKind::Subroutine)
    );
    assert!(
        finish_symbols
            .iter()
            .any(|symbol| symbol.kind == SymbolKind::Def)
    );

    // References: uses in the main file and in the def body resolve to the
    // included-file binding, each with its own file provenance. The
    // augmented assignment lowers to a Binary whose left re-uses the target,
    // so line 9 contributes two reference sites.
    assert_eq!(total.references.len(), 4);
    let main_refs: Vec<_> = total
        .references
        .iter()
        .filter(|reference| reference.file_id == 0)
        .collect();
    assert_eq!(main_refs.len(), 3);
    assert!(
        main_refs
            .iter()
            .all(|reference| reference.path.ends_with("main.opy"))
    );
    let defs_ref = total
        .references
        .iter()
        .find(|reference| reference.file_id == 1)
        .expect("the def body reference");
    assert_eq!(defs_ref.path, "shared/defs.opy");
    assert_eq!(defs_ref.start.line, 13);
    assert_eq!(reset.references.len(), 1);
    assert_eq!(reset.references[0].start.line, 11);
    assert_eq!(model.symbol("doubleIt").expect("macro").references.len(), 1);

    // Span → (file id, path, line/col) provenance, and span-based lookup.
    let main_reference = total
        .references
        .iter()
        .find(|reference| reference.start.line == 9)
        .expect("the augmented-assignment reference in the main file");
    let at_reference = model
        .provenance(main_reference.to_span())
        .expect("reference provenance");
    assert_eq!(at_reference.path, MULTI_MAIN);
    assert_eq!(at_reference.start.line, 9);
    assert_eq!(
        model
            .symbol_at(main_reference.to_span())
            .expect("symbol at reference")
            .name,
        "total"
    );

    // The model serializes for `opy-cli inspect` (declarations, rules,
    // references as one JSON document).
    let json = serde_json::to_value(&model).expect("the model serializes");
    assert_eq!(json["hir"]["protocol"]["name"], "wright/opy-hir");
    assert!(json["symbols"].as_array().expect("symbols array").len() >= 5);
    assert!(json["enums"].as_array().expect("enums array").len() == 1);
}

#[test]
fn tooling_exposes_define_member_identity() {
    let outcome = check(
        "#!defineMember VALUE 2\nglobalvar result\nrule \"member define\":\n    @Event global\n    result = VALUE\n",
        "main.opy",
        Path::new("."),
    );
    let model = outcome
        .model
        .expect("member define project must check clean");

    assert_eq!(model.defines().len(), 1);
    assert!(model.defines()[0].is_member);
}

/// Representative malformed inputs with their stable diagnostic codes (the
/// machine contract: codes and source locations, not wording).
#[rustfmt::skip]
const STABLE_DIAGNOSTICS: &[(&str, &str)] = &[
    // A bare dictionary is now lexed and rejected at the OPY semantic
    // boundary rather than being mistaken for settings content.
    ("globalvar money\nrule \"r\":\n    @Event global\n    money += {\n        Mei.GENERIC: 10,\n    }\n", "dict-access"),
    // Missing colon after the rule name.
    ("rule \"x\"\n    @Event global\n", "parse-error"),
    // Unknown builtin in statement position.
    ("rule \"r\":\n    @Event global\n    frobnicate()\n", "unknown-action"),
    // Unknown builtin in value position.
    ("globalvar x\nrule \"r\":\n    @Event global\n    x = frobnicate()\n", "unknown-value"),
    // Undeclared identifier.
    ("globalvar x\nrule \"r\":\n    @Event global\n    x = nope\n", "unknown-identifier"),
    // Unknown member function.
    ("rule \"r\":\n    @Event eachPlayer\n    eventPlayer.frobnicate()\n", "unknown-member"),
    // Positional overflow.
    ("globalvar g\nrule \"r\":\n    @Event global\n    chaseOverTime(g, 10, 3, 4, 5)\n", "invalid-arity"),
    // Value function in action position.
    ("rule \"r\":\n    @Event global\n    isGameInProgress()\n", "value-in-action-position"),
    // Macro arity mismatch at the expansion site (`#!define` macros expand
    // during preprocessing; `macro` statements do not arity-check).
    ("#!define double(x) x + x\nrule \"r\":\n    @Event global\n    double(1, 2)\n", "macro-arity"),
    // Unterminated settings block.
    ("settings {\n    \"gamemodes\": {}\n", "settings-invalid"),
];

#[test]
fn diagnostic_codes_are_stable_for_malformed_inputs() {
    for (source, expected_code) in STABLE_DIAGNOSTICS {
        let outcome = check(source, "main.opy", Path::new(""));
        let diagnostic = outcome
            .diagnostics
            .first()
            .unwrap_or_else(|| panic!("expected a '{expected_code}' diagnostic for:\n{source}"));
        assert_eq!(
            diagnostic.code, *expected_code,
            "unexpected first diagnostic for:\n{source}\ngot: {:?}",
            outcome.diagnostics
        );
        assert!(
            diagnostic.span.is_some(),
            "the '{expected_code}' diagnostic must be source-located:\n{source}"
        );
        assert!(outcome.model.is_none(), "a failing project has no model");
    }
}

#[test]
fn workshop_script_source_reports_exactly_one_diagnostic() {
    // opy-rs#420: pasted Workshop script must surface as exactly one
    // `workshop-source` diagnostic through `check` — the surface Wright/LPP
    // consume — not only at the parser layer.
    let outcome = check(
        "rule \"workshop style\" {\n    event {\n        Ongoing - Global;\n    }\n    actions {\n        Wait(1, Ignore Condition);\n    }\n}\n",
        "main.opy",
        Path::new(""),
    );
    assert_eq!(
        outcome.diagnostics.len(),
        1,
        "expected one diagnostic, got {:?}",
        outcome.diagnostics
    );
    let diagnostic = &outcome.diagnostics[0];
    assert_eq!(diagnostic.code, "workshop-source");
    assert_eq!(diagnostic.span.as_ref().expect("span").path, "main.opy");
    assert!(outcome.model.is_none(), "a failing project has no model");
}

#[test]
fn include_failures_are_stable_and_source_located() {
    // Missing include names the directive site.
    let outcome = check(
        "#!include \"nope.opy\"\n",
        "main.opy",
        Path::new("/nonexistent-opy-root"),
    );
    let diagnostic = outcome.diagnostics.first().expect("include-not-found");
    assert_eq!(diagnostic.code, "include-not-found");
    assert_eq!(diagnostic.span.as_ref().expect("span").start.line, 1);

    // An include cycle is detected through the tooling API too.
    let dir = std::env::temp_dir().join(format!("opy-tooling-cycle-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join("a.opy"), "#!include \"b.opy\"\n").unwrap();
    std::fs::write(dir.join("b.opy"), "#!include \"a.opy\"\n").unwrap();
    let main = std::fs::read_to_string(dir.join("a.opy")).unwrap();
    let outcome = check(&main, "a.opy", &dir);
    assert_eq!(
        outcome.diagnostics.first().expect("include-cycle").code,
        "include-cycle"
    );
    // The registry retains every file registered before the failure: a (main),
    // b, and the re-included a that triggered the cycle.
    assert_eq!(outcome.files.len(), 3);
    assert_eq!(outcome.files[1].path, "b.opy");
    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn all_parse_diagnostics_are_reported() {
    // Three broken constructs — two rules missing their colon and a stray
    // directive line — all surface as parse-error diagnostics (the parser
    // recovers at statement boundaries; the tooling API reports every one).
    let outcome = check(
        "rule \"a\"\n    @Event global\nrule \"b\"\n",
        "main.opy",
        Path::new(""),
    );
    assert_eq!(outcome.diagnostics.len(), 3);
    assert!(
        outcome
            .diagnostics
            .iter()
            .all(|diagnostic| diagnostic.code == "parse-error")
    );
    assert!(outcome.model.is_none());
}

#[test]
fn semantic_errors_follow_the_compile_first_error_contract() {
    // `x = frobnicate()` produces two semantic errors (unknown-identifier and
    // unknown-value); check reports the first, matching `compile`, so the two
    // entry points never disagree about a project's verdict.
    let outcome = check(
        "rule \"r\":\n    @Event global\n    x = frobnicate()\n",
        "main.opy",
        Path::new(""),
    );
    let diagnostic = outcome.diagnostics.first().expect("first semantic error");
    assert_eq!(diagnostic.code, "unknown-identifier");
    let compile_error = opy_rs::compile(
        "rule \"r\":\n    @Event global\n    x = frobnicate()\n",
        "main.opy",
        Path::new(""),
    )
    .unwrap_err();
    assert_eq!(compile_error.code, diagnostic.code);
}

#[test]
fn check_with_overlay_resolves_unsaved_includes() {
    let mut overlay = std::collections::BTreeMap::new();
    overlay.insert(
        "shared/defs.opy".to_string(),
        "globalvar total\n".to_string(),
    );
    let outcome = tooling::check_with_overlay(
        "#!include \"shared/defs.opy\"\nrule \"r\":\n    @Event global\n    total = 1\n",
        "main.opy",
        Path::new(""),
        &overlay,
    );
    assert!(
        outcome.is_clean(),
        "overlay include must resolve: {:?}",
        outcome.diagnostics
    );
    let model = outcome.model.expect("model");
    assert_eq!(
        model.symbol("total").expect("symbol").kind,
        SymbolKind::Global
    );
}

#[test]
fn unknown_span_lookup_returns_none() {
    let outcome = check("globalvar total\n", "main.opy", Path::new(""));
    let model = outcome.model.expect("clean project");
    let nowhere = Span::new(42, Position::new(1, 1), Position::new(1, 1));
    assert!(model.symbol_at(nowhere).is_none());
    assert!(model.provenance(nowhere).is_none());
    assert_eq!(model.file(42), None);
}

#[test]
fn player_member_references_use_exact_member_span() {
    let source = "playervar I\nrule \"r\":\n    @Event global\n    for hostPlayer.I in range(3):\n        hostPlayer.I = 1\n";
    let outcome = check(source, "main.opy", Path::new(""));
    assert!(
        outcome.is_clean(),
        "player member source must check clean: {:?}",
        outcome.diagnostics
    );
    let model = outcome.model.expect("clean project");
    let symbol = model.symbol("I").expect("player variable symbol");

    assert_eq!(symbol.references.len(), 2);
    assert!(
        symbol
            .references
            .iter()
            .any(|reference| reference.start.line == 4 && reference.start.col == 20)
    );
    assert!(
        symbol
            .references
            .iter()
            .any(|reference| reference.start.line == 5 && reference.start.col == 20)
    );
}

#[test]
fn settings_emission_agreement_between_check_and_compile() {
    // opy-rs#411: `check` and `compile` must agree on every settings member.
    // A member the catalog does not declare compiles as the pinned OverPy
    // writes it and checks with a warning rather than an error: an unknown
    // key with a scalar, empty, list, or object value, and an enum key with
    // an undeclared value.
    let unknown = concat!(
        "settings {\n",
        "    \"main\": {\"description\": \"t\", \"emptyKey\": \"\"},\n",
        "    \"lobby\": {\"mapRotation\": \"sometimes\"},\n",
        "    \"gamemodes\": {\"ffa\": {\"notASetting\": 3, \"aList\": [\"x\", 1], \"anObject\": {\"k\": true}}},\n",
        "    \"heroes\": {\"allTeams\": {\"shion\": {\"ability2Duration\": \"500%\"}}}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    );
    let outcome = check(unknown, "main.opy", Path::new(""));
    assert!(outcome.model.is_some(), "{:?}", outcome.diagnostics);
    let warnings = outcome
        .diagnostics
        .iter()
        .map(|diagnostic| (diagnostic.severity, diagnostic.code.as_str()))
        .collect::<Vec<_>>();
    assert_eq!(
        warnings,
        [(tooling::DiagnosticSeverity::Warning, "unknown-setting"); 6]
    );
    let lines = compiled_lines(unknown);
    for line in [
        "emptyKey:",
        "Map Rotation: sometimes",
        "notASetting: 3",
        "aList {",
        "x",
        "1",
        "anObject {",
        "k: true",
        "ability2Duration: 500%",
    ] {
        assert!(lines.contains(&line.to_string()), "{line:?} in {lines:?}");
    }

    // A scalar directly under a team (upstream reads it as a hero name) still
    // fails both entry points under the same `workshop-emission` code.
    let invalid = concat!(
        "settings {\n",
        "    \"main\": {\"description\": \"t\"},\n",
        "    \"gamemodes\": {}, \"heroes\": {\"allTeams\": {\"notAHero\": 1}}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    );
    let outcome = check(invalid, "main.opy", Path::new(""));
    assert!(outcome.model.is_none());
    let diagnostic = outcome
        .diagnostics
        .iter()
        .find(|diagnostic| diagnostic.severity == tooling::DiagnosticSeverity::Error)
        .expect("a team-level scalar must fail check");
    assert_eq!(diagnostic.code, "workshop-emission");
    assert_eq!(diagnostic.span.as_ref().expect("span").path, "main.opy");
    let compile_error = opy_rs::compile(invalid, "main.opy", Path::new("")).unwrap_err();
    assert_eq!(compile_error.code, "workshop-emission");

    // The inherited `gamemodes.general` key that motivated the issue passes
    // both entry points.
    let valid = concat!(
        "settings {\n",
        "    \"main\": {\"description\": \"t\"},\n",
        "    \"gamemodes\": {\"ffa\": {\"enabledMaps\": [\"workshopIsland\"], \"enableKillCam\": false}}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    );
    let outcome = check(valid, "main.opy", Path::new(""));
    assert!(
        outcome.is_clean(),
        "inherited gamemode key must check clean: {:?}",
        outcome.diagnostics
    );
}

#[test]
fn settings_numbers_render_like_the_pinned_oracle() {
    // opy-rs#496: the pinned OverPy writes numeric settings values with
    // JavaScript `String(value)` semantics and reads decimal exponent forms
    // plus `0x`/`0b`/`0o` integer literals, in scalar positions and inside
    // list elements.
    let source = concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"main\": {\"a\": 0b101, \"b\": 0o17, \"c\": 0x1F, \"d\": 1e21,\n",
        "               \"e\": 0.0000001, \"f\": 123456789012345678901234, \"g\": 0.5e3,\n",
        "               \"h\": 1.0, \"i\": 1e20, \"j\": 12e2, \"k\": 0.1,\n",
        "               \"list\": [0b101, 0o17, 0x1F, 1e21, 0.0000001]}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    );
    let lines = compiled_lines(source);
    for line in [
        "a: 5",
        "b: 15",
        "c: 31",
        "d: 1e+21",
        "e: 1e-7",
        "f: 1.2345678901234569e+23",
        "g: 500",
        "h: 1",
        "i: 100000000000000000000",
        "j: 1200",
        "k: 0.1",
    ] {
        assert!(lines.contains(&line.to_string()), "{line:?} in {lines:?}");
    }
    let list = lines
        .iter()
        .position(|line| line == "list {")
        .expect("list block");
    assert_eq!(
        &lines[list + 1..list + 6],
        ["5", "15", "31", "1e+21", "1e-7"]
    );
}

#[test]
fn hero_settings_apply_the_pinned_per_hero_schema() {
    // opy-rs#495: the pinned schema merges hero settings per hero, so a
    // catalogued key is only translated where it applies; elsewhere it is
    // written fully verbatim.
    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"ana\": {\"ability3Cooldown%\": 50},\n",
        "        \"wreckingBall\": {\"ability3Cooldown%\": 50},\n",
        "        \"reinhardt\": {\"ammoClipSize%\": 50},\n",
        "        \"mercy\": {\"ammoClipSize%\": 50},\n",
        "        \"dva\": {\"ability2Height%\": 50},\n",
        "        \"freja\": {\"ability2Height%\": 50}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    for line in [
        "ability3Cooldown%: 50",
        "ammoClipSize%: 50",
        "ability2Height%: 50",
        "Piledriver Cooldown Time: 50%",
        "Ammunition Clip Size Scalar: 50%",
        "Updraft Height: 50%",
    ] {
        assert!(lines.contains(&line.to_string()), "{line:?} in {lines:?}");
    }
    for translated in [
        "Sleep Dart Cooldown Time",
        "Dynamite Cooldown Time",
        "Biotic Grenade",
    ] {
        assert!(
            !lines.iter().any(|line| line.contains(translated)),
            "{translated:?} unexpectedly in {lines:?}"
        );
    }
}

#[test]
fn hero_settings_rewrite_the_pinned_name_and_key_aliases() {
    // opy-rs#495: the pinned compiler rewrites `mccree`/`hammond` hero names
    // and `ability1KB%` member keys before its schema lookup.
    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"mccree\": {\"ability1Cooldown%\": 50},\n",
        "        \"dva\": {\"ability1KB%\": 50},\n",
        "        \"ana\": {\"ability1KB%\": 50},\n",
        "        \"hammond\": {\"ability3Cooldown%\": 50}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    for line in [
        "Cassidy {",
        "Combat Roll Cooldown Time: 50%",
        "Boosters Knockback Scalar: 50%",
        "ability1Kb%: 50",
        "Piledriver Cooldown Time: 50%",
    ] {
        assert!(lines.contains(&line.to_string()), "{line:?} in {lines:?}");
    }
    assert!(
        !lines.iter().any(|line| line.contains("ability1KB%")),
        "authored spelling must not survive in {lines:?}"
    );
}

#[test]
fn hero_roster_lists_emit_after_hero_groups_with_canonical_names() {
    // opy-rs#495: the pinned compiler emits `enabledHeroes`/`disabledHeroes`
    // last inside a team block, and rewrites the same hero aliases in them.
    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"enabledHeroes\": [\"mccree\", \"hammond\"],\n",
        "        \"dva\": {\"health%\": 50}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    let enabled = lines
        .iter()
        .position(|line| line == "enabled heroes {")
        .expect("hero list");
    let dva = lines
        .iter()
        .position(|line| line == "D.Va {")
        .expect("hero group");
    assert!(
        dva < enabled,
        "hero list must follow hero groups: {lines:?}"
    );
    for line in ["Cassidy", "Wrecking Ball"] {
        assert!(lines.contains(&line.to_string()), "{line:?} in {lines:?}");
    }
}

#[test]
fn hero_kb_rename_uses_the_pinned_assign_delete_order() {
    // opy-rs#495: upstream rewrites `ability1KB%` via assign+delete, so the
    // renamed member moves to the end, or collapses onto an authored
    // `ability1Kb%` position with the source value.
    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"dva\": {\"ability1KB%\": 20, \"health%\": 150}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    let health = lines
        .iter()
        .position(|line| line == "Health: 150%")
        .expect("health member");
    let kb = lines
        .iter()
        .position(|line| line == "Boosters Knockback Scalar: 20%")
        .expect("renamed member");
    assert!(health < kb, "renamed member must move last: {lines:?}");

    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"dva\": {\"ability1Kb%\": 10, \"health%\": 150, \"ability1KB%\": 20}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    let health = lines
        .iter()
        .position(|line| line == "Health: 150%")
        .expect("health member");
    let kb = lines
        .iter()
        .position(|line| line == "Boosters Knockback Scalar: 20%")
        .expect("renamed member");
    assert!(
        kb < health,
        "collapsed member keeps the destination position: {lines:?}"
    );
    assert!(
        !lines.iter().any(|line| line.contains("10%")),
        "the source value wins over the authored destination: {lines:?}"
    );
}

#[test]
fn hero_group_rename_uses_the_pinned_assign_delete_order() {
    // opy-rs#495: upstream renames alias hero groups via assign+delete, so a
    // renamed group moves to the end, or collapses onto the canonical
    // group's position with the source members.
    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"mccree\": {\"health%\": 50},\n",
        "        \"ana\": {\"health%\": 150}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    let ana = lines
        .iter()
        .position(|line| line == "Ana {")
        .expect("ana group");
    let cassidy = lines
        .iter()
        .position(|line| line == "Cassidy {")
        .expect("renamed group");
    assert!(ana < cassidy, "renamed group must move last: {lines:?}");

    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"mccree\": {\"health%\": 50},\n",
        "        \"cassidy\": {\"health%\": 10}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    assert_eq!(
        lines.iter().filter(|line| **line == "Cassidy {").count(),
        1,
        "the canonical group collapses to one: {lines:?}"
    );
    assert!(
        lines.contains(&"Health: 50%".to_string())
            && !lines.iter().any(|line| line.contains("10%")),
        "the source members win: {lines:?}"
    );

    // Both aliases in one team: upstream renames per alias in its fixed
    // order, so appended groups follow that order, not authored order.
    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"hammond\": {\"health%\": 50},\n",
        "        \"mccree\": {\"health%\": 60}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    let cassidy = lines
        .iter()
        .position(|line| line == "Cassidy {")
        .expect("cassidy group");
    let wrecking_ball = lines
        .iter()
        .position(|line| line == "Wrecking Ball {")
        .expect("wrecking ball group");
    assert!(
        cassidy < wrecking_ball,
        "the mccree pass appends before the hammond pass: {lines:?}"
    );
}

#[test]
fn general_children_are_not_reprocessed_as_hero_members() {
    // opy-rs#495: the flattened `general` members take only the team-level
    // pass-through — hero-name and applicability passes must not reach them.
    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"general\": {\n",
        "            \"dva\": {\"ability1KB%\": 50},\n",
        "            \"damageDealt%\": 50\n",
        "        },\n",
        "        \"ana\": {\"health%\": 50}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    for line in ["ability1KB%: 50", "Damage Dealt: 50%", "Health: 50%"] {
        assert!(lines.contains(&line.to_string()), "{line:?} in {lines:?}");
    }
    assert!(
        !lines
            .iter()
            .any(|line| line.contains("Boosters Knockback Scalar")),
        "a nested hero dict stays verbatim: {lines:?}"
    );
}

#[test]
fn a_non_dict_general_yields_index_members_or_drops() {
    // opy-rs#495: upstream iterates the value's `Object.keys`, so a string or
    // list `general` emits index-keyed members while numbers/booleans drop.
    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"general\": \"ab\",\n",
        "        \"ana\": {\"health%\": 50}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    for line in ["0: a", "1: b", "Health: 50%"] {
        assert!(lines.contains(&line.to_string()), "{line:?} in {lines:?}");
    }

    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"general\": [10, 20],\n",
        "        \"ana\": {\"health%\": 50}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    for line in ["0: 10", "1: 20"] {
        assert!(lines.contains(&line.to_string()), "{line:?} in {lines:?}");
    }

    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"general\": 5,\n",
        "        \"ana\": {\"health%\": 50}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    assert!(
        lines.contains(&"Health: 50%".to_string())
            && !lines.iter().any(|line| line == "general: 5"),
        "{lines:?} must drop the number member and keep the hero members"
    );
}

#[test]
fn both_hero_lists_in_one_team_error() {
    // opy-rs#495: upstream rejects a team carrying both rosters.
    let error = opy_rs::compile(
        concat!(
            "settings {\n",
            "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
            "    \"heroes\": {\"allTeams\": {\n",
            "        \"enabledHeroes\": [\"ana\"],\n",
            "        \"disabledHeroes\": [\"genji\"]\n",
            "    }}\n",
            "}\n",
            "rule \"a\":\n    @Event global\n    wait(1)\n",
        ),
        "main.opy",
        Path::new(""),
    )
    .unwrap_err();
    assert_eq!(error.code, "settings-hero-lists");
    assert_eq!(
        error.message,
        "Cannot have both 'enabledHeroes' and 'disabledHeroes' in team 'allTeams'"
    );
}

#[test]
fn inapplicable_hero_key_with_enum_value_stays_verbatim() {
    // opy-rs#495: a non-applicable key is unknown for that hero, so both the
    // name and the value keep their authored spelling.
    let lines = compiled_lines(concat!(
        "settings {\n",
        "    \"gamemodes\": {\"ffa\": {\"enabled\": true}},\n",
        "    \"heroes\": {\"allTeams\": {\n",
        "        \"ana\": {\"enableSecondaryFire\": false},\n",
        "        \"genji\": {\"enableGenericSecondaryFire\": true}\n",
        "    }}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    ));
    for line in [
        "enableSecondaryFire: false",
        "enableGenericSecondaryFire: true",
    ] {
        assert!(lines.contains(&line.to_string()), "{line:?} in {lines:?}");
    }
}

#[test]
fn misspelled_settings_key_warns_with_a_single_candidate_suffix() {
    // A near-miss settings key compiles unchanged like upstream; `check` warns
    // and names the canonical key once.
    let source = concat!(
        "settings {\n",
        "    \"main\": { \"descriptino\": \"x\" },\n",
        "    \"gamemodes\": {}\n",
        "}\n",
        "rule \"a\":\n    @Event global\n    wait(1)\n",
    );
    let expected = "unknown settings key 'descriptino' is passed through unchanged (did you mean 'description'?)";

    let outcome = check(source, "main.opy", Path::new(""));
    let diagnostic = outcome
        .diagnostics
        .iter()
        .find(|diagnostic| diagnostic.code == "unknown-setting")
        .expect("the misspelled key must warn");
    assert_eq!(diagnostic.severity, tooling::DiagnosticSeverity::Warning);
    assert_eq!(diagnostic.message, expected);

    assert!(compiled_lines(source).contains(&"descriptino: x".to_string()));
}

fn compiled_lines(source: &str) -> Vec<String> {
    opy_rs::Compiler::new()
        .expect("compiler")
        .compile_source(source, "main.opy", Path::new(""))
        .expect("compiles")
        .workshop
        .lines()
        .map(|line| line.trim().to_string())
        .collect()
}