188 行
5.9 KiB
Rust
188 行
5.9 KiB
Rust
use desktop_protocol::{ActiveSlotKind, ConnectionSlot};
|
|
use serde::{Deserialize, Serialize};
|
|
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
|
|
use uuid::Uuid;
|
|
|
|
use crate::database::{ApplicationDatabase, DatabaseError};
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
pub struct AssignConnectionSlotRequest {
|
|
pub slot: ActiveSlotKind,
|
|
pub profile_id: String,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SlotCommandError {
|
|
code: &'static str,
|
|
message: &'static str,
|
|
}
|
|
|
|
impl SlotCommandError {
|
|
fn validation(message: &'static str) -> Self {
|
|
Self {
|
|
code: "validationFailed",
|
|
message,
|
|
}
|
|
}
|
|
|
|
fn database(error: DatabaseError) -> Self {
|
|
match error {
|
|
DatabaseError::ProfileNotFound => Self {
|
|
code: "profileNotFound",
|
|
message: "The selected connection profile no longer exists.",
|
|
},
|
|
DatabaseError::ProfileAlreadyAssigned => Self {
|
|
code: "profileAlreadyAssigned",
|
|
message: "One connection profile cannot occupy both active slots.",
|
|
},
|
|
DatabaseError::SlotNotFound | DatabaseError::InvalidStoredSlot => Self {
|
|
code: "invalidSlotState",
|
|
message: "The local connection slot state is invalid.",
|
|
},
|
|
_ => Self {
|
|
code: "databaseUnavailable",
|
|
message: "The local connection slot database is unavailable.",
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn list_connection_slots(
|
|
database: tauri::State<'_, ApplicationDatabase>,
|
|
) -> Result<Vec<ConnectionSlot>, SlotCommandError> {
|
|
list_slots(&database)
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn assign_connection_slot(
|
|
database: tauri::State<'_, ApplicationDatabase>,
|
|
request: AssignConnectionSlotRequest,
|
|
) -> Result<ConnectionSlot, SlotCommandError> {
|
|
assign_slot(&database, request)
|
|
}
|
|
|
|
#[tauri::command]
|
|
pub fn clear_connection_slot(
|
|
database: tauri::State<'_, ApplicationDatabase>,
|
|
slot: ActiveSlotKind,
|
|
) -> Result<ConnectionSlot, SlotCommandError> {
|
|
clear_slot(&database, slot)
|
|
}
|
|
|
|
fn list_slots(database: &ApplicationDatabase) -> Result<Vec<ConnectionSlot>, SlotCommandError> {
|
|
database
|
|
.list_connection_slots()
|
|
.map_err(SlotCommandError::database)
|
|
}
|
|
|
|
fn assign_slot(
|
|
database: &ApplicationDatabase,
|
|
request: AssignConnectionSlotRequest,
|
|
) -> Result<ConnectionSlot, SlotCommandError> {
|
|
validate_profile_id(&request.profile_id)?;
|
|
database
|
|
.assign_connection_slot(request.slot, &request.profile_id, ×tamp()?)
|
|
.map_err(SlotCommandError::database)
|
|
}
|
|
|
|
fn clear_slot(
|
|
database: &ApplicationDatabase,
|
|
slot: ActiveSlotKind,
|
|
) -> Result<ConnectionSlot, SlotCommandError> {
|
|
database
|
|
.clear_connection_slot(slot, ×tamp()?)
|
|
.map_err(SlotCommandError::database)
|
|
}
|
|
|
|
fn validate_profile_id(id: &str) -> Result<(), SlotCommandError> {
|
|
if Uuid::parse_str(id).is_err() {
|
|
return Err(SlotCommandError::validation(
|
|
"Connection profile ID is invalid.",
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn timestamp() -> Result<String, SlotCommandError> {
|
|
OffsetDateTime::now_utc()
|
|
.format(&Rfc3339)
|
|
.map_err(|_| SlotCommandError {
|
|
code: "clockUnavailable",
|
|
message: "The system clock could not produce a slot timestamp.",
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use desktop_protocol::{AstrBotProfileRole, AuthenticationType, ConnectionProfile};
|
|
|
|
use super::*;
|
|
|
|
fn profile(index: usize) -> ConnectionProfile {
|
|
ConnectionProfile {
|
|
id: format!("00000000-0000-4000-8000-{index:012}"),
|
|
display_name: format!("Profile {index}"),
|
|
role: AstrBotProfileRole::General,
|
|
base_url: format!("https://example-{index}.test/"),
|
|
event_stream_url: None,
|
|
authentication_type: AuthenticationType::DeviceToken,
|
|
credential_reference: None,
|
|
allow_insecure_http: false,
|
|
pinned_certificate_fingerprint: None,
|
|
auto_connect_slot: None,
|
|
auto_connect: false,
|
|
server_instance_id: None,
|
|
server_version: None,
|
|
protocol_version: None,
|
|
local_tool_policy_id: None,
|
|
last_connected_at: None,
|
|
created_at: "2026-07-19T00:00:00Z".into(),
|
|
updated_at: "2026-07-19T00:00:00Z".into(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn assigns_and_clears_slots_through_the_bounded_service() {
|
|
let database = ApplicationDatabase::in_memory().expect("database");
|
|
let companion = profile(1);
|
|
database.create_profile(&companion).expect("create profile");
|
|
|
|
let assigned = assign_slot(
|
|
&database,
|
|
AssignConnectionSlotRequest {
|
|
slot: ActiveSlotKind::Companion,
|
|
profile_id: companion.id.clone(),
|
|
},
|
|
)
|
|
.expect("assign slot");
|
|
assert_eq!(assigned.profile_id.as_deref(), Some(companion.id.as_str()));
|
|
|
|
let cleared = clear_slot(&database, ActiveSlotKind::Companion).expect("clear slot");
|
|
assert!(cleared.profile_id.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_unknown_fields_and_malformed_profile_ids() {
|
|
let parsed = serde_json::from_value::<AssignConnectionSlotRequest>(serde_json::json!({
|
|
"slot": "companion",
|
|
"profileId": "00000000-0000-4000-8000-000000000001",
|
|
"token": "[REDACTED]"
|
|
}));
|
|
assert!(parsed.is_err());
|
|
|
|
let database = ApplicationDatabase::in_memory().expect("database");
|
|
assert!(
|
|
assign_slot(
|
|
&database,
|
|
AssignConnectionSlotRequest {
|
|
slot: ActiveSlotKind::Operations,
|
|
profile_id: "not-a-uuid".into(),
|
|
},
|
|
)
|
|
.is_err()
|
|
);
|
|
}
|
|
}
|