bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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

use super::*;

#[test]
fn test_DIST_001_graph_new() {
    let graph = InstallerGraph::new();
    assert!(graph.nodes.is_empty());
    assert!(graph.edges.is_empty());
}

#[test]
fn test_DIST_002_graph_add_node() {
    let mut graph = InstallerGraph::new();
    graph.add_node(GraphNode {
        id: "step-1".to_string(),
        name: "First Step".to_string(),
        estimated_duration_secs: 5.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    assert_eq!(graph.nodes.len(), 1);
    assert_eq!(graph.nodes[0].id, "step-1");
}

#[test]
fn test_DIST_003_graph_add_edge() {
    let mut graph = InstallerGraph::new();
    graph.add_node(GraphNode {
        id: "step-1".to_string(),
        name: "First".to_string(),
        estimated_duration_secs: 1.0,
        capabilities: vec![],
        exclusive_resource: None,
    });
    graph.add_node(GraphNode {
        id: "step-2".to_string(),
        name: "Second".to_string(),
        estimated_duration_secs: 2.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    graph.add_edge("step-1", "step-2").expect("Should add edge");
    assert_eq!(graph.edges.len(), 1);
}

#[test]
fn test_DIST_004_graph_add_edge_invalid() {
    let mut graph = InstallerGraph::new();
    graph.add_node(GraphNode {
        id: "step-1".to_string(),
        name: "First".to_string(),
        estimated_duration_secs: 1.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    let result = graph.add_edge("step-1", "nonexistent");
    assert!(result.is_err());
}

#[test]
fn test_DIST_005_compute_waves_empty() {
    let graph = InstallerGraph::new();
    let waves = graph.compute_waves();
    assert!(waves.is_empty());
}

#[test]
fn test_DIST_006_compute_waves_single() {
    let mut graph = InstallerGraph::new();
    graph.add_node(GraphNode {
        id: "step-1".to_string(),
        name: "Only Step".to_string(),
        estimated_duration_secs: 5.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    let waves = graph.compute_waves();
    assert_eq!(waves.len(), 1);
    assert_eq!(waves[0].step_ids, vec!["step-1"]);
}

#[test]
fn test_DIST_007_compute_waves_parallel() {
    let mut graph = InstallerGraph::new();

    // Two independent steps should be in same wave
    graph.add_node(GraphNode {
        id: "step-1".to_string(),
        name: "First".to_string(),
        estimated_duration_secs: 5.0,
        capabilities: vec![],
        exclusive_resource: None,
    });
    graph.add_node(GraphNode {
        id: "step-2".to_string(),
        name: "Second".to_string(),
        estimated_duration_secs: 3.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    let waves = graph.compute_waves();
    assert_eq!(waves.len(), 1);
    assert_eq!(waves[0].step_ids.len(), 2);
    assert!(!waves[0].is_sequential);
}

#[test]
fn test_DIST_008_compute_waves_sequential() {
    let mut graph = InstallerGraph::new();

    graph.add_node(GraphNode {
        id: "step-1".to_string(),
        name: "First".to_string(),
        estimated_duration_secs: 5.0,
        capabilities: vec![],
        exclusive_resource: None,
    });
    graph.add_node(GraphNode {
        id: "step-2".to_string(),
        name: "Second".to_string(),
        estimated_duration_secs: 3.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    // step-2 depends on step-1
    graph.add_edge("step-1", "step-2").unwrap();

    let waves = graph.compute_waves();
    assert_eq!(waves.len(), 2);
    assert_eq!(waves[0].step_ids, vec!["step-1"]);
    assert_eq!(waves[1].step_ids, vec!["step-2"]);
}

#[test]
fn test_DIST_009_compute_waves_exclusive_resource() {
    let mut graph = InstallerGraph::new();

    // Two steps with same exclusive resource should be sequential
    graph.add_node(GraphNode {
        id: "apt-1".to_string(),
        name: "Install A".to_string(),
        estimated_duration_secs: 10.0,
        capabilities: vec!["apt".to_string()],
        exclusive_resource: Some("apt-lock".to_string()),
    });
    graph.add_node(GraphNode {
        id: "apt-2".to_string(),
        name: "Install B".to_string(),
        estimated_duration_secs: 15.0,
        capabilities: vec!["apt".to_string()],
        exclusive_resource: Some("apt-lock".to_string()),
    });

    let waves = graph.compute_waves();

    // Should have 2 waves, each sequential
    assert_eq!(waves.len(), 2);
    assert!(waves[0].is_sequential);
    assert!(waves[1].is_sequential);
}

#[test]
fn test_DIST_010_compute_waves_mixed() {
    let mut graph = InstallerGraph::new();

    // Step 1: no deps
    graph.add_node(GraphNode {
        id: "check".to_string(),
        name: "Check OS".to_string(),
        estimated_duration_secs: 0.5,
        capabilities: vec![],
        exclusive_resource: None,
    });

    // Steps 2 & 3: both depend on step 1, can run in parallel
    graph.add_node(GraphNode {
        id: "download-a".to_string(),
        name: "Download A".to_string(),
        estimated_duration_secs: 5.0,
        capabilities: vec![],
        exclusive_resource: None,
    });
    graph.add_node(GraphNode {
        id: "download-b".to_string(),
        name: "Download B".to_string(),
        estimated_duration_secs: 3.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    graph.add_edge("check", "download-a").unwrap();
    graph.add_edge("check", "download-b").unwrap();

    // Step 4: depends on both downloads
    graph.add_node(GraphNode {
        id: "install".to_string(),
        name: "Install".to_string(),
        estimated_duration_secs: 30.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    graph.add_edge("download-a", "install").unwrap();
    graph.add_edge("download-b", "install").unwrap();

    let waves = graph.compute_waves();

    // Wave 0: check
    // Wave 1: download-a, download-b (parallel)
    // Wave 2: install
    assert_eq!(waves.len(), 3);
    assert_eq!(waves[0].step_ids, vec!["check"]);
    assert_eq!(waves[1].step_ids.len(), 2);
    assert!(waves[1].step_ids.contains(&"download-a".to_string()));
    assert!(waves[1].step_ids.contains(&"download-b".to_string()));
    assert_eq!(waves[2].step_ids, vec!["install"]);
}

#[test]
fn test_DIST_011_create_plan() {
    let mut graph = InstallerGraph::new();

    graph.add_node(GraphNode {
        id: "step-1".to_string(),
        name: "First".to_string(),
        estimated_duration_secs: 5.0,
        capabilities: vec![],
        exclusive_resource: None,
    });
    graph.add_node(GraphNode {
        id: "step-2".to_string(),
        name: "Second".to_string(),
        estimated_duration_secs: 3.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    let plan = graph.create_plan();

    assert!(!plan.waves.is_empty());
    assert!(plan.total_duration_sequential_secs > 0.0);
    assert!(plan.total_duration_parallel_secs > 0.0);
}

#[test]
fn test_DIST_012_to_mermaid() {
    let mut graph = InstallerGraph::new();

    graph.add_node(GraphNode {
        id: "step-1".to_string(),
        name: "First Step".to_string(),
        estimated_duration_secs: 1.0,
        capabilities: vec![],
        exclusive_resource: None,
    });
    graph.add_node(GraphNode {
        id: "step-2".to_string(),
        name: "Second Step".to_string(),
        estimated_duration_secs: 2.0,
        capabilities: vec![],
        exclusive_resource: None,
    });
    graph.add_edge("step-1", "step-2").unwrap();

    let mermaid = graph.to_mermaid();

    assert!(mermaid.starts_with("graph TD"));
    assert!(mermaid.contains("step-1"));
    assert!(mermaid.contains("step-2"));
    assert!(mermaid.contains("-->"));
}

#[test]
fn test_DIST_013_to_dot() {
    let mut graph = InstallerGraph::new();

    graph.add_node(GraphNode {
        id: "step-1".to_string(),
        name: "First".to_string(),
        estimated_duration_secs: 1.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    let dot = graph.to_dot();

    assert!(dot.starts_with("digraph"));
    assert!(dot.contains("step-1"));
}

#[test]
fn test_DIST_014_parse_duration_secs() {
    assert_eq!(parse_duration_secs("30s"), Some(30.0));
    assert_eq!(parse_duration_secs("5m"), Some(300.0));
    assert_eq!(parse_duration_secs("1h"), Some(3600.0));
    assert_eq!(parse_duration_secs("45"), Some(45.0));
    assert_eq!(parse_duration_secs("invalid"), None);
}

#[test]
fn test_DIST_015_sccache_client_new() {
    let client = SccacheClient::new("10.0.0.50:4226");
    assert_eq!(client.server(), "10.0.0.50:4226");
    assert!(!client.is_connected());
}

#[test]
fn test_DIST_016_sccache_client_connect() {
    let mut client = SccacheClient::new("10.0.0.50:4226");
    client.connect().expect("Should connect");
    assert!(client.is_connected());
}

#[test]
fn test_DIST_017_sccache_client_invalid() {
    let mut client = SccacheClient::new("invalid-address");
    let result = client.connect();
    assert!(result.is_err());
}

#[test]
fn test_DIST_018_cache_stats_hit_rate() {
    let stats = CacheStats {
        hits: 80,
        misses: 20,
        size_bytes: 1000,
    };
    assert!((stats.hit_rate() - 80.0).abs() < 0.01);
}

#[test]
fn test_DIST_019_cache_stats_hit_rate_zero() {
    let stats = CacheStats::default();
    assert_eq!(stats.hit_rate(), 0.0);
}

#[test]
fn test_DIST_020_distributed_config_default() {
    let config = DistributedConfig::default();
    assert!(!config.enabled);
    assert!(config.sccache_server.is_none());
    assert!(config.remote_executors.is_empty());
    assert_eq!(config.max_parallel_steps, 4);
}

#[test]
fn test_DIST_021_format_execution_plan() {
    let plan = ExecutionPlan {
        waves: vec![ExecutionWave {
            wave_number: 0,
            step_ids: vec!["step-1".to_string()],
            is_sequential: false,
            sequential_reason: None,
            estimated_duration_secs: 5.0,
        }],
        total_duration_parallel_secs: 5.0,
        total_duration_sequential_secs: 5.0,
        speedup_percent: 0.0,
    };

    let output = format_execution_plan(&plan, 4);
    assert!(output.contains("Execution Plan"));
    assert!(output.contains("Wave 1"));
    assert!(output.contains("step-1"));
}

#[test]
fn test_DIST_022_from_spec() {
    use crate::installer::spec::InstallerSpec;

    let toml = r#"
[installer]
name = "test"
version = "1.0.0"

[[step]]
id = "step-1"
name = "First Step"
action = "script"
depends_on = []

[step.script]
content = "echo hello"

[[step]]
id = "step-2"
name = "Second Step"
action = "script"
depends_on = ["step-1"]

[step.script]
content = "echo world"
"#;

    let spec = InstallerSpec::parse(toml).expect("Valid TOML");
    let graph = InstallerGraph::from_spec(&spec).expect("Should build graph");

    assert_eq!(graph.nodes.len(), 2);
    assert_eq!(graph.edges.len(), 1);
}

#[test]
fn test_DIST_023_speedup_calculation() {
    let mut graph = InstallerGraph::new();

    // Three parallel steps
    graph.add_node(GraphNode {
        id: "a".to_string(),
        name: "A".to_string(),
        estimated_duration_secs: 10.0,
        capabilities: vec![],
        exclusive_resource: None,
    });
    graph.add_node(GraphNode {
        id: "b".to_string(),
        name: "B".to_string(),
        estimated_duration_secs: 10.0,
        capabilities: vec![],
        exclusive_resource: None,
    });
    graph.add_node(GraphNode {
        id: "c".to_string(),
        name: "C".to_string(),
        estimated_duration_secs: 10.0,
        capabilities: vec![],
        exclusive_resource: None,
    });

    let plan = graph.create_plan();

    // Sequential: 30s, Parallel: 10s, Speedup: ~66%
    assert!((plan.total_duration_sequential_secs - 30.0).abs() < 0.01);
    assert!((plan.total_duration_parallel_secs - 10.0).abs() < 0.01);
    assert!(plan.speedup_percent > 60.0);
}