use std::path::PathBuf;
use velveteen::{ForgeCommand, ForgeSpec, Initiator, MeshAccess, TaskLocation, TaskRuntime};
use velveteen_exec::transforms::{RecipeLocation, RecipePlacement, RecipeStep, TransformRecipe};
use workload_spec::{BuildConfig, BuildMode, ImageRef};
use super::mesofact_static::lower_build_to_forge_spec;
use super::static_asset::lower_recipe_step_to_forge_spec;
const GOLDEN_DIGEST: &str =
"sha256:1111111111111111111111111111111111111111111111111111111111111111";
fn golden_image() -> ImageRef {
ImageRef {
registry: "ghcr.io".into(),
repository: "yah/transform-tool".into(),
tag: "v1.0.0".into(),
digest: GOLDEN_DIGEST.into(),
}
}
fn golden_recipe() -> TransformRecipe {
TransformRecipe {
name: "whisper-quantize".into(),
label: "Whisper Q5_K quantize".into(),
placement: RecipePlacement {
location: RecipeLocation::Local,
runtime: TaskRuntime::Container,
platform: None,
},
image: golden_image(),
steps: vec![RecipeStep {
name: "quantize".into(),
argv: vec![
"./quantize".into(),
"{{YAH_TRANSFORM_IN_0}}".into(),
"{{YAH_TRANSFORM_OUT}}".into(),
"q5_k".into(),
],
timeout: 600,
}],
admission: None,
secrets: vec![],
}
}
fn golden_build_in_container() -> (BuildConfig, BuildMode) {
(
BuildConfig {
command: Some("bun run build".into()),
out_dir: PathBuf::from("dist"),
render_command: None,
},
BuildMode::InContainer {
image: golden_image(),
},
)
}
fn golden_build_host_side() -> (BuildConfig, BuildMode) {
(
BuildConfig {
command: Some("bun run build".into()),
out_dir: PathBuf::from("dist"),
render_command: None,
},
BuildMode::HostSide,
)
}
fn assert_subprocess_local_container_pinned(spec: &ForgeSpec) {
assert_eq!(
spec.where_.location,
TaskLocation::Local,
"lowering must target the local quadrant"
);
assert_eq!(
spec.where_.runtime,
TaskRuntime::Container,
"lowering must target the container runtime"
);
assert_eq!(
spec.mesh_access,
MeshAccess::None,
"neither consumer requests mesh access"
);
match &spec.command {
ForgeCommand::Subprocess { image, .. } => {
let image = image
.as_ref()
.expect("container quadrant requires an image");
assert!(
image.digest.starts_with("sha256:"),
"image digest must be content-addressed; got {:?}",
image.digest
);
assert!(
!image.digest.trim_start_matches("sha256:").is_empty(),
"digest body must be non-empty"
);
}
other => panic!("expected ForgeCommand::Subprocess, got {other:?}"),
}
}
#[test]
fn golden_recipe_step_lowers_to_pinned_local_container_subprocess() {
let recipe = golden_recipe();
let step = &recipe.steps[0];
let argv = vec![
"./quantize".into(),
"/tmp/cache/in.bin".into(),
"/tmp/out/whisper.q5_k.bin".into(),
"q5_k".into(),
];
let spec = lower_recipe_step_to_forge_spec(&recipe, step, argv.clone());
assert_subprocess_local_container_pinned(&spec);
match spec.command {
ForgeCommand::Subprocess {
argv: spec_argv,
image,
} => {
assert_eq!(spec_argv, argv);
let image = image.unwrap();
assert_eq!(image.registry, "ghcr.io");
assert_eq!(image.repository, "yah/transform-tool");
assert_eq!(image.tag, "v1.0.0");
assert_eq!(image.digest, GOLDEN_DIGEST);
}
other => panic!("expected Subprocess, got {other:?}"),
}
let timeout = spec.timeout.expect("non-zero step.timeout maps to Some");
assert_eq!(timeout.as_ms(), 600 * 1000);
assert_eq!(
spec.label.as_deref(),
Some("transform:whisper-quantize:quantize")
);
match spec.initiator {
Initiator::Gnome { camp, shift } => {
assert_eq!(camp, "static-asset-reconciler");
assert_eq!(shift, "derive-whisper-quantize");
}
other => panic!("expected Gnome initiator, got {other:?}"),
}
}
#[test]
fn golden_recipe_step_with_zero_timeout_lowers_to_none() {
let mut recipe = golden_recipe();
recipe.steps[0].timeout = 0;
let step = &recipe.steps[0];
let spec = lower_recipe_step_to_forge_spec(&recipe, step, vec!["./quantize".into()]);
assert!(
spec.timeout.is_none(),
"step.timeout=0 must lower to None (no wall-clock cap)"
);
}
#[test]
fn golden_remote_recipe_step_keeps_its_placement() {
let mut recipe = golden_recipe();
recipe.placement.location = RecipeLocation::RemoteAny {
tier: workload_spec::TierTag("infra".into()),
mesh_tags: vec!["arch:x86".into()],
};
let step = &recipe.steps[0];
let spec = lower_recipe_step_to_forge_spec(&recipe, step, vec!["./quantize".into()]);
assert_eq!(
spec.where_.location,
TaskLocation::RemoteAny {
tier: workload_spec::TierTag("infra".into()),
mesh_tags: vec!["arch:x86".into()],
},
"recipe placement must lower straight through, not re-pin to Local"
);
assert_eq!(spec.where_.runtime, TaskRuntime::Container);
}
#[test]
fn golden_node_pinned_recipe_step_keeps_its_placement() {
let mut recipe = golden_recipe();
recipe.placement.location = RecipeLocation::Remote {
node: workload_spec::MeshIdent("us-west-002".into()),
};
let step = &recipe.steps[0];
let spec = lower_recipe_step_to_forge_spec(&recipe, step, vec!["./quantize".into()]);
assert_eq!(
spec.where_.location,
TaskLocation::Remote {
node: workload_spec::MeshIdent("us-west-002".into()),
}
);
}
#[test]
fn golden_build_in_container_lowers_to_pinned_local_container_subprocess() {
let workload_dir = PathBuf::from("/workspace/app/web");
let (build, mode) = golden_build_in_container();
let spec = lower_build_to_forge_spec(&workload_dir, &build, &mode)
.expect("a golden fixture declares a build.command");
assert_subprocess_local_container_pinned(&spec);
match spec.command {
ForgeCommand::Subprocess { argv, image } => {
assert_eq!(
argv,
vec!["sh".to_string(), "-c".into(), "bun run build".into()]
);
let image = image.unwrap();
assert_eq!(image.registry, "ghcr.io");
assert_eq!(image.repository, "yah/transform-tool");
assert_eq!(image.digest, GOLDEN_DIGEST);
}
other => panic!("expected Subprocess, got {other:?}"),
}
assert!(spec.timeout.is_none(), "build lowering carries no timeout");
assert_eq!(
spec.label.as_deref(),
Some("mesofact-static-build:/workspace/app/web")
);
match spec.initiator {
Initiator::Gnome { camp, shift } => {
assert_eq!(camp, "mesofact-static-reconciler");
assert_eq!(shift, "build");
}
other => panic!("expected Gnome initiator, got {other:?}"),
}
}
#[test]
fn golden_build_host_side_lowers_to_native_quadrant_without_image() {
let workload_dir = PathBuf::from("/workspace/app/web");
let (build, mode) = golden_build_host_side();
let spec = lower_build_to_forge_spec(&workload_dir, &build, &mode)
.expect("a golden fixture declares a build.command");
assert_eq!(spec.where_.location, TaskLocation::Local);
assert_eq!(
spec.where_.runtime,
TaskRuntime::Native,
"host_side must target the native runtime"
);
match spec.command {
ForgeCommand::Subprocess { image, .. } => {
assert!(
image.is_none(),
"host_side must carry no image (Native runtime); got {image:?}"
);
}
other => panic!("expected Subprocess, got {other:?}"),
}
}
#[test]
fn parity_recipe_and_build_in_container_share_quadrant() {
let recipe = golden_recipe();
let recipe_spec = lower_recipe_step_to_forge_spec(
&recipe,
&recipe.steps[0],
vec!["./quantize".into(), "in".into(), "out".into()],
);
let workload_dir = PathBuf::from("/workspace/app/web");
let (build, mode) = golden_build_in_container();
let build_spec = lower_build_to_forge_spec(&workload_dir, &build, &mode)
.expect("a golden fixture declares a build.command");
assert_subprocess_local_container_pinned(&recipe_spec);
assert_subprocess_local_container_pinned(&build_spec);
assert_eq!(recipe_spec.where_, build_spec.where_);
let recipe_image = match &recipe_spec.command {
ForgeCommand::Subprocess { image, .. } => image.as_ref().unwrap(),
_ => unreachable!(),
};
let build_image = match &build_spec.command {
ForgeCommand::Subprocess { image, .. } => image.as_ref().unwrap(),
_ => unreachable!(),
};
assert!(
recipe_image.digest.starts_with("sha256:") && build_image.digest.starts_with("sha256:"),
"both lowerings must emit sha256-pinned images"
);
assert!(matches!(recipe_spec.initiator, Initiator::Gnome { .. }));
assert!(matches!(build_spec.initiator, Initiator::Gnome { .. }));
}