| 1 | mod hf; |
| 2 | mod model; |
| 3 | mod modeldb; |
| 4 | mod models; |
| 5 | mod profile; |
| 6 | mod provider; |
| 7 | mod setup; |
| 8 | |
| 9 | pub use hf::HfCmd; |
| 10 | pub use model::ModelCmd; |
| 11 | pub use modeldb::ModelDbCmd; |
| 12 | pub use models::ModelsCmd; |
| 13 | pub use profile::ProfileCmd; |
| 14 | pub use provider::ProviderCmd; |
| 15 | pub use setup::SetupCmd; |
| 16 | |
| 17 | #[cfg(test)] |
| 18 | mod tests { |
| 19 | use super::*; |
| 20 | use codewhale_command_contract::handler::{CommandContexts, CommandHandler}; |
| 21 | use codewhale_command_contract::metadata::RegisterCommand; |
| 22 | use codewhale_command_contract::model_setup::{ |
| 23 | ModelAction, ProviderAction, SetupAction, SetupStep, |
| 24 | }; |
| 25 | use codewhale_command_contract::outcome::CommandResult; |
| 26 | |
| 27 | struct ModelPicker; |
| 28 | impl TryFrom<ModelAction<'_>> for ModelPicker { |
| 29 | type Error = String; |
| 30 | fn try_from(action: ModelAction<'_>) -> Result<Self, String> { |
| 31 | matches!(action, ModelAction::OpenPicker) |
| 32 | .then_some(Self) |
| 33 | .ok_or_else(|| "expected model picker".into()) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | struct ProviderPicker; |
| 38 | impl TryFrom<ProviderAction<'_>> for ProviderPicker { |
| 39 | type Error = String; |
| 40 | fn try_from(action: ProviderAction<'_>) -> Result<Self, String> { |
| 41 | matches!(action, ProviderAction::OpenPicker) |
| 42 | .then_some(Self) |
| 43 | .ok_or_else(|| "expected provider picker".into()) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | struct RuntimeSetup; |
| 48 | impl TryFrom<SetupAction<'_>> for RuntimeSetup { |
| 49 | type Error = String; |
| 50 | fn try_from(action: SetupAction<'_>) -> Result<Self, String> { |
| 51 | matches!( |
| 52 | action, |
| 53 | SetupAction::OpenWizard(Some(SetupStep::RemoteRuntime)) |
| 54 | ) |
| 55 | .then_some(Self) |
| 56 | .ok_or_else(|| "expected remote setup".into()) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn commands_accept_hosts_with_only_their_own_output_contract() { |
| 62 | fn run<A, C: RegisterCommand<CommandResult<A>>>(arg: Option<&str>) -> CommandResult<A> { |
| 63 | match C::handler() { |
| 64 | CommandHandler::Pure(handler) => handler(arg), |
| 65 | CommandHandler::Contextual { handler, .. } => { |
| 66 | handler(CommandContexts::empty(), arg) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | assert!(run::<ModelPicker, ModelCmd>(None).action.is_some()); |
| 71 | assert!(run::<ProviderPicker, ProviderCmd>(None).action.is_some()); |
| 72 | assert!( |
| 73 | run::<RuntimeSetup, SetupCmd>(Some("phone")) |
| 74 | .action |
| 75 | .is_some() |
| 76 | ); |
| 77 | assert!(run::<ModelPicker, ModelCmd>(Some("auto")).is_error); |
| 78 | assert!(run::<ProviderPicker, ProviderCmd>(Some("fallback")).is_error); |
| 79 | } |
| 80 | } |
| 81 |