//! Tests: unit tables + in-process integration (mock OpenRouter + mock //! runner + temp git repos). //! //! Run: `cargo test -p agentd`. #[cfg(test)] mod unit { use crate::auth::{origin_allowed, session_token_from_cookie, Auth}; use crate::models::{ branch_name, cap_final_diff, cap_plan_markdown, plan_gate_message, render_todos, slugify, truncate_result, trim_placeholder, validate_commit_message, validate_commit_message_for_issue, validate_todos, TodoItem, FINAL_DIFF_CAP_BYTES, MAX_TODOS, MAX_TODO_CHARS, PLAN_MD_CAP_BYTES, RESULT_TRUNCATE_BYTES, INBOX_PLAN_APPROVE, INBOX_PLAN_CHANGES, INBOX_REVIEW_CHANGES, PENDING_PLAN, PENDING_REVIEW, }; use crate::openrouter::{parse_chat_response, Message}; use crate::agent::{format_pending_answer, trim_messages}; use crate::markdown::render_markdown; use crate::models::{ Event, Pending, Task, TaskStatus, EVENT_ASSISTANT, EVENT_STATUS, EVENT_TODOS, EVENT_TOOL_CALL, EVENT_TOOL_RESULT, EVENT_USER_MESSAGE, PENDING_BUDGET, PENDING_CHAT, PENDING_QUESTION, }; use crate::ui::{ dashboard_title, render_cards, render_dashboard_page, render_diff_lines, render_panel, render_task_page, render_timeline_events, }; use crate::rules::ancestor_dirs; use crate::store::{InboxRow, Store}; use crate::tools::{shell_looks_mutating, validate_build_command, validate_shell_command}; fn test_store() -> (Store, tempfile::TempDir) { let dir = tempfile::tempdir().unwrap(); let store = Store::open(dir.path()).unwrap(); (store, dir) } #[test] fn markdown_safe_subset() { // Basic blocks + inline formatting. let html = render_markdown("Hello **bold** and *italic* with `code`."); assert!(html.contains("bold"), "bold: {html}"); assert!(html.contains("italic"), "italic: {html}"); assert!(html.contains("code"), "code: {html}"); // Fenced code block is escaped. let html = render_markdown("```rust\nlet x = a < b;\n```"); assert!(html.contains("") && html.contains("
  • one
  • "), "ul: {html}"); let html = render_markdown("1. one\n2. two\n"); assert!(html.contains("
      "), "ol: {html}"); // Safe links kept, javascript: dropped to text. let html = render_markdown("[site](https://example.com/a?b=1)"); assert!(html.contains("alert(1)"); assert!(!html.contains(""})), ui_event(1, EVENT_ASSISTANT, serde_json::json!({"text": "[x](javascript:alert(1))"})), ui_event(2, EVENT_USER_MESSAGE, serde_json::json!({"text": "hi"})), ]; let html = render_timeline_events(&events).into_string(); assert!(!html.contains("?", "options": []}), ); let card = render_cards(&ui_task(TaskStatus::NeedsInput, Some(p))).into_string(); assert!(!card.contains("alert(1)\n-foo").into_string(); assert!(!diff.contains("(2) agentd"), "title count: {html}"); assert!(html.contains("Needs you (2)"), "section first: {html}"); let needs_pos = html.find("Needs you").unwrap(); let running_pos = html.find(">Running<").unwrap(); let recent_pos = html.find(">Recent<").unwrap(); assert!(needs_pos < running_pos && running_pos < recent_pos, "section order: {html}"); assert!(html.contains("question: Which color?"), "question reason: {html}"); assert!(html.contains("review: All done here."), "review reason: {html}"); } #[test] fn ui_status_events_capped_and_muted() { let events = vec![ ui_event(0, EVENT_STATUS, serde_json::json!({"message": "task queued"})), ui_event(1, "mystery_kind", serde_json::json!({"x": 1})), ]; let html = render_timeline_events(&events).into_string(); assert!(html.contains("status-line"), "muted status line: {html}"); assert!(html.contains("task queued"), "message kept: {html}"); } }