ark-cli 0.1.2

Architectural boundary enforcer for .NET solutions
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
use crate::parser::ProjectFile;
use crate::parser::csproj::ProjectRef;
use petgraph::algo::tarjan_scc;
use petgraph::graph::{DiGraph, NodeIndex};
use std::collections::HashMap;
use std::path::PathBuf;

#[derive(Debug)]
pub struct ScanResult {
    /// Tiers bottom-to-top: index 0 = leaves (most foundational)
    pub tiers: Vec<Vec<String>>,
    /// Projects with no incoming or outgoing refs in the solution
    pub isolated: Vec<String>,
    /// Names of projects filtered out as test/spec projects
    pub test_projects: Vec<String>,
    /// Groups of projects in mutual cycles (each inner Vec has >1 member)
    pub cycles: Vec<Vec<String>>,
}

#[derive(Debug, Clone)]
pub struct LayerDef {
    pub name: String,
    pub projects: Vec<String>,
}

#[derive(Debug)]
pub struct InterLayerEdge {
    pub from: String,
    pub to: String,
    pub ref_count: usize,
    /// True when a lower-indexed (more foundational) layer depends on a higher-indexed one
    pub unusual: bool,
}

pub fn is_test_project(name: &str) -> bool {
    [".Tests", ".Specs", ".IntegrationTests", ".UnitTests"]
        .iter()
        .any(|s| name.ends_with(s))
}

pub fn scan(projects: &[ProjectFile]) -> ScanResult {
    let (test_projects, non_test): (Vec<_>, Vec<_>) =
        projects.iter().partition(|p| is_test_project(&p.name));
    let test_names: Vec<String> = test_projects.iter().map(|p| p.name.clone()).collect();
    let non_test_set: std::collections::HashSet<String> =
        non_test.iter().map(|p| p.name.clone()).collect();

    // Build graph
    let mut graph: DiGraph<String, ()> = DiGraph::new();
    let mut name_to_idx: HashMap<String, NodeIndex> = HashMap::new();
    for p in &non_test {
        let idx = graph.add_node(p.name.clone());
        name_to_idx.insert(p.name.clone(), idx);
    }
    for p in &non_test {
        let from = name_to_idx[&p.name];
        for pref in &p.project_refs {
            let target = resolve_ref_name(pref);
            if non_test_set.contains(&target)
                && let Some(&to) = name_to_idx.get(&target)
            {
                graph.add_edge(from, to, ());
            }
        }
    }

    // Detect cycles via SCC
    let sccs = tarjan_scc(&graph);
    let cycles: Vec<Vec<String>> = sccs
        .iter()
        .filter(|scc| scc.len() > 1)
        .map(|scc| scc.iter().map(|&i| graph[i].clone()).collect())
        .collect();
    let in_cycle: std::collections::HashSet<String> =
        cycles.iter().flat_map(|g| g.iter().cloned()).collect();

    // Assign tiers iteratively (longest path from leaves)
    let mut tier_map: HashMap<String, usize> = HashMap::new();
    let mut changed = true;
    while changed {
        changed = false;
        for p in &non_test {
            if tier_map.contains_key(&p.name) || in_cycle.contains(&p.name) {
                continue;
            }
            let deps: Vec<String> = p
                .project_refs
                .iter()
                .map(resolve_ref_name)
                .filter(|n| non_test_set.contains(n) && !in_cycle.contains(n))
                .collect();
            if deps.iter().all(|d| tier_map.contains_key(d)) {
                let t = deps
                    .iter()
                    .filter_map(|d| tier_map.get(d))
                    .copied()
                    .max()
                    .map(|m| m + 1)
                    .unwrap_or(0);
                tier_map.insert(p.name.clone(), t);
                changed = true;
            }
        }
    }
    // Place cycle members at max-dep-tier + 1, or above all non-cycle tiers
    let max_non_cycle_tier = tier_map.values().copied().max().unwrap_or(0);
    for group in &cycles {
        let t = group
            .iter()
            .flat_map(|name| non_test.iter().find(|p| &p.name == name))
            .flat_map(|p| p.project_refs.iter().map(resolve_ref_name))
            .filter_map(|d| tier_map.get(&d).copied())
            .max()
            .map(|m| m + 1)
            .unwrap_or(max_non_cycle_tier + 1);
        for name in group {
            tier_map.insert(name.clone(), t);
        }
    }

    // Build tier buckets
    let max_tier = tier_map.values().copied().max().unwrap_or(0);
    let mut tiers: Vec<Vec<String>> = vec![vec![]; max_tier + 1];
    for (name, &t) in &tier_map {
        tiers[t].push(name.clone());
    }
    for bucket in &mut tiers {
        bucket.sort();
    }

    // Detect isolated (no in or out edges within non-test solution)
    let has_incoming: std::collections::HashSet<String> = non_test
        .iter()
        .flat_map(|p| p.project_refs.iter().map(resolve_ref_name))
        .filter(|n| non_test_set.contains(n))
        .collect();
    // A project is a candidate for isolation if it has no out-edges and no in-edges.
    let isolated_candidates: Vec<String> = non_test
        .iter()
        .filter(|p| {
            let out_count = p
                .project_refs
                .iter()
                .map(resolve_ref_name)
                .filter(|n| non_test_set.contains(n))
                .count();
            out_count == 0 && !has_incoming.contains(&p.name)
        })
        .map(|p| p.name.clone())
        .collect();
    // Only treat candidates as truly isolated when at least one other project has edges,
    // i.e. don't mark everything isolated in a fully-disconnected or single-project solution.
    let any_connected = non_test.iter().any(|p| {
        let out_count = p
            .project_refs
            .iter()
            .map(resolve_ref_name)
            .filter(|n| non_test_set.contains(n))
            .count();
        out_count > 0 || has_incoming.contains(&p.name)
    });
    let isolated: Vec<String> = if any_connected {
        isolated_candidates
    } else {
        vec![]
    };

    // Remove isolated projects from tier buckets — they're handled separately by the wizard.
    // Only remove if tier 0 would still have non-isolated members after the removal,
    // so that a project which is the sole non-cycle leaf is not accidentally hidden.
    if !isolated.is_empty() {
        let t0_remaining = tiers
            .first()
            .map(|t| t.iter().filter(|n| !isolated.contains(n)).count())
            .unwrap_or(0);
        if t0_remaining > 0 {
            if let Some(t0) = tiers.get_mut(0) {
                t0.retain(|n| !isolated.contains(n));
            }
            tiers.retain(|t| !t.is_empty());
        }
    }

    ScanResult {
        tiers,
        isolated,
        test_projects: test_names,
        cycles,
    }
}

fn resolve_ref_name(pref: &ProjectRef) -> String {
    pref.resolved
        .as_ref()
        .and_then(|r| r.file_stem())
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_else(|| {
            PathBuf::from(&pref.include)
                .file_stem()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_else(|| pref.include.clone())
        })
}

pub fn suggest_layer_name(projects: &[&str]) -> &'static str {
    let score = |hints: &[&str]| {
        projects
            .iter()
            .filter(|p| hints.iter().any(|h| p.ends_with(h)))
            .count()
    };
    [
        (score(&[".Domain", ".Core", ".Entities"]), "Domain"),
        (
            score(&[".Application", ".UseCases", ".Services"]),
            "Application",
        ),
        (
            score(&[".Infrastructure", ".Persistence", ".Adapters"]),
            "Infrastructure",
        ),
        (score(&[".Api", ".Web", ".Host"]), "Presentation"),
    ]
    .iter()
    .filter(|(s, _)| *s > 0)
    .max_by_key(|(s, _)| *s)
    .map(|(_, name)| *name)
    .unwrap_or("Layer")
}

pub fn compute_inter_layer_edges(
    layers: &[LayerDef],
    projects: &[ProjectFile],
) -> Vec<InterLayerEdge> {
    let project_to_layer: HashMap<String, usize> = layers
        .iter()
        .enumerate()
        .flat_map(|(i, l)| l.projects.iter().map(move |p| (p.clone(), i)))
        .collect();

    let mut counts: HashMap<(usize, usize), usize> = HashMap::new();
    for p in projects {
        let Some(&from_idx) = project_to_layer.get(&p.name) else {
            continue;
        };
        for pref in &p.project_refs {
            let target = resolve_ref_name(pref);
            let Some(&to_idx) = project_to_layer.get(&target) else {
                continue;
            };
            if from_idx != to_idx {
                *counts.entry((from_idx, to_idx)).or_insert(0) += 1;
            }
        }
    }

    let mut edges: Vec<InterLayerEdge> = counts
        .into_iter()
        .map(|((fi, ti), count)| InterLayerEdge {
            from: layers[fi].name.clone(),
            to: layers[ti].name.clone(),
            ref_count: count,
            unusual: fi < ti,
        })
        .collect();
    edges.sort_by(|a, b| a.from.cmp(&b.from).then(a.to.cmp(&b.to)));
    edges
}

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

    #[test]
    fn test_projects_detected() {
        assert!(is_test_project("MyApp.Tests"));
        assert!(is_test_project("MyApp.Domain.Tests"));
        assert!(is_test_project("MyApp.Specs"));
        assert!(is_test_project("MyApp.IntegrationTests"));
        assert!(is_test_project("MyApp.UnitTests"));
    }

    #[test]
    fn non_test_projects_not_detected() {
        assert!(!is_test_project("MyApp.Domain"));
        assert!(!is_test_project("MyApp.Api"));
        assert!(!is_test_project("MyApp.TestHelpers"));
    }

    use crate::parser::csproj::ProjectRef;

    fn proj(name: &str, deps: Vec<&str>) -> ProjectFile {
        ProjectFile {
            path: PathBuf::from(format!("{name}.csproj")),
            name: name.to_string(),
            project_refs: deps
                .into_iter()
                .map(|r| ProjectRef {
                    include: format!("..\\{r}\\{r}.csproj"),
                    include_span: (0, 0),
                    resolved: Some(PathBuf::from(format!("{r}.csproj"))),
                })
                .collect(),
            package_refs: vec![],
        }
    }

    #[test]
    fn leaf_is_tier_0() {
        let result = scan(&[proj("MyApp.Domain", vec![])]);
        assert_eq!(result.tiers.len(), 1);
        assert!(result.tiers[0].contains(&"MyApp.Domain".to_string()));
    }

    #[test]
    fn two_tier_chain() {
        let result = scan(&[
            proj("MyApp.Api", vec!["MyApp.Domain"]),
            proj("MyApp.Domain", vec![]),
        ]);
        assert_eq!(result.tiers.len(), 2);
        assert!(result.tiers[0].contains(&"MyApp.Domain".to_string()));
        assert!(result.tiers[1].contains(&"MyApp.Api".to_string()));
    }

    #[test]
    fn test_projects_removed_from_tiers() {
        let result = scan(&[
            proj("MyApp.Domain", vec![]),
            proj("MyApp.Tests", vec!["MyApp.Domain"]),
        ]);
        assert_eq!(result.test_projects, vec!["MyApp.Tests"]);
        assert_eq!(result.tiers.len(), 1);
    }

    #[test]
    fn isolated_project_flagged() {
        let result = scan(&[
            proj("MyApp.Api", vec!["MyApp.Domain"]),
            proj("MyApp.Domain", vec![]),
            proj("MyApp.BuildTools", vec![]),
        ]);
        assert!(result.isolated.contains(&"MyApp.BuildTools".to_string()));
        assert!(!result.isolated.contains(&"MyApp.Domain".to_string()));
    }

    #[test]
    fn isolated_projects_excluded_from_tiers() {
        let projects = vec![
            proj("MyApp.Api", vec!["MyApp.Domain"]),
            proj("MyApp.Domain", vec![]),
            proj("MyApp.BuildTools", vec![]), // isolated
        ];
        let result = scan(&projects);
        assert!(result.isolated.contains(&"MyApp.BuildTools".to_string()));
        // BuildTools must NOT appear in any tier
        assert!(
            !result
                .tiers
                .iter()
                .any(|t| t.contains(&"MyApp.BuildTools".to_string()))
        );
        // Domain and Api are still in tiers
        assert!(result.tiers[0].contains(&"MyApp.Domain".to_string()));
        assert!(result.tiers[1].contains(&"MyApp.Api".to_string()));
    }

    #[test]
    fn cycle_detected() {
        let result = scan(&[proj("A", vec!["B"]), proj("B", vec!["A"])]);
        assert_eq!(result.cycles.len(), 1);
        let cycle = &result.cycles[0];
        assert!(cycle.contains(&"A".to_string()));
        assert!(cycle.contains(&"B".to_string()));
    }

    #[test]
    fn pure_cycle_placed_above_non_cycle_projects() {
        // A ↔ B cycle with no external deps — should NOT land in tier 0
        let projects = vec![
            proj("MyApp.Domain", vec![]), // tier 0 (leaf)
            proj("A", vec!["B"]),         // cycle
            proj("B", vec!["A"]),         // cycle
        ];
        let result = scan(&projects);
        assert_eq!(result.cycles.len(), 1);
        // Domain should be at tier 0; A and B should be above it
        let domain_tier = result
            .tiers
            .iter()
            .position(|t| t.contains(&"MyApp.Domain".to_string()))
            .unwrap();
        let a_tier = result
            .tiers
            .iter()
            .position(|t| t.contains(&"A".to_string()))
            .unwrap();
        assert!(
            a_tier > domain_tier,
            "cycle members should be placed above genuine leaf projects"
        );
    }

    #[test]
    fn suggests_domain() {
        assert_eq!(
            suggest_layer_name(&["MyApp.Domain", "MyApp.Core"]),
            "Domain"
        );
    }

    #[test]
    fn suggests_presentation() {
        assert_eq!(suggest_layer_name(&["MyApp.Api"]), "Presentation");
    }

    #[test]
    fn fallback_layer() {
        assert_eq!(suggest_layer_name(&["MyApp.Weird"]), "Layer");
    }

    #[test]
    fn inter_layer_edges_counted() {
        let projects = vec![
            proj("MyApp.Api", vec!["MyApp.Domain"]),
            proj("MyApp.Domain", vec![]),
        ];
        let layers = vec![
            LayerDef {
                name: "Domain".into(),
                projects: vec!["MyApp.Domain".into()],
            },
            LayerDef {
                name: "Presentation".into(),
                projects: vec!["MyApp.Api".into()],
            },
        ];
        let edges = compute_inter_layer_edges(&layers, &projects);
        assert_eq!(edges.len(), 1);
        assert_eq!(edges[0].from, "Presentation");
        assert_eq!(edges[0].to, "Domain");
        assert_eq!(edges[0].ref_count, 1);
        assert!(!edges[0].unusual);
    }

    #[test]
    fn unusual_edge_flagged() {
        // Domain (idx 0) depends on Presentation (idx 1) — lower depends on higher = unusual
        let projects = vec![
            proj("MyApp.Domain", vec!["MyApp.Api"]),
            proj("MyApp.Api", vec![]),
        ];
        let layers = vec![
            LayerDef {
                name: "Domain".into(),
                projects: vec!["MyApp.Domain".into()],
            },
            LayerDef {
                name: "Presentation".into(),
                projects: vec!["MyApp.Api".into()],
            },
        ];
        let edges = compute_inter_layer_edges(&layers, &projects);
        assert_eq!(edges.len(), 1);
        assert!(edges[0].unusual);
    }

    #[test]
    fn same_layer_refs_excluded() {
        let projects = vec![
            proj("MyApp.Domain", vec!["MyApp.Core"]),
            proj("MyApp.Core", vec![]),
        ];
        let layers = vec![LayerDef {
            name: "Domain".into(),
            projects: vec!["MyApp.Domain".into(), "MyApp.Core".into()],
        }];
        assert!(compute_inter_layer_edges(&layers, &projects).is_empty());
    }
}