splicer 2.4.0

Plan and generate middleware splice operations for WebAssembly component composition graphs.
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
use crate::parse::config::Injection;
use anyhow::Context;
use cviz::model::{compatible_fingerprints, ExportInfo};
use cviz::parse::component::{parse_component, parse_component_imports};
use std::collections::{BTreeMap, HashMap};
use std::fs;

// Generated by build.rs from wit/tier1/world.wit — single source of truth.
include!(concat!(env!("OUT_DIR"), "/tier_interfaces.rs"));

/// Append `@version` to an unversioned interface name, producing the
/// form used in component import/export names (e.g.
/// `"splicer:tier1/before"` + `"0.2.0"` → `"splicer:tier1/before@0.2.0"`).
pub fn versioned_interface(iface: &str, version: &str) -> String {
    format!("{iface}@{version}")
}

/// The outcome of a single middleware contract check.
#[derive(Clone, Debug, PartialEq)]
pub enum ContractResult {
    /// The middleware exports the interface and the type fingerprints match.
    Ok,
    /// The middleware could not be validated — either no path was provided or
    /// the middleware does not export the contracted interface.  Injection can
    /// still proceed, but type safety is unconfirmed.
    Warn(String),
    /// The middleware exports the interface but with an incompatible type
    /// fingerprint.  Injection should be blocked.
    Error(String),
    /// The middleware does not export the target interface but does export at
    /// least one tier-1 type-erased interface (`splicer:tier1/{before,after,blocking}`).
    /// The inner list names the matched interfaces so the adapter generator knows
    /// exactly which hooks to wire up.
    Tier1Compatible(Vec<String>),
    /// The middleware exports at least one tier-2 interface
    /// (`splicer:tier2/{before,after,trap}`). The inner list names the
    /// matched interfaces so the adapter generator knows exactly
    /// which hooks to wire up.
    Tier2Compatible(Vec<String>),
}

/// Check that every middleware in `to_inject` is type-compatible with the
/// interface being contracted on.
///
/// Returns one [`ContractResult`] per injection in the same order.
/// Callers are responsible for acting on the results (logging, aborting, etc.).
pub fn validate_contract(
    to_inject: &[Injection],
    interface_name: &str,
    contract_fingerprint: &Option<String>,
    checked_middlewares: &mut HashMap<String, BTreeMap<String, ExportInfo>>,
) -> Vec<ContractResult> {
    let mut results = vec![];
    for Injection { name, path, .. } in to_inject.iter() {
        if !checked_middlewares.contains_key(name.as_str()) {
            match discover_middleware_exports(path) {
                Ok(exports) => {
                    checked_middlewares.insert(name.clone(), exports);
                }
                Err(err) => {
                    results.push(ContractResult::Warn(format!(
                        "Unable to load middleware '{name}': {err:#}"
                    )));
                    continue;
                }
            }
        }
        let exports = checked_middlewares.get(name.as_str()).unwrap();

        if let Some(ExportInfo { fingerprint, .. }) = exports.get(interface_name) {
            if !compatible_fingerprints(contract_fingerprint, fingerprint) {
                results.push(ContractResult::Error(format!(
                    "incompatible type signatures for middleware '{}' on interface '{}'\n\t{name}:\t{fingerprint:?}\n\ttarget: {contract_fingerprint:?}",
                    name, interface_name
                )));
            } else {
                results.push(ContractResult::Ok);
            }
        } else {
            results.push(classify_tier_export(name, exports, path, interface_name));
        }
    }
    results
}

/// Classify a middleware that doesn't export the target interface
/// directly: detect which (if any) `splicer:tierN/*` package it
/// exports, enforce the one-tier-per-middleware rule, and return the
/// appropriate `ContractResult`.
fn classify_tier_export(
    name: &str,
    exports: &BTreeMap<String, ExportInfo>,
    path: &Option<String>,
    interface_name: &str,
) -> ContractResult {
    let tier1 = match_tier_interfaces(exports, TIER1_INTERFACES, TIER1_VERSION);
    let tier2 = match_tier_interfaces(exports, TIER2_INTERFACES, TIER2_VERSION);

    // One-tier-per-middleware: reject any component exporting
    // interfaces from multiple tier packages. The user-facing rule
    // lives in `docs/adapter-components.md` ("One tier per middleware").
    if !tier1.is_empty() && !tier2.is_empty() {
        return ContractResult::Error(format!(
            "middleware '{name}' exports interfaces from multiple tiers \
             (tier 1: {tier1_list}; tier 2: {tier2_list}).\n\n\
             A middleware must implement exactly one tier. To combine \
             behaviors, ship them as separate components and chain \
             them in `inject: [...]`.",
            tier1_list = tier1.join(", "),
            tier2_list = tier2.join(", "),
        ));
    }

    if !tier1.is_empty() {
        if !is_adapter_eligible(path, interface_name) {
            return warn_not_exported(name, interface_name);
        }
        return ContractResult::Tier1Compatible(tier1);
    }

    if !tier2.is_empty() {
        if !is_adapter_eligible(path, interface_name) {
            return warn_not_exported(name, interface_name);
        }
        return ContractResult::Tier2Compatible(tier2);
    }

    warn_not_exported(name, interface_name)
}

fn warn_not_exported(name: &str, interface_name: &str) -> ContractResult {
    ContractResult::Warn(format!(
        "Middleware '{}' does not export interface '{}'.\n\
         \tIt cannot be spliced on this interface.\n\
         \tCheck that the middleware both imports and re-exports '{}',\n\
         \tor that the interface name in your config exactly matches\n\
         \twhat the middleware binary exports.",
        name, interface_name, interface_name
    ))
}

/// Returns `true` when a middleware is a candidate for tier-1 adapter generation.
///
/// A middleware is tier-1 compatible when:
/// 1. It exports one of the [`TIER1_INTERFACES`] — the positive signal that it was written
///    as a type-erased middleware.  cviz has already validated that the exported
///    interface is structurally sound when it produced the fingerprint.
/// 2. A path to the binary is provided — the adapter cannot be generated without it.
/// 3. It does **not** import `target_interface` — confirming it is not a regular
///    pass-through middleware that simply failed the fingerprint check.
///
/// Check whether `export_name` (possibly versioned, e.g.
/// `"splicer:tier1/before@0.2.0"`) matches the unversioned interface
/// constant `iface` (e.g. `"splicer:tier1/before"`) with semver
/// compatibility against `expected_version`.
///
/// - Exact match (no version suffix) always passes.
/// - Versioned match passes when the export's version is semver-
///   compatible with `expected_version`: same major (or both 0.x with
///   same minor).
fn is_compatible_interface(export_name: &str, iface: &str, expected_version: &str) -> bool {
    if export_name == iface {
        return true;
    }
    // Check for "iface@version" pattern.
    let Some(rest) = export_name.strip_prefix(iface) else {
        return false;
    };
    let Some(version_str) = rest.strip_prefix('@') else {
        return false;
    };
    let Ok(export_ver) = semver::Version::parse(version_str) else {
        return false;
    };
    // Semver compatibility: ^expected_version.
    // For 0.x: same major AND same minor (0.1.x matches 0.1.0).
    // For 1.x+: same major (1.x.y matches 1.0.0).
    let Ok(req) = semver::VersionReq::parse(&format!("^{expected_version}")) else {
        return false;
    };
    req.matches(&export_ver)
}

/// Return the subset of `tier_ifaces` that `exports` includes, with
/// semver-compatible version matching against `tier_version`.
///
/// Export keys may be versioned (e.g. `"splicer:tier1/before@0.2.0"`)
/// while the constants are unversioned (`"splicer:tier1/before"`).
/// Any export with a version semver-compatible with the version
/// splicer was built against passes (see [`is_compatible_interface`]).
fn match_tier_interfaces(
    exports: &BTreeMap<String, ExportInfo>,
    tier_ifaces: &[&str],
    tier_version: &str,
) -> Vec<String> {
    tier_ifaces
        .iter()
        .filter(|iface| {
            exports
                .keys()
                .any(|export_name| is_compatible_interface(export_name, iface, tier_version))
        })
        .map(|iface| iface.to_string())
        .collect()
}

/// Adapter generation is only viable when:
/// 1. A path to the middleware binary is provided — the adapter
///    generator needs the bytes.
/// 2. The middleware does **not** import `target_interface` —
///    confirming it isn't a regular pass-through middleware that
///    simply failed the fingerprint check.
fn is_adapter_eligible(middleware_path: &Option<String>, target_interface: &str) -> bool {
    let Some(path) = middleware_path else {
        return false;
    };
    let Ok(buff) = fs::read(path) else {
        return false;
    };
    let Ok(imports) = parse_component_imports(&buff) else {
        return false;
    };
    !imports.iter().any(|(name, _)| name == target_interface)
}

fn discover_middleware_exports(
    wasm_path: &Option<String>,
) -> anyhow::Result<BTreeMap<String, ExportInfo>> {
    let Some(path) = wasm_path else {
        return Ok(BTreeMap::default());
    };
    let buff = fs::read(path).with_context(|| format!("failed to read '{path}'"))?;
    let graph = parse_component(&buff)
        .with_context(|| format!("failed to parse Wasm component '{path}'"))?;
    Ok(graph.component_exports)
}

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

    // ── is_compatible_interface tests ─────────────────────────────────

    #[test]
    fn exact_match_no_version() {
        assert!(is_compatible_interface(
            "splicer:tier1/before",
            "splicer:tier1/before",
            "0.1.0"
        ));
    }

    #[test]
    fn versioned_match_same_version() {
        assert!(is_compatible_interface(
            "splicer:tier1/before@0.1.0",
            "splicer:tier1/before",
            "0.1.0"
        ));
    }

    #[test]
    fn versioned_match_patch_bump() {
        assert!(is_compatible_interface(
            "splicer:tier1/before@0.1.3",
            "splicer:tier1/before",
            "0.1.0"
        ));
    }

    #[test]
    fn versioned_mismatch_minor_bump_0x() {
        // Under 0.x semver, minor bumps are breaking.
        assert!(!is_compatible_interface(
            "splicer:tier1/before@0.2.0",
            "splicer:tier1/before",
            "0.1.0"
        ));
    }

    #[test]
    fn versioned_mismatch_different_name() {
        assert!(!is_compatible_interface(
            "splicer:tier1/after@0.1.0",
            "splicer:tier1/before",
            "0.1.0"
        ));
    }

    #[test]
    fn versioned_match_major_1x_minor_bump() {
        // Under 1.x semver, minor bumps are compatible.
        assert!(is_compatible_interface(
            "splicer:tier1/before@1.2.0",
            "splicer:tier1/before",
            "1.0.0"
        ));
    }

    #[test]
    fn versioned_mismatch_major_bump() {
        assert!(!is_compatible_interface(
            "splicer:tier1/before@2.0.0",
            "splicer:tier1/before",
            "1.0.0"
        ));
    }

    // ── existing tests ───────────────────────────────────────────────

    fn discover_exports_from_bytes(bytes: &[u8]) -> BTreeMap<String, ExportInfo> {
        let graph = parse_component(bytes).expect("Unable to parse component");
        graph.component_exports
    }

    fn injection(name: &str) -> Injection {
        Injection {
            name: name.to_string(),
            adapter_info: None,
            tier: None,
            builtin: None,
            builtin_config: Default::default(),
            config_as_wave: None,
            config_provider_path: None,
            path: None,
        }
    }

    fn export_with_fingerprint(fp: &str) -> ExportInfo {
        ExportInfo {
            source_instance: 0,
            fingerprint: Some(fp.to_string()),
            ty: None,
        }
    }

    /// Pre-populate the middleware cache so that discovery is bypassed.
    fn cache_with(
        mw_name: &str,
        interface: &str,
        fp: &str,
    ) -> HashMap<String, BTreeMap<String, ExportInfo>> {
        let mut exports = BTreeMap::new();
        exports.insert(interface.to_string(), export_with_fingerprint(fp));
        let mut cache = HashMap::new();
        cache.insert(mw_name.to_string(), exports);
        cache
    }

    // -----------------------------------------------------------------------
    // WARN: unable to validate
    // -----------------------------------------------------------------------

    #[test]
    fn warn_when_no_path() {
        // Injection with no path → discovery returns empty → cannot validate.
        let mut cache = HashMap::new();
        let results = validate_contract(&[injection("mw")], "wasi:http/handler", &None, &mut cache);
        assert_eq!(results.len(), 1);
        assert!(
            matches!(results[0], ContractResult::Warn(_)),
            "expected Warn, got {:?}",
            results[0]
        );
    }

    #[test]
    fn warn_when_interface_not_in_exports() {
        // Middleware is in cache but does not export the contracted interface.
        let mut exports = BTreeMap::new();
        exports.insert(
            "other:pkg/other".to_string(),
            export_with_fingerprint("fp-x"),
        );
        let mut cache = HashMap::new();
        cache.insert("mw".to_string(), exports);

        let results = validate_contract(
            &[injection("mw")],
            "wasi:http/handler",
            &Some("fp-a".to_string()),
            &mut cache,
        );
        assert_eq!(results.len(), 1);
        assert!(
            matches!(results[0], ContractResult::Warn(_)),
            "expected Warn, got {:?}",
            results[0]
        );
    }

    #[test]
    fn warn_for_each_injection_without_path() {
        // Multiple injections, all without paths — each should produce a Warn.
        let injections = vec![injection("mw-a"), injection("mw-b"), injection("mw-c")];
        let mut cache = HashMap::new();
        let results = validate_contract(&injections, "wasi:http/handler", &None, &mut cache);
        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| matches!(r, ContractResult::Warn(_))));
    }

    // -----------------------------------------------------------------------
    // ERROR: incompatible fingerprints
    // -----------------------------------------------------------------------

    #[test]
    fn error_when_fingerprints_incompatible() {
        // Contract expects "fp-a"; middleware exports "fp-b" → Error.
        let mut cache = cache_with("mw", "wasi:http/handler", "fp-b");
        let results = validate_contract(
            &[injection("mw")],
            "wasi:http/handler",
            &Some("fp-a".to_string()),
            &mut cache,
        );
        assert_eq!(results.len(), 1);
        assert!(
            matches!(results[0], ContractResult::Error(_)),
            "expected Error, got {:?}",
            results[0]
        );
    }

    // -----------------------------------------------------------------------
    // OK: compatible fingerprints
    // -----------------------------------------------------------------------

    #[test]
    fn ok_when_fingerprints_match() {
        let mut cache = cache_with("mw", "wasi:http/handler", "fp-a");
        let results = validate_contract(
            &[injection("mw")],
            "wasi:http/handler",
            &Some("fp-a".to_string()),
            &mut cache,
        );
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], ContractResult::Ok);
    }

    // -----------------------------------------------------------------------
    // Tier classification (`match_tier_interfaces`, `classify_tier_export`)
    // -----------------------------------------------------------------------

    /// Build an exports map containing one or more interface keys, each
    /// associated with a fingerprint-less stub. Useful for tier-detection
    /// tests that don't care about fingerprints.
    fn exports_for(ifaces: &[&str]) -> BTreeMap<String, ExportInfo> {
        let mut out = BTreeMap::new();
        for iface in ifaces {
            out.insert(
                iface.to_string(),
                ExportInfo {
                    source_instance: 0,
                    fingerprint: None,
                    ty: None,
                },
            );
        }
        out
    }

    #[test]
    fn match_tier_interfaces_picks_up_versioned_exports() {
        // Versioned export key + unversioned constant → semver-compatible match.
        let exports = exports_for(&["splicer:tier2/before@0.1.0", "splicer:tier2/after@0.1.7"]);
        let matched = match_tier_interfaces(&exports, TIER2_INTERFACES, TIER2_VERSION);
        assert_eq!(matched.len(), 2);
        assert!(matched.iter().any(|i| i == "splicer:tier2/before"));
        assert!(matched.iter().any(|i| i == "splicer:tier2/after"));
    }

    #[test]
    fn match_tier_interfaces_skips_unrelated_packages() {
        let exports = exports_for(&["other:pkg/iface", "wasi:http/handler@0.3.0"]);
        let matched = match_tier_interfaces(&exports, TIER2_INTERFACES, TIER2_VERSION);
        assert!(matched.is_empty());
    }

    #[test]
    fn multi_tier_export_is_rejected_with_clear_error() {
        // Middleware exports both tier-1 AND tier-2 → multi-tier rejection,
        // independent of whether the binary path resolves (the rejection
        // happens before adapter-eligibility checks).
        let mut cache = HashMap::new();
        cache.insert(
            "mw".to_string(),
            exports_for(&[
                &versioned_interface("splicer:tier1/before", TIER1_VERSION),
                &versioned_interface("splicer:tier2/after", TIER2_VERSION),
            ]),
        );
        let results = validate_contract(
            &[injection("mw")],
            "wasi:http/handler@0.3.0",
            &None,
            &mut cache,
        );
        assert_eq!(results.len(), 1);
        let ContractResult::Error(msg) = &results[0] else {
            panic!("expected Error, got {:?}", results[0]);
        };
        assert!(
            msg.contains("multiple tiers"),
            "error message should mention multi-tier rejection: {msg}"
        );
        assert!(
            msg.contains("tier 1") && msg.contains("tier 2"),
            "error message should name both tiers: {msg}"
        );
    }

    #[test]
    fn tier2_only_without_path_falls_through_to_warn() {
        // Tier-2 detection succeeds on the export side, but adapter
        // generation needs the binary path. Without a path we can't
        // confirm adapter-eligibility, so we surface a Warn rather
        // than mis-classifying.
        let mut cache = HashMap::new();
        cache.insert(
            "mw".to_string(),
            exports_for(&["splicer:tier2/before@0.1.0"]),
        );
        let results = validate_contract(
            &[injection("mw")],
            "wasi:http/handler@0.3.0",
            &None,
            &mut cache,
        );
        assert_eq!(results.len(), 1);
        assert!(
            matches!(results[0], ContractResult::Warn(_)),
            "expected Warn (no path), got {:?}",
            results[0]
        );
    }

    #[test]
    fn ok_when_both_fingerprints_none() {
        // If neither side has type info, compatible_fingerprints returns true → Ok.
        let mut cache = HashMap::new();
        let mut exports = BTreeMap::new();
        exports.insert(
            "wasi:http/handler".to_string(),
            ExportInfo {
                source_instance: 0,
                fingerprint: None,
                ty: None,
            },
        );
        cache.insert("mw".to_string(), exports);

        let results = validate_contract(&[injection("mw")], "wasi:http/handler", &None, &mut cache);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], ContractResult::Ok);
    }

    // -----------------------------------------------------------------------
    // WAT-based integration: discover_exports_from_bytes
    // -----------------------------------------------------------------------

    /// Middleware using the FromExports pattern produces a non-None fingerprint.
    #[test]
    fn discover_exports_from_from_exports_mw() {
        let wat = r#"(component
            (import "wasi:http/handler@0.3.0" (instance $host
                (export "handle" (func (param "req" u32) (result u32)))
            ))
            (alias export $host "handle" (func $f))
            (instance $out (export "handle" (func $f)))
            (export "wasi:http/handler@0.3.0" (instance $out))
        )"#;
        let bytes = wat::parse_str(wat).expect("failed to parse WAT");
        let exports = discover_exports_from_bytes(&bytes);

        let export = exports
            .get("wasi:http/handler@0.3.0")
            .expect("expected export for wasi:http/handler@0.3.0");
        assert!(
            export.fingerprint.is_some(),
            "expected fingerprint for FromExports middleware"
        );
    }

    /// Middleware that directly re-exports an imported instance produces a
    /// non-None fingerprint (RC-3 coverage).
    #[test]
    fn discover_exports_from_passthrough_mw() {
        let wat = r#"(component
            (import "wasi:http/handler@0.3.0" (instance $handler
                (export "handle" (func (param "req" u32) (result u32)))
            ))
            (export "wasi:http/handler@0.3.0" (instance $handler))
        )"#;
        let bytes = wat::parse_str(wat).expect("failed to parse WAT");
        let exports = discover_exports_from_bytes(&bytes);

        let export = exports
            .get("wasi:http/handler@0.3.0")
            .expect("expected export for wasi:http/handler@0.3.0");
        assert!(
            export.fingerprint.is_some(),
            "expected fingerprint for import-reexport middleware"
        );
    }

    /// A compatible WAT middleware (same signature as chain) validates as Ok.
    #[test]
    fn ok_result_for_compatible_wat_middleware() {
        // Chain component exports "handle" (param u32) -> u32
        let chain_wat = r#"(component
            (import "wasi:http/handler@0.3.0" (instance $host
                (export "handle" (func (param "req" u32) (result u32)))
            ))
            (alias export $host "handle" (func $f))
            (instance $out (export "handle" (func $f)))
            (export "wasi:http/handler@0.3.0" (instance $out))
        )"#;
        // Middleware with the same signature
        let mw_wat = r#"(component
            (import "wasi:http/handler@0.3.0" (instance $handler
                (export "handle" (func (param "req" u32) (result u32)))
            ))
            (export "wasi:http/handler@0.3.0" (instance $handler))
        )"#;

        let chain_bytes = wat::parse_str(chain_wat).expect("failed to parse chain WAT");
        let mw_bytes = wat::parse_str(mw_wat).expect("failed to parse middleware WAT");

        let chain_exports = discover_exports_from_bytes(&chain_bytes);
        let chain_fp = chain_exports
            .get("wasi:http/handler@0.3.0")
            .and_then(|e| e.fingerprint.clone());

        // Pre-populate cache with the middleware's discovered exports
        let mw_exports = discover_exports_from_bytes(&mw_bytes);
        let mut cache = HashMap::new();
        cache.insert("mw".to_string(), mw_exports);

        let inj = Injection {
            name: "mw".to_string(),
            adapter_info: None,
            tier: None,
            builtin: None,
            builtin_config: Default::default(),
            config_as_wave: None,
            config_provider_path: None,
            path: None,
        };
        let results = validate_contract(&[inj], "wasi:http/handler@0.3.0", &chain_fp, &mut cache);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], ContractResult::Ok);
    }

    /// An incompatible WAT middleware (different signature) validates as Error.
    #[test]
    fn error_result_for_incompatible_wat_middleware() {
        // Chain: (param u32) -> u32
        let chain_wat = r#"(component
            (import "wasi:http/handler@0.3.0" (instance $host
                (export "handle" (func (param "req" u32) (result u32)))
            ))
            (alias export $host "handle" (func $f))
            (instance $out (export "handle" (func $f)))
            (export "wasi:http/handler@0.3.0" (instance $out))
        )"#;
        // Incompatible middleware: different param type
        let mw_wat = r#"(component
            (import "wasi:http/handler@0.3.0" (instance $handler
                (export "handle" (func (param "req" string) (result u32)))
            ))
            (export "wasi:http/handler@0.3.0" (instance $handler))
        )"#;

        let chain_bytes = wat::parse_str(chain_wat).expect("failed to parse chain WAT");
        let mw_bytes = wat::parse_str(mw_wat).expect("failed to parse middleware WAT");

        let chain_exports = discover_exports_from_bytes(&chain_bytes);
        let chain_fp = chain_exports
            .get("wasi:http/handler@0.3.0")
            .and_then(|e| e.fingerprint.clone());

        let mw_exports = discover_exports_from_bytes(&mw_bytes);
        let mut cache = HashMap::new();
        cache.insert("mw".to_string(), mw_exports);

        let inj = Injection {
            name: "mw".to_string(),
            adapter_info: None,
            tier: None,
            builtin: None,
            builtin_config: Default::default(),
            config_as_wave: None,
            config_provider_path: None,
            path: None,
        };
        let results = validate_contract(&[inj], "wasi:http/handler@0.3.0", &chain_fp, &mut cache);
        assert_eq!(results.len(), 1);
        assert!(
            matches!(results[0], ContractResult::Error(_)),
            "expected Error for incompatible middleware"
        );
    }

    #[test]
    fn mixed_results_for_multiple_injections() {
        // mw-ok: matching fingerprint → Ok
        // mw-bad: mismatched fingerprint → Error
        // mw-unknown: not in cache, no path → Warn
        let injections = vec![
            injection("mw-ok"),
            injection("mw-bad"),
            injection("mw-unknown"),
        ];
        let mut cache = HashMap::new();
        cache.insert("mw-ok".to_string(), {
            let mut m = BTreeMap::new();
            m.insert(
                "wasi:http/handler".to_string(),
                export_with_fingerprint("fp-a"),
            );
            m
        });
        cache.insert("mw-bad".to_string(), {
            let mut m = BTreeMap::new();
            m.insert(
                "wasi:http/handler".to_string(),
                export_with_fingerprint("fp-b"),
            );
            m
        });
        // mw-unknown: not inserted → discovery runs, returns empty (path: None)

        let results = validate_contract(
            &injections,
            "wasi:http/handler",
            &Some("fp-a".to_string()),
            &mut cache,
        );
        assert_eq!(results.len(), 3);
        assert_eq!(results[0], ContractResult::Ok);
        assert!(matches!(results[1], ContractResult::Error(_)));
        assert!(matches!(results[2], ContractResult::Warn(_)));
    }
}