greentic-runner 1.2.2

Greentic runner binaries and re-exported host/new-runner APIs
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
//! Unit tests for the stock CLI wiring in [`super`]. The subprocess tests under
//! `tests/` exercise the same code through the real binary; these run in-process
//! so the argument handling, lock-file parsing and describe canonicalisation are
//! covered without spawning one.

use super::*;
use greentic_runner_host::cache::{CpuPolicy, EngineProfile};
use serde_json::json;
use tempfile::TempDir;

#[test]
fn warmup_key_is_the_sha256_of_the_bytes_not_a_lock_claim() {
    let bytes = b"\0asm\x01\0\0\0";
    let key = warmup_artifact_key("profile", bytes);
    assert_eq!(key.engine_profile_id, "profile");
    assert_eq!(key.wasm_digest, sha256_prefixed(bytes));
    // Different bytes can never share a key, whatever a lock says about them.
    assert_ne!(
        key.wasm_digest,
        warmup_artifact_key("profile", b"other").wasm_digest
    );
}

fn parse(args: &[&str]) -> Cli {
    let mut argv = vec!["greentic-runner"];
    argv.extend_from_slice(args);
    Cli::try_parse_from(argv).expect("arguments must parse")
}

fn write(dir: &Path, name: &str, contents: &str) -> PathBuf {
    let path = dir.join(name);
    std::fs::write(&path, contents).expect("write fixture");
    path
}

fn describe_json() -> Value {
    json!({
        "operations": [
            {
                "id": "run",
                "input": { "schema": { "type": "object", "b": 1, "a": 2 } },
                "output": { "schema": { "type": "string" } }
            },
            { "name": "other", "input_schema": { "type": "null" } }
        ],
        "config_schema": { "z": true, "y": false }
    })
}

#[test]
fn cli_parses_run_flags_and_subcommands() {
    let cli = parse(&[
        "--bindings",
        "a.gtbind",
        "--bindings-dir",
        "dir",
        "--port",
        "9001",
        "--no-cache",
        "--json",
        "--trace",
        "always",
        "--trace-capture-inputs",
        "on",
        "--validation",
        "warn",
        "--locale",
        "id",
        "--allow-dev",
        "--config-explain",
    ]);
    assert!(cli.command.is_none());
    assert_eq!(cli.run.port, Some(9001));
    assert!(cli.run.no_cache && cli.run.json && cli.run.allow_dev && cli.run.config_explain);
    assert_eq!(cli.run.locale.as_deref(), Some("id"));
    assert!(matches!(cli.run.trace, Some(TraceArg::Always)));
    assert!(matches!(
        cli.run.trace_capture_inputs,
        Some(TraceCaptureArg::On)
    ));
    assert!(matches!(cli.run.validation, Some(ValidationArg::Warn)));

    assert!(matches!(
        parse(&["info", "--json"]).command,
        Some(Command::Info { json: true })
    ));
    assert!(matches!(
        parse(&["cache", "doctor"]).command,
        Some(Command::Cache(CacheCommand::Doctor))
    ));
    assert!(matches!(
        parse(&["cache", "prune", "--dry-run"]).command,
        Some(Command::Cache(CacheCommand::Prune(CachePruneArgs {
            dry_run: true
        })))
    ));
    assert!(matches!(
        parse(&["cache", "warmup", "--pack", "p", "--mode", "memory"]).command,
        Some(Command::Cache(CacheCommand::Warmup(CacheWarmupArgs {
            mode: CacheWarmupMode::Memory,
            ..
        })))
    ));
    assert!(matches!(
        parse(&["contract", "--contract", "d.cbor"]).command,
        Some(Command::Contract(ContractArgs {
            describe_path: Some(_),
            ..
        }))
    ));
}

#[tokio::test]
async fn info_runs_in_both_output_modes() {
    run_with_cli(parse(&["info"])).await.expect("human info");
    run_with_cli(parse(&["info", "--json"]))
        .await
        .expect("json info");
}

#[tokio::test]
async fn a_locale_flag_is_accepted_alongside_a_subcommand() {
    run_with_cli(parse(&["--locale", "en", "info"]))
        .await
        .expect("info with locale");
}

#[tokio::test]
async fn running_without_bindings_is_refused() {
    let err = run_with_cli(parse(&[])).await.expect_err("no bindings");
    assert!(err.to_string().contains("--bindings"), "got: {err}");
}

#[tokio::test]
async fn config_explain_needs_no_bindings() {
    run_with_cli(parse(&["--config-explain"]))
        .await
        .expect("config explain without bindings");
    // Bindings that are given are still never read in explain mode.
    run_with_cli(parse(&[
        "--bindings",
        "never-read.gtbind",
        "--config-explain",
    ]))
    .await
    .expect("config explain");
}

#[tokio::test]
async fn cache_doctor_and_dry_run_prune_succeed() {
    run_with_cli(parse(&["cache", "doctor"]))
        .await
        .expect("doctor");
    run_with_cli(parse(&["cache", "prune", "--dry-run"]))
        .await
        .expect("dry-run prune");
}

#[test]
fn json_errors_carry_the_cause_chain() {
    let err = anyhow::anyhow!("root cause").context("outer failure");
    // Prints to stdout; the assertion is that it does not panic on a chain.
    emit_json_error(&err);
    emit_json_error(&anyhow::anyhow!("single"));
}

#[test]
fn build_resolver_reads_toml_and_json_layers() {
    let dir = TempDir::new().expect("tmp");
    let toml_path = write(dir.path(), "greentic.toml", "");
    let json_path = write(dir.path(), "greentic.json", "{}");
    assert!(build_resolver(Some(&toml_path), false).is_ok());
    assert!(build_resolver(Some(&json_path), true).is_ok());
    assert!(build_resolver(None, false).is_ok());
}

#[test]
fn build_resolver_reports_unreadable_and_malformed_files() {
    let dir = TempDir::new().expect("tmp");
    assert!(build_resolver(Some(&dir.path().join("missing.toml")), false).is_err());
    let bad_json = write(dir.path(), "bad.json", "{ not json");
    assert!(build_resolver(Some(&bad_json), false).is_err());
    let bad_toml = write(dir.path(), "bad.toml", "= nope");
    assert!(build_resolver(Some(&bad_toml), false).is_err());
}

fn component(value: Value) -> PackLockComponent {
    serde_json::from_value(value).expect("component")
}

#[test]
fn lock_component_prefers_agreeing_names_and_rejects_conflicts() {
    let primary = component(json!({
        "name": "c", "source_ref": "oci://x/y:1", "bundled_path": "a.wasm", "wasm_sha256": "aa"
    }));
    assert_eq!(primary.source_ref().expect("ref"), "oci://x/y:1");
    assert_eq!(primary.bundled_path().expect("path"), Some("a.wasm"));
    assert_eq!(
        primary.wasm_digest().expect("digest").as_deref(),
        Some("aa")
    );

    let legacy = component(json!({
        "name": "c", "ref": "oci://x/y:2", "path": "b.wasm", "sha256": "bb"
    }));
    assert_eq!(legacy.source_ref().expect("ref"), "oci://x/y:2");
    assert_eq!(legacy.bundled_path().expect("path"), Some("b.wasm"));
    assert_eq!(legacy.wasm_digest().expect("digest").as_deref(), Some("bb"));

    let agreeing = component(json!({
        "name": "c", "source_ref": "r", "ref": "r", "bundled_path": "p", "path": "p",
        "wasm_sha256": "d", "sha256": "d"
    }));
    assert_eq!(agreeing.source_ref().expect("ref"), "r");
    assert_eq!(agreeing.bundled_path().expect("path"), Some("p"));
    assert_eq!(
        agreeing.wasm_digest().expect("digest").as_deref(),
        Some("d")
    );

    let conflicting = component(json!({
        "name": "c", "source_ref": "r1", "ref": "r2", "bundled_path": "p1", "path": "p2",
        "wasm_sha256": "d1", "sha256": "d2"
    }));
    let err = conflicting.source_ref().expect_err("conflict");
    assert!(err.to_string().contains("conflicting refs"));
    assert!(
        conflicting
            .bundled_path()
            .expect_err("path conflict")
            .to_string()
            .contains("conflicting paths")
    );
    assert!(
        conflicting
            .wasm_digest()
            .expect_err("digest conflict")
            .to_string()
            .contains("conflicting wasm digests")
    );

    let bare = component(json!({ "name": "bare" }));
    assert!(bare.source_ref().is_err());
}

#[test]
fn digests_and_operations_are_normalised() {
    assert_eq!(normalize_digest("abc").expect("bare"), "sha256:abc");
    assert_eq!(
        normalize_digest("sha256:abc").expect("sha256"),
        "sha256:abc"
    );
    assert_eq!(
        normalize_digest("blake3:abc").expect("blake3"),
        "blake3:abc"
    );
    assert!(normalize_digest("sha512:xyz").is_err());
    assert!(normalize_digest("md5:xyz").is_err());
    assert_eq!(normalize_operation_id("  "), "run");
    assert_eq!(normalize_operation_id(" go "), "go");
    assert_eq!(to_hex(&[0, 255, 16]), "00ff10");
    assert!(sha256_prefixed(b"x").starts_with("sha256:"));
}

#[test]
fn lock_path_helpers_recognise_pack_files() {
    let dir = TempDir::new().expect("tmp");
    assert!(pick_lock_path(dir.path()).is_none());
    write(dir.path(), "pack.lock.json", "{}");
    assert_eq!(
        pick_lock_path(dir.path()),
        Some(dir.path().join("pack.lock.json"))
    );
    write(dir.path(), "pack.lock", "{}");
    assert_eq!(
        pick_lock_path(dir.path()),
        Some(dir.path().join("pack.lock"))
    );
    assert!(is_pack_lock(Path::new("x/pack.lock")));
    assert!(is_pack_lock(Path::new("pack.lock.json")));
    assert!(!is_pack_lock(Path::new("pack.yaml")));
    assert!(is_pack_yaml(Path::new("a/pack.yaml")));
    assert!(!is_pack_yaml(Path::new("a/pack.lock")));
}

#[tokio::test]
async fn read_pack_lock_accepts_a_directory_lock_file_or_pack_yaml() {
    let dir = TempDir::new().expect("tmp");
    let lock = write(
        dir.path(),
        "pack.lock",
        r#"{"schema_version":1,"components":[{"name":"c","source_ref":"oci://a/b:1"}]}"#,
    );
    let yaml = write(dir.path(), "pack.yaml", "name: demo\n");

    for path in [dir.path().to_path_buf(), lock, yaml] {
        let (root, parsed) = read_pack_lock(&path).await.expect("lock");
        assert_eq!(root, dir.path());
        assert_eq!(parsed.components.len(), 1);
        assert_eq!(parsed.components[0].name, "c");
    }
}

#[tokio::test]
async fn read_pack_lock_refuses_bad_inputs() {
    let empty = TempDir::new().expect("tmp");
    let err = read_pack_lock(empty.path()).await.expect_err("no lock");
    assert!(err.to_string().contains("pack.lock not found"));

    let yaml_only = write(empty.path(), "pack.yaml", "name: demo\n");
    let err = read_pack_lock(&yaml_only).await.expect_err("yaml, no lock");
    assert!(err.to_string().contains("pack.lock not found"));

    let other = write(empty.path(), "notes.txt", "hi");
    let err = read_pack_lock(&other).await.expect_err("unsupported");
    assert!(err.to_string().contains("unsupported pack path"));

    let missing = empty.path().join("gone").join("pack.lock");
    assert!(read_pack_lock(&missing).await.is_err());

    let garbage = TempDir::new().expect("tmp");
    let path = write(garbage.path(), "pack.lock", "not json");
    let err = read_pack_lock(&path).await.expect_err("garbage");
    assert!(err.to_string().contains("failed to parse pack.lock"));

    let wrong = TempDir::new().expect("tmp");
    let path = write(
        wrong.path(),
        "pack.lock",
        r#"{"schema_version":2,"components":[]}"#,
    );
    let err = read_pack_lock(&path).await.expect_err("schema version");
    assert!(err.to_string().contains("schema_version must be 1"));
}

#[tokio::test]
async fn warmup_reports_a_lock_entry_without_a_digest() {
    let dir = TempDir::new().expect("tmp");
    let lock = write(
        dir.path(),
        "pack.lock",
        r#"{"schema_version":1,"components":[{"name":"nodigest","source_ref":"oci://a/b:1"}]}"#,
    );
    let err = warmup_cache(CacheWarmupArgs {
        pack: lock,
        mode: CacheWarmupMode::Disk,
    })
    .await
    .expect_err("missing digest");
    assert!(err.to_string().contains("missing digest"), "got: {err}");
}

#[tokio::test]
async fn warmup_reports_a_lock_entry_without_a_source() {
    let dir = TempDir::new().expect("tmp");
    let lock = write(
        dir.path(),
        "pack.lock",
        r#"{"schema_version":1,"components":[{"name":"nosrc","wasm_sha256":"aa"}]}"#,
    );
    let err = warmup_cache(CacheWarmupArgs {
        pack: lock,
        mode: CacheWarmupMode::Memory,
    })
    .await
    .expect_err("missing source");
    assert!(err.to_string().contains("missing source_ref"), "got: {err}");
}

#[tokio::test]
async fn warmup_rejects_conflicting_wasm_digests() {
    let dir = TempDir::new().expect("tmp");
    let lock = write(
        dir.path(),
        "pack.lock",
        r#"{"schema_version":1,"components":[{"name":"c","source_ref":"oci://a/b:1","wasm_sha256":"aa","sha256":"bb"}]}"#,
    );
    let err = warmup_cache(CacheWarmupArgs {
        pack: lock,
        mode: CacheWarmupMode::Disk,
    })
    .await
    .expect_err("conflicting digests");
    assert!(
        err.to_string().contains("conflicting wasm digests"),
        "got: {err}"
    );
}

#[tokio::test]
async fn warmup_rejects_unsupported_digest_algorithm() {
    let dir = TempDir::new().expect("tmp");
    let lock = write(
        dir.path(),
        "pack.lock",
        r#"{"schema_version":1,"components":[{"name":"c","source_ref":"oci://a/b:1","resolved_digest":"sha512:xyz"}]}"#,
    );
    let err = warmup_cache(CacheWarmupArgs {
        pack: lock,
        mode: CacheWarmupMode::Disk,
    })
    .await
    .expect_err("unsupported algorithm");
    assert!(
        format!("{err:#}").contains("unsupported digest algorithm"),
        "got: {err:#}"
    );
}

#[tokio::test]
async fn warmup_verifies_local_bundled_bytes_against_a_sha256_resolved_digest() {
    let dir = TempDir::new().expect("tmp");
    std::fs::write(dir.path().join("c.wasm"), b"wasm-bytes").expect("write");
    let lock = write(
        dir.path(),
        "pack.lock",
        &format!(
            r#"{{"schema_version":1,"components":[{{"name":"c","source_ref":"oci://a/b:1","bundled_path":"c.wasm","resolved_digest":"sha256:{}"}}]}}"#,
            "0".repeat(64)
        ),
    );
    let err = warmup_cache(CacheWarmupArgs {
        pack: lock,
        mode: CacheWarmupMode::Disk,
    })
    .await
    .expect_err("local bytes must match a sha256 digest");
    assert!(err.to_string().contains("digest mismatch"), "got: {err:#}");
}

#[tokio::test]
async fn bundled_component_bytes_are_read_from_the_pack_root() {
    let dir = TempDir::new().expect("tmp");
    std::fs::write(dir.path().join("c.wasm"), b"wasm-bytes").expect("write");
    let entry = component(json!({
        "name": "c", "source_ref": "oci://a/b:1", "bundled_path": "c.wasm"
    }));
    let client = DistClient::new(DistOptions::default());
    let (bytes, from_local) = resolve_component_bytes(dir.path(), &entry, "oci://a/b:1", &client)
        .await
        .expect("bundled bytes");
    assert_eq!(bytes, b"wasm-bytes");
    assert!(from_local);
}

#[tokio::test]
async fn non_oci_component_sources_are_refused() {
    let dir = TempDir::new().expect("tmp");
    let entry = component(json!({ "name": "c", "source_ref": "file:///tmp/x.wasm" }));
    let client = DistClient::new(DistOptions::default());
    let result = resolve_component_bytes(dir.path(), &entry, "file:///tmp/x.wasm", &client).await;
    assert!(result.is_err(), "a non-OCI ref must not resolve");
    let bogus = resolve_component_bytes(dir.path(), &entry, "%%not-a-ref%%", &client).await;
    assert!(bogus.is_err());
}

#[test]
fn describe_artifacts_load_from_json_cbor_and_empty_files() {
    let dir = TempDir::new().expect("tmp");
    let json_path = write(dir.path(), "d.json", &describe_json().to_string());
    let loaded = load_describe_artifact(&json_path).expect("json");
    assert_eq!(loaded.operations.len(), 2);

    let cbor_path = dir.path().join("d.cbor");
    std::fs::write(
        &cbor_path,
        serde_cbor::to_vec(&describe_json()).expect("cbor"),
    )
    .expect("write");
    let loaded = load_describe_artifact(&cbor_path).expect("cbor");
    assert_eq!(loaded.operations.len(), 2);

    let empty = write(dir.path(), "empty", "");
    let loaded = load_describe_artifact(&empty).expect("empty");
    assert!(loaded.operations.is_empty());
    assert!(loaded.config_schema.is_null());

    assert!(load_describe_artifact(&dir.path().join("missing")).is_err());
    let garbage = write(dir.path(), "garbage", "\u{1}\u{2} not a describe");
    assert!(load_describe_artifact(&garbage).is_err());
}

#[test]
fn canonical_describe_round_trips_and_creates_parent_directories() {
    let dir = TempDir::new().expect("tmp");
    let typed =
        parse_typed_describe_from_value(describe_json(), "test payload").expect("typed describe");
    let out = dir.path().join("nested/deeper/describe.cbor");
    write_canonical_describe(&out, &typed).expect("write canonical");
    let reloaded = load_describe_artifact(&out).expect("reload");
    assert_eq!(
        describe_hash(&typed).expect("hash"),
        describe_hash(&reloaded).expect("hash")
    );
    // A bare file name has an empty parent and must not try to create it.
    let cwd_relative = dir.path().join("flat.cbor");
    write_canonical_describe(&cwd_relative, &typed).expect("flat write");
}

#[test]
fn undecodable_describe_payloads_name_their_source() {
    let err = parse_typed_describe_from_value(json!({ "operations": "nope" }), "my source")
        .expect_err("operations must be a list");
    assert!(err.to_string().contains("my source"));
}

#[test]
fn canonicalize_json_sorts_keys_recursively() {
    let value = canonicalize_json(json!({ "b": [ { "z": 1, "a": 2 } ], "a": null }));
    assert_eq!(
        serde_json::to_string(&value).expect("string"),
        r#"{"a":null,"b":[{"a":2,"z":1}]}"#
    );
}

#[test]
fn operation_selection_prefers_requested_then_run_then_first() {
    let typed = parse_typed_describe_from_value(describe_json(), "t").expect("typed");
    assert_eq!(
        select_operation_from_describe(&typed, "run").as_deref(),
        Some("run")
    );
    // The first operation is `run`, which wins over an unknown request.
    assert_eq!(
        select_operation_from_describe(&typed, "unknown").as_deref(),
        Some("run")
    );
    let only_other =
        parse_typed_describe_from_value(json!({ "operations": [ { "name": "other" } ] }), "t")
            .expect("typed");
    assert_eq!(
        select_operation_from_describe(&only_other, "missing").as_deref(),
        Some("other")
    );
    let none = parse_typed_describe_from_value(json!({}), "t").expect("typed");
    assert!(select_operation_from_describe(&none, "run").is_none());
}

#[test]
fn contract_report_carries_schemas_and_stable_hashes() {
    let typed = parse_typed_describe_from_value(describe_json(), "t").expect("typed");
    let report = contract_from_describe("run", &typed, "unit".into()).expect("report");
    assert_eq!(report.selected_operation, "run");
    assert_eq!(report.source, "unit");
    assert_eq!(report.output_schema, json!({ "type": "string" }));
    assert_eq!(report.config_schema, json!({ "y": false, "z": true }));
    assert!(report.describe_hash.starts_with("sha256:"));
    assert!(report.schema_hash.starts_with("sha256:"));
    let again = contract_from_describe("run", &typed, "unit".into()).expect("report");
    assert_eq!(report.describe_hash, again.describe_hash);
    assert_eq!(report.schema_hash, again.schema_hash);

    // A requested operation that is not first is selected, and the legacy
    // `input_schema` field is honoured when `input.schema` is null.
    let other = contract_from_describe("other", &typed, "unit".into()).expect("report");
    assert_eq!(other.selected_operation, "other");
    assert_eq!(other.input_schema, json!({ "type": "null" }));
    let unknown = contract_from_describe("missing", &typed, "unit".into()).expect("report");
    assert_eq!(unknown.selected_operation, "run");

    let empty = parse_typed_describe_from_value(json!({}), "t").expect("typed");
    let err = contract_from_describe("run", &empty, "unit".into()).expect_err("no ops");
    assert!(err.to_string().contains("not found"));
}

#[test]
fn legacy_schema_fields_back_the_operation_schemas() {
    let typed = parse_typed_describe_from_value(
        json!({ "operations": [ {
            "id": "legacy",
            "input_schema": { "type": "integer" },
            "output_schema": { "type": "boolean" }
        } ] }),
        "t",
    )
    .expect("typed");
    let report = contract_from_describe("legacy", &typed, "unit".into()).expect("report");
    assert_eq!(report.input_schema, json!({ "type": "integer" }));
    assert_eq!(report.output_schema, json!({ "type": "boolean" }));
}

fn contract_args(describe: Option<PathBuf>) -> ContractArgs {
    ContractArgs {
        pack: None,
        component: None,
        operation: "run".into(),
        describe_path: describe,
        emit_describe: None,
        print_contract: false,
        no_verify: true,
        validate_only: true,
    }
}

#[tokio::test]
async fn contract_inspection_of_a_gated_artifact_succeeds() {
    let dir = TempDir::new().expect("tmp");
    let path = write(dir.path(), "d.json", &describe_json().to_string());
    run_contract(contract_args(Some(path)))
        .await
        .expect("gated artifact inspection");
}

#[tokio::test]
async fn contract_inspection_enforces_its_gates() {
    let dir = TempDir::new().expect("tmp");
    let path = write(dir.path(), "d.json", &describe_json().to_string());

    let mut ungated = contract_args(Some(path.clone()));
    ungated.no_verify = false;
    let err = run_contract(ungated).await.expect_err("needs both flags");
    assert!(err.to_string().contains("--no-verify and --validate-only"));

    let mut orphan = contract_args(Some(path));
    orphan.component = Some("c".into());
    let err = run_contract(orphan)
        .await
        .expect_err("component needs pack");
    assert!(err.to_string().contains("--pack is required"));

    let mut emit_only = contract_args(None);
    emit_only.emit_describe = Some(dir.path().join("out.cbor"));
    assert!(run_contract(emit_only).await.is_err());

    let err = run_contract(contract_args(None))
        .await
        .expect_err("nothing");
    assert!(err.to_string().contains("provide either --pack"));
}

#[tokio::test]
async fn contract_with_a_pack_but_no_component_is_refused() {
    let mut args = contract_args(None);
    args.pack = Some(PathBuf::from("does-not-matter.gtpack"));
    let err = run_contract(args).await.expect_err("component required");
    assert!(err.to_string().contains("--component is required"));
}

#[tokio::test]
async fn contract_with_an_unreadable_pack_fails_to_load() {
    let dir = TempDir::new().expect("tmp");
    let mut args = contract_args(None);
    args.pack = Some(dir.path().join("missing.gtpack"));
    args.component = Some("c".into());
    assert!(run_contract(args).await.is_err());
}

#[test]
fn operation_selection_finds_a_non_first_requested_operation() {
    let typed = parse_typed_describe_from_value(
        json!({ "operations": [ { "id": "first" }, { "id": "second" } ] }),
        "t",
    )
    .expect("typed");
    assert_eq!(
        select_operation_from_describe(&typed, "second").as_deref(),
        Some("second")
    );
    // `run` fallback is also found when it is not the first operation.
    let with_run = parse_typed_describe_from_value(
        json!({ "operations": [ { "id": "first" }, { "id": "run" } ] }),
        "t",
    )
    .expect("typed");
    assert_eq!(
        select_operation_from_describe(&with_run, "missing").as_deref(),
        Some("run")
    );
}

#[test]
fn port_flag_is_optional_so_env_is_not_always_overridden() {
    assert_eq!(parse(&["--bindings", "a.gtbind"]).run.port, None);
}

#[test]
fn trace_flags_only_override_env_when_given() {
    let base = TraceConfig {
        mode: TraceMode::Always,
        out_path: PathBuf::from("env-trace.json"),
        buffer_size: 8,
        capture_inputs: true,
    };
    let untouched = build_trace_config(base.clone(), None, None, None);
    assert_eq!(untouched.mode, TraceMode::Always);
    assert_eq!(untouched.out_path, PathBuf::from("env-trace.json"));
    assert!(untouched.capture_inputs);

    let overridden = build_trace_config(
        base,
        Some(TraceArg::Off),
        Some(PathBuf::from("cli-trace.json")),
        Some(TraceCaptureArg::Off),
    );
    assert_eq!(overridden.mode, TraceMode::Off);
    assert_eq!(overridden.out_path, PathBuf::from("cli-trace.json"));
    assert!(!overridden.capture_inputs);
}

#[test]
fn trace_out_alone_enables_tracing_but_explicit_off_wins() {
    let base = TraceConfig {
        mode: TraceMode::Off,
        out_path: PathBuf::from("trace.json"),
        buffer_size: 8,
        capture_inputs: false,
    };
    let enabled = build_trace_config(base.clone(), None, Some(PathBuf::from("foo.json")), None);
    assert_eq!(enabled.mode, TraceMode::On);
    assert_eq!(enabled.out_path, PathBuf::from("foo.json"));

    let off = build_trace_config(
        base.clone(),
        Some(TraceArg::Off),
        Some(PathBuf::from("foo.json")),
        None,
    );
    assert_eq!(off.mode, TraceMode::Off);

    let neither = build_trace_config(base, None, None, None);
    assert_eq!(neither.mode, TraceMode::Off);
}

#[test]
fn component_digest_is_verified_before_caching() {
    let bytes = b"component-bytes";
    let good = sha256_prefixed(bytes);
    assert!(verify_component_digest("c", &good, bytes).is_ok());
    let blake = format!("blake3:{}", blake3::hash(bytes).to_hex());
    assert!(verify_component_digest("c", &blake, bytes).is_ok());
    let err = verify_component_digest("c", &good, b"tampered").expect_err("mismatch");
    assert!(err.to_string().contains("digest mismatch"), "got: {err}");
    assert!(verify_component_digest("c", "md5:abc", bytes).is_err());
}

#[test]
fn non_authoritative_lock_digest_mismatch_is_skipped_not_fatal() {
    let bytes = b"component-bytes";
    // An OCI manifest digest does not hash the wasm bytes.
    let manifest_digest = sha256_prefixed(b"some oci manifest json");
    assert!(check_lock_digest("c", &manifest_digest, bytes, false).is_ok());
    assert!(check_lock_digest("c", "md5:abc", bytes, false).is_ok());
    // wasm_sha256 is authoritative: a mismatch still rejects.
    assert!(check_lock_digest("c", &manifest_digest, bytes, true).is_err());
    assert!(check_lock_digest("c", &sha256_prefixed(bytes), bytes, true).is_ok());
}

#[test]
fn cache_cli_profile_matches_the_runtime_profile_not_a_default_engine() -> anyhow::Result<()> {
    let (_engine, profile) = greentic_runner_host::runtime_cache_engine_and_profile()?;
    let (_engine2, again) = greentic_runner_host::runtime_cache_engine_and_profile()?;
    assert_eq!(profile.id(), again.id(), "profile id is deterministic");

    // The old CLI behaviour: a default engine under the "default" fingerprint.
    let default_engine = wasmtime::Engine::default();
    let legacy = EngineProfile::from_engine(&default_engine, CpuPolicy::Native, "default".into());
    assert_ne!(
        profile.id(),
        legacy.id(),
        "cache tools must not act on the default-engine cache dir"
    );
    Ok(())
}