|
| 1 | +// Copyright (c) 2025 RBB S.r.l |
| 2 | + |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// Licensed under the MIT License; |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +//! Speculos runtime handle, provides out-of-band interaction with a simulator instance |
| 17 | +//! via the |
| 18 | +//! [HTTP API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/LedgerHQ/speculos/master/speculos/api/static/swagger/swagger.json) |
| 19 | +//! to allow button pushes and screenshots when executing integration tests. |
| 20 | +//! |
| 21 | +//! |
| 22 | +
|
| 23 | +use std::net::SocketAddr; |
| 24 | + |
| 25 | +use logging::log; |
| 26 | +use reqwest::Client; |
| 27 | +use serde::{Deserialize, Serialize}; |
| 28 | +use strum::{Display, EnumIter}; |
| 29 | + |
| 30 | +/// Button enumeration |
| 31 | +#[derive(Clone, Copy, PartialEq, Debug, Display, EnumIter)] |
| 32 | +#[strum(serialize_all = "kebab-case")] |
| 33 | +pub enum Button { |
| 34 | + Left, |
| 35 | + Right, |
| 36 | + Both, |
| 37 | +} |
| 38 | + |
| 39 | +/// Button actions |
| 40 | +#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize, Display, EnumIter)] |
| 41 | +#[serde(rename_all = "kebab-case")] |
| 42 | +pub enum Action { |
| 43 | + Press, |
| 44 | + Release, |
| 45 | + PressAndRelease, |
| 46 | +} |
| 47 | + |
| 48 | +/// Button action object for serialization and use with the HTTP API |
| 49 | +#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)] |
| 50 | +struct ButtonAction { |
| 51 | + action: Action, |
| 52 | +} |
| 53 | + |
| 54 | +/// Handle for interacting with a Speculos instance |
| 55 | +#[derive(Debug)] |
| 56 | +pub struct Handle { |
| 57 | + addr: SocketAddr, |
| 58 | +} |
| 59 | + |
| 60 | +impl Handle { |
| 61 | + pub fn new(addr: SocketAddr) -> Self { |
| 62 | + Self { addr } |
| 63 | + } |
| 64 | + |
| 65 | + /// Get speculos HTTP address |
| 66 | + pub fn addr(&self) -> SocketAddr { |
| 67 | + self.addr |
| 68 | + } |
| 69 | + |
| 70 | + /// Send a button action to the simulator |
| 71 | + pub async fn button(&self, button: Button, action: Action) -> anyhow::Result<()> { |
| 72 | + log::debug!("Sending button request: {}:{}", button, action); |
| 73 | + |
| 74 | + // Post action to HTTP API |
| 75 | + let r = Client::new() |
| 76 | + .post(format!("http://{}/button/{}", self.addr(), button)) |
| 77 | + .json(&ButtonAction { action }) |
| 78 | + .send() |
| 79 | + .await?; |
| 80 | + |
| 81 | + log::debug!("Button request complete: {}", r.status()); |
| 82 | + |
| 83 | + Ok(()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod tests { |
| 89 | + use super::*; |
| 90 | + use strum::IntoEnumIterator; |
| 91 | + |
| 92 | + /// Check button string encoding |
| 93 | + #[test] |
| 94 | + fn button_encoding() { |
| 95 | + for button in Button::iter() { |
| 96 | + let expected = match button { |
| 97 | + Button::Left => "left", |
| 98 | + Button::Right => "right", |
| 99 | + Button::Both => "both", |
| 100 | + }; |
| 101 | + assert_eq!(&button.to_string(), expected); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// Check button action encoding |
| 106 | + #[test] |
| 107 | + fn action_encoding() { |
| 108 | + for action in Action::iter() { |
| 109 | + let expected = match action { |
| 110 | + Action::Press => r#"{"action":"press"}"#, |
| 111 | + Action::Release => r#"{"action":"release"}"#, |
| 112 | + Action::PressAndRelease => r#"{"action":"press-and-release"}"#, |
| 113 | + }; |
| 114 | + assert_eq!( |
| 115 | + &serde_json::to_string(&ButtonAction { action }).unwrap(), |
| 116 | + expected |
| 117 | + ); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments