use crate::encoding::encode_world;
use anyhow::{Context, Result};
use wasm_encoder::{ComponentBuilder, ComponentExportKind, ComponentTypeRef};
use wasmparser::Validator;
use wit_parser::{Resolve, WorldId};
pub fn targets(resolve: &Resolve, world: WorldId, component_to_test: &[u8]) -> Result<()> {
let mut root_component = ComponentBuilder::default();
let component_to_test_idx = root_component.component_raw(component_to_test);
let test_component_idx = {
let component_ty = encode_world(resolve, world)?;
let mut component = ComponentBuilder::default();
let component_ty_idx = component.type_component(&component_ty);
component.import(
&resolve.worlds[world].name,
ComponentTypeRef::Component(component_ty_idx),
);
root_component.component(component)
};
let args: Vec<(String, ComponentExportKind, u32)> = vec![(
resolve.worlds[world].name.clone(),
ComponentExportKind::Component,
component_to_test_idx,
)];
root_component.instantiate(test_component_idx, args);
let bytes = root_component.finish();
Validator::new()
.validate_all(&bytes)
.context("failed to validate encoded bytes")?;
Ok(())
}