galeon-engine 0.2.0

Core ECS game engine: entities, components, systems, and scheduling.
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
// SPDX-License-Identifier: AGPL-3.0-only OR Commercial

//! Protocol code generation — TypeScript types and protocol descriptors.
//!
//! Reads a [`ProtocolManifest`] and emits:
//!
//! - TypeScript interfaces for commands, queries, events, and DTOs
//! - Protocol descriptors for adapter generation
//!
//! This module is the bridge between Rust protocol definitions and the
//! generated artifacts that TypeScript consumers and runtime adapters use.

use crate::manifest::{ManifestEntry, ManifestField, ProtocolManifest};
use crate::protocol::ProtocolKind;
use serde::{Deserialize, Serialize};

// =============================================================================
// Rust → TypeScript type mapping
// =============================================================================

/// Map a Rust type string (from manifest `ty` field) to a TypeScript type.
///
/// Handles common primitive and collection types. Unknown types pass through
/// as-is (assumed to be protocol DTOs that will also be generated).
pub fn rust_type_to_ts(rust_ty: &str) -> String {
    match rust_ty {
        // Numeric types → number
        "u8" | "u16" | "u32" | "u64" | "u128" | "usize" | "i8" | "i16" | "i32" | "i64" | "i128"
        | "isize" | "f32" | "f64" => "number".to_string(),

        // Boolean
        "bool" => "boolean".to_string(),

        // String types → string
        "String" | "&str" | "str" => "string".to_string(),

        // Unit / empty
        "()" => "void".to_string(),

        other => {
            // Vec<T> → T[]
            if let Some(inner) = other.strip_prefix("Vec<").and_then(|s| s.strip_suffix('>')) {
                return format!("{}[]", rust_type_to_ts(inner));
            }

            // Option<T> → T | null
            if let Some(inner) = other
                .strip_prefix("Option<")
                .and_then(|s| s.strip_suffix('>'))
            {
                return format!("{} | null", rust_type_to_ts(inner));
            }

            // HashMap<K, V> → Record<K, V>
            if let Some(inner) = other
                .strip_prefix("HashMap<")
                .and_then(|s| s.strip_suffix('>'))
                && let Some((k, v)) = inner.split_once(',')
            {
                return format!(
                    "Record<{}, {}>",
                    rust_type_to_ts(k.trim()),
                    rust_type_to_ts(v.trim())
                );
            }

            // Unknown type → pass through (assumed to be another protocol type)
            other.to_string()
        }
    }
}

// =============================================================================
// TypeScript code generation
// =============================================================================

/// Generate a TypeScript interface from a manifest entry.
fn emit_ts_interface(entry: &ManifestEntry) -> String {
    let mut out = String::new();

    // Doc comment
    if !entry.doc.is_empty() {
        out.push_str(&format!("/** {} */\n", entry.doc));
    }

    if entry.fields.is_empty() {
        // Unit struct → type alias to empty object
        out.push_str(&format!(
            "export type {} = Record<string, never>;\n",
            entry.name
        ));
    } else {
        out.push_str(&format!("export interface {} {{\n", entry.name));
        for field in &entry.fields {
            let ts_type = rust_type_to_ts(&field.ty);
            out.push_str(&format!("  {}: {};\n", field.name, ts_type));
        }
        out.push_str("}\n");
    }

    out
}

/// Generate a complete TypeScript module from a protocol manifest.
///
/// Emits all protocol types grouped by kind with section comments.
pub fn generate_typescript(manifest: &ProtocolManifest) -> String {
    let mut out = String::new();

    out.push_str("// Auto-generated by Galeon Engine — do not edit.\n");
    out.push_str(&format!(
        "// Protocol: {} | Manifest v{}\n\n",
        manifest.protocol_version, manifest.manifest_version
    ));

    let sections: &[(&str, &[ManifestEntry])] = &[
        ("Commands", &manifest.commands),
        ("Queries", &manifest.queries),
        ("Events", &manifest.events),
        ("DTOs", &manifest.dtos),
    ];

    for (label, entries) in sections {
        if entries.is_empty() {
            continue;
        }
        out.push_str(&format!("// — {} —\n\n", label));
        for entry in *entries {
            out.push_str(&emit_ts_interface(entry));
            out.push('\n');
        }
    }

    out
}

/// Generate a TypeScript module containing only the items that belong to `surface`.
///
/// Items with an explicit `surfaces` list are included when `surface` appears
/// in that list. Items with an empty `surfaces` list are included when
/// `surface` matches `manifest.default_surface`.
pub fn generate_typescript_for_surface(manifest: &ProtocolManifest, surface: &str) -> String {
    let mut out = String::new();

    out.push_str("// Auto-generated by Galeon Engine — do not edit.\n");
    out.push_str(&format!(
        "// Protocol: {} | Surface: {} | Manifest v{}\n\n",
        manifest.protocol_version, surface, manifest.manifest_version
    ));

    let belongs = |entry: &ManifestEntry| {
        ProtocolManifest::entry_belongs_to_surface(entry, surface, &manifest.default_surface)
    };

    let sections: &[(&str, &Vec<ManifestEntry>)] = &[
        ("Commands", &manifest.commands),
        ("Queries", &manifest.queries),
        ("Events", &manifest.events),
        ("DTOs", &manifest.dtos),
    ];

    for (label, entries) in sections {
        let filtered: Vec<&ManifestEntry> = entries.iter().filter(|e| belongs(e)).collect();
        if filtered.is_empty() {
            continue;
        }
        out.push_str(&format!("// — {} —\n\n", label));
        for entry in filtered {
            out.push_str(&emit_ts_interface(entry));
            out.push('\n');
        }
    }

    out
}

/// Generate one TypeScript module per surface in the manifest.
///
/// Returns `(surface_name, typescript_source)` pairs sorted by surface name.
/// Single-surface manifests delegate to [`generate_typescript`] so that
/// existing consumers see identical output (no `Surface:` header drift).
pub fn generate_all_surface_typescripts(manifest: &ProtocolManifest) -> Vec<(String, String)> {
    let mut names = manifest.resolved_surface_names();
    names.sort();
    names.dedup();

    if names.len() == 1 {
        return vec![(names[0].clone(), generate_typescript(manifest))];
    }

    names
        .into_iter()
        .map(|name| {
            let ts = generate_typescript_for_surface(manifest, &name);
            (name, ts)
        })
        .collect()
}

// =============================================================================
// Protocol descriptors
// =============================================================================

/// HTTP method for remote adapter routing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum HttpMethod {
    Get,
    Post,
}

/// A protocol descriptor for a command, query, or event.
///
/// Descriptors carry the identity and routing metadata that adapters need
/// to dispatch protocol items. They are generated artifacts — not handwritten.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProtocolDescriptor {
    /// Stable protocol name (e.g., `"SpawnUnit"`).
    pub name: String,
    /// Protocol kind.
    pub kind: ProtocolKind,
    /// Route path for remote adapter (e.g., `"/commands/spawn-unit"`).
    pub route: String,
    /// HTTP method for remote adapter.
    pub method: HttpMethod,
    /// Field names (for schema documentation).
    pub fields: Vec<ManifestField>,
}

/// A per-surface descriptor group.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SurfaceDescriptorSet {
    /// Surface name these descriptors belong to.
    pub name: String,
    /// Commands, queries, and events reachable from this surface.
    pub descriptors: Vec<ProtocolDescriptor>,
}

/// A complete set of protocol descriptors for a project.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProtocolDescriptorSet {
    pub protocol_version: String,
    pub surfaces: Vec<SurfaceDescriptorSet>,
}

impl ProtocolDescriptorSet {
    /// Iterate all descriptors across every surface group.
    ///
    /// Items annotated with multiple surfaces will appear once per surface
    /// they belong to. Callers that need unique items should deduplicate
    /// by name.
    pub fn iter_descriptors(&self) -> impl Iterator<Item = &ProtocolDescriptor> {
        self.surfaces
            .iter()
            .flat_map(|surface| surface.descriptors.iter())
    }
}

/// Convert a PascalCase name to a kebab-case route segment.
fn to_kebab_case(name: &str) -> String {
    let mut result = String::new();
    for (i, ch) in name.chars().enumerate() {
        if ch.is_uppercase() && i > 0 {
            result.push('-');
        }
        result.push(ch.to_ascii_lowercase());
    }
    result
}

/// Generate protocol descriptors from a manifest.
///
/// Default routing conventions:
/// - Commands → `POST /commands/<kebab-name>`
/// - Queries → `GET /queries/<kebab-name>` (POST if has fields)
/// - Events → `GET /events/<kebab-name>` (stream subscription)
pub fn generate_descriptors(manifest: &ProtocolManifest) -> ProtocolDescriptorSet {
    let mut surfaces = Vec::new();
    let surface_names = manifest.resolved_surface_names();

    for surface_name in &surface_names {
        let mut descriptors = Vec::new();

        for entry in manifest.commands.iter().filter(|entry| {
            ProtocolManifest::entry_belongs_to_surface(
                entry,
                surface_name,
                &manifest.default_surface,
            )
        }) {
            let slug = to_kebab_case(&entry.name);
            descriptors.push(ProtocolDescriptor {
                name: entry.name.clone(),
                kind: ProtocolKind::Command,
                route: format!("/commands/{}", slug),
                method: HttpMethod::Post,
                fields: entry.fields.clone(),
            });
        }

        for entry in manifest.queries.iter().filter(|entry| {
            ProtocolManifest::entry_belongs_to_surface(
                entry,
                surface_name,
                &manifest.default_surface,
            )
        }) {
            let slug = to_kebab_case(&entry.name);
            let method = if entry.fields.is_empty() {
                HttpMethod::Get
            } else {
                HttpMethod::Post
            };
            descriptors.push(ProtocolDescriptor {
                name: entry.name.clone(),
                kind: ProtocolKind::Query,
                route: format!("/queries/{}", slug),
                method,
                fields: entry.fields.clone(),
            });
        }

        for entry in manifest.events.iter().filter(|entry| {
            ProtocolManifest::entry_belongs_to_surface(
                entry,
                surface_name,
                &manifest.default_surface,
            )
        }) {
            let slug = to_kebab_case(&entry.name);
            descriptors.push(ProtocolDescriptor {
                name: entry.name.clone(),
                kind: ProtocolKind::Event,
                route: format!("/events/{}", slug),
                method: HttpMethod::Get,
                fields: entry.fields.clone(),
            });
        }

        surfaces.push(SurfaceDescriptorSet {
            name: surface_name.clone(),
            descriptors,
        });
    }

    ProtocolDescriptorSet {
        protocol_version: manifest.protocol_version.clone(),
        surfaces,
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    // -- Type mapping --

    #[test]
    fn primitives_map_to_ts() {
        assert_eq!(rust_type_to_ts("u64"), "number");
        assert_eq!(rust_type_to_ts("f32"), "number");
        assert_eq!(rust_type_to_ts("bool"), "boolean");
        assert_eq!(rust_type_to_ts("String"), "string");
        assert_eq!(rust_type_to_ts("()"), "void");
    }

    #[test]
    fn vec_maps_to_array() {
        assert_eq!(rust_type_to_ts("Vec<u64>"), "number[]");
        assert_eq!(rust_type_to_ts("Vec<String>"), "string[]");
    }

    #[test]
    fn option_maps_to_nullable() {
        assert_eq!(rust_type_to_ts("Option<u32>"), "number | null");
        assert_eq!(rust_type_to_ts("Option<String>"), "string | null");
    }

    #[test]
    fn hashmap_maps_to_record() {
        assert_eq!(
            rust_type_to_ts("HashMap<String, u64>"),
            "Record<string, number>"
        );
    }

    #[test]
    fn unknown_type_passes_through() {
        assert_eq!(rust_type_to_ts("UnitView"), "UnitView");
        assert_eq!(rust_type_to_ts("Vec<UnitView>"), "UnitView[]");
    }

    // -- TS generation --

    fn sample_manifest() -> ProtocolManifest {
        ProtocolManifest {
            manifest_version: "2".into(),
            protocol_version: "test@0.1".into(),
            default_surface: "default".into(),
            surfaces: vec!["default".into()],
            commands: vec![ManifestEntry {
                name: "SpawnUnit".into(),
                kind: ProtocolKind::Command,
                fields: vec![
                    ManifestField {
                        name: "unit_id".into(),
                        ty: "u64".into(),
                    },
                    ManifestField {
                        name: "location_id".into(),
                        ty: "u64".into(),
                    },
                ],
                doc: "Spawn a unit at a location.".into(),
                surfaces: vec![],
            }],
            queries: vec![ManifestEntry {
                name: "GetWorldSnapshot".into(),
                kind: ProtocolKind::Query,
                fields: vec![],
                doc: "Get world status.".into(),
                surfaces: vec![],
            }],
            events: vec![ManifestEntry {
                name: "UnitDestroyed".into(),
                kind: ProtocolKind::Event,
                fields: vec![
                    ManifestField {
                        name: "unit_id".into(),
                        ty: "u64".into(),
                    },
                    ManifestField {
                        name: "destination".into(),
                        ty: "String".into(),
                    },
                ],
                doc: "".into(),
                surfaces: vec![],
            }],
            dtos: vec![ManifestEntry {
                name: "WorldSnapshot".into(),
                kind: ProtocolKind::Dto,
                fields: vec![
                    ManifestField {
                        name: "units_active".into(),
                        ty: "u32".into(),
                    },
                    ManifestField {
                        name: "units_idle".into(),
                        ty: "u32".into(),
                    },
                ],
                doc: "World overview.".into(),
                surfaces: vec![],
            }],
        }
    }

    fn sample_multi_surface_manifest() -> ProtocolManifest {
        ProtocolManifest {
            manifest_version: "2".into(),
            protocol_version: "test@0.1".into(),
            default_surface: "gameplay".into(),
            surfaces: vec!["authority".into(), "gameplay".into()],
            commands: vec![
                ManifestEntry {
                    name: "SpawnUnit".into(),
                    kind: ProtocolKind::Command,
                    fields: vec![ManifestField {
                        name: "unit_id".into(),
                        ty: "u64".into(),
                    }],
                    doc: "".into(),
                    surfaces: vec![],
                },
                ManifestEntry {
                    name: "AdminReset".into(),
                    kind: ProtocolKind::Command,
                    fields: vec![ManifestField {
                        name: "zone_id".into(),
                        ty: "u64".into(),
                    }],
                    doc: "".into(),
                    surfaces: vec!["authority".into()],
                },
            ],
            queries: vec![],
            events: vec![ManifestEntry {
                name: "UnitDestroyed".into(),
                kind: ProtocolKind::Event,
                fields: vec![ManifestField {
                    name: "unit_id".into(),
                    ty: "u64".into(),
                }],
                doc: "".into(),
                surfaces: vec!["authority".into(), "gameplay".into()],
            }],
            dtos: vec![],
        }
    }

    #[test]
    fn generate_ts_produces_valid_output() {
        let ts = generate_typescript(&sample_manifest());

        // Header
        assert!(ts.contains("Auto-generated by Galeon Engine"));
        assert!(ts.contains("test@0.1"));

        // Command interface
        assert!(ts.contains("export interface SpawnUnit {"));
        assert!(ts.contains("unit_id: number;"));
        assert!(ts.contains("location_id: number;"));

        // Query unit struct
        assert!(ts.contains("export type GetWorldSnapshot = Record<string, never>;"));

        // Event interface
        assert!(ts.contains("export interface UnitDestroyed {"));
        assert!(ts.contains("destination: string;"));

        // DTO interface
        assert!(ts.contains("export interface WorldSnapshot {"));
        assert!(ts.contains("units_active: number;"));

        // Doc comment
        assert!(ts.contains("/** Spawn a unit at a location. */"));
    }

    #[test]
    fn generate_ts_empty_manifest() {
        let manifest = ProtocolManifest {
            manifest_version: "2".into(),
            protocol_version: "empty@0.0".into(),
            default_surface: "default".into(),
            surfaces: vec![],
            commands: vec![],
            queries: vec![],
            events: vec![],
            dtos: vec![],
        };
        let ts = generate_typescript(&manifest);
        assert!(ts.contains("Auto-generated"));
        // No section headers for empty categories
        assert!(!ts.contains("Commands"));
    }

    // -- Descriptors --

    #[test]
    fn to_kebab_case_converts_pascal() {
        assert_eq!(to_kebab_case("SpawnUnit"), "spawn-unit");
        assert_eq!(to_kebab_case("GetWorldSnapshot"), "get-world-snapshot");
        assert_eq!(to_kebab_case("A"), "a");
    }

    #[test]
    fn generate_descriptors_correct_routes() {
        let descs = generate_descriptors(&sample_manifest());

        assert_eq!(descs.protocol_version, "test@0.1");
        assert_eq!(descs.surfaces.len(), 1);
        assert_eq!(descs.surfaces[0].name, "default");
        // DTOs are type-only — no descriptor (3 = command + query + event)
        assert_eq!(descs.surfaces[0].descriptors.len(), 3);

        let cmd = descs.surfaces[0]
            .descriptors
            .iter()
            .find(|d| d.name == "SpawnUnit")
            .unwrap();
        assert_eq!(cmd.route, "/commands/spawn-unit");
        assert_eq!(cmd.method, HttpMethod::Post);
        assert_eq!(cmd.kind, ProtocolKind::Command);

        let query = descs.surfaces[0]
            .descriptors
            .iter()
            .find(|d| d.name == "GetWorldSnapshot")
            .unwrap();
        assert_eq!(query.route, "/queries/get-world-snapshot");
        assert_eq!(query.method, HttpMethod::Get);

        let event = descs.surfaces[0]
            .descriptors
            .iter()
            .find(|d| d.name == "UnitDestroyed")
            .unwrap();
        assert_eq!(event.route, "/events/unit-destroyed");
        assert_eq!(event.method, HttpMethod::Get);
    }

    #[test]
    fn generate_descriptors_groups_entries_by_surface() {
        let descs = generate_descriptors(&sample_multi_surface_manifest());

        assert_eq!(descs.surfaces.len(), 2);
        let authority = descs
            .surfaces
            .iter()
            .find(|surface| surface.name == "authority")
            .unwrap();
        let gameplay = descs
            .surfaces
            .iter()
            .find(|surface| surface.name == "gameplay")
            .unwrap();

        assert_eq!(authority.descriptors.len(), 2);
        assert!(
            authority
                .descriptors
                .iter()
                .any(|desc| desc.name == "AdminReset")
        );
        assert!(
            authority
                .descriptors
                .iter()
                .any(|desc| desc.name == "UnitDestroyed")
        );

        assert_eq!(gameplay.descriptors.len(), 2);
        assert!(
            gameplay
                .descriptors
                .iter()
                .any(|desc| desc.name == "SpawnUnit")
        );
        assert!(
            gameplay
                .descriptors
                .iter()
                .any(|desc| desc.name == "UnitDestroyed")
        );
    }

    #[test]
    fn query_with_fields_uses_post() {
        let manifest = ProtocolManifest {
            manifest_version: "2".into(),
            protocol_version: "test@0.1".into(),
            default_surface: "default".into(),
            surfaces: vec!["default".into()],
            commands: vec![],
            queries: vec![ManifestEntry {
                name: "SearchUnits".into(),
                kind: ProtocolKind::Query,
                fields: vec![ManifestField {
                    name: "name_filter".into(),
                    ty: "String".into(),
                }],
                doc: "".into(),
                surfaces: vec![],
            }],
            events: vec![],
            dtos: vec![],
        };
        let descs = generate_descriptors(&manifest);
        let q = &descs.surfaces[0].descriptors[0];
        assert_eq!(q.method, HttpMethod::Post);
    }

    #[test]
    fn descriptors_serialize_to_json() {
        let descs = generate_descriptors(&sample_manifest());
        let json = serde_json::to_string_pretty(&descs).unwrap();
        assert!(json.contains("/commands/spawn-unit"));
        assert!(json.contains("\"Post\""));
        assert!(json.contains("\"surfaces\""));

        // Round-trip
        let back: ProtocolDescriptorSet = serde_json::from_str(&json).unwrap();
        assert_eq!(back.surfaces.len(), 1);
        assert_eq!(back.iter_descriptors().count(), 3);
    }

    #[test]
    fn generate_descriptors_derives_surfaces_for_legacy_manifests() {
        let manifest: ProtocolManifest = serde_json::from_str(
            r#"{
                "manifest_version": "1",
                "protocol_version": "legacy@0.1",
                "commands": [
                    {
                        "name": "SpawnUnit",
                        "kind": "Command",
                        "fields": [],
                        "doc": ""
                    }
                ],
                "queries": [],
                "events": [],
                "dtos": []
            }"#,
        )
        .unwrap();

        let descs = generate_descriptors(&manifest);
        assert_eq!(descs.surfaces.len(), 1);
        assert_eq!(descs.surfaces[0].name, "default");
        assert_eq!(descs.surfaces[0].descriptors.len(), 1);
        assert_eq!(descs.surfaces[0].descriptors[0].name, "SpawnUnit");
    }

    #[test]
    fn generate_descriptors_empty_manifest_has_default_surface() {
        let manifest = ProtocolManifest {
            manifest_version: "2".into(),
            protocol_version: "empty@0.0".into(),
            default_surface: "default".into(),
            surfaces: vec![],
            commands: vec![],
            queries: vec![],
            events: vec![],
            dtos: vec![],
        };
        let descs = generate_descriptors(&manifest);
        assert_eq!(descs.surfaces.len(), 1);
        assert_eq!(descs.surfaces[0].name, "default");
        assert!(descs.surfaces[0].descriptors.is_empty());
    }

    // -- Per-surface TypeScript generation --

    #[test]
    fn generate_ts_for_surface_filters_entries() {
        let manifest = sample_multi_surface_manifest();
        let gameplay_ts = generate_typescript_for_surface(&manifest, "gameplay");
        let authority_ts = generate_typescript_for_surface(&manifest, "authority");

        // SpawnUnit has no explicit surfaces → belongs to default ("gameplay")
        assert!(gameplay_ts.contains("export interface SpawnUnit"));
        assert!(!authority_ts.contains("SpawnUnit"));

        // AdminReset is authority-only
        assert!(authority_ts.contains("export interface AdminReset"));
        assert!(!gameplay_ts.contains("AdminReset"));

        // UnitDestroyed belongs to both surfaces
        assert!(gameplay_ts.contains("export interface UnitDestroyed"));
        assert!(authority_ts.contains("export interface UnitDestroyed"));

        // Headers include surface name
        assert!(gameplay_ts.contains("Surface: gameplay"));
        assert!(authority_ts.contains("Surface: authority"));
    }

    #[test]
    fn generate_ts_for_nonexistent_surface_is_empty() {
        let manifest = sample_multi_surface_manifest();
        let ts = generate_typescript_for_surface(&manifest, "nonexistent");
        assert!(ts.contains("Surface: nonexistent"));
        // No section headers
        assert!(!ts.contains("Commands"));
        assert!(!ts.contains("Events"));
    }

    #[test]
    fn generate_all_surface_typescripts_returns_one_per_surface() {
        let manifest = sample_multi_surface_manifest();
        let all = generate_all_surface_typescripts(&manifest);

        assert_eq!(all.len(), 2);
        assert_eq!(all[0].0, "authority");
        assert_eq!(all[1].0, "gameplay");

        // Each module is self-contained
        assert!(all[0].1.contains("AdminReset"));
        assert!(all[1].1.contains("SpawnUnit"));
    }

    #[test]
    fn single_surface_generate_all_matches_generate_typescript() {
        let manifest = sample_manifest();
        let all = generate_all_surface_typescripts(&manifest);
        assert_eq!(all.len(), 1);
        assert_eq!(all[0].0, "default");
        // Single-surface output must be byte-identical to generate_typescript()
        assert_eq!(all[0].1, generate_typescript(&manifest));
    }

    #[test]
    fn generate_all_surface_typescripts_sorts_and_dedups_surface_names() {
        let mut manifest = sample_multi_surface_manifest();
        // Intentionally out-of-order with a duplicate
        manifest.surfaces = vec!["gameplay".into(), "authority".into(), "authority".into()];

        let all = generate_all_surface_typescripts(&manifest);

        assert_eq!(all.len(), 2);
        assert_eq!(all[0].0, "authority");
        assert_eq!(all[1].0, "gameplay");
    }

    // -- T6: End-to-end multi-surface proof --

    #[test]
    fn multi_surface_end_to_end_isolation() {
        let manifest = sample_multi_surface_manifest();

        // TS codegen: each surface gets its own module
        let ts_modules = generate_all_surface_typescripts(&manifest);
        assert_eq!(ts_modules.len(), 2);

        // Descriptor codegen: each surface gets its own descriptor set
        let descs = generate_descriptors(&manifest);
        assert_eq!(descs.surfaces.len(), 2);

        let authority_ts = &ts_modules.iter().find(|(n, _)| n == "authority").unwrap().1;
        let gameplay_ts = &ts_modules.iter().find(|(n, _)| n == "gameplay").unwrap().1;
        let authority_descs = descs
            .surfaces
            .iter()
            .find(|s| s.name == "authority")
            .unwrap();
        let gameplay_descs = descs
            .surfaces
            .iter()
            .find(|s| s.name == "gameplay")
            .unwrap();

        // Authority surface: AdminReset + UnitDestroyed
        assert!(authority_ts.contains("AdminReset"));
        assert!(authority_ts.contains("UnitDestroyed"));
        assert!(!authority_ts.contains("SpawnUnit"));
        assert_eq!(authority_descs.descriptors.len(), 2);

        // Gameplay surface: SpawnUnit + UnitDestroyed
        assert!(gameplay_ts.contains("SpawnUnit"));
        assert!(gameplay_ts.contains("UnitDestroyed"));
        assert!(!gameplay_ts.contains("AdminReset"));
        assert_eq!(gameplay_descs.descriptors.len(), 2);

        // Descriptor names match TS interface names
        for surface_descs in &descs.surfaces {
            let ts = if surface_descs.name == "authority" {
                authority_ts
            } else {
                gameplay_ts
            };
            for desc in &surface_descs.descriptors {
                assert!(
                    ts.contains(&desc.name),
                    "surface {:?} descriptor {:?} missing from TS output",
                    surface_descs.name,
                    desc.name
                );
            }
        }
    }
}