consortium-builder 0.1.0

Build system for Consortium applications and runtimes
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
// Copyright 2026 Ethan Wu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

use crate::compile::env::{DbgConfig, HmiConfig, SharedMemConfig};
use crate::compile::web::HmiBuildCommand;
use crate::progress::BuildReporter;
use std::path::{Path, PathBuf};
use std::process::{ExitStatus, Stdio};
use tokio::io::{AsyncRead, AsyncReadExt};
use tokio::process::Command;

/// Injects a shared-memory region's layout as `CONSORTIUM_<PREFIX>_*` env vars
/// onto a pending `cargo build` command, so the firmware/application crates can
/// read them at compile time with `option_env!`. A `None` config injects
/// nothing, leaving the consuming crate to emit its "build via consortium-builder"
/// hint.
pub fn inject_shared_mem(cmd: &mut Command, prefix: &str, config: Option<&SharedMemConfig>) {
    if let Some(cfg) = config {
        for (key, value) in cfg.env_pairs(prefix) {
            cmd.env(key, value);
        }
    }
}

/// Injects the debug ring's `CONSORTIUM_DBG_BASE`/`_LENGTH` env vars onto a
/// pending `cargo build` command, so `consortium-dbg`'s `config` module can read
/// them at compile time with `option_env!`. A `None` config injects nothing,
/// leaving the logger unconfigured (a no-op).
pub fn inject_dbg(cmd: &mut Command, config: Option<&DbgConfig>) {
    if let Some(cfg) = config {
        for (key, value) in cfg.env_pairs() {
            cmd.env(key, value);
        }
    }
}

/// Injects the HMI build configuration as `CONSORTIUM_HMI_*` env vars onto a
/// pending `cargo build` command, so `consortium-hmi-webkit` can read them at compile
/// time with `option_env!`.
///
/// `CONSORTIUM_HMI_DIST` (the compiled frontend dist directory) is always set
/// when a config is present; `CONSORTIUM_HMI_DEBUG_URL` is set only when the
/// manifest declares a dev-server URL, so its absence lets debug builds fall
/// back to the static `csti://` handler. A `None` config injects nothing.
pub fn inject_hmi(cmd: &mut Command, config: Option<&HmiConfig>) {
    if let Some(hmi) = config {
        for (key, value) in hmi.env_pairs() {
            cmd.env(key, value);
        }
    }
}

/// Runs a child command, streaming its output through `reporter` when present.
///
/// Without a reporter, the command inherits stdout/stderr just like
/// `Command::status`; with a reporter, output is piped so the CLI can redraw its
/// sticky status prompt after each chunk.
pub async fn run_command(
    cmd: &mut Command,
    reporter: Option<&dyn BuildReporter>,
) -> std::io::Result<ExitStatus> {
    let Some(reporter) = reporter else {
        return cmd.status().await;
    };

    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
    let mut child = cmd.spawn()?;
    let stdout = child.stdout.take();
    let stderr = child.stderr.take();

    let stdout_stream = stream_output(stdout, reporter);
    let stderr_stream = stream_output(stderr, reporter);
    let wait = child.wait();
    let (stdout_result, stderr_result, status) = tokio::join!(stdout_stream, stderr_stream, wait);
    stdout_result?;
    stderr_result?;
    status
}

async fn stream_output<R>(reader: Option<R>, reporter: &dyn BuildReporter) -> std::io::Result<()>
where
    R: AsyncRead + Unpin,
{
    let Some(mut reader) = reader else {
        return Ok(());
    };
    let mut buf = [0_u8; 8192];
    loop {
        let n = reader.read(&mut buf).await?;
        if n == 0 {
            return Ok(());
        }
        reporter.command_output(&String::from_utf8_lossy(&buf[..n]));
    }
}

/// Strips ST SDK path entries from PATH using OECORE_NATIVE_SYSROOT as the
/// anchor. All ST-injected entries are subdirectories of the native sysroot,
/// so this is precise - upstream rustup/cargo/rustc remain reachable.
pub fn clean_path(native_sysroot: &str) -> String {
    let path = std::env::var("PATH").unwrap_or_default();

    std::env::split_paths(&path)
        .filter(|p| {
            if native_sysroot.is_empty() {
                // Fallback heuristic if OECORE_NATIVE_SYSROOT was somehow empty
                let s = p.to_string_lossy();
                !s.contains("aarch64-ostl_sdk-linux") && !s.contains("aarch64-ostl-linux")
            } else {
                !p.starts_with(native_sysroot)
            }
        })
        .map(|p| p.to_string_lossy().into_owned())
        .collect::<Vec<_>>()
        .join(":")
}

/// Resolves a cross-tool binary to its absolute path by walking PATH.
/// Must be called before PATH is sanitized.
pub fn resolve_cross_tool(tool: &str) -> Result<String, String> {
    let path = std::env::var("PATH").unwrap_or_default();

    for dir in std::env::split_paths(&path) {
        let candidate = dir.join(tool);
        if candidate.is_file() {
            return Ok(candidate.to_string_lossy().into_owned());
        }
    }

    Err(format!(
        "Cross tool '{}' not found on PATH. \
             Is the ST environment-setup script sourced?",
        tool
    ))
}

/// Resolves the HMI frontend build command, preferring the
/// `CONSORTIUM_HMI_BUILD_CMD` override, then `vp`, then `npm` on PATH.
///
/// Mirrors [`resolve_cargo`]'s env-var-first pattern: an explicit override wins,
/// otherwise the first available known toolchain is used.
pub fn resolve_hmi_build_command() -> Result<HmiBuildCommand, String> {
    resolve_hmi_build_command_from(std::env::var("CONSORTIUM_HMI_BUILD_CMD").ok(), |tool| {
        resolve_cross_tool(tool).ok()
    })
}

/// The env-var/PATH resolution logic for [`resolve_hmi_build_command`], factored
/// out so it can be tested deterministically without touching the process's
/// global `PATH`/env (which parallel tests would race on).
fn resolve_hmi_build_command_from(
    env_cmd: Option<String>,
    resolve_tool: impl Fn(&str) -> Option<String>,
) -> Result<HmiBuildCommand, String> {
    if let Some(cmd) = env_cmd {
        let parts: Vec<String> = cmd.split_whitespace().map(String::from).collect();
        if parts.is_empty() {
            return Err("CONSORTIUM_HMI_BUILD_CMD is set but empty".to_string());
        }
        return Ok(HmiBuildCommand::Custom(parts));
    }
    if let Some(program) = resolve_tool("vp") {
        return Ok(HmiBuildCommand::Resolved {
            program,
            args: vec!["build".to_string()],
        });
    }
    if let Some(program) = resolve_tool("npm") {
        return Ok(HmiBuildCommand::Resolved {
            program,
            args: vec!["run".to_string(), "build".to_string()],
        });
    }
    Err(
        "no HMI build tool found: set CONSORTIUM_HMI_BUILD_CMD, or install `vp` \
         or `npm` on PATH"
            .to_string(),
    )
}

/// Resolves the PocketJS frontend build command for `app`, writing artifacts to
/// `outdir` (the manifest's resolved `[hmi].build`).
///
/// A `CONSORTIUM_HMI_BUILD_CMD` override wins verbatim; otherwise this runs
/// `bun tools/build.ts <app> --outdir=<outdir>` from the project directory.
pub fn resolve_pocket_build_command(app: &str, outdir: &Path) -> Result<HmiBuildCommand, String> {
    resolve_pocket_build_command_from(
        std::env::var("CONSORTIUM_HMI_BUILD_CMD").ok(),
        app,
        outdir,
        |tool| resolve_cross_tool(tool).ok(),
    )
}

/// Testable core of [`resolve_pocket_build_command`]; see
/// [`resolve_hmi_build_command_from`] for why resolution is injected.
fn resolve_pocket_build_command_from(
    env_cmd: Option<String>,
    app: &str,
    outdir: &Path,
    resolve_tool: impl Fn(&str) -> Option<String>,
) -> Result<HmiBuildCommand, String> {
    if let Some(cmd) = env_cmd {
        let parts: Vec<String> = cmd.split_whitespace().map(String::from).collect();
        if parts.is_empty() {
            return Err("CONSORTIUM_HMI_BUILD_CMD is set but empty".to_string());
        }
        return Ok(HmiBuildCommand::Custom(parts));
    }
    let program = resolve_tool("bun").ok_or_else(|| {
        "PocketJS build needs `bun`: install it (https://bun.sh) or set \
         CONSORTIUM_HMI_BUILD_CMD"
            .to_string()
    })?;
    Ok(HmiBuildCommand::Resolved {
        program,
        args: vec![
            "tools/build.ts".to_string(),
            app.to_string(),
            format!("--outdir={}", outdir.display()),
        ],
    })
}

/// Resolves upstream cargo or rustc via `rustup which --toolchain stable`.
pub async fn resolve_rustup(tool: &str) -> Result<String, String> {
    let output = Command::new("rustup")
        .args(["which", "--toolchain", "stable", tool])
        .output()
        .await
        .map_err(|e| format!("Failed to run rustup: {}", e))?;

    if !output.status.success() {
        return Err(format!(
            "rustup could not resolve '{}' for the stable toolchain: {}",
            tool,
            String::from_utf8_lossy(&output.stderr).trim(),
        ));
    }

    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

/// Reads the binary name from Cargo.toml, preferring [[bin]] over [package].
pub async fn binary_name(path: &Path) -> Result<String, String> {
    let cargo_toml_path = path.join("Cargo.toml");
    let content = tokio::fs::read_to_string(&cargo_toml_path)
        .await
        .map_err(|e| format!("Failed to read Cargo.toml at {:?}: {}", cargo_toml_path, e))?;

    let manifest: toml::Value =
        toml::from_str(&content).map_err(|e| format!("Failed to parse Cargo.toml: {}", e))?;

    // 1. Prefer [[bin]] name if explicitly declared
    if let Some(name) = manifest
        .get("bin")
        .and_then(|b| b.as_array())
        .and_then(|arr| arr.first())
        .and_then(|b| b.get("name"))
        .and_then(|n| n.as_str())
    {
        return Ok(name.to_string());
    }

    // 2. Fall back to [package] name (default binary)
    manifest
        .get("package")
        .and_then(|p| p.get("name"))
        .and_then(|n| n.as_str())
        .map(|n| n.to_string())
        .ok_or_else(|| "Could not determine binary name from Cargo.toml".to_string())
}

/// Verifies that the output file has a valid ELF magic header.
pub async fn verify_elf(path: &PathBuf) -> Result<(), String> {
    let mut file = tokio::fs::File::open(path)
        .await
        .map_err(|e| format!("Failed to open ELF file {:?}: {}", path, e))?;

    let mut magic = [0u8; 4];
    file.read_exact(&mut magic)
        .await
        .map_err(|e| format!("Failed to read ELF magic bytes: {}", e))?;

    if magic != [0x7f, b'E', b'L', b'F'] {
        return Err(format!(
            "File {:?} is not a valid ELF binary (magic: {:02x?})",
            path, magic
        ));
    }

    Ok(())
}

/// Resolves the cargo binary path, preferring `CONSORTIUM_CARGO` env var over `rustup which`.
pub async fn resolve_cargo() -> Result<String, String> {
    match std::env::var("CONSORTIUM_CARGO") {
        Ok(v) => Ok(v),
        Err(_) => resolve_rustup("cargo").await,
    }
}

/// Resolves the rustc binary path, preferring `CONSORTIUM_RUSTC` env var over `rustup which`.
pub async fn resolve_rustc() -> Result<String, String> {
    match std::env::var("CONSORTIUM_RUSTC") {
        Ok(v) => Ok(v),
        Err(_) => resolve_rustup("rustc").await,
    }
}

/// Returns the expected ELF output path after a successful build.
pub async fn elf_path(path: &Path, target: &str, release: bool) -> Result<PathBuf, String> {
    let binary_name = binary_name(path).await?;
    let profile = if release { "release" } else { "debug" };

    Ok(path
        .join("target")
        .join(target)
        .join(profile)
        .join(binary_name))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::ffi::OsStr;

    /// Collects the `CONSORTIUM_*` env vars a builder set on a command, so a test
    /// can assert exactly what a downstream `cargo build` would see.
    fn injected_vars(cmd: &Command) -> HashMap<String, String> {
        cmd.as_std()
            .get_envs()
            .filter_map(|(k, v)| {
                let key = k.to_str()?;
                if !key.starts_with("CONSORTIUM_") {
                    return None;
                }
                // `get_envs` yields `None` for vars marked for removal; the
                // builder only ever sets, so a present value is expected.
                Some((key.to_string(), v?.to_str()?.to_string()))
            })
            .collect()
    }

    #[test]
    fn inject_shared_mem_sets_all_ipc_vars() {
        let cfg = SharedMemConfig {
            length: 0x1000,
            uio: 3,
            mtu: 64,
        };
        let mut cmd = Command::new("true");
        inject_shared_mem(&mut cmd, "IPC", Some(&cfg));

        let vars = injected_vars(&cmd);
        // No base: nothing reads `CONSORTIUM_IPC_BASE`.
        assert!(!vars.contains_key("CONSORTIUM_IPC_BASE"));
        assert_eq!(
            vars.get("CONSORTIUM_IPC_LENGTH").map(String::as_str),
            Some("4096")
        );
        assert_eq!(
            vars.get("CONSORTIUM_IPC_UIO").map(String::as_str),
            Some("3")
        );
        assert_eq!(
            vars.get("CONSORTIUM_IPC_MTU").map(String::as_str),
            Some("64")
        );
        assert_eq!(vars.len(), 3);
    }

    #[test]
    fn inject_shared_mem_none_injects_nothing() {
        // A missing `[ipc]` section must leave the consuming crate to emit its own
        // "build via consortium-builder" hint, not a silent zero.
        let mut cmd = Command::new("true");
        inject_shared_mem(&mut cmd, "IPC", None);
        assert!(injected_vars(&cmd).is_empty());
    }

    #[test]
    fn inject_dbg_emits_base_and_length() {
        // The debug carrier keeps the base + length `consortium-dbg` reads.
        let cfg = DbgConfig::new(0x204C_1000, 0x800);
        let mut cmd = Command::new("true");
        inject_dbg(&mut cmd, Some(&cfg));

        let vars = injected_vars(&cmd);
        assert!(vars.keys().all(|k| k.starts_with("CONSORTIUM_DBG_")));
        assert_eq!(
            vars.get("CONSORTIUM_DBG_BASE").map(String::as_str),
            Some("541855744")
        );
        assert_eq!(
            vars.get("CONSORTIUM_DBG_LENGTH").map(String::as_str),
            Some("2048")
        );
        assert!(!vars.contains_key("CONSORTIUM_IPC_BASE"));
    }

    #[test]
    fn inject_dbg_none_injects_nothing() {
        let mut cmd = Command::new("true");
        inject_dbg(&mut cmd, None);
        assert!(injected_vars(&cmd).is_empty());
    }

    /// End-to-end: a `Consortium.toml` `[ipc]` table parses into a
    /// [`SharedMemConfig`], the builder injects it as env vars, and parsing those
    /// values back as decimals recovers the original numbers.
    /// Hex TOML literals are normalized to decimal env-var strings.
    #[test]
    fn toml_ipc_section_round_trips_through_injected_env() {
        #[derive(serde::Deserialize)]
        struct Doc {
            ipc: SharedMemConfig,
        }

        // No base: the IPC carrier resolves its region from generated code, not
        // the environment.
        let manifest = "\
            [ipc]\n\
            length = 0x1000\n\
            uio    = 0\n\
            mtu    = 64\n";

        let doc: Doc = toml::from_str(manifest).expect("[ipc] should deserialize");
        let cfg = doc.ipc;

        let mut cmd = Command::new("true");
        inject_shared_mem(&mut cmd, "IPC", Some(&cfg));
        let vars = injected_vars(&cmd);

        // Re-parse the injected strings exactly as the consuming crate does.
        let read = |suffix: &str| -> &str {
            vars.get(&format!("CONSORTIUM_IPC_{suffix}"))
                .map(String::as_str)
                .unwrap_or_else(|| panic!("missing CONSORTIUM_IPC_{suffix}"))
        };
        assert_eq!(read("LENGTH"), "4096");
        assert_eq!(read("UIO"), "0");
        assert_eq!(read("MTU"), "64");
        assert_eq!(read("LENGTH").parse::<u64>().unwrap(), cfg.length);
        assert_eq!(read("UIO").parse::<u32>().unwrap(), cfg.uio);
        assert_eq!(read("MTU").parse::<u32>().unwrap(), cfg.mtu);

        assert!(!vars.contains_key("CONSORTIUM_IPC_BASE"));
        assert_eq!(cfg.uio, 0);
    }

    #[test]
    fn toml_ipc_section_rejects_unknown_fields() {
        // `SharedMemConfig` is `deny_unknown_fields`, so a typo'd key is a hard
        // error rather than a silently dropped layout parameter.
        #[derive(serde::Deserialize)]
        struct Doc {
            #[allow(dead_code)]
            ipc: SharedMemConfig,
        }
        let manifest = "[ipc]\nlength = 0\nuio = 0\nmtu = 0\nbogus = 1\n";
        let err = match toml::from_str::<Doc>(manifest) {
            Ok(_) => panic!("unknown field `bogus` should have been rejected"),
            Err(e) => e,
        };
        assert!(err.to_string().contains("bogus"), "unexpected error: {err}");
    }

    // ── HMI ─────────────────────────────────────────────────────────────────

    #[test]
    fn inject_hmi_sets_dist_and_debug_url() {
        let hmi = HmiConfig::Cog {
            dist: "/opt/app/dist".to_string(),
            debug_url: Some("http://localhost:5173".to_string()),
        };
        let mut cmd = Command::new("true");
        inject_hmi(&mut cmd, Some(&hmi));

        let vars = injected_vars(&cmd);
        assert_eq!(
            vars.get("CONSORTIUM_HMI_DIST").map(String::as_str),
            Some("/opt/app/dist")
        );
        assert_eq!(
            vars.get("CONSORTIUM_HMI_DEBUG_URL").map(String::as_str),
            Some("http://localhost:5173")
        );
        assert_eq!(vars.len(), 2);
    }

    #[test]
    fn inject_hmi_omits_debug_url_when_absent() {
        // No dev-server URL: only DIST is injected, so debug builds fall back to
        // the static `csti://` handler rather than a stale URL.
        let hmi = HmiConfig::Cog {
            dist: "/opt/app/dist".to_string(),
            debug_url: None,
        };
        let mut cmd = Command::new("true");
        inject_hmi(&mut cmd, Some(&hmi));

        let vars = injected_vars(&cmd);
        assert!(vars.contains_key("CONSORTIUM_HMI_DIST"));
        assert!(!vars.contains_key("CONSORTIUM_HMI_DEBUG_URL"));
    }

    #[test]
    fn inject_hmi_none_injects_nothing() {
        let mut cmd = Command::new("true");
        inject_hmi(&mut cmd, None);
        assert!(injected_vars(&cmd).is_empty());
    }

    /// End-to-end: a `Consortium.toml` `[hmi]` table parses into an
    /// [`HmiConfig`] and the builder injects exactly the `CONSORTIUM_HMI_*` env
    /// vars `consortium-hmi-webkit` reads at compile time.
    #[test]
    fn toml_hmi_section_round_trips_through_injected_env() {
        #[derive(serde::Deserialize)]
        struct Doc {
            hmi: HmiConfig,
        }
        let manifest = "\
            [hmi]\n\
            engine    = \"cog\"\n\
            dist      = \"dist\"\n\
            debug_url = \"http://localhost:5173\"\n";

        let doc: Doc = toml::from_str(manifest).expect("[hmi] should deserialize");
        let mut cmd = Command::new("true");
        inject_hmi(&mut cmd, Some(&doc.hmi));

        let vars = injected_vars(&cmd);
        assert_eq!(
            vars.get("CONSORTIUM_HMI_DIST").map(String::as_str),
            Some("dist")
        );
        assert_eq!(
            vars.get("CONSORTIUM_HMI_DEBUG_URL").map(String::as_str),
            Some("http://localhost:5173")
        );
    }

    // ── TEE ─────────────────────────────────────────────────────────────────

    /// End-to-end: a `Consortium.toml` `[tee]` table parses into a [`Tee`] whose
    /// `uuid` is a real [`Uuid`]. The OP-TEE compiler injects that UUID under
    /// [`TEE_UUID_ENV`] (`config.ta_uuid.to_string()`), and each TA's `build.rs`
    /// reads the same variable back, so the manifest, the builder, and the TA
    /// build script all agree on one value.
    #[test]
    fn toml_tee_section_round_trips_to_injected_uuid() {
        use consortium_cfg_common::config::{TEE_UUID_ENV, Tee};

        #[derive(serde::Deserialize)]
        struct Doc {
            tee: Tee,
        }
        let manifest = "[tee]\nuuid = \"12345678-1234-1234-1234-123456789012\"\n";
        let doc: Doc = toml::from_str(manifest).expect("[tee] should deserialize");

        // Mirror the env var the OP-TEE compiler sets from the parsed UUID, then
        // check it round-trips back to the manifest's canonical form.
        let mut cmd = Command::new("true");
        cmd.env(TEE_UUID_ENV, doc.tee.uuid.to_string());
        let vars = injected_vars(&cmd);
        assert_eq!(
            vars.get(TEE_UUID_ENV).map(String::as_str),
            Some("12345678-1234-1234-1234-123456789012")
        );
        assert_eq!(TEE_UUID_ENV, "CONSORTIUM_TEE_UUID");
    }

    // ── HMI build command resolution ─────────────────────────────────────────

    #[test]
    fn hmi_command_override_beats_path_tools() {
        // An explicit override is honoured verbatim even when `vp`/`npm` exist.
        let cmd = resolve_hmi_build_command_from(Some("pnpm build".to_string()), |_| {
            Some("/tools/ignored".to_string())
        })
        .expect("override resolves");
        assert_eq!(
            cmd,
            HmiBuildCommand::Custom(vec!["pnpm".to_string(), "build".to_string()])
        );
    }

    #[test]
    fn hmi_command_prefers_vp_then_npm() {
        // With no override, `vp` wins when present and keeps its resolved path.
        let cmd = resolve_hmi_build_command_from(None, |tool| Some(format!("/tools/{tool}")))
            .expect("vp resolves");
        assert_eq!(
            cmd,
            HmiBuildCommand::Resolved {
                program: "/tools/vp".to_string(),
                args: vec!["build".to_string()],
            }
        );

        // Falls back to `npm` when only it is on PATH.
        let cmd = resolve_hmi_build_command_from(None, |tool| {
            (tool == "npm").then(|| "/tools/npm".to_string())
        })
        .expect("npm resolves");
        assert_eq!(
            cmd,
            HmiBuildCommand::Resolved {
                program: "/tools/npm".to_string(),
                args: vec!["run".to_string(), "build".to_string()],
            }
        );
    }

    #[test]
    fn hmi_command_errors_when_nothing_available() {
        let err = resolve_hmi_build_command_from(None, |_| None)
            .expect_err("no tool and no override is an error");
        assert!(err.contains("CONSORTIUM_HMI_BUILD_CMD"));

        // An empty override is rejected rather than launching an empty program.
        let err = resolve_hmi_build_command_from(Some("   ".to_string()), |_| None)
            .expect_err("empty override is an error");
        assert!(err.contains("empty"));
    }

    #[test]
    fn pocket_command_runs_bun_build_with_outdir() {
        let cmd =
            resolve_pocket_build_command_from(None, "hero-main", Path::new("/proj/dist"), |tool| {
                (tool == "bun").then(|| "/tools/bun".to_string())
            })
            .expect("bun resolves");
        assert_eq!(
            cmd,
            HmiBuildCommand::Resolved {
                program: "/tools/bun".to_string(),
                args: vec![
                    "tools/build.ts".to_string(),
                    "hero-main".to_string(),
                    "--outdir=/proj/dist".to_string(),
                ],
            }
        );
    }

    #[test]
    fn pocket_command_override_beats_bun() {
        let cmd = resolve_pocket_build_command_from(
            Some("just pocket hero-main".to_string()),
            "hero-main",
            Path::new("/proj/dist"),
            |_| Some("/tools/ignored".to_string()),
        )
        .expect("override resolves");
        assert_eq!(
            cmd,
            HmiBuildCommand::Custom(vec![
                "just".to_string(),
                "pocket".to_string(),
                "hero-main".to_string(),
            ])
        );
    }

    #[test]
    fn pocket_command_errors_without_bun() {
        let err =
            resolve_pocket_build_command_from(None, "hero-main", Path::new("/proj/dist"), |_| None)
                .expect_err("missing bun is an error");
        assert!(err.contains("bun"));
    }

    #[test]
    fn injected_vars_helper_ignores_unrelated_env() {
        // Sanity check the test helper itself does not over-match.
        let mut cmd = Command::new("true");
        cmd.env(OsStr::new("PATH"), "/usr/bin");
        assert!(injected_vars(&cmd).is_empty());
    }
}