link-cli 0.2.2

A CLI tool for links manipulation
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
//! Tests for the QueryProcessor module

use anyhow::Result;
use link_cli::{LinkStorage, QueryProcessor};
use tempfile::NamedTempFile;

fn auto_processor() -> QueryProcessor {
    QueryProcessor::new(false).with_auto_create_missing_references(true)
}

#[test]
fn test_query_processor_create() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    // Create a simple link: (() ((1 2)))
    let changes = processor.process_query(&mut storage, "(()((1 2)))")?;

    assert!(!changes.is_empty());
    assert!(changes[0].0.is_none()); // No before (creation)
    assert!(changes[0].1.is_some()); // Has after

    Ok(())
}

#[test]
fn test_query_processor_empty() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    let changes = processor.process_query(&mut storage, "")?;
    assert!(changes.is_empty());

    Ok(())
}

#[test]
fn test_missing_numeric_reference_fails_without_auto_create() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = QueryProcessor::new(false);

    let error = processor
        .process_query(&mut storage, "(() ((1: 10 20)))")
        .expect_err("missing numeric references should fail validation");

    assert!(error.to_string().contains("10"));
    assert!(error
        .to_string()
        .contains("--auto-create-missing-references"));

    Ok(())
}

#[test]
fn test_future_numeric_references_succeed_without_auto_create() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = QueryProcessor::new(false);

    processor.process_query(&mut storage, "(() ((1: 1 2) (2: 2 1)))")?;

    assert_eq!(storage.get(1).unwrap().source, 1);
    assert_eq!(storage.get(1).unwrap().target, 2);
    assert_eq!(storage.get(2).unwrap().source, 2);
    assert_eq!(storage.get(2).unwrap().target, 1);

    Ok(())
}

#[test]
fn test_auto_create_missing_numeric_reference_creates_point_link() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    processor.process_query(&mut storage, "(() ((20: 10 20)))")?;

    let point = storage
        .get(10)
        .expect("missing numeric reference should be created");
    assert_eq!(point.source, 10);
    assert_eq!(point.target, 10);
    let link = storage.get(20).expect("defined link should be created");
    assert_eq!(link.source, 10);
    assert_eq!(link.target, 20);

    Ok(())
}

#[test]
fn test_auto_create_missing_numeric_reference_fills_existing_gap() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    processor.process_query(&mut storage, "(() ((3: 3 3)))")?;
    processor.process_query(&mut storage, "(() ((4: 1 4)))")?;

    let point = storage
        .get(1)
        .expect("missing lower numeric reference should be created");
    assert_eq!(point.source, 1);
    assert_eq!(point.target, 1);
    let link = storage.get(4).expect("defined link should be created");
    assert_eq!(link.source, 1);
    assert_eq!(link.target, 4);

    Ok(())
}

#[test]
fn test_missing_named_reference_fails_without_auto_create() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = QueryProcessor::new(false);

    let error = processor
        .process_query(&mut storage, "(() ((child: father mother)))")
        .expect_err("missing named references should fail validation");

    assert!(error.to_string().contains("father"));
    assert!(error
        .to_string()
        .contains("--auto-create-missing-references"));

    Ok(())
}

#[test]
fn test_auto_create_missing_named_references_creates_point_links() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    processor.process_query(&mut storage, "(() ((child: father mother)))")?;

    let father_id = storage.get_by_name("father").expect("father should exist");
    let mother_id = storage.get_by_name("mother").expect("mother should exist");
    let child_id = storage.get_by_name("child").expect("child should exist");

    let father = storage.get(father_id).unwrap();
    assert_eq!(father.source, father_id);
    assert_eq!(father.target, father_id);
    let mother = storage.get(mother_id).unwrap();
    assert_eq!(mother.source, mother_id);
    assert_eq!(mother.target, mother_id);
    let child = storage.get(child_id).unwrap();
    assert_eq!(child.source, father_id);
    assert_eq!(child.target, mother_id);

    Ok(())
}

// ============================================
// Link Deduplication Tests (Issue #65)
// ============================================

#[test]
fn test_deduplicate_duplicate_pair_with_named_links() -> Result<()> {
    // Issue #65: Test deduplication of (m a) (m a) pattern
    // Query: () (((m a) (m a)))
    // Expected: m, a (named self-refs), link for (m a), link for ((m a) (m a))
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    processor.process_query(&mut storage, "(() (((m a) (m a))))")?;

    let all_links = storage.all();
    assert_eq!(all_links.len(), 4);

    // Get the named link IDs
    let m_id = storage.get_by_name("m").expect("m should exist");
    let a_id = storage.get_by_name("a").expect("a should exist");

    // m and a should be self-referencing
    let m_link = storage.get(m_id).unwrap();
    assert_eq!(m_link.source, m_id);
    assert_eq!(m_link.target, m_id);

    let a_link = storage.get(a_id).unwrap();
    assert_eq!(a_link.source, a_id);
    assert_eq!(a_link.target, a_id);

    // Find the (m a) link
    let ma_id = storage.search(m_id, a_id).expect("(m a) link should exist");

    // Find the outer link ((m a) (m a)) - should have same source and target
    let outer_id = storage
        .search(ma_id, ma_id)
        .expect("((m a) (m a)) link should exist");
    let outer_link = storage.get(outer_id).unwrap();
    assert_eq!(
        outer_link.source, outer_link.target,
        "Outer link should reference the same deduplicated sub-link"
    );

    Ok(())
}

#[test]
fn test_deduplicate_duplicate_pair_with_numeric_links() -> Result<()> {
    // Issue #65: Test deduplication with numeric IDs
    // Query: () (((1 2) (1 2)))
    // When using numeric IDs directly, they are treated as references (not creating self-refs)
    // So (1 2) creates link with source=1, target=2
    // The deduplication still works: ((1 2) (1 2)) creates only one (1 2) link
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    processor.process_query(&mut storage, "(() (((1 2) (1 2))))")?;

    let all_links = storage.all();

    // Should have 2 links: (1 2) and ((1 2) (1 2))
    assert_eq!(all_links.len(), 2);

    // Link 1 should be (1 2) - the deduplicated sub-link
    let link1 = storage.get(1).expect("Link 1 should exist");
    assert_eq!(link1.source, 1);
    assert_eq!(link1.target, 2);

    // Link 2 should be (1 1) - referencing the same sub-link twice
    let link2 = storage.get(2).expect("Link 2 should exist");
    assert_eq!(link2.source, 1);
    assert_eq!(link2.target, 1);

    Ok(())
}

#[test]
fn test_deduplicate_triple_duplicate_pair() -> Result<()> {
    // Test with three identical pairs using named links: (((a b) ((a b) (a b))))
    // The (a b) should only be created once
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    processor.process_query(&mut storage, "(() (((a b) ((a b) (a b)))))")?;

    let all_links = storage.all();
    assert_eq!(all_links.len(), 5);

    let a_id = storage.get_by_name("a").expect("a should exist");
    let b_id = storage.get_by_name("b").expect("b should exist");

    // a and b should be self-referencing
    let a_link = storage.get(a_id).unwrap();
    assert_eq!(a_link.source, a_id);
    assert_eq!(a_link.target, a_id);

    let b_link = storage.get(b_id).unwrap();
    assert_eq!(b_link.source, b_id);
    assert_eq!(b_link.target, b_id);

    // Find (a b) link - the deduplicated sub-link
    let ab_id = storage.search(a_id, b_id).expect("(a b) link should exist");

    // Find ((a b) (a b)) - should reference (a b) twice
    let inner_id = storage
        .search(ab_id, ab_id)
        .expect("((a b) (a b)) link should exist");

    // Find outer link ((a b) ((a b) (a b)))
    let outer_id = storage
        .search(ab_id, inner_id)
        .expect("outer link should exist");
    assert!(outer_id > 0);

    Ok(())
}

#[test]
fn test_deduplicate_with_different_pairs() -> Result<()> {
    // Test that different pairs are NOT deduplicated
    // Query: () (((a b) (b a))) - using named links
    // (a b) and (b a) are different and should both be created
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    processor.process_query(&mut storage, "(() (((a b) (b a))))")?;

    let all_links = storage.all();
    assert_eq!(all_links.len(), 5);

    let a_id = storage.get_by_name("a").expect("a should exist");
    let b_id = storage.get_by_name("b").expect("b should exist");

    // a and b should be self-referencing
    let a_link = storage.get(a_id).unwrap();
    assert_eq!(a_link.source, a_id);
    assert_eq!(a_link.target, a_id);

    let b_link = storage.get(b_id).unwrap();
    assert_eq!(b_link.source, b_id);
    assert_eq!(b_link.target, b_id);

    // Find (a b) link
    let ab_id = storage.search(a_id, b_id).expect("(a b) link should exist");

    // Find (b a) link
    let ba_id = storage.search(b_id, a_id).expect("(b a) link should exist");

    // Find outer link ((a b) (b a)) - should have different source and target
    let outer_id = storage
        .search(ab_id, ba_id)
        .expect("outer link should exist");
    let outer_link = storage.get(outer_id).unwrap();
    assert_ne!(outer_link.source, outer_link.target);

    Ok(())
}

#[test]
fn test_deduplicate_nested_duplicates() -> Result<()> {
    // Test deeply nested deduplication using named links
    // Query: () ((((x y) (x y)) ((x y) (x y))))
    // (x y) is duplicated at multiple levels
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    processor.process_query(&mut storage, "(() ((((x y) (x y)) ((x y) (x y)))))")?;

    let all_links = storage.all();
    assert_eq!(all_links.len(), 5);

    let x_id = storage.get_by_name("x").expect("x should exist");
    let y_id = storage.get_by_name("y").expect("y should exist");

    // x and y should be self-referencing
    let x_link = storage.get(x_id).unwrap();
    assert_eq!(x_link.source, x_id);
    assert_eq!(x_link.target, x_id);

    let y_link = storage.get(y_id).unwrap();
    assert_eq!(y_link.source, y_id);
    assert_eq!(y_link.target, y_id);

    // Find (x y) - the base link
    let xy_id = storage.search(x_id, y_id).expect("(x y) link should exist");

    // Find ((x y) (x y)) - references (x y) twice (deduplicated)
    let level1_id = storage
        .search(xy_id, xy_id)
        .expect("((x y) (x y)) link should exist");

    // Find (((x y) (x y)) ((x y) (x y))) - references level1 twice (deduplicated)
    let level2_id = storage
        .search(level1_id, level1_id)
        .expect("outer link should exist");
    assert!(level2_id > 0);

    Ok(())
}

#[test]
fn test_deduplicate_named_links_multiple_queries() -> Result<()> {
    // Issue #65: Verify that named links maintain consistent IDs across operations
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    // First create named links
    processor.process_query(&mut storage, "(() ((p: p p)))")?;
    processor.process_query(&mut storage, "(() ((a: a a)))")?;

    let p_id = storage.get_by_name("p").expect("p should exist");
    let a_id = storage.get_by_name("a").expect("a should exist");

    // Now create ((p a) (p a)) - should reuse existing p and a
    processor.process_query(&mut storage, "(() (((p a) (p a))))")?;

    // p and a should still have the same IDs
    assert_eq!(storage.get_by_name("p"), Some(p_id));
    assert_eq!(storage.get_by_name("a"), Some(a_id));

    // Verify p and a are still self-referencing
    let p_link = storage.get(p_id).unwrap();
    assert_eq!(p_link.source, p_id);
    assert_eq!(p_link.target, p_id);

    let a_link = storage.get(a_id).unwrap();
    assert_eq!(a_link.source, a_id);
    assert_eq!(a_link.target, a_id);

    // Find (p a) link
    let pa_id = storage.search(p_id, a_id).expect("(p a) link should exist");

    // Find ((p a) (p a)) link - should reference pa_id twice
    let outer_id = storage
        .search(pa_id, pa_id)
        .expect("((p a) (p a)) link should exist");
    let outer_link = storage.get(outer_id).unwrap();
    assert_eq!(outer_link.source, pa_id);
    assert_eq!(outer_link.target, pa_id);

    Ok(())
}

#[test]
fn test_deduplicate_mixed_named_and_numeric() -> Result<()> {
    // Test that named links are reused across queries
    let temp_file = NamedTempFile::new()?;
    let db_path = temp_file.path().to_str().unwrap();

    let mut storage = LinkStorage::new(db_path, false)?;
    let processor = auto_processor();

    // First query creates (m a)
    processor.process_query(&mut storage, "(() ((m a)))")?;

    let m_id = storage.get_by_name("m").expect("m should exist");
    let a_id = storage.get_by_name("a").expect("a should exist");

    // Second query should reuse existing m and a links
    processor.process_query(&mut storage, "(() (((m a) (m a))))")?;

    // m and a should still have the same IDs
    assert_eq!(storage.get_by_name("m"), Some(m_id));
    assert_eq!(storage.get_by_name("a"), Some(a_id));

    // Should have 4 links total: m, a, (m a), ((m a) (m a))
    let all_links = storage.all();
    assert_eq!(all_links.len(), 4);

    Ok(())
}