ratado 0.2.0

A fast, keyboard-driven terminal task manager built with Rust and Ratatui
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! End-to-End Test Infrastructure for Ratado
//!
//! This module provides utilities for PTY-based interactive testing of the
//! Ratado TUI application using expectrl.
//!
//! # Overview
//!
//! These tests spawn the actual Ratado binary in a pseudo-terminal (PTY),
//! simulating real user interaction. This catches issues that unit tests miss:
//! - Terminal rendering and escape sequence handling
//! - Input buffering and timing
//! - Signal handling
//! - Race conditions in the event loop
//!
//! # Verification Strategy
//!
//! Tests use two complementary verification approaches:
//! - **UI verification** (during test): `expect()` patterns confirm visible output
//! - **Database verification** (after quit): query the DB once the app releases its lock
//!
//! # Test Isolation
//!
//! Each test uses a temporary database via the `--db-path` flag to ensure
//! complete isolation from user data and other tests.

use expectrl::session::OsSession;
use expectrl::{Eof, Expect, Regex};
use ratado::storage::Database;
use std::path::PathBuf;
use std::time::Duration;
use tempfile::TempDir;

/// Default timeout for expect operations
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);

/// Extended timeout for slower operations (startup, saving)
pub const EXTENDED_TIMEOUT: Duration = Duration::from_secs(10);

/// Terminal dimensions
const TERM_WIDTH: u16 = 120;
const TERM_HEIGHT: u16 = 80;

/// Test harness for Ratado E2E tests.
///
/// While the app is running, use `expect_text` for UI verification.
/// Call `quit()` to get a `DbVerifier` for post-quit database assertions.
pub struct RatadoTest {
    /// The expectrl session controlling the PTY
    pub session: OsSession,
    /// Temporary directory containing the test database
    _temp_dir: Option<TempDir>,
    /// Path to the test database
    db_path: PathBuf,
    /// Whether the app has already been quit
    exited: bool,
}

/// Verifier for post-quit database assertions.
///
/// Created by `RatadoTest::quit()` after the app exits and releases
/// the database lock.
pub struct DbVerifier {
    _temp_dir: TempDir,
    db_path: PathBuf,
}

impl RatadoTest {
    /// Spawns a new Ratado instance with a fresh temporary database.
    pub fn spawn() -> Self {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let db_path = temp_dir.path().join("test_ratado.db");

        // Disable animations in spawned process (inherited env var)
        unsafe { std::env::set_var("RATADO_NO_ANIMATIONS", "1") };

        let binary_path = env!("CARGO_BIN_EXE_ratado");
        let cmd = format!("{} -d {}", binary_path, db_path.to_str().unwrap());

        let mut session = expectrl::spawn(&cmd).expect("Failed to spawn ratado");

        session
            .get_process_mut()
            .set_window_size(TERM_WIDTH, TERM_HEIGHT)
            .expect("Failed to set terminal size");

        session.set_expect_timeout(Some(DEFAULT_TIMEOUT));

        RatadoTest {
            session,
            _temp_dir: Some(temp_dir),
            db_path,
            exited: false,
        }
    }

    /// Waits for the application to fully initialize by expecting the main view.
    ///
    /// Animations and splash screen are disabled via RATADO_NO_ANIMATIONS env var.
    pub fn wait_for_startup(&mut self) -> &mut Self {
        self.session.set_expect_timeout(Some(EXTENDED_TIMEOUT));
        // Wait for main view to be ready (Tasks header or empty state)
        let _ = self.session.expect(Regex("Tasks|liftoff"));
        self.session.set_expect_timeout(Some(DEFAULT_TIMEOUT));
        self
    }

    // =========================================================================
    // Input Methods
    // =========================================================================

    pub fn press(&mut self, key: &str) -> &mut Self {
        self.session.send(key).expect("Failed to send key");
        std::thread::sleep(Duration::from_millis(50));
        self
    }

    pub fn type_text(&mut self, text: &str) -> &mut Self {
        self.session.send(text).expect("Failed to send text");
        std::thread::sleep(Duration::from_millis(50));
        self
    }

    pub fn press_enter(&mut self) -> &mut Self {
        self.session.send("\r").expect("Failed to send enter");
        std::thread::sleep(Duration::from_millis(50));
        self
    }

    pub fn press_escape(&mut self) -> &mut Self {
        self.session.send("\x1b").expect("Failed to send escape");
        std::thread::sleep(Duration::from_millis(50));
        self
    }

    pub fn press_tab(&mut self) -> &mut Self {
        self.session.send("\t").expect("Failed to send tab");
        std::thread::sleep(Duration::from_millis(50));
        self
    }

    pub fn press_arrow(&mut self, direction: &str) -> &mut Self {
        let seq = match direction {
            "up" => "\x1b[A",
            "down" => "\x1b[B",
            "right" => "\x1b[C",
            "left" => "\x1b[D",
            _ => panic!("Invalid arrow direction: {}", direction),
        };
        self.session.send(seq).expect("Failed to send arrow key");
        std::thread::sleep(Duration::from_millis(50));
        self
    }

    pub fn wait(&mut self, duration: Duration) -> &mut Self {
        std::thread::sleep(duration);
        self
    }

    // =========================================================================
    // UI Verification (while app is running)
    // =========================================================================

    /// Expects the given regex pattern to appear in the terminal output.
    pub fn expect_text(&mut self, pattern: &str) -> &mut Self {
        self.session
            .expect(Regex(pattern))
            .unwrap_or_else(|_| panic!("Expected pattern '{}' in terminal output", pattern));
        self
    }

    // =========================================================================
    // High-Level Actions
    // =========================================================================

    /// Quits the app and returns a `DbVerifier` for post-quit database checks.
    pub fn quit(&mut self) -> DbVerifier {
        self.session.send("q").expect("Failed to send quit");
        let _ = self.session.expect(Eof);
        self.exited = true;
        std::thread::sleep(Duration::from_millis(200));
        DbVerifier {
            _temp_dir: self._temp_dir.take().expect("TempDir already taken"),
            db_path: self.db_path.clone(),
        }
    }

    /// Opens the Quick Capture dialog, types the title, and submits with Enter.
    pub fn add_task(&mut self, title: &str) -> &mut Self {
        // Open quick capture dialog
        self.press("a");
        std::thread::sleep(Duration::from_millis(100));
        // Type the title
        self.type_text(title);
        // Submit with Enter
        self.press_enter();
        std::thread::sleep(Duration::from_millis(200));
        self
    }

    pub fn move_down(&mut self) -> &mut Self {
        self.press("j")
    }

    pub fn move_up(&mut self) -> &mut Self {
        self.press("k")
    }

    /// Toggles task completion with Space key.
    pub fn toggle_complete(&mut self) -> &mut Self {
        self.press(" ")
    }

    /// Deletes the selected task (presses 'd' then 'y' to confirm).
    pub fn delete_task(&mut self) -> &mut Self {
        self.press("d");
        std::thread::sleep(Duration::from_millis(100));
        self.press("y");
        std::thread::sleep(Duration::from_millis(200));
        self
    }

    pub fn edit_task(&mut self) -> &mut Self {
        self.press("e")
    }

    pub fn open_search(&mut self) -> &mut Self {
        self.press("/")
    }

    /// Switches the task filter to "All" so completed tasks remain visible.
    /// Default filter is "Pending" which hides completed tasks.
    pub fn set_filter_all(&mut self) -> &mut Self {
        // Open filter dialog
        self.press("f");
        std::thread::sleep(Duration::from_millis(100));
        // Move up from "Pending" (index 1) to "All" (index 0)
        self.press("k");
        std::thread::sleep(Duration::from_millis(50));
        // Confirm
        self.press_enter();
        std::thread::sleep(Duration::from_millis(200));
        self
    }

    /// Checks if the database file was created on disk.
    pub fn database_exists(&self) -> bool {
        self.db_path.exists()
    }
}

impl Drop for RatadoTest {
    fn drop(&mut self) {
        if !self.exited {
            let _ = self.session.send("q");
            std::thread::sleep(Duration::from_millis(100));
        }
    }
}

// =============================================================================
// DbVerifier - post-quit database assertions
// =============================================================================

impl DbVerifier {
    async fn open_db(&self) -> Database {
        Database::open(&self.db_path)
            .await
            .expect("Failed to open test database for verification")
    }

    pub fn assert_task_count(&self, expected: usize) -> &Self {
        let actual = self.get_task_count();
        assert_eq!(
            actual, expected,
            "Expected {} tasks, but found {}",
            expected, actual
        );
        self
    }

    pub fn assert_task_exists(&self, title: &str) -> &Self {
        assert!(
            self.task_exists(title),
            "Expected task '{}' to exist in database",
            title
        );
        self
    }

    pub fn assert_task_not_exists(&self, title: &str) -> &Self {
        assert!(
            !self.task_exists(title),
            "Expected task '{}' to NOT exist in database",
            title
        );
        self
    }

    pub fn assert_task_completed(&self, title: &str) -> &Self {
        assert!(
            self.task_is_completed(title),
            "Expected task '{}' to be completed",
            title
        );
        self
    }

    pub fn assert_task_pending(&self, title: &str) -> &Self {
        assert!(
            !self.task_is_completed(title),
            "Expected task '{}' to be pending (not completed)",
            title
        );
        self
    }

    pub fn assert_completed_count(&self, expected: usize) -> &Self {
        let actual = self.get_completed_task_count();
        assert_eq!(
            actual, expected,
            "Expected {} completed tasks, but found {}",
            expected, actual
        );
        self
    }

    pub fn assert_pending_count(&self, expected: usize) -> &Self {
        let actual = self.get_pending_task_count();
        assert_eq!(
            actual, expected,
            "Expected {} pending tasks, but found {}",
            expected, actual
        );
        self
    }

    fn get_task_count(&self) -> usize {
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async {
                let db = self.open_db().await;
                let mut rows = db
                    .query("SELECT COUNT(*) FROM tasks", ())
                    .await
                    .expect("Failed to count tasks");
                if let Ok(Some(row)) = rows.next().await {
                    row.get::<i64>(0).unwrap_or(0) as usize
                } else {
                    0
                }
            })
    }

    fn get_completed_task_count(&self) -> usize {
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async {
                let db = self.open_db().await;
                let mut rows = db
                    .query(
                        "SELECT COUNT(*) FROM tasks WHERE status = 'completed'",
                        (),
                    )
                    .await
                    .expect("Failed to count completed tasks");
                if let Ok(Some(row)) = rows.next().await {
                    row.get::<i64>(0).unwrap_or(0) as usize
                } else {
                    0
                }
            })
    }

    fn get_pending_task_count(&self) -> usize {
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async {
                let db = self.open_db().await;
                let mut rows = db
                    .query(
                        "SELECT COUNT(*) FROM tasks WHERE status = 'pending'",
                        (),
                    )
                    .await
                    .expect("Failed to count pending tasks");
                if let Ok(Some(row)) = rows.next().await {
                    row.get::<i64>(0).unwrap_or(0) as usize
                } else {
                    0
                }
            })
    }

    fn task_exists(&self, title: &str) -> bool {
        let title = title.to_string();
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async {
                let db = self.open_db().await;
                let mut rows = db
                    .query(
                        "SELECT COUNT(*) FROM tasks WHERE title = ?1",
                        [title.as_str()],
                    )
                    .await
                    .expect("Failed to check task existence");
                if let Ok(Some(row)) = rows.next().await {
                    row.get::<i64>(0).unwrap_or(0) > 0
                } else {
                    false
                }
            })
    }

    fn task_is_completed(&self, title: &str) -> bool {
        let title = title.to_string();
        tokio::runtime::Runtime::new()
            .unwrap()
            .block_on(async {
                let db = self.open_db().await;
                let mut rows = db
                    .query(
                        "SELECT status FROM tasks WHERE title = ?1",
                        [title.as_str()],
                    )
                    .await
                    .expect("Failed to check task completion");
                if let Ok(Some(row)) = rows.next().await {
                    row.get::<String>(0).unwrap_or_default() == "completed"
                } else {
                    false
                }
            })
    }
}