zlayer-spec 0.11.2

ZLayer deployment specification parsing and validation
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! `ZLayer` V1 Service Specification
//!
//! This crate provides types for parsing and validating `ZLayer` deployment specifications.

mod error;
mod types;
mod validate;

pub use error::*;
pub use types::*;
pub use validate::*;

use validator::Validate;

/// Parse a deployment spec from YAML string
///
/// # Errors
///
/// Returns `SpecError` if parsing or validation fails.
pub fn from_yaml_str(yaml: &str) -> Result<DeploymentSpec, SpecError> {
    let spec: DeploymentSpec = serde_yaml::from_str(yaml)?;

    // Run validator crate validation
    spec.validate().map_err(|e| {
        SpecError::Validation(ValidationError {
            kind: ValidationErrorKind::Generic {
                message: e.to_string(),
            },
            path: String::new(),
        })
    })?;

    // Cross-field validation
    validate_dependencies(&spec)?;
    validate_unique_service_endpoints(&spec)?;
    validate_cron_schedules(&spec)?;
    validate_tunnels(&spec)?;
    validate_wasm_configs(&spec)?;

    Ok(spec)
}

/// Parse a deployment spec from YAML file
///
/// # Errors
///
/// Returns `SpecError` if the file cannot be read, or parsing/validation fails.
pub fn from_yaml_file(path: &std::path::Path) -> Result<DeploymentSpec, SpecError> {
    let content = std::fs::read_to_string(path)?;
    from_yaml_str(&content)
}

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

    #[test]
    fn test_parse_from_yaml_str() {
        let yaml = r"
version: v1
deployment: test
services:
  hello:
    rtype: service
    image:
      name: hello-world:latest
";
        let result = from_yaml_str(yaml);
        assert!(result.is_ok());
        let spec = result.unwrap();
        assert_eq!(spec.version, "v1");
        assert_eq!(spec.deployment, "test");
    }

    // =========================================================================
    // Integration tests for validation (B.12)
    // =========================================================================

    #[test]
    fn test_invalid_version_rejected() {
        let yaml = r"
version: v2
deployment: my-app
services:
  hello:
    image:
      name: hello-world:latest
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("version") || err_str.contains("v1"),
            "Error should mention version: {err_str}",
        );
    }

    #[test]
    fn test_empty_deployment_name_rejected() {
        let yaml = r"
version: v1
deployment: ab
services:
  hello:
    image:
      name: hello-world:latest
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("deployment") || err_str.contains("3-63"),
            "Error should mention deployment name: {err_str}",
        );
    }

    #[test]
    fn test_invalid_port_zero_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  hello:
    image:
      name: hello-world:latest
    endpoints:
      - name: http
        protocol: http
        port: 0
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("port"),
            "Error should mention port: {err_str}",
        );
    }

    #[test]
    fn test_unknown_dependency_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    depends:
      - service: database
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("database") || err_str.contains("unknown"),
            "Error should mention unknown dependency: {err_str}",
        );
    }

    #[test]
    fn test_duplicate_endpoint_names_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    endpoints:
      - name: http
        protocol: http
        port: 8080
      - name: http
        protocol: https
        port: 8443
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("http") || err_str.contains("duplicate"),
            "Error should mention duplicate endpoint: {err_str}",
        );
    }

    #[test]
    fn test_invalid_scale_range_min_gt_max_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    scale:
      mode: adaptive
      min: 10
      max: 5
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("scale") || err_str.contains("min") || err_str.contains("max"),
            "Error should mention scale range: {err_str}",
        );
    }

    #[test]
    fn test_invalid_cpu_zero_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    resources:
      cpu: 0
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("cpu") || err_str.contains("CPU"),
            "Error should mention CPU: {err_str}",
        );
    }

    #[test]
    fn test_invalid_memory_format_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    resources:
      memory: 512MB
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("memory"),
            "Error should mention memory format: {err_str}",
        );
    }

    #[test]
    fn test_empty_image_name_rejected() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  api:
    image:
      name: ""
"#;
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("image") || err_str.contains("empty"),
            "Error should mention empty image name: {err_str}",
        );
    }

    #[test]
    fn test_valid_spec_passes_validation() {
        let yaml = r"
version: v1
deployment: my-production-app
services:
  api:
    image:
      name: ghcr.io/myorg/api:v1.2.3
    resources:
      cpu: 0.5
      memory: 512Mi
    endpoints:
      - name: http
        protocol: http
        port: 8080
        expose: public
      - name: metrics
        protocol: http
        port: 9090
        expose: internal
    scale:
      mode: adaptive
      min: 2
      max: 10
      targets:
        cpu: 70
    depends:
      - service: database
        condition: healthy
  database:
    image:
      name: postgres:15
    endpoints:
      - name: postgres
        protocol: tcp
        port: 5432
    scale:
      mode: fixed
      replicas: 1
";
        let result = from_yaml_str(yaml);
        assert!(result.is_ok(), "Valid spec should pass: {result:?}");
        let spec = result.unwrap();
        assert_eq!(spec.version, "v1");
        assert_eq!(spec.deployment, "my-production-app");
        assert_eq!(spec.services.len(), 2);
    }

    #[test]
    fn test_valid_dependency_passes() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    depends:
      - service: database
  database:
    image:
      name: postgres:15
";
        let result = from_yaml_str(yaml);
        assert!(result.is_ok(), "Valid dependency should pass: {result:?}");
    }

    // =========================================================================
    // Cron schedule validation tests (Feature 4, Phase 1)
    // =========================================================================

    #[test]
    fn test_valid_cron_job_with_schedule() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  cleanup:
    rtype: cron
    image:
      name: cleanup:latest
    schedule: "0 0 0 * * * *"
"#;
        let result = from_yaml_str(yaml);
        assert!(result.is_ok(), "Valid cron job should pass: {result:?}");
        let spec = result.unwrap();
        let cleanup = spec.services.get("cleanup").unwrap();
        assert_eq!(cleanup.rtype, ResourceType::Cron);
        assert_eq!(cleanup.schedule, Some("0 0 0 * * * *".to_string()));
    }

    #[test]
    fn test_cron_without_schedule_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  cleanup:
    rtype: cron
    image:
      name: cleanup:latest
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("schedule") || err_str.contains("cron"),
            "Error should mention missing schedule: {err_str}",
        );
    }

    #[test]
    fn test_service_with_schedule_rejected() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  api:
    rtype: service
    image:
      name: api:latest
    schedule: "0 0 0 * * * *"
"#;
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("schedule") || err_str.contains("cron"),
            "Error should mention schedule/cron mismatch: {err_str}",
        );
    }

    #[test]
    fn test_job_with_schedule_rejected() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  backup:
    rtype: job
    image:
      name: backup:latest
    schedule: "0 0 0 * * * *"
"#;
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("schedule") || err_str.contains("cron"),
            "Error should mention schedule/cron mismatch: {err_str}",
        );
    }

    #[test]
    fn test_invalid_cron_expression_rejected() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  cleanup:
    rtype: cron
    image:
      name: cleanup:latest
    schedule: "not a valid cron"
"#;
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("cron") || err_str.contains("schedule") || err_str.contains("invalid"),
            "Error should mention invalid cron expression: {err_str}",
        );
    }

    #[test]
    fn test_valid_extended_cron_expression() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  cleanup:
    rtype: cron
    image:
      name: cleanup:latest
    schedule: "0 30 2 * * * *"
"#;
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "Extended cron expression should be valid: {result:?}"
        );
    }

    #[test]
    fn test_mixed_service_types_valid() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  api:
    rtype: service
    image:
      name: api:latest
    endpoints:
      - name: http
        protocol: http
        port: 8080
  backup:
    rtype: job
    image:
      name: backup:latest
  cleanup:
    rtype: cron
    image:
      name: cleanup:latest
    schedule: "0 0 0 * * * *"
"#;
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "Mixed service types should be valid: {result:?}"
        );
        let spec = result.unwrap();
        assert_eq!(spec.services.len(), 3);
        assert_eq!(spec.services["api"].rtype, ResourceType::Service);
        assert_eq!(spec.services["backup"].rtype, ResourceType::Job);
        assert_eq!(spec.services["cleanup"].rtype, ResourceType::Cron);
    }

    // =========================================================================
    // Tunnel integration tests
    // =========================================================================

    #[test]
    fn test_valid_endpoint_tunnel() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    endpoints:
      - name: http
        protocol: http
        port: 8080
        tunnel:
          enabled: true
          remote_port: 8080
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "Valid endpoint tunnel should pass: {result:?}"
        );
    }

    #[test]
    fn test_valid_top_level_tunnel() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
tunnels:
  db-access:
    from: api-node
    to: db-node
    local_port: 5432
    remote_port: 5432
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "Valid top-level tunnel should pass: {result:?}"
        );
        let spec = result.unwrap();
        assert!(spec.tunnels.contains_key("db-access"));
    }

    #[test]
    fn test_invalid_tunnel_ttl_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    endpoints:
      - name: http
        protocol: http
        port: 8080
        tunnel:
          enabled: true
          access:
            enabled: true
            max_ttl: invalid
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("ttl") || err_str.contains("TTL") || err_str.contains("invalid"),
            "Error should mention invalid TTL: {err_str}",
        );
    }

    #[test]
    fn test_invalid_tunnel_local_port_zero_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services: {}
tunnels:
  bad-tunnel:
    from: node-a
    to: node-b
    local_port: 0
    remote_port: 8080
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("port") || err_str.contains("local"),
            "Error should mention invalid port: {err_str}",
        );
    }

    #[test]
    fn test_invalid_tunnel_remote_port_zero_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services: {}
tunnels:
  bad-tunnel:
    from: node-a
    to: node-b
    local_port: 8080
    remote_port: 0
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("port") || err_str.contains("remote"),
            "Error should mention invalid port: {err_str}",
        );
    }

    #[test]
    fn test_valid_tunnel_with_access_config() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    endpoints:
      - name: http
        protocol: http
        port: 8080
        tunnel:
          enabled: true
          remote_port: 0
          access:
            enabled: true
            max_ttl: 4h
            audit: true
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "Valid tunnel with access config should pass: {result:?}"
        );
        let spec = result.unwrap();
        let tunnel = spec.services["api"].endpoints[0].tunnel.as_ref().unwrap();
        let access = tunnel.access.as_ref().unwrap();
        assert!(access.enabled);
        assert_eq!(access.max_ttl, Some("4h".to_string()));
        assert!(access.audit);
    }

    // =========================================================================
    // WASM validation integration tests
    // =========================================================================

    #[test]
    fn test_wasm_config_on_non_wasm_type_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  api:
    image:
      name: api:latest
    service_type: standard
    wasm:
      min_instances: 1
      max_instances: 4
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("wasm") || err_str.contains("WASM"),
            "Error should mention wasm config on non-wasm type: {err_str}",
        );
    }

    #[test]
    fn test_wasm_service_without_config_is_ok() {
        // WASM service type without explicit wasm config is fine (defaults will be used)
        let yaml = r"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "WASM service without explicit config should pass: {result:?}"
        );
    }

    #[test]
    fn test_wasm_min_instances_gt_max_rejected() {
        let yaml = r"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
    wasm:
      min_instances: 10
      max_instances: 2
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("min_instances") || err_str.contains("max_instances"),
            "Error should mention instance range: {err_str}",
        );
    }

    #[test]
    fn test_wasm_valid_instance_range() {
        let yaml = r"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
    wasm:
      min_instances: 2
      max_instances: 10
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "Valid WASM instance range should pass: {result:?}"
        );
    }

    #[test]
    fn test_wasm_invalid_max_memory_format() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
    wasm:
      max_memory: "512MB"
"#;
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("memory") || err_str.contains("512MB"),
            "Error should mention invalid memory format: {err_str}",
        );
    }

    #[test]
    fn test_wasm_valid_max_memory_format() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
    wasm:
      max_memory: "256Mi"
"#;
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "Valid WASM max_memory should pass: {result:?}"
        );
    }

    #[test]
    fn test_wasm_capability_escalation_rejected() {
        // WasmTransformer defaults: secrets=false, so requesting secrets=true should fail
        let yaml = r"
version: v1
deployment: my-app
services:
  transform:
    image:
      name: transform:latest
    service_type: wasm_transformer
    wasm:
      capabilities:
        secrets: true
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("secrets") || err_str.contains("capability"),
            "Error should mention capability escalation: {err_str}",
        );
    }

    #[test]
    fn test_wasm_capability_restriction_allowed() {
        // WasmHttp defaults: config=true, keyvalue=true, logging=true, http_client=true
        // and metrics=false, secrets=false, cli=false, filesystem=false, sockets=false.
        // Restricting config=false while keeping all others at or below defaults should pass.
        let yaml = r"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
    wasm:
      capabilities:
        config: false
        keyvalue: true
        logging: true
        secrets: false
        metrics: false
        http_client: true
        cli: false
        filesystem: false
        sockets: false
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "Restricting a default capability should pass: {result:?}"
        );
    }

    #[test]
    fn test_wasm_http_with_tcp_only_endpoints_rejected() {
        // WasmHttp with endpoints but none are HTTP should fail
        let yaml = r"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
    wasm:
      min_instances: 1
    endpoints:
      - name: raw
        protocol: tcp
        port: 9090
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("HTTP") || err_str.contains("http") || err_str.contains("endpoint"),
            "Error should mention missing HTTP endpoint: {err_str}",
        );
    }

    #[test]
    fn test_wasm_http_with_http_endpoint_passes() {
        let yaml = r"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
    wasm:
      min_instances: 1
    endpoints:
      - name: web
        protocol: http
        port: 8080
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "WasmHttp with HTTP endpoint should pass: {result:?}"
        );
    }

    #[test]
    fn test_wasm_http_with_no_endpoints_passes() {
        // WasmHttp with no endpoints at all is fine (endpoints are optional)
        let yaml = r"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
    wasm:
      min_instances: 1
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "WasmHttp with no endpoints should pass: {result:?}"
        );
    }

    #[test]
    fn test_wasm_preopen_empty_source_rejected() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_plugin
    wasm:
      preopens:
        - source: ""
          target: /data
"#;
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("preopen") || err_str.contains("source") || err_str.contains("empty"),
            "Error should mention empty preopen source: {err_str}",
        );
    }

    #[test]
    fn test_wasm_preopen_empty_target_rejected() {
        let yaml = r#"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_plugin
    wasm:
      preopens:
        - source: /host/data
          target: ""
"#;
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("preopen") || err_str.contains("target") || err_str.contains("empty"),
            "Error should mention empty preopen target: {err_str}",
        );
    }

    #[test]
    fn test_wasm_valid_preopen_passes() {
        let yaml = r"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_plugin
    wasm:
      preopens:
        - source: /host/data
          target: /data
          readonly: true
";
        let result = from_yaml_str(yaml);
        assert!(result.is_ok(), "Valid WASM preopen should pass: {result:?}");
    }

    #[test]
    fn test_wasm_plugin_can_use_all_capabilities() {
        // WasmPlugin has all capabilities except sockets as defaults
        let yaml = r"
version: v1
deployment: my-app
services:
  plugin:
    image:
      name: plugin:latest
    service_type: wasm_plugin
    wasm:
      capabilities:
        config: true
        keyvalue: true
        logging: true
        secrets: true
        metrics: true
        http_client: true
        cli: true
        filesystem: true
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "WasmPlugin with all its default capabilities should pass: {result:?}"
        );
    }

    #[test]
    fn test_wasm_plugin_cannot_use_sockets() {
        // WasmPlugin defaults: sockets=false
        let yaml = r"
version: v1
deployment: my-app
services:
  plugin:
    image:
      name: plugin:latest
    service_type: wasm_plugin
    wasm:
      capabilities:
        sockets: true
";
        let result = from_yaml_str(yaml);
        assert!(result.is_err());
        let err_str = result.unwrap_err().to_string();
        assert!(
            err_str.contains("sockets") || err_str.contains("capability"),
            "Error should mention sockets capability escalation: {err_str}",
        );
    }

    #[test]
    fn test_wasm_equal_min_max_instances_passes() {
        let yaml = r"
version: v1
deployment: my-app
services:
  handler:
    image:
      name: handler:latest
    service_type: wasm_http
    wasm:
      min_instances: 5
      max_instances: 5
";
        let result = from_yaml_str(yaml);
        assert!(
            result.is_ok(),
            "Equal min/max instances should pass: {result:?}"
        );
    }
}