Skip to main content

greentic_component/cmd/
build.rs

1#![cfg(feature = "cli")]
2
3use std::env;
4use std::fs;
5use std::path::{Path, PathBuf};
6use std::process::Command;
7
8use anyhow::{Context, Result, anyhow, bail};
9use clap::Args;
10use serde_json::Value as JsonValue;
11use wasmtime::component::{Component, Linker, Val};
12use wasmtime::{Engine, Store};
13use wasmtime_wasi::{ResourceTable, WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView};
14
15use crate::abi::{self, AbiError};
16use crate::cmd::component_world::{canonical_component_world, is_fallback_world};
17use crate::cmd::flow::{
18    FlowUpdateResult, manifest_component_id, resolve_operation, update_with_manifest,
19};
20use crate::cmd::i18n;
21use crate::config::{
22    ConfigInferenceOptions, ConfigSchemaSource, load_manifest_with_schema, resolve_manifest_path,
23};
24use crate::describe::{DescribePayload, from_wit_world};
25use crate::embedded_descriptor::embed_and_verify_wasm;
26use crate::parse_manifest;
27use crate::path_safety::normalize_under_root;
28use crate::schema_quality::{SchemaQualityMode, validate_operation_schemas};
29use greentic_types::cbor::canonical;
30use greentic_types::schemas::component::v0_6_0::ComponentDescribe;
31
32const DEFAULT_MANIFEST: &str = "component.manifest.json";
33
34#[derive(Args, Debug, Clone)]
35pub struct BuildArgs {
36    /// Path to component.manifest.json (or directory containing it)
37    #[arg(long = "manifest", value_name = "PATH", default_value = DEFAULT_MANIFEST)]
38    pub manifest: PathBuf,
39    /// Path to the cargo binary (fallback: $CARGO, then `cargo` on PATH)
40    #[arg(long = "cargo", value_name = "PATH")]
41    pub cargo_bin: Option<PathBuf>,
42    /// Skip flow regeneration
43    #[arg(long = "no-flow")]
44    pub no_flow: bool,
45    /// Skip config inference; fail if config_schema is missing
46    #[arg(long = "no-infer-config")]
47    pub no_infer_config: bool,
48    /// Do not write inferred config_schema back to the manifest
49    #[arg(long = "no-write-schema")]
50    pub no_write_schema: bool,
51    /// Overwrite existing config_schema with inferred schema
52    #[arg(long = "force-write-schema")]
53    pub force_write_schema: bool,
54    /// Skip schema validation
55    #[arg(long = "no-validate")]
56    pub no_validate: bool,
57    /// Emit machine-readable JSON summary
58    #[arg(long = "json")]
59    pub json: bool,
60    /// Allow empty operation schemas (warnings only)
61    #[arg(long)]
62    pub permissive: bool,
63}
64
65#[derive(Debug, serde::Serialize)]
66struct BuildSummary {
67    manifest: PathBuf,
68    wasm_path: PathBuf,
69    wasm_hash: String,
70    config_source: ConfigSchemaSource,
71    schema_written: bool,
72    #[serde(skip_serializing_if = "Option::is_none")]
73    flows: Option<FlowUpdateResult>,
74}
75
76pub fn run(args: BuildArgs) -> Result<()> {
77    let manifest_path = resolve_manifest_path(&args.manifest);
78    let cwd = env::current_dir().context("failed to read current directory")?;
79    let manifest_path = if manifest_path.is_absolute() {
80        manifest_path
81    } else {
82        cwd.join(manifest_path)
83    };
84    if !manifest_path.exists() {
85        bail!(
86            "{}",
87            i18n::tr_lit("manifest not found at {}").replacen(
88                "{}",
89                &manifest_path.display().to_string(),
90                1
91            )
92        );
93    }
94    let cargo_bin = args
95        .cargo_bin
96        .clone()
97        .or_else(|| env::var_os("CARGO").map(PathBuf::from))
98        .unwrap_or_else(|| PathBuf::from("cargo"));
99    let inference_opts = ConfigInferenceOptions {
100        allow_infer: !args.no_infer_config,
101        write_schema: !args.no_write_schema,
102        force_write_schema: args.force_write_schema,
103        validate: !args.no_validate,
104    };
105    println!(
106        "Using manifest at {} (cargo: {})",
107        manifest_path.display(),
108        cargo_bin.display()
109    );
110
111    let config = load_manifest_with_schema(&manifest_path, &inference_opts)?;
112    let mode = if args.permissive {
113        SchemaQualityMode::Permissive
114    } else {
115        SchemaQualityMode::Strict
116    };
117    let manifest_component = parse_manifest(
118        &serde_json::to_string(&config.manifest)
119            .context("failed to serialize manifest for schema validation")?,
120    )
121    .context("failed to parse manifest for schema validation")?;
122    let schema_warnings = validate_operation_schemas(&manifest_component, mode)?;
123    for warning in schema_warnings {
124        eprintln!("warning[W_OP_SCHEMA_EMPTY]: {}", warning.message);
125    }
126    let component_id = manifest_component_id(&config.manifest)?;
127    let _operation = resolve_operation(&config.manifest, component_id)?;
128    let flow_outcome = if args.no_flow {
129        None
130    } else {
131        Some(update_with_manifest(&config)?)
132    };
133
134    let mut manifest_to_write = flow_outcome
135        .as_ref()
136        .map(|outcome| outcome.manifest.clone())
137        .unwrap_or_else(|| config.manifest.clone());
138    let canonical_manifest = parse_manifest(
139        &serde_json::to_string(&manifest_to_write)
140            .context("failed to serialize manifest for embedded descriptor")?,
141    )
142    .context("failed to parse canonical manifest for embedded descriptor")?;
143
144    let manifest_dir = manifest_path.parent().unwrap_or_else(|| Path::new("."));
145    build_wasm(manifest_dir, &cargo_bin, &manifest_to_write)?;
146    check_canonical_world_export(manifest_dir, &manifest_to_write)?;
147    let wasm_path_for_embedding = resolve_wasm_path(manifest_dir, &manifest_to_write)?;
148    embed_and_verify_wasm(&wasm_path_for_embedding, &canonical_manifest)
149        .context("failed to embed canonical manifest into built wasm")?;
150
151    if !config.persist_schema {
152        manifest_to_write
153            .as_object_mut()
154            .map(|obj| obj.remove("config_schema"));
155    }
156    let (wasm_path, wasm_hash) = update_manifest_hashes(manifest_dir, &mut manifest_to_write)?;
157    emit_describe_artifacts(manifest_dir, &manifest_to_write, &wasm_path)?;
158    write_manifest(&manifest_path, &manifest_to_write)?;
159
160    if args.json {
161        let payload = BuildSummary {
162            manifest: manifest_path.clone(),
163            wasm_path,
164            wasm_hash,
165            config_source: config.source,
166            schema_written: config.schema_written && config.persist_schema,
167            flows: flow_outcome.as_ref().map(|outcome| outcome.result),
168        };
169        serde_json::to_writer_pretty(std::io::stdout(), &payload)?;
170        println!();
171    } else {
172        println!("Built wasm artifact at {}", wasm_path.display());
173        println!("Updated {} hashes (blake3)", manifest_path.display());
174        if config.schema_written && config.persist_schema {
175            println!(
176                "Updated {} with inferred config_schema ({:?})",
177                manifest_path.display(),
178                config.source
179            );
180        }
181        if let Some(outcome) = flow_outcome {
182            let flows = outcome.result;
183            println!(
184                "Flows updated (default: {}, custom: {})",
185                flows.default_updated, flows.custom_updated
186            );
187        } else {
188            println!("Flow regeneration skipped (--no-flow)");
189        }
190    }
191
192    Ok(())
193}
194
195fn build_wasm(manifest_dir: &Path, cargo_bin: &Path, manifest: &JsonValue) -> Result<()> {
196    let resolved_world = manifest.get("world").and_then(|v| v.as_str()).unwrap_or("");
197    if resolved_world.is_empty() {
198        println!("Resolved manifest world: <missing>");
199    } else {
200        println!("Resolved manifest world: {resolved_world}");
201    }
202    let require_component = resolved_world.contains("component@0.6.0");
203
204    if require_component {
205        if cargo_component_available(cargo_bin) {
206            println!(
207                "Running cargo component build via {} in {}",
208                cargo_bin.display(),
209                manifest_dir.display()
210            );
211            let mut cmd = Command::new(cargo_bin);
212            if let Some(flags) = resolved_wasm_rustflags() {
213                cmd.env("RUSTFLAGS", sanitize_wasm_rustflags(&flags));
214            }
215            maybe_add_offline_flag(&mut cmd);
216            let status = cmd
217                .arg("component")
218                .arg("build")
219                .arg("--target")
220                .arg("wasm32-wasip2")
221                .arg("--release")
222                .current_dir(manifest_dir)
223                .status()
224                .with_context(|| {
225                    format!(
226                        "failed to run cargo component build via {}",
227                        cargo_bin.display()
228                    )
229                })?;
230            if !status.success() {
231                bail!(
232                    "cargo component build --target wasm32-wasip2 --release failed with status {}",
233                    status
234                );
235            }
236            return Ok(());
237        }
238        bail!(
239            "component@0.6.0 manifests require cargo-component; install it with `cargo install cargo-component --locked`"
240        );
241    }
242
243    println!(
244        "Running cargo build via {} in {}",
245        cargo_bin.display(),
246        manifest_dir.display()
247    );
248    let mut cmd = Command::new(cargo_bin);
249    if let Some(flags) = resolved_wasm_rustflags() {
250        cmd.env("RUSTFLAGS", sanitize_wasm_rustflags(&flags));
251    }
252    maybe_add_offline_flag(&mut cmd);
253    let status = cmd
254        .arg("build")
255        .arg("--target")
256        .arg("wasm32-wasip2")
257        .arg("--release")
258        .current_dir(manifest_dir)
259        .status()
260        .with_context(|| format!("failed to run cargo build via {}", cargo_bin.display()))?;
261
262    if !status.success() {
263        bail!(
264            "cargo build --target wasm32-wasip2 --release failed with status {}",
265            status
266        );
267    }
268    Ok(())
269}
270
271fn cargo_component_available(cargo_bin: &Path) -> bool {
272    Command::new(cargo_bin)
273        .arg("component")
274        .arg("--version")
275        .status()
276        .map(|status| status.success())
277        .unwrap_or(false)
278}
279
280fn maybe_add_offline_flag(cmd: &mut Command) {
281    if cargo_offline_requested() {
282        cmd.arg("--offline");
283    }
284}
285
286fn cargo_offline_requested() -> bool {
287    env_truthy(env::var_os("CARGO_NET_OFFLINE").as_deref())
288}
289
290fn env_truthy(value: Option<&std::ffi::OsStr>) -> bool {
291    value
292        .and_then(|raw| raw.to_str())
293        .map(|raw| {
294            matches!(
295                raw.trim().to_ascii_lowercase().as_str(),
296                "1" | "true" | "yes" | "on"
297            )
298        })
299        .unwrap_or(false)
300}
301
302/// Reads the wasm-specific rustflags that CI exports for wasm builds.
303fn resolved_wasm_rustflags() -> Option<String> {
304    env::var("WASM_RUSTFLAGS")
305        .ok()
306        .or_else(|| env::var("RUSTFLAGS").ok())
307}
308
309/// Drops linker arguments that `wasm-component-ld` rejects and normalizes whitespace.
310fn sanitize_wasm_rustflags(flags: &str) -> String {
311    flags
312        .replace("-Wl,", "")
313        .replace("-C link-arg=--no-keep-memory", "")
314        .replace("-C link-arg=--threads=1", "")
315        .split_whitespace()
316        .collect::<Vec<_>>()
317        .join(" ")
318}
319
320fn check_canonical_world_export(manifest_dir: &Path, manifest: &JsonValue) -> Result<()> {
321    if env::var_os("GREENTIC_SKIP_NODE_EXPORT_CHECK").is_some() {
322        println!("World export check skipped (GREENTIC_SKIP_NODE_EXPORT_CHECK=1)");
323        return Ok(());
324    }
325    let wasm_path = resolve_wasm_path(manifest_dir, manifest)?;
326    let canonical_world = canonical_component_world();
327    match abi::check_world_base(&wasm_path, canonical_world) {
328        Ok(exported) => println!("Exported world: {exported}"),
329        Err(err) => match err {
330            AbiError::WorldMismatch { expected, found } if is_fallback_world(&found) => {
331                println!("Exported world: {expected} (compatible fallback export: {found})");
332            }
333            err => {
334                return Err(err)
335                    .with_context(|| format!("component must export world {canonical_world}"));
336            }
337        },
338    }
339    Ok(())
340}
341
342fn update_manifest_hashes(
343    manifest_dir: &Path,
344    manifest: &mut JsonValue,
345) -> Result<(PathBuf, String)> {
346    let artifact_path = resolve_wasm_path(manifest_dir, manifest)?;
347    let wasm_bytes = fs::read(&artifact_path)
348        .with_context(|| format!("failed to read wasm at {}", artifact_path.display()))?;
349    let digest = blake3::hash(&wasm_bytes).to_hex().to_string();
350
351    manifest["artifacts"]["component_wasm"] =
352        JsonValue::String(path_string_relative(manifest_dir, &artifact_path)?);
353    manifest["hashes"]["component_wasm"] = JsonValue::String(format!("blake3:{digest}"));
354
355    Ok((artifact_path, format!("blake3:{digest}")))
356}
357
358fn path_string_relative(base: &Path, target: &Path) -> Result<String> {
359    let rel = pathdiff::diff_paths(target, base).unwrap_or_else(|| target.to_path_buf());
360    rel.to_str()
361        .map(|s| s.to_string())
362        .ok_or_else(|| anyhow!("failed to stringify path {}", target.display()))
363}
364
365fn resolve_wasm_path(manifest_dir: &Path, manifest: &JsonValue) -> Result<PathBuf> {
366    let manifest_root = manifest_dir
367        .canonicalize()
368        .with_context(|| format!("failed to canonicalize {}", manifest_dir.display()))?;
369    let candidate = manifest
370        .get("artifacts")
371        .and_then(|a| a.get("component_wasm"))
372        .and_then(|v| v.as_str())
373        .map(PathBuf::from)
374        .unwrap_or_else(|| {
375            let raw_name = manifest
376                .get("name")
377                .and_then(|v| v.as_str())
378                .or_else(|| manifest.get("id").and_then(|v| v.as_str()))
379                .unwrap_or("component");
380            let sanitized = raw_name.replace(['-', '.'], "_");
381            manifest_dir.join(format!("target/wasm32-wasip2/release/{sanitized}.wasm"))
382        });
383    if candidate.exists() {
384        let normalized = normalize_under_root(&manifest_root, &candidate).or_else(|_| {
385            if candidate.is_absolute() {
386                candidate
387                    .canonicalize()
388                    .with_context(|| format!("failed to canonicalize {}", candidate.display()))
389            } else {
390                normalize_under_root(&manifest_root, &candidate)
391            }
392        })?;
393        return Ok(normalized);
394    }
395
396    if let Some(cargo_target_dir) = env::var_os("CARGO_TARGET_DIR") {
397        let relative = candidate
398            .strip_prefix(manifest_dir)
399            .unwrap_or(&candidate)
400            .to_path_buf();
401        if relative.starts_with("target") {
402            let alt =
403                PathBuf::from(cargo_target_dir).join(relative.strip_prefix("target").unwrap());
404            if alt.exists() {
405                return alt
406                    .canonicalize()
407                    .with_context(|| format!("failed to canonicalize {}", alt.display()));
408            }
409        }
410    }
411
412    let normalized = normalize_under_root(&manifest_root, &candidate).or_else(|_| {
413        if candidate.is_absolute() {
414            candidate
415                .canonicalize()
416                .with_context(|| format!("failed to canonicalize {}", candidate.display()))
417        } else {
418            normalize_under_root(&manifest_root, &candidate)
419        }
420    })?;
421    Ok(normalized)
422}
423
424fn write_manifest(manifest_path: &Path, manifest: &JsonValue) -> Result<()> {
425    let formatted = serde_json::to_string_pretty(manifest)?;
426    fs::write(manifest_path, formatted + "\n")
427        .with_context(|| format!("failed to write {}", manifest_path.display()))
428}
429
430fn emit_describe_artifacts(
431    manifest_dir: &Path,
432    manifest: &JsonValue,
433    wasm_path: &Path,
434) -> Result<()> {
435    let abi_version = read_abi_version(manifest_dir);
436    let require_describe = abi_version.as_deref() == Some("0.6.0");
437    let manifest_model = parse_manifest(
438        &serde_json::to_string(manifest).context("failed to serialize manifest for describe")?,
439    )
440    .context("failed to parse manifest for describe")?;
441
442    let describe_bytes = match call_describe(wasm_path) {
443        Ok(bytes) => bytes,
444        Err(err) => {
445            if require_describe {
446                match from_wit_world(wasm_path, manifest_model.world.as_str()) {
447                    Ok(payload) => {
448                        write_wit_describe_artifacts(
449                            manifest_dir,
450                            manifest,
451                            wasm_path,
452                            abi_version.as_deref(),
453                            &payload,
454                        )?;
455                        eprintln!(
456                            "warning: describe export unavailable, emitted WIT-derived describe.json instead ({err})"
457                        );
458                        return Ok(());
459                    }
460                    Err(wit_err) => {
461                        return Err(anyhow!(
462                            "describe failed: {err}; WIT fallback failed: {wit_err}"
463                        ));
464                    }
465                }
466            }
467            eprintln!("warning: skipping describe artifacts ({err})");
468            return Ok(());
469        }
470    };
471
472    let payload = strip_self_describe_tag(&describe_bytes);
473    let canonical_bytes = canonical::canonicalize_allow_floats(payload)
474        .map_err(|err| anyhow!("describe canonicalization failed: {err}"))?;
475    let describe: ComponentDescribe = canonical::from_cbor(&canonical_bytes)
476        .map_err(|err| anyhow!("describe decode failed: {err}"))?;
477
478    let dist_dir = manifest_dir.join("dist");
479    fs::create_dir_all(&dist_dir)
480        .with_context(|| format!("failed to create {}", dist_dir.display()))?;
481
482    let (name, abi_underscore) = artifact_basename(manifest, wasm_path, abi_version.as_deref());
483    let base = format!("{name}__{abi_underscore}");
484    let describe_cbor_path = dist_dir.join(format!("{base}.describe.cbor"));
485    fs::write(&describe_cbor_path, &canonical_bytes)
486        .with_context(|| format!("failed to write {}", describe_cbor_path.display()))?;
487
488    let describe_json_path = dist_dir.join(format!("{base}.describe.json"));
489    let json = serde_json::to_string_pretty(&describe)?;
490    fs::write(&describe_json_path, json + "\n")
491        .with_context(|| format!("failed to write {}", describe_json_path.display()))?;
492
493    let wasm_out = dist_dir.join(format!("{base}.wasm"));
494    if wasm_out != wasm_path {
495        let _ = fs::copy(wasm_path, &wasm_out);
496    }
497
498    Ok(())
499}
500
501fn write_wit_describe_artifacts(
502    manifest_dir: &Path,
503    manifest: &JsonValue,
504    wasm_path: &Path,
505    abi_version: Option<&str>,
506    payload: &DescribePayload,
507) -> Result<()> {
508    let dist_dir = manifest_dir.join("dist");
509    fs::create_dir_all(&dist_dir)
510        .with_context(|| format!("failed to create {}", dist_dir.display()))?;
511
512    let (name, abi_underscore) = artifact_basename(manifest, wasm_path, abi_version);
513    let base = format!("{name}__{abi_underscore}");
514    let describe_cbor_path = dist_dir.join(format!("{base}.describe.cbor"));
515    let cbor = canonical::to_canonical_cbor_allow_floats(payload)
516        .map_err(|err| anyhow!("describe fallback canonicalization failed: {err}"))?;
517    fs::write(&describe_cbor_path, cbor)
518        .with_context(|| format!("failed to write {}", describe_cbor_path.display()))?;
519
520    let describe_json_path = dist_dir.join(format!("{base}.describe.json"));
521    let json = serde_json::to_string_pretty(payload)?;
522    fs::write(&describe_json_path, json + "\n")
523        .with_context(|| format!("failed to write {}", describe_json_path.display()))?;
524
525    let wasm_out = dist_dir.join(format!("{base}.wasm"));
526    if wasm_out != wasm_path {
527        let _ = fs::copy(wasm_path, &wasm_out);
528    }
529
530    Ok(())
531}
532
533fn read_abi_version(manifest_dir: &Path) -> Option<String> {
534    let cargo_path = manifest_dir.join("Cargo.toml");
535    let contents = fs::read_to_string(cargo_path).ok()?;
536    let doc: toml::Value = toml::from_str(&contents).ok()?;
537    doc.get("package")
538        .and_then(|pkg| pkg.get("metadata"))
539        .and_then(|meta| meta.get("greentic"))
540        .and_then(|g| g.get("abi_version"))
541        .and_then(|v| v.as_str())
542        .map(|s| s.to_string())
543}
544
545fn artifact_basename(
546    manifest: &JsonValue,
547    wasm_path: &Path,
548    abi_version: Option<&str>,
549) -> (String, String) {
550    let name = manifest
551        .get("name")
552        .and_then(|v| v.as_str())
553        .or_else(|| manifest.get("id").and_then(|v| v.as_str()))
554        .map(sanitize_name)
555        .unwrap_or_else(|| {
556            wasm_path
557                .file_stem()
558                .and_then(|s| s.to_str())
559                .map(sanitize_name)
560                .unwrap_or_else(|| "component".to_string())
561        });
562    let abi = abi_version.unwrap_or("0.6.0").replace('.', "_");
563    (name, abi)
564}
565
566fn sanitize_name(raw: &str) -> String {
567    raw.chars()
568        .map(|ch| {
569            if ch.is_ascii_alphanumeric() || ch == '-' {
570                ch
571            } else {
572                '_'
573            }
574        })
575        .collect::<String>()
576        .trim_matches('_')
577        .to_string()
578}
579
580fn call_describe(wasm_path: &Path) -> Result<Vec<u8>> {
581    let mut config = wasmtime::Config::new();
582    config.wasm_component_model(true);
583    let engine = Engine::new(&config).map_err(|err| anyhow!("failed to create engine: {err}"))?;
584    let component = Component::from_file(&engine, wasm_path)
585        .map_err(|err| anyhow!("failed to load component {}: {err}", wasm_path.display()))?;
586    let mut linker = Linker::new(&engine);
587    wasmtime_wasi::p2::add_to_linker_sync(&mut linker)
588        .map_err(|err| anyhow!("failed to add wasi: {err}"))?;
589    let mut store = Store::new(&engine, BuildWasi::new()?);
590    let instance = linker
591        .instantiate(&mut store, &component)
592        .map_err(|err| anyhow!("failed to instantiate component: {err}"))?;
593    let instance_index = resolve_interface_index(&instance, &mut store, "component-descriptor")
594        .ok_or_else(|| anyhow!("missing export interface component-descriptor"))?;
595    let func_index = instance
596        .get_export_index(&mut store, Some(&instance_index), "describe")
597        .ok_or_else(|| anyhow!("missing export component-descriptor.describe"))?;
598    let func = instance
599        .get_func(&mut store, func_index)
600        .ok_or_else(|| anyhow!("describe export is not callable"))?;
601    let mut results = vec![Val::Bool(false); func.ty(&mut store).results().len()];
602    func.call(&mut store, &[], &mut results)
603        .map_err(|err| anyhow!("describe call failed: {err}"))?;
604    let val = results
605        .first()
606        .ok_or_else(|| anyhow!("describe returned no value"))?;
607    val_to_bytes(val).map_err(|err| anyhow!(err))
608}
609
610fn resolve_interface_index(
611    instance: &wasmtime::component::Instance,
612    store: &mut Store<BuildWasi>,
613    interface: &str,
614) -> Option<wasmtime::component::ComponentExportIndex> {
615    for candidate in interface_candidates(interface) {
616        if let Some(index) = instance.get_export_index(&mut *store, None, &candidate) {
617            return Some(index);
618        }
619    }
620    None
621}
622
623fn interface_candidates(interface: &str) -> [String; 3] {
624    [
625        interface.to_string(),
626        format!("greentic:component/{interface}@0.6.0"),
627        format!("greentic:component/{interface}"),
628    ]
629}
630
631fn val_to_bytes(val: &Val) -> Result<Vec<u8>, String> {
632    match val {
633        Val::List(items) => {
634            let mut out = Vec::with_capacity(items.len());
635            for item in items {
636                match item {
637                    Val::U8(byte) => out.push(*byte),
638                    _ => return Err("expected list<u8>".to_string()),
639                }
640            }
641            Ok(out)
642        }
643        _ => Err("expected list<u8>".to_string()),
644    }
645}
646
647fn strip_self_describe_tag(bytes: &[u8]) -> &[u8] {
648    const SELF_DESCRIBE_TAG: [u8; 3] = [0xd9, 0xd9, 0xf7];
649    if bytes.starts_with(&SELF_DESCRIBE_TAG) {
650        &bytes[SELF_DESCRIBE_TAG.len()..]
651    } else {
652        bytes
653    }
654}
655
656struct BuildWasi {
657    ctx: WasiCtx,
658    table: ResourceTable,
659}
660
661impl BuildWasi {
662    fn new() -> Result<Self> {
663        let ctx = WasiCtxBuilder::new().build();
664        Ok(Self {
665            ctx,
666            table: ResourceTable::new(),
667        })
668    }
669}
670
671impl WasiView for BuildWasi {
672    fn ctx(&mut self) -> WasiCtxView<'_> {
673        WasiCtxView {
674            ctx: &mut self.ctx,
675            table: &mut self.table,
676        }
677    }
678}
679
680#[cfg(test)]
681mod tests {
682    use std::ffi::OsStr;
683    use std::path::Path;
684
685    use serde_json::json;
686    use wasmtime::component::Val;
687
688    use super::{
689        env_truthy, path_string_relative, resolve_wasm_path, sanitize_name,
690        sanitize_wasm_rustflags, strip_self_describe_tag, val_to_bytes,
691    };
692
693    #[test]
694    fn sanitize_name_preserves_hyphens_for_dist_artifacts() {
695        assert_eq!(
696            sanitize_name("wizard-smoke-advanced"),
697            "wizard-smoke-advanced"
698        );
699        assert_eq!(
700            sanitize_name("wizard_smoke_advanced"),
701            "wizard_smoke_advanced"
702        );
703    }
704
705    #[test]
706    fn env_truthy_accepts_common_true_spellings() {
707        for value in ["1", "true", "TRUE", " yes ", "on"] {
708            assert!(
709                env_truthy(Some(OsStr::new(value))),
710                "{value} should be truthy"
711            );
712        }
713    }
714
715    #[test]
716    fn env_truthy_rejects_falsey_and_missing_values() {
717        for value in [
718            None,
719            Some(OsStr::new("0")),
720            Some(OsStr::new("false")),
721            Some(OsStr::new("")),
722        ] {
723            assert!(!env_truthy(value));
724        }
725    }
726
727    #[test]
728    fn sanitize_wasm_rustflags_drops_unsupported_linker_args() {
729        let sanitized = sanitize_wasm_rustflags(
730            "-C opt-level=z -Wl,--export-table -C link-arg=--no-keep-memory -C link-arg=--threads=1",
731        );
732
733        assert_eq!(sanitized, "-C opt-level=z --export-table");
734    }
735
736    #[test]
737    fn path_string_relative_prefers_relative_path() {
738        let base = Path::new("/tmp/project");
739        let target = Path::new("/tmp/project/dist/component.wasm");
740
741        let relative = path_string_relative(base, target).expect("relative path");
742
743        assert_eq!(relative, "dist/component.wasm");
744    }
745
746    #[test]
747    fn resolve_wasm_path_uses_default_target_location_when_manifest_omits_artifact() {
748        let dir = tempfile::tempdir().expect("tempdir");
749        let target = dir
750            .path()
751            .join("target/wasm32-wasip2/release/com_greentic_demo.wasm");
752        std::fs::create_dir_all(target.parent().expect("target parent"))
753            .expect("create target dir");
754        std::fs::write(&target, b"wasm").expect("write wasm");
755
756        let manifest = json!({
757            "id": "com.greentic.demo"
758        });
759
760        let resolved = resolve_wasm_path(dir.path(), &manifest).expect("resolve default wasm path");
761        assert_eq!(resolved, target.canonicalize().expect("canonical target"));
762    }
763
764    #[test]
765    fn val_to_bytes_rejects_non_byte_lists() {
766        let err = val_to_bytes(&Val::List(vec![Val::String("oops".to_string())]))
767            .expect_err("non-u8 list should fail");
768        assert_eq!(err, "expected list<u8>");
769    }
770
771    #[test]
772    fn strip_self_describe_tag_removes_only_known_prefix() {
773        let tagged = [0xd9, 0xd9, 0xf7, 0x01, 0x02];
774        assert_eq!(strip_self_describe_tag(&tagged), &[0x01, 0x02]);
775        assert_eq!(strip_self_describe_tag(&[0x01, 0x02]), &[0x01, 0x02]);
776    }
777}