semver-analyzer-ts 0.0.4

TypeScript/JavaScript support for the semver-analyzer
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
//! Package manifest (`package.json`) diff engine.
//!
//! Analyzes structural changes to `package.json` between two refs.
//! These are deterministic checks that don't require code parsing —
//! just JSON comparison with semantic awareness of entry points,
//! exports maps, module systems, peer dependencies, engines, and bin entries.

use crate::language::{TsManifestChangeType, TypeScript};
use semver_analyzer_core::ManifestChange;
use serde_json::Value;
use std::collections::BTreeMap;
use std::path::Path;

/// Compare two `package.json` files and produce manifest changes.
pub fn diff_manifests(old: &Value, new: &Value) -> Vec<ManifestChange<TypeScript>> {
    let mut changes = Vec::new();

    diff_entry_points(old, new, &mut changes);
    diff_module_system(old, new, &mut changes);
    diff_exports(old, new, &mut changes);
    diff_peer_dependencies(old, new, &mut changes);
    diff_engines(old, new, &mut changes);
    diff_bin(old, new, &mut changes);

    changes
}

/// Parse a `package.json` file and diff it against another.
pub fn diff_manifest_files(
    old_path: &Path,
    new_path: &Path,
) -> anyhow::Result<Vec<ManifestChange<TypeScript>>> {
    let old_content = std::fs::read_to_string(old_path)?;
    let new_content = std::fs::read_to_string(new_path)?;
    let old: Value = serde_json::from_str(&old_content)?;
    let new: Value = serde_json::from_str(&new_content)?;
    Ok(diff_manifests(&old, &new))
}

// ─── Entry points ────────────────────────────────────────────────────────

/// Diff `main`, `module`, and `types` entry point fields.
fn diff_entry_points(old: &Value, new: &Value, changes: &mut Vec<ManifestChange<TypeScript>>) {
    for field in &["main", "module", "types", "typings"] {
        let old_val = old.get(field).and_then(|v| v.as_str());
        let new_val = new.get(field).and_then(|v| v.as_str());

        match (old_val, new_val) {
            (Some(o), Some(n)) if o != n => {
                changes.push(ManifestChange {
                    field: field.to_string(),
                    change_type: TsManifestChangeType::EntryPointChanged,
                    before: Some(o.to_string()),
                    after: Some(n.to_string()),
                    description: format!("`{}` entry point changed from `{}` to `{}`", field, o, n),
                    is_breaking: true,
                    source_package: None,
                });
            }
            (Some(o), None) => {
                changes.push(ManifestChange {
                    field: field.to_string(),
                    change_type: TsManifestChangeType::EntryPointChanged,
                    before: Some(o.to_string()),
                    after: None,
                    description: format!("`{}` entry point was removed (was `{}`)", field, o),
                    is_breaking: true,
                    source_package: None,
                });
            }
            // Adding an entry point is not breaking
            _ => {}
        }
    }
}

// ─── Module system ───────────────────────────────────────────────────────

/// Diff `"type"` field (CJS ↔ ESM transition).
fn diff_module_system(old: &Value, new: &Value, changes: &mut Vec<ManifestChange<TypeScript>>) {
    let old_type = old
        .get("type")
        .and_then(|v| v.as_str())
        .unwrap_or("commonjs");
    let new_type = new
        .get("type")
        .and_then(|v| v.as_str())
        .unwrap_or("commonjs");

    if old_type != new_type {
        let description = if new_type == "module" {
            "Package switched from CJS to ESM (`\"type\": \"module\"` added). `require()` consumers will break."
        } else {
            "Package switched from ESM to CJS (`\"type\": \"module\"` removed). `import` consumers will break."
        };
        changes.push(ManifestChange {
            field: "type".to_string(),
            change_type: TsManifestChangeType::ModuleSystemChanged,
            before: Some(old_type.to_string()),
            after: Some(new_type.to_string()),
            description: description.to_string(),
            is_breaking: true,
            source_package: None,
        });
    }
}

// ─── Exports map ─────────────────────────────────────────────────────────

/// Diff the `"exports"` field.
///
/// The exports map can be:
/// - A string: `"exports": "./index.js"`
/// - An object with subpath keys: `"exports": { ".": "./index.js", "./utils": "./utils.js" }`
/// - Nested with conditions: `"exports": { ".": { "import": "./index.mjs", "require": "./index.cjs" } }`
fn diff_exports(old: &Value, new: &Value, changes: &mut Vec<ManifestChange<TypeScript>>) {
    let old_exports = old.get("exports");
    let new_exports = new.get("exports");

    match (old_exports, new_exports) {
        (None, None) => {}    // Neither has exports
        (None, Some(_)) => {} // Adding exports is not breaking
        (Some(old_exp), None) => {
            // Removing exports entirely is breaking
            changes.push(ManifestChange {
                field: "exports".to_string(),
                change_type: TsManifestChangeType::ExportsEntryRemoved,
                before: Some(old_exp.to_string()),
                after: None,
                description: "The `exports` field was removed entirely".to_string(),
                is_breaking: true,
                source_package: None,
            });
        }
        (Some(old_exp), Some(new_exp)) => {
            // Flatten both exports into a map of path → conditions
            let old_flat = flatten_exports(old_exp, ".");
            let new_flat = flatten_exports(new_exp, ".");

            // Find removed entries
            for (path, old_conditions) in &old_flat {
                if let Some(new_conditions) = new_flat.get(path.as_str()) {
                    // Path exists in both — check for removed conditions
                    for (cond, old_target) in old_conditions {
                        if let Some(new_target) = new_conditions.get(cond.as_str()) {
                            // Condition exists in both — check target change
                            if old_target != new_target {
                                changes.push(ManifestChange {
                                    field: format!("exports.{}.{}", path, cond),
                                    change_type: TsManifestChangeType::EntryPointChanged,
                                    before: Some(old_target.clone()),
                                    after: Some(new_target.clone()),
                                    description: format!(
                                        "Export `{}` condition `{}` target changed from `{}` to `{}`",
                                        path, cond, old_target, new_target
                                    ),
                                    is_breaking: true,
                    source_package: None,
                                });
                            }
                        } else {
                            // Condition removed
                            changes.push(ManifestChange {
                                field: format!("exports.{}.{}", path, cond),
                                change_type: TsManifestChangeType::ExportsConditionRemoved,
                                before: Some(old_target.clone()),
                                after: None,
                                description: format!(
                                    "Export condition `{}` was removed from `{}`",
                                    cond, path
                                ),
                                is_breaking: true,
                                source_package: None,
                            });
                        }
                    }
                } else {
                    // Entire path removed
                    changes.push(ManifestChange {
                        field: format!("exports.{}", path),
                        change_type: TsManifestChangeType::ExportsEntryRemoved,
                        before: Some(format!("{:?}", old_conditions)),
                        after: None,
                        description: format!("Export path `{}` was removed", path),
                        is_breaking: true,
                        source_package: None,
                    });
                }
            }

            // Find added entries
            for path in new_flat.keys() {
                if !old_flat.contains_key(path.as_str()) {
                    changes.push(ManifestChange {
                        field: format!("exports.{}", path),
                        change_type: TsManifestChangeType::ExportsEntryAdded,
                        before: None,
                        after: Some(path.clone()),
                        description: format!("Export path `{}` was added", path),
                        is_breaking: false,
                        source_package: None,
                    });
                }
            }
        }
    }
}

/// Flatten a potentially nested exports value into a map of
/// `subpath → { condition → target }`.
///
/// Examples:
/// - `"./index.js"` → `{ "." → { "default" → "./index.js" } }`
/// - `{ ".": "./index.js" }` → `{ "." → { "default" → "./index.js" } }`
/// - `{ ".": { "import": "./index.mjs", "require": "./index.cjs" } }` →
///   `{ "." → { "import" → "./index.mjs", "require" → "./index.cjs" } }`
fn flatten_exports(value: &Value, prefix: &str) -> BTreeMap<String, BTreeMap<String, String>> {
    let mut result = BTreeMap::new();

    match value {
        Value::String(s) => {
            let mut conditions = BTreeMap::new();
            conditions.insert("default".to_string(), s.clone());
            result.insert(prefix.to_string(), conditions);
        }
        Value::Object(map) => {
            // Check if this is a conditions object (keys are conditions like "import", "require")
            // or a subpath object (keys are paths like ".", "./utils")
            let is_conditions = map.keys().all(|k| !k.starts_with('.'));

            if is_conditions && !map.is_empty() {
                let mut conditions = BTreeMap::new();
                for (key, val) in map {
                    match val {
                        Value::String(s) => {
                            conditions.insert(key.clone(), s.clone());
                        }
                        Value::Object(_) => {
                            // Nested conditions — recurse
                            let nested = flatten_exports(val, prefix);
                            for (path, conds) in nested {
                                result.entry(path).or_default().extend(conds);
                            }
                        }
                        _ => {}
                    }
                }
                if !conditions.is_empty() {
                    result
                        .entry(prefix.to_string())
                        .or_default()
                        .extend(conditions);
                }
            } else {
                // Subpath keys
                for (key, val) in map {
                    let nested = flatten_exports(val, key);
                    result.extend(nested);
                }
            }
        }
        _ => {}
    }

    result
}

// ─── Peer dependencies ───────────────────────────────────────────────────

/// Diff `peerDependencies`.
fn diff_peer_dependencies(old: &Value, new: &Value, changes: &mut Vec<ManifestChange<TypeScript>>) {
    let old_peers = extract_string_map(old, "peerDependencies");
    let new_peers = extract_string_map(new, "peerDependencies");

    // Added peer deps
    for (name, version) in &new_peers {
        if !old_peers.contains_key(name.as_str()) {
            changes.push(ManifestChange {
                field: format!("peerDependencies.{}", name),
                change_type: TsManifestChangeType::PeerDependencyAdded,
                before: None,
                after: Some(version.clone()),
                description: format!(
                    "Peer dependency `{}@{}` was added. Consumers must install it.",
                    name, version
                ),
                is_breaking: true,
                source_package: None,
            });
        }
    }

    // Removed peer deps
    for (name, version) in &old_peers {
        if !new_peers.contains_key(name.as_str()) {
            changes.push(ManifestChange {
                field: format!("peerDependencies.{}", name),
                change_type: TsManifestChangeType::PeerDependencyRemoved,
                before: Some(version.clone()),
                after: None,
                description: format!("Peer dependency `{}` was removed", name),
                is_breaking: false,
                source_package: None,
            });
        }
    }

    // Changed peer dep ranges
    for (name, old_range) in &old_peers {
        if let Some(new_range) = new_peers.get(name.as_str()) {
            if old_range != new_range {
                // Determine if the range was narrowed or widened.
                // Simple heuristic: if the new range is a subset string-wise,
                // or if major versions differ, it's likely narrowed.
                // Full semver range comparison would need a semver crate.
                // For now, report all range changes and let the description indicate both values.
                changes.push(ManifestChange {
                    field: format!("peerDependencies.{}", name),
                    change_type: TsManifestChangeType::PeerDependencyRangeChanged,
                    before: Some(old_range.clone()),
                    after: Some(new_range.clone()),
                    description: format!(
                        "Peer dependency `{}` range changed from `{}` to `{}`",
                        name, old_range, new_range
                    ),
                    // Conservative: treat any range change as potentially breaking
                    is_breaking: true,
                    source_package: None,
                });
            }
        }
    }
}

// ─── Engines ─────────────────────────────────────────────────────────────

/// Diff `engines` field (e.g., `node`, `npm`).
fn diff_engines(old: &Value, new: &Value, changes: &mut Vec<ManifestChange<TypeScript>>) {
    let old_engines = extract_string_map(old, "engines");
    let new_engines = extract_string_map(new, "engines");

    // Engine constraint added
    for (engine, constraint) in &new_engines {
        if !old_engines.contains_key(engine.as_str()) {
            changes.push(ManifestChange {
                field: format!("engines.{}", engine),
                change_type: TsManifestChangeType::EngineConstraintChanged,
                before: None,
                after: Some(constraint.clone()),
                description: format!(
                    "Engine constraint `{}: {}` was added. Consumers on unsupported runtimes will be affected.",
                    engine, constraint
                ),
                is_breaking: true,
                    source_package: None,
            });
        }
    }

    // Engine constraint changed
    for (engine, old_constraint) in &old_engines {
        if let Some(new_constraint) = new_engines.get(engine.as_str()) {
            if old_constraint != new_constraint {
                changes.push(ManifestChange {
                    field: format!("engines.{}", engine),
                    change_type: TsManifestChangeType::EngineConstraintChanged,
                    before: Some(old_constraint.clone()),
                    after: Some(new_constraint.clone()),
                    description: format!(
                        "Engine constraint for `{}` changed from `{}` to `{}`",
                        engine, old_constraint, new_constraint
                    ),
                    // Conservative: any engine constraint change could be breaking
                    is_breaking: true,
                    source_package: None,
                });
            }
        }
        // Engine constraint removed — loosening, not breaking
    }
}

// ─── Bin entries ─────────────────────────────────────────────────────────

/// Diff `bin` field (CLI entry points).
fn diff_bin(old: &Value, new: &Value, changes: &mut Vec<ManifestChange<TypeScript>>) {
    let old_bins = extract_bin_map(old);
    let new_bins = extract_bin_map(new);

    // Removed bin entries
    for (name, path) in &old_bins {
        if !new_bins.contains_key(name.as_str()) {
            changes.push(ManifestChange {
                field: format!("bin.{}", name),
                change_type: TsManifestChangeType::BinEntryRemoved,
                before: Some(path.clone()),
                after: None,
                description: format!("CLI command `{}` was removed", name),
                is_breaking: true,
                source_package: None,
            });
        }
    }
    // Added bin entries are not breaking
}

// ─── Utility functions ───────────────────────────────────────────────────

/// Extract a string map from a JSON object field.
fn extract_string_map(value: &Value, field: &str) -> BTreeMap<String, String> {
    value
        .get(field)
        .and_then(|v| v.as_object())
        .map(|obj| {
            obj.iter()
                .filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
                .collect()
        })
        .unwrap_or_default()
}

/// Extract bin entries as a map. Handles both string and object forms:
/// - `"bin": "./cli.js"` → uses package name as key
/// - `"bin": { "myapp": "./cli.js" }` → as-is
fn extract_bin_map(value: &Value) -> BTreeMap<String, String> {
    match value.get("bin") {
        Some(Value::String(s)) => {
            let name = value
                .get("name")
                .and_then(|v| v.as_str())
                .unwrap_or("<unknown>");
            let mut map = BTreeMap::new();
            map.insert(name.to_string(), s.clone());
            map
        }
        Some(Value::Object(obj)) => obj
            .iter()
            .filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
            .collect(),
        _ => BTreeMap::new(),
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────

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

    fn find_manifest_change(
        changes: &[ManifestChange<TypeScript>],
        ct: TsManifestChangeType,
    ) -> &ManifestChange<TypeScript> {
        changes
            .iter()
            .find(|c| c.change_type == ct)
            .unwrap_or_else(|| {
                panic!(
                    "ManifestChange {:?} not found in: {:?}",
                    ct,
                    changes.iter().map(|c| &c.change_type).collect::<Vec<_>>()
                )
            })
    }

    fn has_manifest_change(
        changes: &[ManifestChange<TypeScript>],
        ct: TsManifestChangeType,
    ) -> bool {
        changes.iter().any(|c| c.change_type == ct)
    }

    // ── Entry points ─────────────────────────────────────────────────

    #[test]
    fn detect_main_entry_changed() {
        let old = json!({ "main": "./dist/index.js" });
        let new = json!({ "main": "./lib/index.js" });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::EntryPointChanged);
        assert!(c.is_breaking);
        assert_eq!(c.field, "main");
    }

    #[test]
    fn detect_types_entry_removed() {
        let old = json!({ "types": "./dist/index.d.ts" });
        let new = json!({});
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::EntryPointChanged);
        assert!(c.is_breaking);
        assert_eq!(c.field, "types");
    }

    #[test]
    fn adding_entry_point_not_breaking() {
        let old = json!({});
        let new = json!({ "main": "./dist/index.js" });
        let changes = diff_manifests(&old, &new);
        assert!(changes.is_empty());
    }

    #[test]
    fn identical_entry_points_no_changes() {
        let pkg = json!({ "main": "./index.js", "types": "./index.d.ts" });
        let changes = diff_manifests(&pkg, &pkg);
        assert!(changes.is_empty());
    }

    // ── Module system ────────────────────────────────────────────────

    #[test]
    fn detect_cjs_to_esm() {
        let old = json!({});
        let new = json!({ "type": "module" });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::ModuleSystemChanged);
        assert!(c.is_breaking);
        assert!(c.description.contains("CJS to ESM"));
    }

    #[test]
    fn detect_esm_to_cjs() {
        let old = json!({ "type": "module" });
        let new = json!({});
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::ModuleSystemChanged);
        assert!(c.is_breaking);
        assert!(c.description.contains("ESM to CJS"));
    }

    #[test]
    fn same_module_system_no_change() {
        let old = json!({ "type": "module" });
        let new = json!({ "type": "module" });
        let changes = diff_manifests(&old, &new);
        assert!(!has_manifest_change(
            &changes,
            TsManifestChangeType::ModuleSystemChanged
        ));
    }

    // ── Exports map ──────────────────────────────────────────────────

    #[test]
    fn detect_exports_entry_removed() {
        let old = json!({
            "exports": {
                ".": "./index.js",
                "./utils": "./utils.js"
            }
        });
        let new = json!({
            "exports": {
                ".": "./index.js"
            }
        });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::ExportsEntryRemoved);
        assert!(c.is_breaking);
        assert!(c.field.contains("./utils"));
    }

    #[test]
    fn detect_exports_entry_added() {
        let old = json!({
            "exports": { ".": "./index.js" }
        });
        let new = json!({
            "exports": { ".": "./index.js", "./utils": "./utils.js" }
        });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::ExportsEntryAdded);
        assert!(!c.is_breaking);
    }

    #[test]
    fn detect_exports_condition_removed() {
        let old = json!({
            "exports": {
                ".": {
                    "import": "./index.mjs",
                    "require": "./index.cjs"
                }
            }
        });
        let new = json!({
            "exports": {
                ".": {
                    "import": "./index.mjs"
                }
            }
        });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::ExportsConditionRemoved);
        assert!(c.is_breaking);
        assert!(c.field.contains("require"));
    }

    #[test]
    fn detect_exports_entirely_removed() {
        let old = json!({ "exports": { ".": "./index.js" } });
        let new = json!({});
        let changes = diff_manifests(&old, &new);

        assert!(has_manifest_change(
            &changes,
            TsManifestChangeType::ExportsEntryRemoved
        ));
    }

    #[test]
    fn exports_string_form() {
        let old = json!({ "exports": "./index.js" });
        let new = json!({ "exports": "./dist/index.js" });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::EntryPointChanged);
        assert!(c.is_breaking);
    }

    // ── Peer dependencies ────────────────────────────────────────────

    #[test]
    fn detect_peer_dependency_added() {
        let old = json!({});
        let new = json!({ "peerDependencies": { "react": "^18.0.0" } });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::PeerDependencyAdded);
        assert!(c.is_breaking);
        assert!(c.description.contains("react"));
    }

    #[test]
    fn detect_peer_dependency_removed() {
        let old = json!({ "peerDependencies": { "react": "^18.0.0" } });
        let new = json!({});
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::PeerDependencyRemoved);
        assert!(!c.is_breaking);
    }

    #[test]
    fn detect_peer_dependency_range_changed() {
        let old = json!({ "peerDependencies": { "react": "^17.0.0 || ^18.0.0" } });
        let new = json!({ "peerDependencies": { "react": "^18.0.0" } });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::PeerDependencyRangeChanged);
        assert!(c.is_breaking);
    }

    // ── Engines ──────────────────────────────────────────────────────

    #[test]
    fn detect_engine_constraint_added() {
        let old = json!({});
        let new = json!({ "engines": { "node": ">=18" } });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::EngineConstraintChanged);
        assert!(c.is_breaking);
    }

    #[test]
    fn detect_engine_constraint_changed() {
        let old = json!({ "engines": { "node": ">=16" } });
        let new = json!({ "engines": { "node": ">=18" } });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::EngineConstraintChanged);
        assert!(c.is_breaking);
        assert_eq!(c.before.as_deref(), Some(">=16"));
        assert_eq!(c.after.as_deref(), Some(">=18"));
    }

    // ── Bin entries ──────────────────────────────────────────────────

    #[test]
    fn detect_bin_removed() {
        let old = json!({
            "name": "myapp",
            "bin": { "myapp": "./cli.js", "myapp-dev": "./dev.js" }
        });
        let new = json!({
            "name": "myapp",
            "bin": { "myapp": "./cli.js" }
        });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::BinEntryRemoved);
        assert!(c.is_breaking);
        assert!(c.field.contains("myapp-dev"));
    }

    #[test]
    fn detect_bin_string_form_removed() {
        let old = json!({ "name": "myapp", "bin": "./cli.js" });
        let new = json!({ "name": "myapp" });
        let changes = diff_manifests(&old, &new);

        let c = find_manifest_change(&changes, TsManifestChangeType::BinEntryRemoved);
        assert!(c.is_breaking);
    }

    // ── No changes ───────────────────────────────────────────────────

    #[test]
    fn identical_manifests_no_changes() {
        let pkg = json!({
            "name": "my-lib",
            "version": "1.0.0",
            "main": "./dist/index.js",
            "types": "./dist/index.d.ts",
            "exports": {
                ".": {
                    "import": "./dist/index.mjs",
                    "require": "./dist/index.cjs"
                }
            },
            "peerDependencies": { "react": "^18.0.0" },
            "engines": { "node": ">=16" },
            "bin": { "myapp": "./cli.js" }
        });
        let changes = diff_manifests(&pkg, &pkg);
        assert!(changes.is_empty());
    }

    // ── Flatten exports tests ────────────────────────────────────────

    #[test]
    fn flatten_string_exports() {
        let val = json!("./index.js");
        let flat = flatten_exports(&val, ".");
        assert_eq!(flat.get(".").unwrap().get("default").unwrap(), "./index.js");
    }

    #[test]
    fn flatten_subpath_exports() {
        let val = json!({
            ".": "./index.js",
            "./utils": "./utils.js"
        });
        let flat = flatten_exports(&val, ".");
        assert_eq!(flat.len(), 2);
        assert!(flat.contains_key("."));
        assert!(flat.contains_key("./utils"));
    }

    #[test]
    fn flatten_conditional_exports() {
        let val = json!({
            ".": {
                "import": "./index.mjs",
                "require": "./index.cjs"
            }
        });
        let flat = flatten_exports(&val, ".");
        let root = flat.get(".").unwrap();
        assert_eq!(root.get("import").unwrap(), "./index.mjs");
        assert_eq!(root.get("require").unwrap(), "./index.cjs");
    }
}