aven 0.1.4

Local-first task manager CLI and sync server
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#![allow(dead_code)]

use std::ffi::OsStr;
use std::io::{BufRead, BufReader, Write};
use std::net::TcpListener;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Output, Stdio};
use std::sync::{Arc, Mutex, mpsc};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode};
use sqlx::{Connection as _, SqliteConnection};
use tempfile::TempDir;

pub struct TestEnv {
    temp: TempDir,
}

impl TestEnv {
    pub fn new() -> Self {
        Self {
            temp: tempfile::tempdir().expect("create temp dir"),
        }
    }

    pub fn path(&self, name: &str) -> PathBuf {
        self.temp.path().join(name)
    }

    pub fn db(&self, name: &str) -> PathBuf {
        self.path(name)
    }

    pub fn config_dir(&self) -> PathBuf {
        self.path("config")
    }

    pub fn config_file(&self) -> PathBuf {
        self.config_dir().join("aven").join("config.yaml")
    }

    pub fn state_dir(&self) -> PathBuf {
        self.path("state")
    }

    fn configure_command(&self, command: &mut Command) {
        command
            .env("XDG_STATE_HOME", self.state_dir())
            .env("AVEN_CONFIG_DIR", self.config_dir().join("aven"))
            .env_remove("AVEN_DB")
            .env_remove("AVEN_SYNC_SERVER");
    }

    pub fn free_loopback_addr(&self) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind free loopback port");
        let addr = listener.local_addr().expect("free loopback addr");
        addr.to_string()
    }

    pub fn write_config(&self, text: &str) {
        let path = self.config_file();
        std::fs::create_dir_all(path.parent().expect("config parent")).expect("create config dir");
        std::fs::write(path, text).expect("write config");
    }

    pub fn write_daemon_config(
        &self,
        db: &Path,
        server: &TestServer,
        wake_addr: &str,
        interval: u64,
    ) {
        self.write_daemon_config_with_auth(db, server, wake_addr, interval, None);
    }

    pub fn write_daemon_config_with_auth(
        &self,
        db: &Path,
        server: &TestServer,
        wake_addr: &str,
        interval: u64,
        auth_token: Option<&str>,
    ) {
        let auth_line = match auth_token {
            Some(token) => format!("  auth_token: \"{token}\"\n"),
            None => String::new(),
        };
        self.write_config(&format!(
            r#"
local:
  db_path: "{}"

sync:
  enabled: true
  server_url: "{}"
  interval_seconds: {}
{auth_line}daemon:
  wake_addr: "{}"
"#,
            db.display(),
            server.url,
            interval,
            wake_addr
        ));
    }

    pub fn aven_config<I, S>(&self, args: I) -> Output
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        let mut command = command();
        self.configure_command(&mut command);
        command
            .env("AVEN_CONFIG_DIR", self.config_dir().join("aven"))
            .env_remove("AVEN_DB")
            .env_remove("AVEN_SYNC_SERVER");
        command.args(args).output().expect("run aven with config")
    }

    pub fn aven_config_stdin<I, S>(&self, args: I, input: &str) -> Output
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        let mut child = command();
        self.configure_command(&mut child);
        child
            .env("AVEN_CONFIG_DIR", self.config_dir().join("aven"))
            .env_remove("AVEN_DB")
            .env_remove("AVEN_SYNC_SERVER")
            .args(args)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        let mut child = child.spawn().expect("spawn aven with config stdin");
        child
            .stdin
            .as_mut()
            .expect("stdin pipe")
            .write_all(input.as_bytes())
            .expect("write stdin");
        child.wait_with_output().expect("wait for aven")
    }

    pub fn aven<I, S>(&self, db: &Path, args: I) -> Output
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        let mut command = command_with_db(db);
        self.configure_command(&mut command);
        command.args(args).output().expect("run aven")
    }

    pub fn aven_in<I, S>(&self, db: &Path, cwd: &Path, args: I) -> Output
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        let mut command = command_with_db(db);
        self.configure_command(&mut command);
        command
            .current_dir(cwd)
            .args(args)
            .output()
            .expect("run aven in cwd")
    }

    pub fn aven_stdin<I, S>(&self, db: &Path, args: I, input: &str) -> Output
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        let mut command = command_with_db(db);
        self.configure_command(&mut command);
        let mut child = command
            .args(args)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("spawn aven with stdin");
        child
            .stdin
            .as_mut()
            .expect("stdin pipe")
            .write_all(input.as_bytes())
            .expect("write stdin");
        child.wait_with_output().expect("wait for aven")
    }

    pub fn aven_ok<I, S>(&self, db: &Path, args: I) -> Output
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        let output = self.aven(db, args);
        assert!(
            output.status.success(),
            "{}",
            String::from_utf8_lossy(&output.stderr)
        );
        output
    }

    pub fn sync_ok(&self, db: &Path, server_url: &str) -> Output {
        let output = self.aven(db, ["sync", "--server", server_url]);
        assert!(
            output.status.success(),
            "sync failed:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        output
    }
}

pub fn bin() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_aven"))
}

pub fn command() -> Command {
    Command::new(bin())
}

pub fn command_with_db(db: &Path) -> Command {
    let mut command = command();
    command.arg("--db").arg(db);
    command
}

pub fn ok(output: Output) -> String {
    let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
    let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
    assert!(
        output.status.success(),
        "expected success\nstatus: {}\nstdout:\n{}\nstderr:\n{}",
        output.status,
        stdout,
        stderr
    );
    stdout
}

pub fn fail(output: Output) -> String {
    let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
    let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
    assert!(
        !output.status.success(),
        "expected failure\nstdout:\n{}\nstderr:\n{}",
        stdout,
        stderr
    );
    format!("{stdout}{stderr}")
}

pub fn extract_ref(output: &str) -> String {
    output
        .split_whitespace()
        .nth(1)
        .expect("mutation output ref")
        .to_string()
}

pub fn suffix(task_ref: &str) -> String {
    task_ref
        .split_once('-')
        .map(|(_, suffix)| suffix.to_string())
        .unwrap_or_else(|| task_ref.to_string())
}

pub fn contains_all(text: &str, needles: &[&str]) {
    for needle in needles {
        assert!(text.contains(needle), "missing {needle:?}\ntext:\n{text}");
    }
}

pub fn contains_none(text: &str, needles: &[&str]) {
    for needle in needles {
        assert!(
            !text.contains(needle),
            "unexpected {needle:?}\ntext:\n{text}"
        );
    }
}

pub struct TestProcess {
    child: Child,
    output: Arc<Mutex<String>>,
    stdout_thread: Option<JoinHandle<()>>,
    stderr_thread: Option<JoinHandle<()>>,
}

impl TestProcess {
    fn capture(mut child: Child) -> Self {
        let output = Arc::new(Mutex::new(String::new()));
        let stdout = child.stdout.take().expect("process stdout");
        let stdout_output = Arc::clone(&output);
        let stdout_thread = thread::spawn(move || {
            let reader = BufReader::new(stdout);
            for line in reader.lines().map_while(Result::ok) {
                let mut output = stdout_output.lock().expect("process output lock");
                output.push_str(&line);
                output.push('\n');
            }
        });

        let stderr = child.stderr.take().expect("process stderr");
        let stderr_output = Arc::clone(&output);
        let stderr_thread = thread::spawn(move || {
            let reader = BufReader::new(stderr);
            for line in reader.lines().map_while(Result::ok) {
                let mut output = stderr_output.lock().expect("process output lock");
                output.push_str(&line);
                output.push('\n');
            }
        });

        Self {
            child,
            output,
            stdout_thread: Some(stdout_thread),
            stderr_thread: Some(stderr_thread),
        }
    }

    pub fn start_server<I, S>(env: &TestEnv, args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<OsStr>,
    {
        let mut command = command();
        env.configure_command(&mut command);
        let child = command
            .env("AVEN_CONFIG_DIR", env.config_dir().join("aven"))
            .env_remove("AVEN_DB")
            .env_remove("AVEN_SYNC_SERVER")
            .arg("server")
            .args(args)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("spawn aven server");
        Self::capture(child)
    }

    pub fn start_daemon(env: &TestEnv) -> Self {
        Self::start_daemon_with_env(env, std::iter::empty::<(&str, &str)>())
    }

    pub fn start_daemon_with_env<I, K, V>(env: &TestEnv, envs: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<OsStr>,
        V: AsRef<OsStr>,
    {
        let mut command = command();
        env.configure_command(&mut command);
        command
            .env("AVEN_CONFIG_DIR", env.config_dir().join("aven"))
            .env_remove("AVEN_DB")
            .env_remove("AVEN_SYNC_SERVER");
        for (key, value) in envs {
            command.env(key, value);
        }
        let child = command
            .args(["daemon"])
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("spawn aven daemon");
        let process = Self::capture(child);
        process.wait_for_log("daemon db=", Duration::from_secs(10));
        process
    }

    pub fn output(&self) -> String {
        self.output.lock().expect("process output lock").clone()
    }

    pub fn log_mark(&self) -> usize {
        self.output().len()
    }

    pub fn wait_for_log(&self, pattern: &str, timeout: Duration) {
        self.wait_for_log_after(0, pattern, timeout);
    }

    pub fn wait_for_log_after(&self, mark: usize, pattern: &str, timeout: Duration) {
        let deadline = Instant::now() + timeout;
        while Instant::now() < deadline {
            let output = self.output();
            if output
                .get(mark..)
                .is_some_and(|text| text.contains(pattern))
            {
                return;
            }
            thread::sleep(Duration::from_millis(50));
        }
        panic!("timed out waiting for {pattern:?}\n{}", self.output());
    }
}

fn kill_child_and_join_threads(
    child: &mut Child,
    stdout_thread: &mut Option<JoinHandle<()>>,
    stderr_thread: &mut Option<JoinHandle<()>>,
) {
    let _ = child.kill();
    let _ = child.wait();
    if let Some(thread) = stdout_thread.take() {
        let _ = thread.join();
    }
    if let Some(thread) = stderr_thread.take() {
        let _ = thread.join();
    }
}

impl Drop for TestProcess {
    fn drop(&mut self) {
        kill_child_and_join_threads(
            &mut self.child,
            &mut self.stdout_thread,
            &mut self.stderr_thread,
        );
    }
}

pub fn eventually<F>(timeout: Duration, mut check: F)
where
    F: FnMut() -> bool,
{
    let deadline = Instant::now() + timeout;
    while Instant::now() < deadline {
        if check() {
            return;
        }
        thread::sleep(Duration::from_millis(50));
    }
    assert!(check(), "condition was not met within {timeout:?}");
}

pub fn meta_value(db: &Path, key: &str) -> Option<String> {
    let runtime = test_runtime();
    runtime.block_on(async {
        let mut conn = open_test_db(db).await;
        sqlx::query_scalar::<_, String>("SELECT value FROM meta WHERE key = ?")
            .bind(key)
            .fetch_optional(&mut conn)
            .await
            .expect("read meta value")
    })
}

pub fn scalar_i64(db: &Path, sql: &'static str) -> i64 {
    let runtime = test_runtime();
    runtime.block_on(async {
        let mut conn = open_test_db(db).await;
        sqlx::query_scalar::<_, i64>(sql)
            .fetch_one(&mut conn)
            .await
            .expect("read scalar value")
    })
}

fn test_runtime() -> tokio::runtime::Runtime {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .expect("create tokio runtime")
}

pub async fn insert_task_fixtures(pool: &sqlx::SqlitePool, fixtures: &[(&str, &str, &str)]) {
    for (id, title, project_key) in fixtures {
        sqlx::query(
            "INSERT INTO tasks(id,title,description,project_id,status,priority,created_at,updated_at)
             VALUES (?, ?, '', (SELECT id FROM projects WHERE key = ?), 'inbox', 'none', 't', 't')",
        )
        .bind(id)
        .bind(title)
        .bind(project_key)
        .execute(pool)
        .await
        .expect("insert task fixture");
    }
}

async fn open_test_db(db: &Path) -> SqliteConnection {
    let options = SqliteConnectOptions::new()
        .filename(db)
        .create_if_missing(false)
        .foreign_keys(true)
        .journal_mode(SqliteJournalMode::Wal)
        .busy_timeout(Duration::from_secs(5));
    SqliteConnection::connect_with(&options)
        .await
        .expect("open sqlite db")
}

pub struct TestServer {
    child: Child,
    output: Arc<Mutex<String>>,
    stdout_thread: Option<JoinHandle<()>>,
    stderr_thread: Option<JoinHandle<()>>,
    pub url: String,
}

impl TestServer {
    pub fn start(env: &TestEnv) -> Self {
        Self::start_with_data(env, "server.sqlite")
    }

    pub fn start_configured(env: &TestEnv, data: &str) -> Self {
        Self::start_configured_with_env(env, data, std::iter::empty::<(&str, &str)>())
    }

    pub fn start_configured_with_env<I, K, V>(env: &TestEnv, data: &str, envs: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<OsStr>,
        V: AsRef<OsStr>,
    {
        Self::start_with_data_and_config(env, data, Some(env.config_dir().join("aven")), envs)
    }

    pub fn start_with_data(env: &TestEnv, data: &str) -> Self {
        Self::start_with_data_and_config(env, data, None, std::iter::empty::<(&str, &str)>())
    }

    fn start_with_data_and_config<I, K, V>(
        env: &TestEnv,
        data: &str,
        config_dir: Option<PathBuf>,
        envs: I,
    ) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<OsStr>,
        V: AsRef<OsStr>,
    {
        let output = Arc::new(Mutex::new(String::new()));
        let (url_tx, url_rx) = mpsc::channel();
        let mut command = command();
        env.configure_command(&mut command);
        command.args([
            "server",
            "--bind",
            "127.0.0.1:0",
            "--data",
            env.path(data).to_str().expect("utf8 temp path"),
        ]);
        if let Some(config_dir) = config_dir {
            command
                .env("AVEN_CONFIG_DIR", config_dir)
                .env_remove("AVEN_DB")
                .env_remove("AVEN_SYNC_SERVER");
        }
        for (key, value) in envs {
            command.env(key, value);
        }
        let mut child = command
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .expect("spawn aven server");

        let stdout = child.stdout.take().expect("server stdout");
        let stdout_output = Arc::clone(&output);
        let stdout_thread = thread::spawn(move || {
            let reader = BufReader::new(stdout);
            for line in reader.lines().map_while(Result::ok) {
                {
                    let mut output = stdout_output.lock().expect("server output lock");
                    output.push_str(&line);
                    output.push('\n');
                }
                if let Some(rest) = line.strip_prefix("listening url=") {
                    let url = rest.split_whitespace().next().expect("listening url value");
                    let _ = url_tx.send(url.to_string());
                }
            }
        });

        let stderr = child.stderr.take().expect("server stderr");
        let stderr_output = Arc::clone(&output);
        let stderr_thread = thread::spawn(move || {
            let reader = BufReader::new(stderr);
            for line in reader.lines().map_while(Result::ok) {
                let mut output = stderr_output.lock().expect("server output lock");
                output.push_str(&line);
                output.push('\n');
            }
        });

        let deadline = Instant::now() + Duration::from_secs(10);
        let url = loop {
            if let Ok(url) = url_rx.try_recv() {
                break url;
            }
            if let Some(status) = child.try_wait().expect("check server status") {
                panic!(
                    "server exited during startup: {status}\n{}",
                    output.lock().expect("server output lock")
                );
            }
            assert!(
                Instant::now() < deadline,
                "server did not print listening url\n{}",
                output.lock().expect("server output lock")
            );
            thread::sleep(Duration::from_millis(50));
        };

        Self {
            child,
            output,
            stdout_thread: Some(stdout_thread),
            stderr_thread: Some(stderr_thread),
            url,
        }
    }

    pub fn output(&self) -> String {
        self.output.lock().expect("server output lock").clone()
    }
}

impl Drop for TestServer {
    fn drop(&mut self) {
        kill_child_and_join_threads(
            &mut self.child,
            &mut self.stdout_thread,
            &mut self.stderr_thread,
        );
    }
}