forjar 1.4.2

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
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
//! FJ-3400/2602/2105/3405: Plugin types, behavior specs, distribution, shell providers.
//! Usage: cargo test --test falsification_plugin_behavior_dist

use forjar::core::types::*;

// ── FJ-3400: PluginManifest ──

fn sample_manifest() -> PluginManifest {
    PluginManifest {
        name: "k8s-deployment".into(),
        version: "0.1.0".into(),
        description: Some("Manage K8s Deployments".into()),
        abi_version: PLUGIN_ABI_VERSION,
        wasm: "k8s-deployment.wasm".into(),
        blake3: "placeholder".into(),
        permissions: PluginPermissions::default(),
        schema: None,
    }
}

#[test]
fn manifest_resource_type() {
    assert_eq!(sample_manifest().resource_type(), "plugin:k8s-deployment");
}

#[test]
fn manifest_abi_compatible() {
    assert!(sample_manifest().is_abi_compatible());
}

#[test]
fn manifest_abi_incompatible() {
    let mut m = sample_manifest();
    m.abi_version = 99;
    assert!(!m.is_abi_compatible());
}

#[test]
fn manifest_verify_hash_correct() {
    let data = b"fake wasm module bytes";
    let hash = blake3::hash(data).to_hex().to_string();
    let mut m = sample_manifest();
    m.blake3 = hash;
    assert!(m.verify_hash(data));
}

#[test]
fn manifest_verify_hash_tampered() {
    let mut m = sample_manifest();
    m.blake3 = blake3::hash(b"original").to_hex().to_string();
    assert!(!m.verify_hash(b"tampered"));
}

#[test]
fn manifest_display() {
    let m = sample_manifest();
    let display = format!("{m}");
    assert!(display.contains("k8s-deployment"));
    assert!(display.contains("0.1.0"));
    assert!(display.contains("ABI v"));
}

#[test]
fn manifest_serde_roundtrip() {
    let yaml = r#"
name: test-plugin
version: "1.0.0"
abi_version: 1
wasm: test.wasm
blake3: "abc123"
permissions:
  fs:
    read: ["/etc/config"]
  exec:
    allow: ["kubectl"]
  env:
    read: ["KUBECONFIG"]
"#;
    let m: PluginManifest = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(m.name, "test-plugin");
    assert_eq!(m.permissions.fs.read, vec!["/etc/config"]);
    assert_eq!(m.permissions.exec.allow, vec!["kubectl"]);
    assert_eq!(m.permissions.env.read, vec!["KUBECONFIG"]);
}

// ── FJ-3400: PluginPermissions ──

#[test]
fn permissions_empty_default() {
    assert!(PluginPermissions::default().is_empty());
}

#[test]
fn permissions_not_empty_fs() {
    let mut p = PluginPermissions::default();
    p.fs.read.push("/etc".into());
    assert!(!p.is_empty());
}

#[test]
fn permissions_not_empty_net() {
    let mut p = PluginPermissions::default();
    p.net.connect.push("api.example.com:443".into());
    assert!(!p.is_empty());
}

#[test]
fn permissions_not_empty_exec() {
    let mut p = PluginPermissions::default();
    p.exec.allow.push("curl".into());
    assert!(!p.is_empty());
}

// ── FJ-3408: PluginSchema validation ──

#[test]
fn schema_validate_required_missing() {
    let schema = PluginSchema {
        required: vec!["name".into(), "image".into()],
        properties: indexmap::IndexMap::new(),
    };
    let mut props = indexmap::IndexMap::new();
    props.insert("name".into(), serde_yaml_ng::Value::String("app".into()));
    let errors = schema.validate(&props);
    assert_eq!(errors.len(), 1);
    assert!(errors[0].contains("image"));
}

#[test]
fn schema_validate_type_mismatch() {
    let mut properties = indexmap::IndexMap::new();
    properties.insert(
        "replicas".into(),
        SchemaProperty {
            prop_type: Some("integer".into()),
            default: None,
            items: None,
        },
    );
    let schema = PluginSchema {
        required: vec![],
        properties,
    };
    let mut props = indexmap::IndexMap::new();
    props.insert(
        "replicas".into(),
        serde_yaml_ng::Value::String("three".into()),
    );
    let errors = schema.validate(&props);
    assert_eq!(errors.len(), 1);
    assert!(errors[0].contains("expected type 'integer'"));
}

#[test]
fn schema_validate_all_pass() {
    let schema = PluginSchema {
        required: vec!["name".into()],
        properties: indexmap::IndexMap::new(),
    };
    let mut props = indexmap::IndexMap::new();
    props.insert("name".into(), serde_yaml_ng::Value::String("ok".into()));
    assert!(schema.validate(&props).is_empty());
}

// ── FJ-3400: PluginStatus ──

#[test]
fn plugin_status_display() {
    assert_eq!(PluginStatus::Converged.to_string(), "converged");
    assert_eq!(PluginStatus::Drifted.to_string(), "drifted");
    assert_eq!(PluginStatus::Missing.to_string(), "missing");
    assert_eq!(PluginStatus::Error.to_string(), "error");
}

#[test]
fn plugin_status_serde() {
    for status in [
        PluginStatus::Converged,
        PluginStatus::Drifted,
        PluginStatus::Missing,
        PluginStatus::Error,
    ] {
        let json = serde_json::to_string(&status).unwrap();
        let parsed: PluginStatus = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, status);
    }
}

// ── FJ-3400: PluginApplyOutcome ──

#[test]
fn plugin_apply_outcome_serde() {
    let outcome = PluginApplyOutcome {
        success: true,
        status: PluginStatus::Converged,
        changes: vec!["created deployment".into()],
        error: None,
    };
    let json = serde_json::to_string(&outcome).unwrap();
    let parsed: PluginApplyOutcome = serde_json::from_str(&json).unwrap();
    assert!(parsed.success);
    assert_eq!(parsed.status, PluginStatus::Converged);
    assert_eq!(parsed.changes.len(), 1);
}

// ── FJ-2602: BehaviorSpec ──

fn entry(name: &str, resource: Option<&str>, btype: Option<&str>, verify: bool) -> BehaviorEntry {
    BehaviorEntry {
        name: name.into(),
        resource: resource.map(Into::into),
        behavior_type: btype.map(Into::into),
        assert_state: if verify { Some("present".into()) } else { None },
        verify: if verify {
            Some(VerifyCommand {
                command: "dpkg -l nginx".into(),
                exit_code: Some(0),
                ..Default::default()
            })
        } else {
            None
        },
        convergence: if btype == Some("convergence") {
            Some(ConvergenceAssert {
                second_apply: Some("noop".into()),
                state_unchanged: Some(true),
            })
        } else {
            None
        },
    }
}

fn sample_behavior_spec() -> BehaviorSpec {
    BehaviorSpec {
        name: "nginx test".into(),
        config: "examples/nginx.yaml".into(),
        machine: Some("web-1".into()),
        behaviors: vec![
            entry("nginx installed", Some("nginx-pkg"), None, true),
            entry("idempotency", None, Some("convergence"), false),
            entry("service running", Some("nginx-svc"), None, false),
        ],
    }
}

#[test]
fn behavior_spec_count() {
    assert_eq!(sample_behavior_spec().behavior_count(), 3);
}

#[test]
fn behavior_spec_referenced_resources() {
    let spec = sample_behavior_spec();
    let refs = spec.referenced_resources();
    assert_eq!(refs, vec!["nginx-pkg", "nginx-svc"]);
}

#[test]
fn behavior_spec_serde_roundtrip() {
    let yaml = r#"
name: nginx test
config: examples/nginx.yaml
machine: web-1
behaviors:
  - name: nginx installed
    resource: nginx-pkg
    state: present
    verify:
      command: "dpkg -l nginx"
      exit_code: 0
  - name: idempotency
    type: convergence
    convergence:
      second_apply: noop
      state_unchanged: true
"#;
    let spec: BehaviorSpec = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(spec.name, "nginx test");
    assert_eq!(spec.behavior_count(), 2);
    assert!(spec.behaviors[1].is_convergence());
}

// ── FJ-2602: BehaviorEntry ──

#[test]
fn behavior_entry_is_convergence() {
    let entry = &sample_behavior_spec().behaviors[1];
    assert!(entry.is_convergence());
    assert!(!entry.has_verify());
}

#[test]
fn behavior_entry_has_verify() {
    let entry = &sample_behavior_spec().behaviors[0];
    assert!(entry.has_verify());
    assert!(!entry.is_convergence());
}

#[test]
fn behavior_entry_neither() {
    let entry = &sample_behavior_spec().behaviors[2];
    assert!(!entry.is_convergence());
    assert!(!entry.has_verify());
}

// ── FJ-2602: BehaviorReport ──

fn br(name: &str, passed: bool, failure: Option<&str>) -> BehaviorResult {
    BehaviorResult {
        name: name.into(),
        passed,
        failure: failure.map(Into::into),
        actual_exit_code: None,
        actual_stdout: None,
        duration_ms: 50,
    }
}

#[test]
fn behavior_report_all_passed() {
    let report = BehaviorReport::from_results(
        "nginx".into(),
        vec![br("a", true, None), br("b", true, None)],
    );
    assert!(report.all_passed());
    assert_eq!(report.total, 2);
    assert_eq!(report.passed, 2);
}

#[test]
fn behavior_report_with_failures() {
    let report = BehaviorReport::from_results(
        "nginx".into(),
        vec![br("ok", true, None), br("fail", false, Some("exit 1"))],
    );
    assert!(!report.all_passed());
    assert_eq!(report.passed, 1);
    assert_eq!(report.failed, 1);
}

#[test]
fn behavior_report_format_summary() {
    let report = BehaviorReport::from_results(
        "test".into(),
        vec![
            br("pkg ok", true, None),
            br("svc fail", false, Some("not active")),
        ],
    );
    let summary = report.format_summary();
    assert!(summary.contains("[PASS] pkg ok"));
    assert!(summary.contains("[FAIL] svc fail"));
    assert!(summary.contains("not active"));
    assert!(summary.contains("1/2 passed"));
}

#[test]
fn behavior_report_empty() {
    let report = BehaviorReport::from_results("empty".into(), vec![]);
    assert!(report.all_passed());
    assert_eq!(report.total, 0);
}

#[test]
fn behavior_report_display() {
    let report = BehaviorReport::from_results("spec".into(), vec![]);
    let display = format!("{report}");
    assert!(display.contains("Behavior Spec: spec"));
}

// ── FJ-2602: VerifyCommand / ConvergenceAssert ──

#[test]
fn verify_command_defaults() {
    let vc = VerifyCommand::default();
    assert!(vc.command.is_empty());
    assert!(vc.exit_code.is_none());
    assert!(vc.stdout.is_none());
    assert!(vc.port_open.is_none());
    assert!(vc.retries.is_none());
}

#[test]
fn convergence_assert_defaults() {
    let ca = ConvergenceAssert::default();
    assert!(ca.second_apply.is_none());
    assert!(ca.state_unchanged.is_none());
}

// ── FJ-2105: DistTarget ──

#[test]
fn dist_target_load() {
    let t = DistTarget::Load {
        runtime: "docker".into(),
    };
    assert_eq!(t.description(), "docker load");
}

#[test]
fn dist_target_push() {
    let t = DistTarget::Push {
        registry: "ghcr.io".into(),
        name: "myorg/myapp".into(),
        tag: "v1".into(),
    };
    assert_eq!(t.description(), "ghcr.io/myorg/myapp:v1");
}

#[test]
fn dist_target_far() {
    let t = DistTarget::Far {
        output_path: "/tmp/out.far".into(),
    };
    assert_eq!(t.description(), "FAR → /tmp/out.far");
}

#[test]
fn dist_target_serde() {
    let t = DistTarget::Push {
        registry: "r".into(),
        name: "n".into(),
        tag: "t".into(),
    };
    let json = serde_json::to_string(&t).unwrap();
    let parsed: DistTarget = serde_json::from_str(&json).unwrap();
    assert!(matches!(parsed, DistTarget::Push { .. }));
}

// ── FJ-2105: ArchBuild ──

#[test]
fn arch_build_amd64() {
    let a = ArchBuild::linux_amd64();
    assert_eq!(a.platform, "linux/amd64");
    assert_eq!(a.os, "linux");
    assert_eq!(a.architecture, "amd64");
    assert!(a.variant.is_none());
}

#[test]
fn arch_build_arm64() {
    let a = ArchBuild::linux_arm64();
    assert_eq!(a.platform, "linux/arm64");
    assert_eq!(a.architecture, "arm64");
    assert_eq!(a.variant.as_deref(), Some("v8"));
}

// ── FJ-2105: BuildReport ──

fn layer(idx: u32, name: &str, cached: bool) -> LayerReport {
    LayerReport {
        index: idx,
        name: name.into(),
        store_hash: format!("blake3:{name}"),
        size: 25_000_000,
        cached,
        duration_secs: if cached { 0.2 } else { 48.3 },
    }
}

fn sample_build_report() -> BuildReport {
    BuildReport {
        image_ref: "myregistry.io/app:1.0".into(),
        digest: "sha256:abc123".into(),
        total_size: 50_000_000,
        layer_count: 2,
        duration_secs: 48.5,
        layers: vec![layer(0, "base", true), layer(1, "app", false)],
        distribution: vec![],
        architectures: vec![],
    }
}

#[test]
fn build_report_size_mb() {
    let report = sample_build_report();
    assert!((report.size_mb() - 47.7).abs() < 0.1);
}

#[test]
fn build_report_format_summary() {
    let s = sample_build_report().format_summary();
    assert!(s.contains("myregistry.io/app:1.0"));
    assert!(s.contains("2 layers"));
    assert!(s.contains("cached"));
    assert!(s.contains("new"));
}