syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
use std::fs;
use std::path::Path;
use tempfile::TempDir;
use tokio;

use syncable_cli::analyzer::{
    dependency_parser::{DependencyParser, Language},
    runtime::{DetectionConfidence, JavaScriptRuntime, PackageManager, RuntimeDetector},
    tool_management::ToolDetector,
    vulnerability::VulnerabilityChecker,
};

/// Integration tests for end-to-end bun audit workflow
/// These tests simulate real project scenarios and test the complete pipeline

#[tokio::test]
async fn test_bun_project_detection_and_audit_workflow() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Create a simulated Bun project
    create_bun_project(project_path);

    // Test 1: Runtime Detection
    let runtime_detector = RuntimeDetector::new(project_path.to_path_buf());
    let detection_result = runtime_detector.detect_js_runtime_and_package_manager();

    assert_eq!(detection_result.package_manager, PackageManager::Bun);
    assert_eq!(detection_result.runtime, JavaScriptRuntime::Bun);

    // Test 2: Tool Detection
    let mut tool_detector = ToolDetector::new();
    let js_managers = tool_detector.detect_js_package_managers();

    assert!(js_managers.contains_key("bun"));
    assert!(js_managers.contains_key("npm"));
    assert!(js_managers.contains_key("yarn"));
    assert!(js_managers.contains_key("pnpm"));

    // Test 3: Dependency Parsing
    let parser = DependencyParser::new();
    let dependencies = parser.parse_all_dependencies(project_path).unwrap();

    assert!(dependencies.contains_key(&Language::JavaScript));
    let js_deps = &dependencies[&Language::JavaScript];
    assert!(!js_deps.is_empty());

    // Verify we have the expected dependencies
    assert!(js_deps.iter().any(|d| d.name == "express"));
    assert!(js_deps.iter().any(|d| d.name == "lodash"));

    // Test 4: Vulnerability Checking (will use mock data since we can't guarantee bun is installed)
    let checker = VulnerabilityChecker::new();
    let report = checker
        .check_all_dependencies(&dependencies, project_path)
        .await;

    // Should complete without error (may find 0 vulnerabilities if tools aren't installed)
    assert!(report.is_ok());
    let vulnerability_report = report.unwrap();

    // Verify report structure exists (counts are usize and always >= 0)
    // No assertions needed - the fact that we got a report is sufficient
}

#[tokio::test]
async fn test_npm_project_detection_and_audit_workflow() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Create a simulated npm project
    create_npm_project(project_path);

    // Test runtime detection
    let runtime_detector = RuntimeDetector::new(project_path.to_path_buf());
    let detection_result = runtime_detector.detect_js_runtime_and_package_manager();

    assert_eq!(detection_result.package_manager, PackageManager::Npm);
    assert_eq!(detection_result.runtime, JavaScriptRuntime::Node);

    // Test dependency parsing
    let parser = DependencyParser::new();
    let dependencies = parser.parse_all_dependencies(project_path).unwrap();

    assert!(dependencies.contains_key(&Language::JavaScript));
    let js_deps = &dependencies[&Language::JavaScript];
    assert!(!js_deps.is_empty());

    // Test vulnerability checking
    let checker = VulnerabilityChecker::new();
    let report = checker
        .check_all_dependencies(&dependencies, project_path)
        .await;
    assert!(report.is_ok());
}

#[tokio::test]
async fn test_yarn_project_detection_and_audit_workflow() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Create a simulated yarn project
    create_yarn_project(project_path);

    // Test runtime detection
    let runtime_detector = RuntimeDetector::new(project_path.to_path_buf());
    let detection_result = runtime_detector.detect_js_runtime_and_package_manager();

    assert_eq!(detection_result.package_manager, PackageManager::Yarn);
    assert_eq!(detection_result.runtime, JavaScriptRuntime::Node);

    // Test the complete workflow
    let parser = DependencyParser::new();
    let dependencies = parser.parse_all_dependencies(project_path).unwrap();
    let checker = VulnerabilityChecker::new();
    let report = checker
        .check_all_dependencies(&dependencies, project_path)
        .await;
    assert!(report.is_ok());
}

#[tokio::test]
async fn test_pnpm_project_detection_and_audit_workflow() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Create a simulated pnpm project
    create_pnpm_project(project_path);

    // Test runtime detection
    let runtime_detector = RuntimeDetector::new(project_path.to_path_buf());
    let detection_result = runtime_detector.detect_js_runtime_and_package_manager();

    assert_eq!(detection_result.package_manager, PackageManager::Pnpm);
    assert_eq!(detection_result.runtime, JavaScriptRuntime::Node);

    // Test the complete workflow
    let parser = DependencyParser::new();
    let dependencies = parser.parse_all_dependencies(project_path).unwrap();
    let checker = VulnerabilityChecker::new();
    let report = checker
        .check_all_dependencies(&dependencies, project_path)
        .await;
    assert!(report.is_ok());
}

#[tokio::test]
async fn test_multi_runtime_project_priority() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Create a project with multiple lockfiles (Bun should have priority)
    create_multi_runtime_project(project_path);

    let runtime_detector = RuntimeDetector::new(project_path.to_path_buf());
    let detection_result = runtime_detector.detect_js_runtime_and_package_manager();

    // Bun should be detected as primary despite other lockfiles present
    assert_eq!(detection_result.package_manager, PackageManager::Bun);
    assert_eq!(detection_result.runtime, JavaScriptRuntime::Bun);

    // Test that vulnerability checking uses the detected runtime
    let parser = DependencyParser::new();
    let dependencies = parser.parse_all_dependencies(project_path).unwrap();
    let checker = VulnerabilityChecker::new();
    let report = checker
        .check_all_dependencies(&dependencies, project_path)
        .await;
    assert!(report.is_ok());
}

#[tokio::test]
#[ignore] // Requires external tools (npm audit, pip-audit, cargo audit, go) to be installed
async fn test_vulnerability_checking_with_mixed_languages() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Create a project with multiple languages
    create_polyglot_project(project_path);

    let parser = DependencyParser::new();
    let dependencies = parser.parse_all_dependencies(project_path).unwrap();

    // Should detect multiple languages
    assert!(dependencies.contains_key(&Language::JavaScript));
    assert!(dependencies.contains_key(&Language::Python));
    assert!(dependencies.contains_key(&Language::Rust));

    // Test vulnerability checking across all languages
    let checker = VulnerabilityChecker::new();
    let report = checker
        .check_all_dependencies(&dependencies, project_path)
        .await;
    assert!(report.is_ok());

    let vulnerability_report = report.unwrap();

    // Should handle mixed language vulnerabilities (counts are usize and always >= 0)
    // No assertion needed - the fact that we got a report is sufficient
}

#[test]
fn test_tool_detection_comprehensive() {
    let mut tool_detector = ToolDetector::new();

    // Test detection of all JavaScript package managers
    let js_tools = tool_detector.detect_js_package_managers();

    // Should attempt to detect all package managers
    assert_eq!(js_tools.len(), 4);
    assert!(js_tools.contains_key("bun"));
    assert!(js_tools.contains_key("npm"));
    assert!(js_tools.contains_key("yarn"));
    assert!(js_tools.contains_key("pnpm"));

    // Test bun-specific detection
    let bun_status = tool_detector.detect_bun();
    assert!(bun_status.last_checked.elapsed().unwrap().as_secs() < 5);

    // Test caching behavior
    let bun_status_cached = tool_detector.detect_bun();
    assert_eq!(bun_status.last_checked, bun_status_cached.last_checked);

    // Test cache clearing
    tool_detector.clear_cache();
    let bun_status_fresh = tool_detector.detect_bun();
    assert!(bun_status_fresh.last_checked >= bun_status.last_checked);
}

#[test]
fn test_runtime_detection_edge_cases() {
    let temp_dir = TempDir::new().unwrap();
    let project_path = temp_dir.path();

    // Test empty project
    let runtime_detector = RuntimeDetector::new(project_path.to_path_buf());
    let detection_result = runtime_detector.detect_js_runtime_and_package_manager();
    assert_eq!(detection_result.package_manager, PackageManager::Unknown);

    // Test project with only package.json but no specific indicators
    fs::write(
        project_path.join("package.json"),
        r#"{"name": "test", "version": "1.0.0"}"#,
    )
    .unwrap();

    // Create NEW detector after creating package.json
    let runtime_detector_with_pkg = RuntimeDetector::new(project_path.to_path_buf());
    let detection_result = runtime_detector_with_pkg.detect_js_runtime_and_package_manager();

    // Should default to npm when package.json exists but no specific indicators
    assert_eq!(detection_result.package_manager, PackageManager::Npm); // Default fallback
    assert_eq!(detection_result.runtime, JavaScriptRuntime::Node);
    assert_eq!(detection_result.confidence, DetectionConfidence::Low);

    // Test project with explicit packageManager field
    fs::write(
        project_path.join("package.json"),
        r#"{"name": "test", "version": "1.0.0", "packageManager": "bun@1.0.0"}"#,
    )
    .unwrap();

    let detection_result = runtime_detector_with_pkg.detect_js_runtime_and_package_manager();
    assert_eq!(detection_result.package_manager, PackageManager::Bun);
}

// Helper functions to create test projects

fn create_bun_project(project_path: &Path) {
    // Create package.json with bun-specific configuration
    fs::write(
        project_path.join("package.json"),
        r#"{
  "name": "test-bun-project",
  "version": "1.0.0",
  "packageManager": "bun@1.0.0",
  "engines": {
    "bun": ">=1.0.0"
  },
  "scripts": {
    "start": "bun run index.js",
    "dev": "bun --watch index.js"
  },
  "dependencies": {
    "express": "^4.18.0",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "@types/node": "^18.0.0",
    "bun-types": "^1.0.0"
  }
}"#,
    )
    .unwrap();

    // Create bun.lockb (simulated)
    fs::write(
        project_path.join("bun.lockb"),
        "Binary lockfile content (simulated)",
    )
    .unwrap();

    // Create bunfig.toml
    fs::write(
        project_path.join("bunfig.toml"),
        r#"[install]
cache = true

[install.scopes]
"@myorg" = { token = "$NPM_TOKEN", url = "https://registry.npmjs.org/" }
"#,
    )
    .unwrap();
}

fn create_npm_project(project_path: &Path) {
    fs::write(
        project_path.join("package.json"),
        r#"{
  "name": "test-npm-project",
  "version": "1.0.0",
  "dependencies": {
    "react": "^18.0.0",
    "axios": "^1.0.0"
  },
  "devDependencies": {
    "jest": "^29.0.0"
  }
}"#,
    )
    .unwrap();

    fs::write(
        project_path.join("package-lock.json"),
        r#"{
  "name": "test-npm-project",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {}
}"#,
    )
    .unwrap();
}

fn create_yarn_project(project_path: &Path) {
    fs::write(
        project_path.join("package.json"),
        r#"{
  "name": "test-yarn-project",
  "version": "1.0.0",
  "packageManager": "yarn@3.6.0",
  "dependencies": {
    "vue": "^3.0.0",
    "vuex": "^4.0.0"
  }
}"#,
    )
    .unwrap();

    fs::write(
        project_path.join("yarn.lock"),
        r#"# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1

vue@^3.0.0:
  version "3.3.4"
  resolved "https://registry.yarnpkg.com/vue/-/vue-3.3.4.tgz"
"#,
    )
    .unwrap();
}

fn create_pnpm_project(project_path: &Path) {
    fs::write(
        project_path.join("package.json"),
        r#"{
  "name": "test-pnpm-project",
  "version": "1.0.0",
  "packageManager": "pnpm@8.0.0",
  "dependencies": {
    "svelte": "^4.0.0"
  }
}"#,
    )
    .unwrap();

    fs::write(
        project_path.join("pnpm-lock.yaml"),
        r#"lockfileVersion: '6.0'

settings:
  autoInstallPeers: true
  excludeLinksFromLockfile: false

dependencies:
  svelte:
    specifier: ^4.0.0
    version: 4.2.0
"#,
    )
    .unwrap();
}

fn create_multi_runtime_project(project_path: &Path) {
    // Create package.json with explicit bun preference
    fs::write(
        project_path.join("package.json"),
        r#"{
  "name": "test-multi-runtime",
  "version": "1.0.0",
  "packageManager": "bun@1.0.0",
  "engines": {
    "bun": ">=1.0.0",
    "node": ">=18.0.0"
  },
  "dependencies": {
    "fastify": "^4.0.0"
  }
}"#,
    )
    .unwrap();

    // Create all lockfiles to test priority
    fs::write(project_path.join("bun.lockb"), "bun lockfile").unwrap();
    fs::write(project_path.join("yarn.lock"), "yarn lockfile").unwrap();
    fs::write(project_path.join("pnpm-lock.yaml"), "pnpm lockfile").unwrap();
    fs::write(project_path.join("package-lock.json"), "{}").unwrap();
}

fn create_polyglot_project(project_path: &Path) {
    // JavaScript/Node.js
    fs::write(
        project_path.join("package.json"),
        r#"{
  "name": "polyglot-project",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.18.0"
  }
}"#,
    )
    .unwrap();

    // Python
    fs::write(
        project_path.join("requirements.txt"),
        "flask==2.3.0\nrequests==2.31.0\n",
    )
    .unwrap();

    // Rust
    fs::write(
        project_path.join("Cargo.toml"),
        r#"[package]
name = "polyglot-project"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = "1.0"
tokio = "1.0"
"#,
    )
    .unwrap();

    // Go
    fs::write(
        project_path.join("go.mod"),
        r#"module polyglot-project

go 1.19

require (
    github.com/gin-gonic/gin v1.9.0
    github.com/gorilla/mux v1.8.0
)
"#,
    )
    .unwrap();
}