Files
khadhroony-bot3/kb-lib/tests/external_executor_api.rs
2026-07-24 17:14:02 +02:00

96 lines
3.3 KiB
Rust

// file: kb-lib/tests/external_executor_api.rs
// version: 2
//! Downstream-style compilation contract for externally implemented executors.
struct ExternalExecutor;
struct ExternalTypedExecutor;
impl kb_lib::ExApiInstructionExecutor for ExternalExecutor {
fn executor_name(&self) -> &'static str {
return "external_executor";
}
fn executor_version(&self) -> &'static str {
return "1";
}
fn program_ids(&self) -> &'static [&'static str] {
return &[];
}
fn supports_request(
&self,
_request: &kb_lib::ExApiExecutionRequest,
) -> kb_lib::ExApiExecutionSupport {
return kb_lib::ExApiExecutionSupport::No;
}
fn build_plan(
&self,
request: &kb_lib::ExApiExecutionRequest,
) -> kb_core::Result<kb_lib::ExApiExecutionPlan> {
return std::result::Result::Ok(kb_lib::ExApiExecutionPlan {
executor_name: self.executor_name().to_string(),
instruction_count: 0,
payload_json: request.payload_json.clone(),
});
}
}
impl kb_lib::ExApiTypedInstructionExecutor for ExternalTypedExecutor {
type Intent = kb_lib::MdPubkey;
fn capability(
&self,
_program_id: &kb_lib::MdProgramId,
operation_code: &str,
) -> kb_lib::ExApiExecutionCapability {
return kb_lib::ExApiExecutionCapability::unsupported("external_executor", operation_code);
}
fn build_prepared_plan(
&self,
intent: &Self::Intent,
) -> kb_core::Result<kb_lib::ExApiPreparedExecutionPlan> {
return std::result::Result::Ok(kb_lib::ExApiPreparedExecutionPlan {
executor_name: "external_typed_executor".to_string(),
executor_version: "1".to_string(),
intent_id: "external-intent".to_string(),
operation_code: "external-operation".to_string(),
fee_payer: intent.clone(),
instructions: std::vec::Vec::new(),
required_signers: std::vec::Vec::new(),
policy: kb_lib::ExApiExecutionPolicy::default(),
requested_spend_lamports: 0,
requested_compute_unit_price_micro_lamports: std::option::Option::None,
});
}
}
#[test]
fn external_executor_uses_only_the_public_kb_lib_contract() {
let executor: &dyn kb_lib::ExApiInstructionExecutor = &ExternalExecutor;
assert_eq!(executor.executor_name(), "external_executor");
assert_eq!(executor.executor_version(), "1");
assert!(executor.program_ids().is_empty());
let typed_executor = ExternalTypedExecutor;
let program_id = kb_lib::MdProgramId("external-program".to_string());
let capability = kb_lib::ExApiTypedInstructionExecutor::capability(
&typed_executor,
&program_id,
"external-operation",
);
assert!(!capability.is_supported());
let fee_payer = kb_lib::MdPubkey("external-fee-payer".to_string());
let plan =
kb_lib::ExApiTypedInstructionExecutor::build_prepared_plan(&typed_executor, &fee_payer)
.unwrap_or_else(|error| panic!("external typed executor failed: {error}"));
assert_eq!(plan.fee_payer, fee_payer);
assert!(plan.instructions.is_empty());
assert!(plan.required_signers.is_empty());
assert_eq!(plan.policy.simulation, kb_lib::ExApiExecutionSimulationPolicy::Required);
assert!(plan.policy.dry_run);
}