Files
IdeA/crates/app-tauri/tests/dto_agents.rs
Blomios 3ed0f6b45f feat(terminals): reprise de conversation par cellule + fix ordre d'écriture
Permet de recharger la conversation CLI précédente de chaque cellule à la
réouverture du projet, de façon universelle (indépendant du modèle/CLI).

- profil AgentRuntime: bloc déclaratif optionnel `session { assignFlag, resumeFlag }`
- LeafCell: `conversationId` (persistant, distinct du SessionId PTY) + `agentWasRunning`
- runtime: SessionPlan (None/Assign/Resume) + composition pure des args
- LaunchAgent: décide Assign vs Resume, génère l'UUID, remonte l'id assigné
  (persistance par l'appelant via setCellConversation — découplage SRP)
- close: SnapshotRunningAgents fige `agentWasRunning` avant le kill-all
  (statut clot/en cours universel, sans parsing CLI)
- SessionInspector: port optionnel best-effort + adapter ClaudeTranscriptInspector
- popup de reprise par cellule (statut + sujet/tokens si dispo), intercalée
  avant le Resume auto, jamais sur le chemin reattach

fix(terminals): sérialise les écritures PTY (file FIFO par handle) — corrige
les caractères mélangés/accents dus au réordonnancement des invoke Tauri concurrents

fix(layout): l'opération `move` préservait mal les champs du leaf (perdait `agent`)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-07 22:27:08 +02:00

300 lines
10 KiB
Rust

//! L6 tests for the agent DTO (de)serialisation contract: camelCase on the
//! wire, embedded [`Agent`] shape preserved, `parse_agent_id` error behaviour,
//! and `From<LaunchAgentOutput>` for [`TerminalSessionDto`].
use app_tauri_lib::dto::{
parse_agent_id, AgentDto, AgentListDto, ConversationDetailsDto, CreateAgentRequestDto,
InspectConversationRequestDto, LaunchAgentRequestDto, TerminalSessionDto,
UpdateAgentContextRequestDto,
};
use application::{
CreateAgentOutput, InspectConversationOutput, LaunchAgentOutput, ListAgentsOutput,
};
use domain::ports::ConversationDetails;
use domain::ids::{AgentId, NodeId, ProfileId, SessionId};
use domain::terminal::{PtySize, SessionKind, SessionStatus, TerminalSession};
use domain::{Agent, AgentOrigin, ProjectPath};
use serde_json::json;
use uuid::Uuid;
/// Helper: build a minimal validated [`Agent`].
fn make_agent(agent_uuid: u128, profile_uuid: u128) -> Agent {
Agent::new(
AgentId::from_uuid(Uuid::from_u128(agent_uuid)),
"My Agent",
"agents/my-agent.md",
ProfileId::from_uuid(Uuid::from_u128(profile_uuid)),
AgentOrigin::Scratch,
false,
)
.expect("valid agent")
}
// ---------------------------------------------------------------------------
// AgentDto / AgentListDto serialisation
// ---------------------------------------------------------------------------
#[test]
fn agent_dto_serialises_camelcase() {
let agent = make_agent(1, 2);
let dto = AgentDto(agent.clone());
let v = serde_json::to_value(&dto).unwrap();
assert_eq!(v["id"], agent.id.to_string());
assert_eq!(v["name"], "My Agent");
assert_eq!(v["contextPath"], "agents/my-agent.md", "camelCase key");
assert_eq!(v["profileId"], ProfileId::from_uuid(Uuid::from_u128(2)).to_string());
assert_eq!(v["synchronized"], false);
// origin: tagged `{ "type": "scratch" }`
assert_eq!(v["origin"]["type"], "scratch");
// no snake_case leak
assert!(v.get("context_path").is_none());
assert!(v.get("profile_id").is_none());
}
#[test]
fn agent_list_dto_is_transparent_array() {
let out = ListAgentsOutput {
agents: vec![make_agent(1, 2), make_agent(3, 4)],
};
let dto = AgentListDto::from(out);
let v = serde_json::to_value(&dto).unwrap();
let arr = v.as_array().expect("transparent array");
assert_eq!(arr.len(), 2);
assert_eq!(arr[0]["name"], "My Agent");
}
#[test]
fn create_agent_output_maps_to_agent_dto() {
let agent = make_agent(5, 6);
let out = CreateAgentOutput { agent: agent.clone() };
let dto = AgentDto::from(out);
assert_eq!(dto.0.id, agent.id);
}
// ---------------------------------------------------------------------------
// Request DTO deserialisation
// ---------------------------------------------------------------------------
#[test]
fn create_agent_request_deserialises_camelcase() {
let project_id = Uuid::from_u128(10).to_string();
let profile_id = Uuid::from_u128(20).to_string();
let raw = json!({
"projectId": project_id,
"name": "Backend Dev",
"profileId": profile_id,
"initialContent": "# Hello"
});
let dto: CreateAgentRequestDto = serde_json::from_value(raw).unwrap();
assert_eq!(dto.project_id, project_id);
assert_eq!(dto.name, "Backend Dev");
assert_eq!(dto.profile_id, profile_id);
assert_eq!(dto.initial_content.as_deref(), Some("# Hello"));
}
#[test]
fn create_agent_request_initial_content_defaults_to_none() {
let raw = json!({
"projectId": Uuid::nil().to_string(),
"name": "Agent",
"profileId": Uuid::nil().to_string()
});
let dto: CreateAgentRequestDto = serde_json::from_value(raw).unwrap();
assert!(dto.initial_content.is_none());
}
#[test]
fn update_agent_context_request_deserialises_camelcase() {
let raw = json!({
"projectId": Uuid::nil().to_string(),
"agentId": Uuid::nil().to_string(),
"content": "# Updated"
});
let dto: UpdateAgentContextRequestDto = serde_json::from_value(raw).unwrap();
assert_eq!(dto.content, "# Updated");
}
#[test]
fn launch_agent_request_deserialises_camelcase() {
let project_id = Uuid::from_u128(1).to_string();
let agent_id = Uuid::from_u128(2).to_string();
let raw = json!({
"projectId": project_id,
"agentId": agent_id,
"rows": 24,
"cols": 80
});
let dto: LaunchAgentRequestDto = serde_json::from_value(raw).unwrap();
assert_eq!(dto.rows, 24);
assert_eq!(dto.cols, 80);
assert_eq!(dto.project_id, project_id);
assert_eq!(dto.agent_id, agent_id);
// Omitting the resume id defaults to None (a fresh cell).
assert_eq!(dto.conversation_id, None);
}
#[test]
fn launch_agent_request_carries_conversation_id_for_resume() {
let raw = json!({
"projectId": Uuid::from_u128(1).to_string(),
"agentId": Uuid::from_u128(2).to_string(),
"rows": 24,
"cols": 80,
"conversationId": "resume-me"
});
let dto: LaunchAgentRequestDto = serde_json::from_value(raw).unwrap();
// The leaf's persisted id reaches the backend so the launch resumes (T4b).
assert_eq!(dto.conversation_id.as_deref(), Some("resume-me"));
}
// ---------------------------------------------------------------------------
// InspectConversation DTOs (T7)
// ---------------------------------------------------------------------------
#[test]
fn inspect_conversation_request_deserialises_camelcase() {
let raw = json!({
"projectId": Uuid::from_u128(1).to_string(),
"agentId": Uuid::from_u128(2).to_string(),
"conversationId": "conv-7"
});
let dto: InspectConversationRequestDto = serde_json::from_value(raw).unwrap();
assert_eq!(dto.project_id, Uuid::from_u128(1).to_string());
assert_eq!(dto.agent_id, Uuid::from_u128(2).to_string());
assert_eq!(dto.conversation_id, "conv-7");
}
#[test]
fn conversation_details_dto_serialises_camelcase_when_present() {
let out = InspectConversationOutput {
details: ConversationDetails {
last_topic: Some("refactor parser".to_owned()),
token_count: Some(1234),
},
};
let dto = ConversationDetailsDto::from(out);
let v = serde_json::to_value(&dto).unwrap();
assert_eq!(v["lastTopic"], "refactor parser");
assert_eq!(v["tokenCount"], 1234);
// No snake_case leak.
assert!(v.get("last_topic").is_none());
assert!(v.get("token_count").is_none());
}
#[test]
fn conversation_details_dto_omits_fields_when_none() {
let out = InspectConversationOutput {
details: ConversationDetails {
last_topic: None,
token_count: None,
},
};
let dto = ConversationDetailsDto::from(out);
let v = serde_json::to_value(&dto).unwrap();
// Both optional fields are omitted from the wire (absent, not null).
assert!(v.get("lastTopic").is_none(), "absent topic ⇒ field omitted");
assert!(v.get("tokenCount").is_none(), "absent tokens ⇒ field omitted");
assert_eq!(v, json!({}), "fully degraded ⇒ empty object");
}
// ---------------------------------------------------------------------------
// parse_agent_id
// ---------------------------------------------------------------------------
#[test]
fn parse_agent_id_accepts_uuid() {
let id = Uuid::from_u128(99);
assert_eq!(
parse_agent_id(&id.to_string()).unwrap(),
AgentId::from_uuid(id)
);
}
#[test]
fn parse_agent_id_rejects_garbage() {
let err = parse_agent_id("not-a-uuid").expect_err("garbage rejected");
assert_eq!(err.code, "INVALID");
}
// ---------------------------------------------------------------------------
// From<LaunchAgentOutput> for TerminalSessionDto
// ---------------------------------------------------------------------------
#[test]
fn launch_agent_output_maps_to_terminal_session_dto() {
let session_id = SessionId::from_uuid(Uuid::from_u128(7));
let node_id = NodeId::from_uuid(Uuid::from_u128(8));
let agent_id = AgentId::from_uuid(Uuid::from_u128(9));
let cwd = ProjectPath::new("/tmp/project".to_owned()).expect("valid path");
let size = PtySize::new(24, 80).unwrap();
let mut session = TerminalSession::starting(
session_id,
node_id,
cwd,
SessionKind::Agent { agent_id },
size,
);
session.status = SessionStatus::Running;
let out = LaunchAgentOutput {
session,
assigned_conversation_id: None,
};
let dto = TerminalSessionDto::from(out);
assert_eq!(dto.session_id, session_id.to_string());
assert_eq!(dto.cwd, "/tmp/project");
assert_eq!(dto.rows, 24);
assert_eq!(dto.cols, 80);
// Nothing assigned ⇒ no conversation id surfaced.
assert_eq!(dto.assigned_conversation_id, None);
// Also verify camelCase serialisation.
let v = serde_json::to_value(&dto).unwrap();
assert_eq!(v["sessionId"], session_id.to_string());
assert_eq!(v["cwd"], "/tmp/project");
assert!(v.get("session_id").is_none(), "no snake_case leak");
// Absent assignment must be omitted from the wire payload entirely.
assert!(
v.get("assignedConversationId").is_none(),
"no assignment ⇒ field omitted"
);
}
#[test]
fn launch_agent_output_propagates_assigned_conversation_id() {
let session_id = SessionId::from_uuid(Uuid::from_u128(70));
let node_id = NodeId::from_uuid(Uuid::from_u128(80));
let agent_id = AgentId::from_uuid(Uuid::from_u128(90));
let cwd = ProjectPath::new("/tmp/project".to_owned()).expect("valid path");
let size = PtySize::new(24, 80).unwrap();
let mut session = TerminalSession::starting(
session_id,
node_id,
cwd,
SessionKind::Agent { agent_id },
size,
);
session.status = SessionStatus::Running;
let out = LaunchAgentOutput {
session,
assigned_conversation_id: Some("conv-xyz".to_owned()),
};
let dto = TerminalSessionDto::from(out);
// The id minted by the launch is carried through to the DTO …
assert_eq!(dto.assigned_conversation_id.as_deref(), Some("conv-xyz"));
// … and serialised in camelCase for the front to persist on the leaf.
let v = serde_json::to_value(&dto).unwrap();
assert_eq!(v["assignedConversationId"], "conv-xyz");
assert!(
v.get("assigned_conversation_id").is_none(),
"no snake_case leak"
);
}