use desktop_protocol::{ ActiveSlotKind, BoundGatewayEvent, GatewayEventBody, PROTOCOL_VERSION, ProtocolError, RawProtocolEnvelope, RiskLevel, ToolProposal, }; use serde_json::json; fn raw_event(payload: serde_json::Value) -> RawProtocolEnvelope { RawProtocolEnvelope { protocol_version: PROTOCOL_VERSION.to_owned(), event_type: "system.heartbeat".to_owned(), event_id: "shared-event-id".to_owned(), request_id: None, server_profile_hint: Some("spoofed-operations-profile".to_owned()), server_instance_id: "server-companion".to_owned(), conversation_id: Some("same-conversation".to_owned()), message_id: None, session_id: None, timestamp: "2026-01-01T00:00:00Z".to_owned(), sequence: Some(1), payload, } } #[test] fn local_profile_binding_ignores_server_profile_hint() { let bound = BoundGatewayEvent::bind( raw_event(json!({ "intervalMs": 1_000 })), "local-companion-profile", ActiveSlotKind::Companion, "server-companion", ) .expect("valid event should bind"); assert_eq!(bound.profile_id, "local-companion-profile"); assert_eq!(bound.slot, ActiveSlotKind::Companion); assert_eq!( bound.event.metadata.server_profile_hint.as_deref(), Some("spoofed-operations-profile") ); assert!(matches!(bound.event.body, GatewayEventBody::Heartbeat(_))); } #[test] fn server_instance_mismatch_is_rejected() { let error = BoundGatewayEvent::bind( raw_event(json!({ "intervalMs": 1_000 })), "local-profile", ActiveSlotKind::Companion, "different-server", ) .expect_err("changed server identity must be rejected"); assert!(matches!( error, ProtocolError::ServerInstanceMismatch { .. } )); } #[test] fn unsupported_event_type_is_explicit_and_non_panicking() { let mut raw = raw_event(json!({})); raw.event_type = "future.event".to_owned(); let error = desktop_protocol::GatewayEvent::try_from(raw) .expect_err("unknown event should be surfaced to the caller"); assert!(matches!(error, ProtocolError::UnsupportedEventType { .. })); } #[test] fn tool_proposal_rejects_unknown_outer_fields() { let value = json!({ "actionId": "action-1", "toolName": "query_recent_event_logs", "reason": "Inspect an application failure", "riskLevel": "read", "arguments": { "provider": "Application", "maxEntries": 20 }, "requestedAt": "2026-01-01T00:00:00Z", "expiresAt": "2026-01-01T00:05:00Z", "requiresApproval": true, "executeCommand": "not-allowed" }); assert!(serde_json::from_value::(value).is_err()); } #[test] fn tool_risk_level_uses_stable_wire_values() { assert_eq!( serde_json::to_value(RiskLevel::ReversibleWrite).unwrap(), json!("reversible-write") ); }