arf-console 0.4.5

A cross-platform R console written in Rust
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
use super::super::*;
use super::create_test_targets;

// === Dedup (anti-join) tests ===

#[test]
fn test_import_skips_duplicates_with_timestamp() {
    use reedline::History;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
        .unwrap()
        .with_timezone(&Utc);

    // First import: should succeed
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: Some(ts),
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 1);
    assert_eq!(result.duplicates_skipped, 0);

    // Second import of the same entry: should be skipped as duplicate
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: Some(ts),
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 0);
    assert_eq!(result.duplicates_skipped, 1);

    // Verify only one entry exists in the database
    let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let items = targets.r_history.search(query).unwrap();
    assert_eq!(items.len(), 1);
}

#[test]
fn test_import_skips_duplicates_without_timestamp() {
    use reedline::History;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    // First import (no timestamp)
    let entries = vec![ImportEntry {
        command: "summary(iris)".to_string(),
        timestamp: None,
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 1);
    assert_eq!(result.duplicates_skipped, 0);

    // Second import of the same command (no timestamp): should be skipped
    let entries = vec![ImportEntry {
        command: "summary(iris)".to_string(),
        timestamp: None,
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 0);
    assert_eq!(result.duplicates_skipped, 1);

    // Verify only one entry exists
    let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let items = targets.r_history.search(query).unwrap();
    assert_eq!(items.len(), 1);
}

#[test]
fn test_import_allows_same_command_different_timestamp() {
    use reedline::History;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    let ts1 = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
        .unwrap()
        .with_timezone(&Utc);
    let ts2 = DateTime::parse_from_rfc3339("2024-06-15T15:00:00Z")
        .unwrap()
        .with_timezone(&Utc);

    // Import the same command with two different timestamps
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: Some(ts1),
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 1);

    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: Some(ts2),
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 1);
    assert_eq!(result.duplicates_skipped, 0);

    // Both should exist
    let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let items = targets.r_history.search(query).unwrap();
    assert_eq!(items.len(), 2);
}

#[test]
fn test_import_duplicates_flag_disables_dedup() {
    use reedline::History;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
        .unwrap()
        .with_timezone(&Utc);

    // First import
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: Some(ts),
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, false).unwrap();
    assert_eq!(result.r_imported, 1);

    // Second import with skip_duplicates=false (--import-duplicates)
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: Some(ts),
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, false).unwrap();
    assert_eq!(result.r_imported, 1);
    assert_eq!(result.duplicates_skipped, 0);

    // Both entries should exist (duplicate allowed)
    let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let items = targets.r_history.search(query).unwrap();
    assert_eq!(items.len(), 2);
}

#[test]
fn test_import_dedup_works_per_database() {
    use reedline::History;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    // Import an R command
    let entries = vec![ImportEntry {
        command: "ls -la".to_string(),
        timestamp: None,
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 1);

    // Import the same command as shell — should NOT be a duplicate
    // because it's checked against the shell database, not R
    let entries = vec![ImportEntry {
        command: "ls -la".to_string(),
        timestamp: None,
        mode: Some("shell".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.shell_imported, 1);
    assert_eq!(result.duplicates_skipped, 0);

    // Verify both databases have the entry
    let r_query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let r_items = targets.r_history.search(r_query).unwrap();
    assert_eq!(r_items.len(), 1);

    let shell_query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let shell_items = targets.shell_history.search(shell_query).unwrap();
    assert_eq!(shell_items.len(), 1);
}

#[test]
fn test_import_dry_run_with_dedup() {
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
        .unwrap()
        .with_timezone(&Utc);

    // Pre-populate the database
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: Some(ts),
        mode: Some("r".to_string()),
    }];
    import_entries(&mut targets, entries, None, false).unwrap();

    // Build dedup sets
    let r_dedup = DedupSet::from_history(&targets.r_history).unwrap();
    let shell_dedup = DedupSet::from_history(&targets.shell_history).unwrap();

    // Dry run with existing + new entries
    let entries = vec![
        ImportEntry {
            command: "library(dplyr)".to_string(), // duplicate
            timestamp: Some(ts),
            mode: Some("r".to_string()),
        },
        ImportEntry {
            command: "print(1)".to_string(), // new
            timestamp: None,
            mode: Some("r".to_string()),
        },
    ];

    let result = import_entries_dry_run(&entries, Some(&r_dedup), Some(&shell_dedup));
    assert_eq!(result.r_imported, 1);
    assert_eq!(result.duplicates_skipped, 1);
}

#[test]
fn test_import_mixed_dedup_new_and_existing() {
    use reedline::History;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
        .unwrap()
        .with_timezone(&Utc);

    // Pre-populate with one entry
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: Some(ts),
        mode: Some("r".to_string()),
    }];
    import_entries(&mut targets, entries, None, false).unwrap();

    // Import a batch with duplicates and new entries
    let entries = vec![
        ImportEntry {
            command: "library(dplyr)".to_string(), // duplicate
            timestamp: Some(ts),
            mode: Some("r".to_string()),
        },
        ImportEntry {
            command: "print(1)".to_string(), // new
            timestamp: None,
            mode: Some("r".to_string()),
        },
        ImportEntry {
            command: "git status".to_string(), // new (shell)
            timestamp: None,
            mode: Some("shell".to_string()),
        },
    ];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 1);
    assert_eq!(result.shell_imported, 1);
    assert_eq!(result.duplicates_skipped, 1);

    // Verify databases
    let r_query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let r_items = targets.r_history.search(r_query).unwrap();
    assert_eq!(r_items.len(), 2); // original + new

    let shell_query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let shell_items = targets.shell_history.search(shell_query).unwrap();
    assert_eq!(shell_items.len(), 1);
}

#[test]
fn test_import_dry_run_with_partial_dedup() {
    // Regression test: dry-run dedup should work when only one database
    // has a dedup set (e.g., r.db exists but shell.db doesn't).
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    // Pre-populate only the R database
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: None,
        mode: Some("r".to_string()),
    }];
    import_entries(&mut targets, entries, None, false).unwrap();

    // Build dedup set only for R (simulating shell.db not existing)
    let r_dedup = DedupSet::from_history(&targets.r_history).unwrap();

    let entries = vec![
        ImportEntry {
            command: "library(dplyr)".to_string(), // duplicate in R
            timestamp: None,
            mode: Some("r".to_string()),
        },
        ImportEntry {
            command: "print(1)".to_string(), // new R entry
            timestamp: None,
            mode: Some("r".to_string()),
        },
        ImportEntry {
            command: "ls -la".to_string(), // shell entry, no dedup set
            timestamp: None,
            mode: Some("shell".to_string()),
        },
    ];

    // Pass R dedup but None for shell
    let result = import_entries_dry_run(&entries, Some(&r_dedup), None);
    assert_eq!(result.r_imported, 1); // only "print(1)"
    assert_eq!(result.shell_imported, 1); // "ls -la" not checked (no shell dedup)
    assert_eq!(result.duplicates_skipped, 1); // "library(dplyr)"
}

#[test]
fn test_import_skips_notimestamp_when_timestamped_exists() {
    // Regression test: a no-timestamp import entry should be skipped if
    // the same command already exists in the DB with any timestamp.
    // This is documented in lines 265-270.
    use reedline::History;
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    let ts = DateTime::parse_from_rfc3339("2024-06-15T14:30:45Z")
        .unwrap()
        .with_timezone(&Utc);

    // Pre-populate with a timestamped entry
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: Some(ts),
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 1);

    // Try to import the same command without a timestamp: should be skipped
    let entries = vec![ImportEntry {
        command: "library(dplyr)".to_string(),
        timestamp: None,
        mode: Some("r".to_string()),
    }];
    let result = import_entries(&mut targets, entries, None, true).unwrap();
    assert_eq!(result.r_imported, 0);
    assert_eq!(result.duplicates_skipped, 1);

    // Verify only the original entry exists
    let query = reedline::SearchQuery::everything(reedline::SearchDirection::Backward, None);
    let items = targets.r_history.search(query).unwrap();
    assert_eq!(items.len(), 1);
    assert!(items[0].start_timestamp.is_some());
}

#[test]
fn test_from_db_matches_from_history() {
    // Verify that from_db (read-only SQLite) and from_history (via reedline)
    // produce the same dedup set for the same database contents.
    use tempfile::TempDir;

    let temp_dir = TempDir::new().unwrap();
    let mut targets = create_test_targets(&temp_dir);

    let ts1 = Utc::now();
    let ts2 = ts1 + chrono::Duration::seconds(60);

    let entries = vec![
        ImportEntry {
            command: "library(dplyr)".to_string(),
            timestamp: Some(ts1),
            mode: Some("r".to_string()),
        },
        ImportEntry {
            command: "print(1)".to_string(),
            timestamp: Some(ts2),
            mode: Some("r".to_string()),
        },
        ImportEntry {
            command: "summary(iris)".to_string(),
            timestamp: None, // no timestamp
            mode: Some("r".to_string()),
        },
    ];
    import_entries(&mut targets, entries, None, false).unwrap();

    let r_path = temp_dir.path().join("r.db");
    let from_history = DedupSet::from_history(&targets.r_history).unwrap();
    let from_db = DedupSet::from_db(&r_path).unwrap();

    // Both should have the same commands set
    assert_eq!(from_history.commands, from_db.commands);
    // Both should have the same command_timestamps set
    assert_eq!(from_history.command_timestamps, from_db.command_timestamps);

    // Verify dedup behavior is identical for both
    assert!(from_history.is_duplicate("library(dplyr)", Some(&ts1)));
    assert!(from_db.is_duplicate("library(dplyr)", Some(&ts1)));
    assert!(!from_history.is_duplicate("new_cmd", None));
    assert!(!from_db.is_duplicate("new_cmd", None));
    assert!(from_history.is_duplicate("summary(iris)", None));
    assert!(from_db.is_duplicate("summary(iris)", None));
}