bynk-project 0.309.4

The project model of the Bynk compiler: discovery, the unit dependency graph, path resolution, cross-unit consistency checks, and the schema-registry document.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
use std::collections::HashMap;
use std::fs;
use std::path::{Component, Path, PathBuf};

use crate::discovery::read_source;
use crate::json::json_string;

/// v0.17 [DECISION L] stub: a version range is *unpinned* — and rejected — when
/// it is empty, `*`/`x`/`latest`, or otherwise carries no concrete version
/// number. A pinned range names at least one digit (`^5`, `~1.2`, `1.2.3`,
/// `>=1.0 <2`). No allow-list or registry check yet.
pub fn is_unpinned_range(range: &str) -> bool {
    let r = range.trim();
    if r.is_empty() || r == "*" || r.eq_ignore_ascii_case("x") || r.eq_ignore_ascii_case("latest") {
        return true;
    }
    !r.chars().any(|c| c.is_ascii_digit())
}

/// Render a minimal `package.json` carrying the adapter-declared dependencies.
pub fn render_package_json(deps: &std::collections::BTreeMap<String, String>) -> String {
    let mut out = String::from("{\n  \"dependencies\": {\n");
    let entries: Vec<String> = deps
        .iter()
        .map(|(pkg, range)| format!("    {}: {}", json_string(pkg), json_string(range)))
        .collect();
    out.push_str(&entries.join(",\n"));
    out.push_str("\n  }\n}\n");
    out
}

/// Normalise a relative path by resolving `.` and `..` components, so a binding
/// clause like `./tokens.binding.ts` beside `src/tokens.bynk` yields the output
/// path `tokens.binding.ts`.
pub fn normalize_rel(p: &Path) -> PathBuf {
    let mut out: Vec<std::ffi::OsString> = Vec::new();
    for c in p.components() {
        match c {
            Component::CurDir => {}
            Component::ParentDir => {
                out.pop();
            }
            Component::Normal(s) => out.push(s.to_os_string()),
            Component::RootDir | Component::Prefix(_) => {}
        }
    }
    out.iter().collect()
}

/// v0.113 (DECISION S): the project's source tree, read from `bynk.toml`'s
/// `[paths]` section. Test-ness is a property of the `suite` declaration, not of
/// a directory, so the layout is a flat **`include`** list of trees to compile
/// and an **`exclude`** list of subtrees to skip — not the role-named
/// `src`/`tests` split. Each `include` entry is a root walked for `.bynk` files;
/// a file's identity path is relative to the `include` root that contains it.
#[derive(Debug, Clone)]
pub struct ProjectPaths {
    /// Trees to compile, relative to the project root. Defaults to the
    /// conventional roots that exist (`src`, and `tests` when present), else the
    /// project root itself.
    pub include: Vec<PathBuf>,
    /// Subtrees to skip during discovery (monorepo, vendored, or generated
    /// `.bynk`), relative to the project root.
    pub exclude: Vec<PathBuf>,
}

impl ProjectPaths {
    /// The default layout when `bynk.toml` declares no `[paths] include`: the
    /// conventional `src`/`tests` roots that exist under `project_root`, or the
    /// project root itself when neither does. This keeps a conventional
    /// `src/`(+`tests/`) project working with no config, and lets a flat project
    /// (`.bynk` at the root, no `src/`) compile with no config either.
    pub fn conventional(project_root: &Path) -> Self {
        let mut include = Vec::new();
        for role in ["src", "tests"] {
            if project_root.join(role).is_dir() {
                include.push(PathBuf::from(role));
            }
        }
        if include.is_empty() {
            include.push(PathBuf::from("."));
        }
        ProjectPaths {
            include,
            exclude: Vec::new(),
        }
    }
}

/// Like [`try_read_project_paths`], but honours `overlay` for `bynk.toml`
/// itself — the in-memory test seam's (#57) one remaining disk read outside
/// `discovery::read_source`, now routed through the same helper so a test
/// can supply a virtual `bynk.toml` with no on-disk file at all. `#[cfg(test)]`
/// because that's its only consumer today; drop the gate if a non-test caller
/// needs it (`try_read_project_paths_with`, which this wraps, has none of that
/// restriction — production code already reaches it through the always-on
/// `try_read_project_paths`).
#[cfg(test)]
pub(crate) fn read_project_paths_with(
    project_root: &Path,
    overlay: &HashMap<PathBuf, String>,
) -> ProjectPaths {
    try_read_project_paths_with(project_root, overlay)
        .unwrap_or_else(|_| ProjectPaths::conventional(project_root))
}

/// A problem in `bynk.toml` that [`try_read_project_paths`] surfaces instead
/// of silently falling back to the conventional layout.
#[derive(Debug)]
pub enum ProjectPathsError {
    /// `bynk.toml` exists but does not parse as TOML (e.g. a trailing comma).
    Malformed,
    /// `[paths]` has a key other than `include`/`exclude` — most likely a typo
    /// (`inculde`) that was silently read as "no include list".
    UnknownKey(String),
    /// #1665: a top-level entry that isn't one of the manifest's tables
    /// ([`MANIFEST_TABLES`]): a typo (`[pahts]`), or a table for a planned
    /// feature (`[dependencies]`) that would otherwise read as working.
    UnknownTable(String),
    /// #1665: a key at the top level of `bynk.toml`, outside any table.
    TopLevelKey(String),
    /// #1665: a key in `[project]` or `[lsp]` that the table doesn't have.
    /// (`[paths]` reports [`Self::UnknownKey`]; `[fmt]` is checked by its own
    /// reader, which owns its keys.)
    UnknownTableKey { table: &'static str, key: String },
}

/// #1665: the tables `bynk.toml` may hold, each with the keys it accepts.
/// `[fmt]`'s keys are `bynk-fmt`'s to check (`FmtConfig`, `deny_unknown_fields`),
/// so its list here is empty and unchecked.
pub const MANIFEST_TABLES: &[(&str, &[&str])] = &[
    ("project", &["name", "version"]),
    ("paths", &["include", "exclude"]),
    ("fmt", &[]),
    ("lsp", &["diagnostics_mode", "diagnostics_debounce_ms"]),
];

/// Tables a user might write for a feature that is designed but not built,
/// with the issue that tracks it.
const PLANNED_TABLES: &[(&str, &str)] = &[
    ("dependencies", "#843"),
    ("dev-dependencies", "#843"),
    ("workspace", "#843"),
    ("deploy", "#551"),
];

/// The manifest's tables as `` `[project]`, `[paths]`, `[fmt]` <conj> `[lsp]` ``,
/// from [`MANIFEST_TABLES`], so a message can't fall behind it.
fn table_list(conj: &str) -> String {
    let names: Vec<String> = MANIFEST_TABLES
        .iter()
        .map(|(t, _)| format!("`[{t}]`"))
        .collect();
    match names.split_last() {
        Some((last, rest)) if !rest.is_empty() => format!("{} {conj} {last}", rest.join(", ")),
        _ => names.join(""),
    }
}

/// The closest of `candidates` to `name`, if any is within two edits.
fn did_you_mean<'a>(name: &str, candidates: impl IntoIterator<Item = &'a str>) -> Option<&'a str> {
    candidates
        .into_iter()
        .map(|c| (edit_distance(name, c), c))
        .filter(|(d, _)| *d <= 2)
        .min_by_key(|(d, _)| *d)
        .map(|(_, c)| c)
}

/// Levenshtein distance over chars.
fn edit_distance(a: &str, b: &str) -> usize {
    let b: Vec<char> = b.chars().collect();
    let mut row: Vec<usize> = (0..=b.len()).collect();
    for (i, ca) in a.chars().enumerate() {
        let mut prev = row[0];
        row[0] = i + 1;
        for (j, cb) in b.iter().enumerate() {
            let cur = row[j + 1];
            row[j + 1] = (prev + usize::from(ca != *cb)).min(row[j] + 1).min(cur + 1);
            prev = cur;
        }
    }
    row[b.len()]
}

impl std::fmt::Display for ProjectPathsError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ProjectPathsError::Malformed => write!(f, "`bynk.toml` is not valid TOML"),
            ProjectPathsError::UnknownKey(k) => {
                write!(
                    f,
                    "`[paths]` has no key named `{k}` — did you mean `include` or `exclude`?"
                )
            }
            ProjectPathsError::UnknownTable(name) => {
                write!(f, "`bynk.toml` has no table named `[{name}]` — ")?;
                if let Some((_, issue)) = PLANNED_TABLES.iter().find(|(t, _)| *t == name) {
                    write!(f, "it is planned but not yet supported ({issue})")
                } else if let Some(t) = did_you_mean(name, MANIFEST_TABLES.iter().map(|(t, _)| *t))
                {
                    write!(f, "did you mean `[{t}]`?")
                } else {
                    write!(f, "the tables are {}", table_list("and"))
                }
            }
            ProjectPathsError::TopLevelKey(key) => {
                // #1770 review: a table's own name written as a plain value
                // (`paths = "src"`).
                if MANIFEST_TABLES.iter().any(|(t, _)| t == key) {
                    return write!(
                        f,
                        "`{key}` in `bynk.toml` must be a table — write it as `[{key}]`"
                    );
                }
                write!(f, "`bynk.toml` has a key `{key}` outside any table — ")?;
                match MANIFEST_TABLES
                    .iter()
                    .find(|(_, keys)| keys.contains(&key.as_str()))
                {
                    Some((table, _)) => write!(f, "did you mean it under `[{table}]`?"),
                    None => write!(f, "keys belong in {}", table_list("or")),
                }
            }
            ProjectPathsError::UnknownTableKey { table, key } => {
                let keys = MANIFEST_TABLES
                    .iter()
                    .find(|(t, _)| t == table)
                    .map(|(_, k)| *k)
                    .unwrap_or_default();
                write!(f, "`[{table}]` has no key named `{key}` — ")?;
                match did_you_mean(key, keys.iter().copied()) {
                    Some(k) => write!(f, "did you mean `{k}`?"),
                    None => {
                        let list: Vec<String> = keys.iter().map(|k| format!("`{k}`")).collect();
                        write!(f, "its keys are {}", list.join(", "))
                    }
                }
            }
        }
    }
}

/// #1665: check `bynk.toml`'s table set and the keys of `[project]` and
/// `[lsp]` ([`MANIFEST_TABLES`]). Every other reader takes only its own table
/// and ignores the rest, so an unknown table (`[dependencies]`, a typo'd
/// `[pahts]`) used to build cleanly with none of its intended behaviour.
///
/// Kept apart from [`try_read_project_paths_with`] on purpose: the CLIs call
/// both and refuse an unknown table, while the language server reads `[paths]`
/// alone and keeps serving, so an extra table can't cost an editor its
/// `include` layout. `Ok` when there is no `bynk.toml`. A manifest that doesn't
/// parse is [`ProjectPathsError::Malformed`].
pub fn check_manifest(
    project_root: &Path,
    overlay: &HashMap<PathBuf, String>,
) -> Result<(), ProjectPathsError> {
    let toml_path = project_root.join("bynk.toml");
    let Ok(content) = read_source(&toml_path, overlay) else {
        return Ok(());
    };
    check_manifest_str(&content)
}

/// [`check_manifest`] over a manifest's text.
pub fn check_manifest_str(content: &str) -> Result<(), ProjectPathsError> {
    let doc = content
        .parse::<toml::Table>()
        .map_err(|_| ProjectPathsError::Malformed)?;
    for (name, value) in &doc {
        // #1770 review: a planned table names its issue whatever shape it is
        // written in (`[[dependencies]]`, `dependencies = [...]`).
        if PLANNED_TABLES.iter().any(|(t, _)| t == name) {
            return Err(ProjectPathsError::UnknownTable(name.clone()));
        }
        let Some((table, keys)) = MANIFEST_TABLES.iter().find(|(t, _)| t == name) else {
            return Err(if value.is_table() {
                ProjectPathsError::UnknownTable(name.clone())
            } else {
                ProjectPathsError::TopLevelKey(name.clone())
            });
        };
        // #1770 review: a known table's name holding a plain value
        // (`paths = "src"`) would otherwise read as an absent table.
        let Some(entries) = value.as_table() else {
            return Err(ProjectPathsError::TopLevelKey(name.clone()));
        };
        if keys.is_empty() {
            continue;
        }
        for key in entries.keys() {
            if !keys.contains(&key.as_str()) {
                return Err(match *table {
                    "paths" => ProjectPathsError::UnknownKey(key.clone()),
                    _ => ProjectPathsError::UnknownTableKey {
                        table,
                        key: key.clone(),
                    },
                });
            }
        }
    }
    Ok(())
}

/// Read `bynk.toml`'s `[paths]` section, surfacing a malformed manifest — a
/// parse failure or an unrecognised `[paths]` key — as an error instead of
/// silently falling back to the conventional layout (the previous
/// `read_project_paths` total form's behaviour, R3.8 — deleted in favour of
/// this at all 18 of its callers, #1113).
///
/// R3.9 (#1113): `[paths] include` is no longer capped at one or two trees —
/// [`crate::roots::Roots::trees`] walks every entry, so this no longer rejects
/// a longer list.
pub fn try_read_project_paths(project_root: &Path) -> Result<ProjectPaths, ProjectPathsError> {
    let toml_path = project_root.join("bynk.toml");
    let overlay = match fs::read_to_string(&toml_path) {
        Ok(text) => HashMap::from([(toml_path, text)]),
        Err(_) => HashMap::new(),
    };
    try_read_project_paths_with(project_root, &overlay)
}

/// Like [`try_read_project_paths`], but honours `overlay` for `bynk.toml`
/// itself, the same way `discovery::read_source` does for every other file.
pub fn try_read_project_paths_with(
    project_root: &Path,
    overlay: &HashMap<PathBuf, String>,
) -> Result<ProjectPaths, ProjectPathsError> {
    let toml_path = project_root.join("bynk.toml");
    let Ok(content) = read_source(&toml_path, overlay) else {
        return Ok(ProjectPaths::conventional(project_root));
    };
    let Ok(doc) = content.parse::<toml::Table>() else {
        return Err(ProjectPathsError::Malformed);
    };
    let paths = doc.get("paths").and_then(|v| v.as_table());
    if let Some(t) = paths {
        for k in t.keys() {
            if k != "include" && k != "exclude" {
                return Err(ProjectPathsError::UnknownKey(k.clone()));
            }
        }
    }
    let list = |key: &str| -> Vec<PathBuf> {
        match paths.and_then(|t| t.get(key)) {
            Some(toml::Value::Array(items)) => items
                .iter()
                .filter_map(|v| v.as_str())
                .map(PathBuf::from)
                .collect(),
            Some(toml::Value::String(s)) => vec![PathBuf::from(s)],
            _ => Vec::new(),
        }
    };
    let mut include = list("include");
    let exclude = list("exclude");
    if include.is_empty() {
        include = ProjectPaths::conventional(project_root).include;
    }
    Ok(ProjectPaths { include, exclude })
}

pub fn commons_dir_for(name: &str) -> PathBuf {
    let parts: Vec<&str> = name.split('.').collect();
    let mut p = PathBuf::new();
    for part in parts {
        p.push(part);
    }
    p
}

pub fn ts_output_path(source: &Path) -> PathBuf {
    let mut out = source.to_path_buf();
    out.set_extension("ts");
    out
}

/// v0.8: directory name of a Worker for a given context, with dots replaced
/// by dashes (`commerce.payment` → `commerce-payment`).
pub fn worker_dir_name(context: &str) -> String {
    context.replace('.', "-")
}

/// v0.8: project-relative synthetic source path of the workers-mode
/// handlers file for a given context. Used so the emitter's relative-import
/// machinery resolves correctly against the workers layout.
pub fn worker_handlers_source_path(context: &str) -> PathBuf {
    PathBuf::from(format!(
        "workers/{}/handlers.bynk",
        worker_dir_name(context)
    ))
}

/// v0.8: project-relative output path of the workers-mode handlers file.
pub fn worker_handlers_output_path(context: &str) -> PathBuf {
    PathBuf::from(format!("workers/{}/handlers.ts", worker_dir_name(context)))
}

/// The src-stripped stem components of a path (`learner/uln.bynk` → `["learner",
/// "uln"]`), dropping the extension and any non-`Normal` components.
fn stem_parts(rel_path: &Path) -> Vec<String> {
    rel_path
        .with_extension("")
        .components()
        .filter_map(|c| match c {
            Component::Normal(s) => Some(s.to_string_lossy().to_string()),
            _ => None,
        })
        .collect()
}

/// v0.9.1: shared between source-unit and test-unit path validation. The
/// caller decides which root to strip from the file path before calling.
///
/// A file belongs to `qualified_name` when it is either the single file
/// `<name>.bynk` (`single_file_match`: stem parts == name parts) or one file of
/// the directory layout `<name>/*.bynk` (`multi_file_match`: parent-dir parts ==
/// name parts). These two branches are the single source of truth the v0.132
/// barrel trigger reads via [`is_multi_file_layout`].
pub fn unit_path_matches(rel_path: &Path, qualified_name: &str) -> bool {
    let name_parts: Vec<&str> = qualified_name.split('.').collect();
    let stem_parts = stem_parts(rel_path);
    let single_file_match = stem_parts.len() == name_parts.len()
        && stem_parts
            .iter()
            .zip(name_parts.iter())
            .all(|(a, b)| a == b);
    single_file_match || is_multi_file_parts(&stem_parts, &name_parts)
}

/// True when `stem_parts` is one file of the `<name>/*.bynk` directory layout —
/// the file's parent-directory parts equal the name parts.
fn is_multi_file_parts(stem_parts: &[String], name_parts: &[&str]) -> bool {
    if stem_parts.is_empty() {
        return false;
    }
    let parent_parts = &stem_parts[..stem_parts.len() - 1];
    parent_parts.len() == name_parts.len()
        && parent_parts
            .iter()
            .zip(name_parts.iter())
            .all(|(a, b)| a == b)
}

/// v0.132: does `rel_path` (src-stripped) place `qualified_name` under a
/// directory of that name — the `multi_file_match` branch of
/// [`unit_path_matches`]?
///
/// This is the layout where production emits `out/<name>/*.ts` per file and no
/// aggregate `out/<name>.ts`, so the test path's `import * as ns from
/// "./<name>.js"` dangles and needs an aggregating barrel. A single-file commons
/// (`<name>.bynk`) already owns `out/<name>.ts` and returns false, so a barrel
/// keyed on this predicate can never collide with it.
pub fn is_multi_file_layout(rel_path: &Path, qualified_name: &str) -> bool {
    let name_parts: Vec<&str> = qualified_name.split('.').collect();
    is_multi_file_parts(&stem_parts(rel_path), &name_parts)
}

/// #302: the qualified name a file moved from `old_rel` to `new_rel` should now
/// declare, preserving whichever [`unit_path_matches`] arrangement `old_rel`
/// used to satisfy against `old_name` — the dotted stem for a single-file
/// unit, or the dotted parent-directory for one file of a multi-file unit.
/// Returns `None` if `old_rel`/`old_name` don't actually satisfy either
/// arrangement (a pre-existing inconsistency the caller should not guess at).
///
/// `old_name` is matched as a **suffix** of `old_rel`'s stem/parent, not the
/// whole thing: the LSP's caller passes project-relative paths, which (unlike
/// the `source_path` `unit_path_matches` itself is checked against) still
/// carry a leading `include`-root segment (e.g. `src/`) that the qualified
/// name never mentions. Whatever prefix length that suffix match implies for
/// `old_rel` is applied unchanged to `new_rel` — correct as long as the file
/// stays under the same `include` root, which a rename/move normally does.
pub fn renamed_unit_name(old_rel: &Path, old_name: &str, new_rel: &Path) -> Option<String> {
    let name_parts: Vec<&str> = old_name.split('.').collect();
    let old_stem = stem_parts(old_rel);
    let new_stem = stem_parts(new_rel);

    let suffix_matches = |haystack: &[String]| {
        haystack.len() >= name_parts.len() && {
            let prefix_len = haystack.len() - name_parts.len();
            haystack[prefix_len..]
                .iter()
                .zip(name_parts.iter())
                .all(|(a, b)| a == b)
        }
    };

    if suffix_matches(&old_stem) {
        let prefix_len = old_stem.len() - name_parts.len();
        return (new_stem.len() >= prefix_len).then(|| new_stem[prefix_len..].join("."));
    }
    if !old_stem.is_empty() {
        let old_parent = &old_stem[..old_stem.len() - 1];
        if suffix_matches(old_parent) {
            let prefix_len = old_parent.len() - name_parts.len();
            if new_stem.is_empty() {
                return None;
            }
            let new_parent = &new_stem[..new_stem.len() - 1];
            return (new_parent.len() >= prefix_len).then(|| new_parent[prefix_len..].join("."));
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::{Path, PathBuf};

    // -- is_unpinned_range ----------------------------------------------------
    #[test]
    fn is_unpinned_range_true_for_wildcards_and_digitless() {
        assert!(is_unpinned_range(""));
        assert!(is_unpinned_range("*"));
        assert!(is_unpinned_range("x"));
        assert!(is_unpinned_range("X"));
        assert!(is_unpinned_range("latest"));
        assert!(is_unpinned_range("LATEST"));
        assert!(is_unpinned_range("  *  ")); // trimmed before the checks
        assert!(is_unpinned_range("workspace:*")); // no ascii digit
        assert!(is_unpinned_range("beta"));
    }

    #[test]
    fn is_unpinned_range_false_when_a_digit_is_present() {
        assert!(!is_unpinned_range("1.0.0"));
        assert!(!is_unpinned_range("^1.2"));
        assert!(!is_unpinned_range("~0.1"));
        assert!(!is_unpinned_range(">=2"));
        assert!(!is_unpinned_range("18"));
    }

    // -- read_project_paths_with (#57, in-memory test seam) -------------------
    #[test]
    fn read_project_paths_with_honours_a_virtual_bynk_toml() {
        let root = PathBuf::from("/nonexistent-bynk-test-root-57");
        let mut overlay = HashMap::new();
        overlay.insert(
            root.join("bynk.toml"),
            "[paths]\ninclude = [\"app\"]\nexclude = [\"vendor\"]\n".to_string(),
        );
        let paths = read_project_paths_with(&root, &overlay);
        assert_eq!(paths.include, vec![PathBuf::from("app")]);
        assert_eq!(paths.exclude, vec![PathBuf::from("vendor")]);
    }

    #[test]
    fn read_project_paths_with_falls_back_to_conventional_with_no_overlay_entry() {
        // No overlay entry and no real file at this (nonexistent) root — same
        // fallback a missing on-disk `bynk.toml` gives.
        let root = PathBuf::from("/nonexistent-bynk-test-root-57-empty");
        let paths = read_project_paths_with(&root, &HashMap::new());
        let conventional = ProjectPaths::conventional(&root);
        assert_eq!(paths.include, conventional.include);
        assert_eq!(paths.exclude, conventional.exclude);
    }

    /// R3.9 (#1113): three or more `[paths] include` entries all round-trip —
    /// `try_read_project_paths_with` no longer caps the list at two.
    #[test]
    fn read_project_paths_with_honours_three_or_more_include_entries() {
        let root = PathBuf::from("/nonexistent-bynk-test-root-1113-many-includes");
        let mut overlay = HashMap::new();
        overlay.insert(
            root.join("bynk.toml"),
            "[paths]\ninclude = [\"src\", \"tests\", \"examples\"]\n".to_string(),
        );
        let paths = try_read_project_paths_with(&root, &overlay).expect("must parse");
        assert_eq!(
            paths.include,
            vec![
                PathBuf::from("src"),
                PathBuf::from("tests"),
                PathBuf::from("examples"),
            ]
        );
    }

    // -- render_package_json --------------------------------------------------
    #[test]
    fn render_package_json_renders_sorted_dependencies() {
        let mut deps = std::collections::BTreeMap::new();
        deps.insert("zod".to_string(), "^3.22.4".to_string());
        deps.insert("hono".to_string(), "^4.0.0".to_string());
        let out = render_package_json(&deps);
        // BTreeMap ordering keeps the file byte-stable across builds.
        assert!(
            out.find("\"hono\"").unwrap() < out.find("\"zod\"").unwrap(),
            "dependencies render in sorted order:\n{out}"
        );
        assert!(out.contains("\"hono\": \"^4.0.0\""), "{out}");
    }

    /// A package name and version range reach here from adapter declarations in
    /// Bynk source, so they are arbitrary text. This module used to escape only
    /// `"` and `\`, which let a control character through as a literal — and a
    /// literal control character inside a JSON string is a parse error, so the
    /// emitted `package.json` was invalid rather than merely odd.
    #[test]
    fn render_package_json_escapes_the_control_range() {
        let mut deps = std::collections::BTreeMap::new();
        deps.insert("pkg\nname".to_string(), "^1.0\u{1}0".to_string());
        let out = render_package_json(&deps);
        assert!(out.contains("\"pkg\\nname\""), "{out}");
        assert!(out.contains("\"^1.0\\u00010\""), "{out}");
        // No raw control character survives into the rendered document (the
        // pretty-printer's own newlines are all that remain).
        assert!(
            !out.lines().any(|l| l.chars().any(|c| (c as u32) < 0x20)),
            "a raw control character reached the output:\n{out:?}"
        );
    }

    #[test]
    fn render_package_json_escapes_structural_characters() {
        let mut deps = std::collections::BTreeMap::new();
        deps.insert("a\"b".to_string(), "c\\d".to_string());
        let out = render_package_json(&deps);
        assert!(out.contains(r#""a\"b": "c\\d""#), "{out}");
    }

    // -- normalize_rel --------------------------------------------------------
    #[test]
    fn normalize_rel_resolves_dot_and_parent() {
        assert_eq!(
            normalize_rel(Path::new("./tokens.binding.ts")),
            PathBuf::from("tokens.binding.ts")
        );
        assert_eq!(normalize_rel(Path::new("a/./b")), PathBuf::from("a/b"));
        assert_eq!(normalize_rel(Path::new("a/../b")), PathBuf::from("b"));
        assert_eq!(normalize_rel(Path::new("a/b/../../c")), PathBuf::from("c"));
        assert_eq!(normalize_rel(Path::new("a/b")), PathBuf::from("a/b"));
    }

    #[test]
    fn normalize_rel_drops_root_and_pops_through_empty() {
        // RootDir / Prefix components are dropped.
        assert_eq!(normalize_rel(Path::new("/a/b")), PathBuf::from("a/b"));
        // A leading `..` pops an empty stack (a no-op), so it vanishes.
        assert_eq!(normalize_rel(Path::new("../a")), PathBuf::from("a"));
    }

    // -- commons_dir_for / ts_output_path -------------------------------------
    #[test]
    fn commons_dir_for_splits_dotted_name_into_dirs() {
        assert_eq!(commons_dir_for("a.b.c"), PathBuf::from("a/b/c"));
        assert_eq!(commons_dir_for("foo"), PathBuf::from("foo"));
    }

    #[test]
    fn ts_output_path_sets_ts_extension() {
        assert_eq!(
            ts_output_path(Path::new("foo.bynk")),
            PathBuf::from("foo.ts")
        );
        assert_eq!(
            ts_output_path(Path::new("a/b.bynk")),
            PathBuf::from("a/b.ts")
        );
        assert_eq!(ts_output_path(Path::new("foo")), PathBuf::from("foo.ts"));
    }

    // -- worker path helpers --------------------------------------------------
    #[test]
    fn worker_paths_dasherise_and_root_under_workers() {
        assert_eq!(worker_dir_name("commerce.payment"), "commerce-payment");
        assert_eq!(worker_dir_name("plain"), "plain");
        assert_eq!(
            worker_handlers_source_path("commerce.payment"),
            PathBuf::from("workers/commerce-payment/handlers.bynk")
        );
        assert_eq!(
            worker_handlers_output_path("commerce.payment"),
            PathBuf::from("workers/commerce-payment/handlers.ts")
        );
    }

    // -- unit_path_matches ----------------------------------------------------
    #[test]
    fn unit_path_matches_single_file_layout() {
        assert!(unit_path_matches(Path::new("a/b/c.bynk"), "a.b.c"));
        assert!(unit_path_matches(Path::new("foo.bynk"), "foo"));
    }

    #[test]
    fn unit_path_matches_multi_file_layout() {
        // `a/b/c/<any>.bynk` declaring `a.b.c` (the directory is the unit).
        assert!(unit_path_matches(Path::new("a/b/c/handlers.bynk"), "a.b.c"));
        assert!(unit_path_matches(Path::new("a/b/c/anything.bynk"), "a.b.c"));
    }

    #[test]
    fn unit_path_matches_rejects_misalignment() {
        assert!(!unit_path_matches(Path::new("a/b.bynk"), "a.b.c"));
        assert!(!unit_path_matches(Path::new("x/y/z.bynk"), "a.b.c"));
    }

    // -- is_multi_file_layout (v0.132 barrel trigger) -------------------------
    #[test]
    fn is_multi_file_layout_true_only_for_directory_layout() {
        // Directory layout: `<name>/*.bynk` — the branch with no `out/<name>.ts`.
        assert!(is_multi_file_layout(Path::new("thing/a.bynk"), "thing"));
        assert!(is_multi_file_layout(Path::new("thing/b.bynk"), "thing"));
        // Dotted commons split across `src/a/b/*.bynk`.
        assert!(is_multi_file_layout(Path::new("a/b/one.bynk"), "a.b"));
    }

    #[test]
    fn is_multi_file_layout_false_for_single_file_and_misalignment() {
        // Single file `<name>.bynk` already owns `out/<name>.ts` — no barrel.
        assert!(!is_multi_file_layout(Path::new("thing.bynk"), "thing"));
        // Dotted single file `a/b.bynk` for `a.b` — the file *is* `out/a/b.ts`.
        assert!(!is_multi_file_layout(Path::new("a/b.bynk"), "a.b"));
        // Wrong directory — not this unit's file.
        assert!(!is_multi_file_layout(Path::new("other/a.bynk"), "thing"));
    }

    // -- renamed_unit_name (#302) ----------------------------------------------
    #[test]
    fn renamed_unit_name_single_file() {
        assert_eq!(
            renamed_unit_name(
                Path::new("a/b/c.bynk"),
                "a.b.c",
                Path::new("a/b/renamed.bynk")
            ),
            Some("a.b.renamed".to_string())
        );
        assert_eq!(
            renamed_unit_name(Path::new("foo.bynk"), "foo", Path::new("bar.bynk")),
            Some("bar".to_string())
        );
    }

    #[test]
    fn renamed_unit_name_multi_file_member_rename_is_a_no_op() {
        // Renaming one member file within the same directory doesn't change
        // the unit's name — the qualified name is the directory, not the
        // filename.
        assert_eq!(
            renamed_unit_name(
                Path::new("a/b/c/old.bynk"),
                "a.b.c",
                Path::new("a/b/c/new.bynk")
            ),
            Some("a.b.c".to_string())
        );
    }

    #[test]
    fn renamed_unit_name_multi_file_directory_move() {
        assert_eq!(
            renamed_unit_name(
                Path::new("a/b/c/handlers.bynk"),
                "a.b.c",
                Path::new("a/b/renamed/handlers.bynk")
            ),
            Some("a.b.renamed".to_string())
        );
    }

    #[test]
    fn renamed_unit_name_none_on_preexisting_misalignment() {
        assert_eq!(
            renamed_unit_name(Path::new("x/y/z.bynk"), "a.b.c", Path::new("x/y/w.bynk")),
            None
        );
    }

    #[test]
    fn renamed_unit_name_tolerates_a_shared_include_root_prefix() {
        // The LSP passes project-relative paths (ADR 0198), which still carry
        // a split project's `src`/`tests` root segment — `unit_path_matches`
        // itself is only ever checked against the root-stripped `source_path`.
        // `old_name` must match as a *suffix*, and the same leading-segment
        // count is preserved onto `new_rel`.
        assert_eq!(
            renamed_unit_name(
                Path::new("src/billing/charge.bynk"),
                "billing.charge",
                Path::new("src/billing/pay.bynk")
            ),
            Some("billing.pay".to_string())
        );
        // Multi-file arrangement under the same prefix.
        assert_eq!(
            renamed_unit_name(
                Path::new("src/a/b/c/handlers.bynk"),
                "a.b.c",
                Path::new("src/a/b/renamed/handlers.bynk")
            ),
            Some("a.b.renamed".to_string())
        );
    }
}

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

    fn err(manifest: &str) -> String {
        check_manifest_str(manifest)
            .expect_err("the manifest is refused")
            .to_string()
    }

    /// #1665: every table the docs describe, with every key, is accepted.
    #[test]
    fn the_documented_tables_are_accepted() {
        check_manifest_str(
            "[project]\nname = \"p\"\nversion = \"0.1.0\"\n\n[paths]\ninclude = [\"src\"]\nexclude = []\n\n\
             [fmt]\nindent = \"tab\"\nmax_line_width = 100\n\n[lsp]\ndiagnostics_mode = \"live\"\n\
             diagnostics_debounce_ms = 300\n",
        )
        .expect("accepted");
        check_manifest_str("").expect("an empty manifest is accepted");
    }

    #[test]
    fn a_planned_table_names_its_issue() {
        assert!(err("[dependencies]\nx = \"1\"\n").contains("not yet supported (#843)"));
        assert!(err("[deploy]\ngroups = []\n").contains("not yet supported (#551)"));
        assert!(err("[workspace]\nmembers = []\n").contains("(#843)"));
    }

    #[test]
    fn a_typod_table_suggests_the_nearest() {
        assert!(err("[pahts]\n").contains("did you mean `[paths]`?"));
        assert!(err("[fnt]\n").contains("did you mean `[fmt]`?"));
        assert!(err("[zzzzzz]\n").contains("the tables are"));
    }

    #[test]
    fn an_unknown_key_in_project_or_lsp_suggests_the_nearest() {
        assert!(err("[project]\nnmae = \"p\"\n").contains("did you mean `name`?"));
        assert!(
            err("[lsp]\ndiagnostics_mod = \"live\"\n").contains("did you mean `diagnostics_mode`?")
        );
        assert!(err("[project]\nlicense = \"MIT\"\n").contains("its keys are `name`, `version`"));
    }

    #[test]
    fn a_paths_key_keeps_its_own_error() {
        assert!(matches!(
            check_manifest_str("[paths]\nout = \"out\"\n"),
            Err(ProjectPathsError::UnknownKey(k)) if k == "out"
        ));
    }

    #[test]
    fn a_top_level_key_points_at_its_table() {
        assert!(err("name = \"p\"\n").contains("did you mean it under `[project]`?"));
    }

    /// #1770 review: a known table's name holding a plain value is refused,
    /// not read as an absent table.
    #[test]
    fn a_table_name_with_a_plain_value_must_be_a_table() {
        assert!(err("paths = \"src\"\n").contains("must be a table — write it as `[paths]`"));
        assert!(err("lsp = 3\n").contains("`[lsp]`"));
        assert!(err("fmt = true\n").contains("`[fmt]`"));
        // An inline table is a table.
        check_manifest_str("paths = { include = [\"src\"] }\n").expect("accepted");
    }

    /// #1770 review: a planned table keeps its issue whatever shape it takes.
    #[test]
    fn a_planned_table_in_any_shape_names_its_issue() {
        assert!(err("[[dependencies]]\nname = \"acme\"\n").contains("(#843)"));
        assert!(err("dependencies = [\"acme\"]\n").contains("(#843)"));
    }

    /// `[fmt]` keys are `bynk-fmt`'s to check, not this one's.
    #[test]
    fn fmt_keys_are_left_to_the_formatter() {
        check_manifest_str("[fmt]\nanything = 1\n").expect("not this check's concern");
    }

    #[test]
    fn a_malformed_manifest_is_malformed() {
        assert!(matches!(
            check_manifest_str("[project\n"),
            Err(ProjectPathsError::Malformed)
        ));
    }
}