tatara-nix 0.2.187

Nix's academic essence, re-expressed in Rust + Lisp. Derivation, StorePath, Module, Overlay, Flake — all typed, all Lisp-authorable via #[derive(TataraDomain)], all attestable via BLAKE3.
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
//! Module fixpoint resolver — NixOS's `lib.evalModules`, typed.
//!
//! Multiple modules contribute assignments to a shared config namespace
//! (`services.nginx.enable`, etc.). Each contribution is a `MkExpr`:
//!
//!   - `Set { value }`            normal priority (100)
//!   - `If { cond, value }`       normal priority, filtered if cond is false
//!   - `Force { value }`          high priority (50) — override
//!   - `Default { value }`        low priority (1000) — fallback
//!   - `Merge { values }`         multi-contribution (treated as N `Set`s)
//!   - `Order { Before, value }`  list ordering — prepend
//!   - `Order { After,  value }`  list ordering — append
//!
//! **Resolution** walks every module, collects contributions grouped by
//! dotted path, picks the highest-priority group at each path, then joins
//! contributions within that group by value type (lists concat respecting
//! Before/After, attrsets deep-merge, scalars conflict).
//!
//! The whole algorithm is `tatara-lattice` in practice: same-priority
//! contributions meet (`⊓`) — different priorities resolve via the priority
//! order, which IS a lattice over `Priority`.

use serde_json::{Map as JMap, Value};
use std::collections::BTreeMap;
use thiserror::Error;

use crate::module::{MkExpr, Module, ModuleOption, OptionType, Placement};
// ModuleOption is used in the tests (fn opt helper below); the lint is
// wrong to flag it — keeping the import.
const _: fn() = || {
    let _ = std::marker::PhantomData::<ModuleOption>;
};

/// Resolution priority. Lower number = higher priority (matches Nix convention).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Priority(pub i32);

impl Priority {
    pub const FORCE: Self = Self(50);
    pub const NORMAL: Self = Self(100);
    pub const DEFAULT: Self = Self(1000);
}

/// A single resolved contribution: path + priority + value + placement.
#[derive(Clone, Debug)]
struct Contribution {
    priority: Priority,
    value: Value,
    order: Option<Placement>,
}

/// Errors surfaced by module resolution.
#[derive(Debug, Error)]
pub enum ResolveError {
    #[error("conflicting values at path {path:?}: {left} vs {right}")]
    ScalarConflict {
        path: String,
        left: Value,
        right: Value,
    },
    #[error("at path {path:?}: cannot merge {kind_a} with {kind_b}")]
    TypeMismatch {
        path: String,
        kind_a: &'static str,
        kind_b: &'static str,
    },
    #[error("mkIf condition at path {path:?} is not a boolean or truthy value")]
    NonBoolCondition { path: String },
    #[error("option {path:?} expected {expected}, got {got} ({value})")]
    OptionTypeMismatch {
        path: String,
        expected: String,
        got: &'static str,
        value: String,
    },
    #[error("option {path:?}: enum value {value:?} is not one of {choices:?}")]
    EnumChoiceInvalid {
        path: String,
        value: String,
        choices: Vec<String>,
    },
}

type ResolveResult<T> = Result<T, ResolveError>;

/// Resolve a single module to a final JSON `Value` — the NixOS-equivalent of
/// `(lib.evalModules { modules = [m]; }).config`.
pub fn resolve_module(module: &Module) -> ResolveResult<Value> {
    resolve_modules(std::slice::from_ref(module))
}

/// Resolve a set of modules to a single merged config.
/// **Also validates** every contribution against its declared `ModuleOption`
/// type — any option whose resolved value fails its type check surfaces
/// `ResolveError::OptionTypeMismatch` or `EnumChoiceInvalid`.
pub fn resolve_modules(modules: &[Module]) -> ResolveResult<Value> {
    // 1. Collect all contributions, keyed by dotted path.
    let mut contributions: BTreeMap<String, Vec<Contribution>> = BTreeMap::new();
    for module in modules {
        for assign in &module.config {
            let path = assign.path.clone();
            collect(&assign.expr, &path, &mut contributions)?;
        }
    }

    // 2. For each path, resolve priority → join contributions at top priority.
    let mut resolved: BTreeMap<String, Value> = BTreeMap::new();
    for (path, contribs) in contributions {
        if contribs.is_empty() {
            continue;
        }
        let min_priority = contribs.iter().map(|c| c.priority).min().unwrap();
        let winners: Vec<Contribution> = contribs
            .into_iter()
            .filter(|c| c.priority == min_priority)
            .collect();
        let value = join(&path, winners)?;
        resolved.insert(path, value);
    }

    // 3. Lift dotted paths into a nested `Value::Object`.
    let config = lift(resolved);

    // 4. Validate against declared options across all modules.
    //    Missing options that have a default are treated as satisfied.
    validate_options(modules, &config)?;

    Ok(config)
}

/// Expand an `MkExpr` at `path` into zero or more `Contribution`s.
fn collect(
    expr: &MkExpr,
    path: &str,
    out: &mut BTreeMap<String, Vec<Contribution>>,
) -> ResolveResult<()> {
    match expr {
        MkExpr::Set { value } => push(out, path, Priority::NORMAL, value.clone(), None),
        MkExpr::If { condition, value } => {
            if is_truthy(condition) {
                push(out, path, Priority::NORMAL, value.clone(), None);
            }
            // Explicit `false` is a skip — not a value.
        }
        MkExpr::Force { value } => push(out, path, Priority::FORCE, value.clone(), None),
        MkExpr::Default { value } => push(out, path, Priority::DEFAULT, value.clone(), None),
        MkExpr::Merge { values } => {
            for v in values {
                push(out, path, Priority::NORMAL, v.clone(), None);
            }
        }
        MkExpr::Order { placement, value } => {
            push(out, path, Priority::NORMAL, value.clone(), Some(*placement));
        }
    }
    Ok(())
}

fn push(
    out: &mut BTreeMap<String, Vec<Contribution>>,
    path: &str,
    priority: Priority,
    value: Value,
    order: Option<Placement>,
) {
    out.entry(path.to_string()).or_default().push(Contribution {
        priority,
        value,
        order,
    });
}

/// Combine same-priority contributions at a path into one `Value`.
///   - Single contribution: pass through.
///   - Multiple lists: concat (Before items first, then normal, then After).
///   - Multiple objects: deep-merge.
///   - Multiple scalars: error.
fn join(path: &str, mut contribs: Vec<Contribution>) -> ResolveResult<Value> {
    if contribs.len() == 1 {
        return Ok(contribs.pop().unwrap().value);
    }
    let first_kind = value_kind(&contribs[0].value);
    for c in &contribs {
        let k = value_kind(&c.value);
        if k != first_kind && !(first_kind == "object" && k == "object") {
            return Err(ResolveError::TypeMismatch {
                path: path.into(),
                kind_a: first_kind,
                kind_b: k,
            });
        }
    }
    match first_kind {
        "array" => Ok(join_lists(contribs)),
        "object" => join_objects(path, contribs),
        _ => {
            // Scalars can't join at same priority — that's a spec error.
            let [l, r] = match contribs.as_slice() {
                [a, b] => [a.value.clone(), b.value.clone()],
                [a, b, ..] => [a.value.clone(), b.value.clone()],
                _ => unreachable!(),
            };
            Err(ResolveError::ScalarConflict {
                path: path.into(),
                left: l,
                right: r,
            })
        }
    }
}

fn join_lists(contribs: Vec<Contribution>) -> Value {
    let mut before: Vec<Value> = Vec::new();
    let mut normal: Vec<Value> = Vec::new();
    let mut after: Vec<Value> = Vec::new();
    for c in contribs {
        let items = match c.value {
            Value::Array(a) => a,
            _ => continue,
        };
        match c.order {
            Some(Placement::Before) => before.extend(items),
            Some(Placement::After) => after.extend(items),
            None => normal.extend(items),
        }
    }
    let mut out = before;
    out.append(&mut normal);
    out.append(&mut after);
    Value::Array(out)
}

fn join_objects(path: &str, contribs: Vec<Contribution>) -> ResolveResult<Value> {
    let mut merged = JMap::new();
    for c in contribs {
        let obj = match c.value {
            Value::Object(m) => m,
            other => {
                return Err(ResolveError::TypeMismatch {
                    path: path.into(),
                    kind_a: "object",
                    kind_b: value_kind(&other),
                });
            }
        };
        for (k, v) in obj {
            if let Some(existing) = merged.remove(&k) {
                let subpath = format!("{path}.{k}");
                // Recursively join `existing` + `v` as two "contributions" at the subpath.
                let joined = join(
                    &subpath,
                    vec![
                        Contribution {
                            priority: Priority::NORMAL,
                            value: existing,
                            order: None,
                        },
                        Contribution {
                            priority: Priority::NORMAL,
                            value: v,
                            order: None,
                        },
                    ],
                )?;
                merged.insert(k, joined);
            } else {
                merged.insert(k, v);
            }
        }
    }
    Ok(Value::Object(merged))
}

/// Map dotted paths → nested JSON object.
/// `{"a.b": 1, "a.c": 2}` → `{"a": {"b": 1, "c": 2}}`.
fn lift(resolved: BTreeMap<String, Value>) -> Value {
    let mut root = Value::Object(JMap::new());
    for (path, value) in resolved {
        let parts: Vec<&str> = path.split('.').collect();
        insert_nested(&mut root, &parts, value);
    }
    root
}

fn insert_nested(root: &mut Value, parts: &[&str], value: Value) {
    if parts.is_empty() {
        *root = value;
        return;
    }
    if !root.is_object() {
        *root = Value::Object(JMap::new());
    }
    let obj = root.as_object_mut().expect("object");
    if parts.len() == 1 {
        obj.insert(parts[0].to_string(), value);
        return;
    }
    let head = parts[0];
    let entry = obj
        .entry(head.to_string())
        .or_insert_with(|| Value::Object(JMap::new()));
    insert_nested(entry, &parts[1..], value);
}

fn value_kind(v: &Value) -> &'static str {
    match v {
        Value::Null => "null",
        Value::Bool(_) => "bool",
        Value::Number(_) => "number",
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
    }
}

fn is_truthy(v: &Value) -> bool {
    match v {
        Value::Bool(b) => *b,
        Value::Null => false,
        Value::Number(n) => n.as_f64().map(|x| x != 0.0).unwrap_or(true),
        Value::String(s) => !s.is_empty(),
        Value::Array(a) => !a.is_empty(),
        Value::Object(o) => !o.is_empty(),
    }
}

// ── Option validation ────────────────────────────────────────────────

/// For every module option declared across the `modules`, look up the resolved
/// value at that option's path and check it satisfies the option's type.
///   - Missing options with a `default` are skipped (the default would apply).
///   - Missing options without a default are also skipped (Nix treats options
///     as advertisements, not requirements — resolver doesn't enforce presence
///     at this layer).
fn validate_options(modules: &[Module], config: &Value) -> ResolveResult<()> {
    for module in modules {
        for option in &module.options {
            match read_path(config, &option.name) {
                Some(value) => validate(&option.name, value, &option.option_type)?,
                None => {
                    // Missing — OK if there's a default.
                    if option.default.is_some() {
                        continue;
                    }
                }
            }
        }
    }
    Ok(())
}

fn read_path<'a>(root: &'a Value, dotted: &str) -> Option<&'a Value> {
    let mut cur = root;
    for segment in dotted.split('.') {
        cur = cur.as_object()?.get(segment)?;
    }
    Some(cur)
}

fn validate(path: &str, value: &Value, ty: &OptionType) -> ResolveResult<()> {
    match ty {
        OptionType::Bool => expect(path, value, "bool", |v| v.is_boolean()),
        OptionType::Int => expect(path, value, "int", |v| {
            v.as_i64().is_some() || v.as_u64().is_some()
        }),
        OptionType::Str => expect(path, value, "string", Value::is_string),
        OptionType::Float => expect(path, value, "number", |v| {
            v.is_f64() || v.is_i64() || v.is_u64()
        }),
        OptionType::Path => expect(path, value, "path (string)", Value::is_string),
        OptionType::Package => expect(path, value, "package (object)", Value::is_object),
        OptionType::Any => Ok(()),
        OptionType::ListOf { item } => {
            let arr = value
                .as_array()
                .ok_or_else(|| type_err(path, "list", value))?;
            for (i, entry) in arr.iter().enumerate() {
                validate(&format!("{path}[{i}]"), entry, item)?;
            }
            Ok(())
        }
        OptionType::AttrsOf { value: vty } => {
            let obj = value
                .as_object()
                .ok_or_else(|| type_err(path, "attrset", value))?;
            for (k, v) in obj {
                validate(&format!("{path}.{k}"), v, vty)?;
            }
            Ok(())
        }
        OptionType::Enum { choices } => {
            let s = value
                .as_str()
                .ok_or_else(|| type_err(path, "enum (string)", value))?;
            if !choices.iter().any(|c| c == s) {
                return Err(ResolveError::EnumChoiceInvalid {
                    path: path.into(),
                    value: s.to_string(),
                    choices: choices.clone(),
                });
            }
            Ok(())
        }
        OptionType::Submodule { options } => {
            let obj = value
                .as_object()
                .ok_or_else(|| type_err(path, "submodule (object)", value))?;
            for sub in options {
                if let Some(v) = obj.get(&sub.name) {
                    validate(&format!("{path}.{}", sub.name), v, &sub.option_type)?;
                }
            }
            Ok(())
        }
    }
}

fn expect(
    path: &str,
    value: &Value,
    expected: &str,
    predicate: impl Fn(&Value) -> bool,
) -> ResolveResult<()> {
    if predicate(value) {
        Ok(())
    } else {
        Err(type_err(path, expected, value))
    }
}

fn type_err(path: &str, expected: &str, value: &Value) -> ResolveError {
    ResolveError::OptionTypeMismatch {
        path: path.into(),
        expected: expected.into(),
        got: value_kind(value),
        value: value.to_string(),
    }
}

// ── tests ────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::module::{ConfigAssignment, Module};
    use serde_json::json;

    fn m(name: &str, config: Vec<ConfigAssignment>) -> Module {
        Module {
            name: name.into(),
            imports: vec![],
            options: vec![],
            config,
            description: None,
        }
    }

    fn set(path: &str, value: Value) -> ConfigAssignment {
        ConfigAssignment {
            path: path.into(),
            expr: MkExpr::Set { value },
        }
    }

    #[test]
    fn simple_set_resolves_to_nested_object() {
        let module = m("demo", vec![set("services.nginx.enable", json!(true))]);
        let resolved = resolve_module(&module).unwrap();
        assert_eq!(
            resolved,
            json!({ "services": { "nginx": { "enable": true }}})
        );
    }

    #[test]
    fn mk_if_true_includes_value() {
        let module = m(
            "demo",
            vec![ConfigAssignment {
                path: "x".into(),
                expr: MkExpr::If {
                    condition: json!(true),
                    value: json!(42),
                },
            }],
        );
        let resolved = resolve_module(&module).unwrap();
        assert_eq!(resolved, json!({ "x": 42 }));
    }

    #[test]
    fn mk_if_false_skips_value() {
        let module = m(
            "demo",
            vec![ConfigAssignment {
                path: "x".into(),
                expr: MkExpr::If {
                    condition: json!(false),
                    value: json!(42),
                },
            }],
        );
        let resolved = resolve_module(&module).unwrap();
        assert_eq!(resolved, json!({}));
    }

    #[test]
    fn mk_force_overrides_normal_across_modules() {
        let a = m("a", vec![set("retention", json!(30))]);
        let b = m(
            "b",
            vec![ConfigAssignment {
                path: "retention".into(),
                expr: MkExpr::Force { value: json!(7) },
            }],
        );
        let resolved = resolve_modules(&[a, b]).unwrap();
        assert_eq!(resolved, json!({ "retention": 7 }));
    }

    #[test]
    fn mk_merge_combines_contributions() {
        let module = m(
            "demo",
            vec![ConfigAssignment {
                path: "tags".into(),
                expr: MkExpr::Merge {
                    values: vec![json!(["a"]), json!(["b"]), json!(["c"])],
                },
            }],
        );
        let resolved = resolve_module(&module).unwrap();
        assert_eq!(resolved, json!({ "tags": ["a", "b", "c"] }));
    }

    #[test]
    fn list_ordering_respects_before_and_after() {
        let a = m(
            "a",
            vec![ConfigAssignment {
                path: "path".into(),
                expr: MkExpr::Order {
                    placement: Placement::Before,
                    value: json!(["/bin"]),
                },
            }],
        );
        let b = m("b", vec![set("path", json!(["/usr/bin"]))]);
        let c = m(
            "c",
            vec![ConfigAssignment {
                path: "path".into(),
                expr: MkExpr::Order {
                    placement: Placement::After,
                    value: json!(["/opt/bin"]),
                },
            }],
        );
        let resolved = resolve_modules(&[a, b, c]).unwrap();
        assert_eq!(
            resolved,
            json!({ "path": ["/bin", "/usr/bin", "/opt/bin"] })
        );
    }

    #[test]
    fn scalar_conflict_at_same_priority_errors() {
        let a = m("a", vec![set("port", json!(80))]);
        let b = m("b", vec![set("port", json!(8080))]);
        let err = resolve_modules(&[a, b]).unwrap_err();
        assert!(matches!(err, ResolveError::ScalarConflict { .. }));
    }

    #[test]
    fn objects_deep_merge_at_same_priority() {
        let a = m(
            "a",
            vec![set("services", json!({ "nginx": { "enable": true }}))],
        );
        let b = m(
            "b",
            vec![set("services", json!({ "nginx": { "port": 80 }}))],
        );
        let resolved = resolve_modules(&[a, b]).unwrap();
        assert_eq!(
            resolved,
            json!({ "services": { "nginx": { "enable": true, "port": 80 }}})
        );
    }

    #[test]
    fn type_mismatch_between_object_and_list_errors() {
        let a = m("a", vec![set("x", json!({ "k": 1 }))]);
        let b = m("b", vec![set("x", json!([1, 2]))]);
        let err = resolve_modules(&[a, b]).unwrap_err();
        assert!(matches!(err, ResolveError::TypeMismatch { .. }));
    }

    #[test]
    fn force_beats_conflicting_same_priority() {
        // Two normal-priority scalars conflict — but mkForce at higher priority
        // short-circuits the conflict by winning alone.
        let a = m("a", vec![set("port", json!(80))]);
        let b = m("b", vec![set("port", json!(8080))]);
        let c = m(
            "c",
            vec![ConfigAssignment {
                path: "port".into(),
                expr: MkExpr::Force { value: json!(9999) },
            }],
        );
        let resolved = resolve_modules(&[a, b, c]).unwrap();
        assert_eq!(resolved, json!({ "port": 9999 }));
    }

    #[test]
    fn multi_module_nested_paths_compose() {
        let a = m(
            "observability",
            vec![
                set("services.prometheus.enable", json!(true)),
                set("services.prometheus.port", json!(9090)),
            ],
        );
        let b = m(
            "grafana",
            vec![
                set("services.grafana.enable", json!(true)),
                set("services.grafana.port", json!(3000)),
            ],
        );
        let resolved = resolve_modules(&[a, b]).unwrap();
        assert_eq!(
            resolved,
            json!({
                "services": {
                    "prometheus": { "enable": true, "port": 9090 },
                    "grafana":    { "enable": true, "port": 3000 }
                }
            })
        );
    }

    // ── mkDefault tests ──────────────────────────────────────────────

    #[test]
    fn mk_default_used_when_no_other_contribution() {
        let module = m(
            "demo",
            vec![ConfigAssignment {
                path: "port".into(),
                expr: MkExpr::Default { value: json!(8080) },
            }],
        );
        let resolved = resolve_module(&module).unwrap();
        assert_eq!(resolved, json!({ "port": 8080 }));
    }

    #[test]
    fn mk_default_loses_to_normal() {
        let a = m(
            "a",
            vec![ConfigAssignment {
                path: "port".into(),
                expr: MkExpr::Default { value: json!(8080) },
            }],
        );
        let b = m("b", vec![set("port", json!(80))]);
        let resolved = resolve_modules(&[a, b]).unwrap();
        assert_eq!(resolved, json!({ "port": 80 }));
    }

    #[test]
    fn mk_default_loses_to_force() {
        let a = m(
            "a",
            vec![ConfigAssignment {
                path: "port".into(),
                expr: MkExpr::Default { value: json!(8080) },
            }],
        );
        let b = m(
            "b",
            vec![ConfigAssignment {
                path: "port".into(),
                expr: MkExpr::Force { value: json!(443) },
            }],
        );
        let resolved = resolve_modules(&[a, b]).unwrap();
        assert_eq!(resolved, json!({ "port": 443 }));
    }

    // ── Option validation tests ───────────────────────────────────────

    fn opt(name: &str, ty: OptionType, default: Option<Value>) -> ModuleOption {
        ModuleOption {
            name: name.into(),
            option_type: ty,
            default,
            description: None,
            read_only: false,
        }
    }

    #[test]
    fn bool_option_accepts_bool() {
        let m = Module {
            name: "ok".into(),
            imports: vec![],
            options: vec![opt("enable", OptionType::Bool, None)],
            config: vec![set("enable", json!(true))],
            description: None,
        };
        resolve_module(&m).unwrap();
    }

    #[test]
    fn bool_option_rejects_string() {
        let m = Module {
            name: "bad".into(),
            imports: vec![],
            options: vec![opt("enable", OptionType::Bool, None)],
            config: vec![set("enable", json!("true"))],
            description: None,
        };
        let err = resolve_module(&m).unwrap_err();
        assert!(matches!(
            err,
            ResolveError::OptionTypeMismatch { expected, got: "string", .. } if expected == "bool"
        ));
    }

    #[test]
    fn enum_option_accepts_valid_choice() {
        let m = Module {
            name: "ok".into(),
            imports: vec![],
            options: vec![opt(
                "level",
                OptionType::Enum {
                    choices: vec!["info".into(), "warn".into(), "error".into()],
                },
                None,
            )],
            config: vec![set("level", json!("warn"))],
            description: None,
        };
        resolve_module(&m).unwrap();
    }

    #[test]
    fn enum_option_rejects_invalid_choice() {
        let m = Module {
            name: "bad".into(),
            imports: vec![],
            options: vec![opt(
                "level",
                OptionType::Enum {
                    choices: vec!["info".into(), "warn".into(), "error".into()],
                },
                None,
            )],
            config: vec![set("level", json!("debug"))],
            description: None,
        };
        let err = resolve_module(&m).unwrap_err();
        assert!(matches!(
            err,
            ResolveError::EnumChoiceInvalid { value, .. } if value == "debug"
        ));
    }

    #[test]
    fn list_of_int_rejects_non_int_element() {
        let m = Module {
            name: "bad".into(),
            imports: vec![],
            options: vec![opt(
                "ports",
                OptionType::ListOf {
                    item: Box::new(OptionType::Int),
                },
                None,
            )],
            config: vec![set("ports", json!([80, "443", 8080]))],
            description: None,
        };
        let err = resolve_module(&m).unwrap_err();
        assert!(matches!(err, ResolveError::OptionTypeMismatch { .. }));
    }

    #[test]
    fn missing_option_with_default_is_ok() {
        let m = Module {
            name: "ok".into(),
            imports: vec![],
            options: vec![opt("retention", OptionType::Int, Some(json!(30)))],
            config: vec![], // option not assigned
            description: None,
        };
        resolve_module(&m).unwrap();
    }

    #[test]
    fn submodule_validates_nested_fields() {
        let m = Module {
            name: "bad".into(),
            imports: vec![],
            options: vec![opt(
                "server",
                OptionType::Submodule {
                    options: vec![
                        opt("host", OptionType::Str, None),
                        opt("port", OptionType::Int, None),
                    ],
                },
                None,
            )],
            config: vec![set("server", json!({ "host": "x", "port": "bad" }))],
            description: None,
        };
        let err = resolve_module(&m).unwrap_err();
        assert!(matches!(err, ResolveError::OptionTypeMismatch { .. }));
    }
}