link-cli 0.2.10

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
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
//! Tests for the LinkStorage module

use anyhow::Result;
use link_cli::LinkStorage;
use tempfile::NamedTempFile;

#[test]
fn test_storage_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 id = storage.create(2, 3);

    assert!(id > 0);
    let link = storage.get(id).unwrap();
    assert_eq!(link.source, 2);
    assert_eq!(link.target, 3);

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    let id = storage.create(2, 3);
    storage.update(id, 4, 5)?;

    let link = storage.get(id).unwrap();
    assert_eq!(link.source, 4);
    assert_eq!(link.target, 5);

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    let id = storage.create(2, 3);
    storage.delete(id)?;

    assert!(storage.get(id).is_none());

    Ok(())
}

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

    // Create and save
    {
        let mut storage = LinkStorage::new(db_path, false)?;
        storage.create(2, 3);
        storage.save()?;
    }

    // Load and verify
    {
        let storage = LinkStorage::new(db_path, false)?;
        let links = storage.all();
        assert_eq!(links.len(), 1);
        assert_eq!(links[0].source, 2);
        assert_eq!(links[0].target, 3);
    }

    Ok(())
}

#[test]
fn test_storage_named_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 id = storage.get_or_create_named("test");

    assert!(id > 0);
    assert_eq!(storage.get_name(id), Some(&"test".to_string()));
    assert_eq!(storage.get_by_name("test"), Some(id));

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    let id = storage.create(2, 3);

    assert_eq!(storage.search(2, 3), Some(id));
    assert_eq!(storage.search(1, 1), None);

    Ok(())
}

#[test]
fn test_storage_get_or_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 id1 = storage.get_or_create(2, 3);
    let id2 = storage.get_or_create(2, 3);

    assert_eq!(id1, id2);

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    storage.create(1, 1);
    storage.create(1, 2);

    assert_eq!(storage.lino_lines(), vec!["(1: 1 1)", "(2: 1 2)"]);

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    let father = storage.get_or_create_named("father");
    let mother = storage.get_or_create_named("mother");
    let child = storage.create(father, mother);
    storage.set_name(child, "child");

    assert_eq!(
        storage.lino_lines(),
        vec![
            "(father: father father)",
            "(mother: mother mother)",
            "(child: father mother)"
        ]
    );

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    let source = storage.create(1, 1);
    storage.set_name(source, "source name");
    let target = storage.create(2, 2);
    storage.set_name(target, "target:ref");
    let child = storage.create(source, target);
    storage.set_name(child, "child(ref)");

    assert_eq!(
        storage.lino_lines(),
        vec![
            "('source name': 'source name' 'source name')",
            "('target:ref': 'target:ref' 'target:ref')",
            "('child(ref)': 'source name' 'target:ref')"
        ]
    );

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    let single_quote = storage.create(1, 1);
    storage.set_name(single_quote, "single'quote");
    let double_quote = storage.create(2, 2);
    storage.set_name(double_quote, "double\"quote");
    let both_quotes = storage.create(single_quote, double_quote);
    storage.set_name(both_quotes, "both'\"quote");

    assert_eq!(
        storage.lino_lines(),
        vec![
            "(\"single'quote\": \"single'quote\" \"single'quote\")",
            "('double\"quote': 'double\"quote' 'double\"quote')",
            "('both\\'\"quote': \"single'quote\" 'double\"quote')"
        ]
    );

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    storage.create(1, 1);
    storage.create(2, 2);

    storage.write_lino_output(output_path)?;

    assert_eq!(
        std::fs::read_to_string(output_path)?,
        "(1: 1 1)\n(2: 2 2)\n"
    );

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    let first = storage.create(0, 0);
    let second = storage.create(first, first);
    let third = storage.create(second, first);
    let fourth = storage.create(third, second);

    assert_eq!(
        storage.format_structure(fourth)?,
        "(4: (3: (2: (1: 0 0) 1) 1) 2)"
    );

    Ok(())
}

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

    let mut storage = LinkStorage::new(db_path, false)?;
    let first = storage.create(0, 0);
    let second = storage.create(first, first);
    storage.create(second, first);
    let fourth = storage.create(second, second);

    assert_eq!(storage.format_structure(fourth)?, "(4: (2: (1: 0 0) 1) 2)");

    Ok(())
}

/// `LinkStorage::get_or_create` must match `(source, target)` literally.
///
/// `LinkStorage` also implements the upstream `doublets` traits — including
/// for `&mut LinkStorage`, so a borrowed store can be decorated — and inside an
/// inherent `&mut self` method the receiver's type is exactly
/// `&mut LinkStorage`. Method resolution reaches the trait impl on the
/// reference before it derefs to the inherent impl, so a bare `self.search(..)`
/// resolves to `Doublets::search`, which treats `LinksConstants::any` as a
/// wildcard. That made `get_or_create(any, target)` hand back an unrelated
/// existing link instead of creating a new one.
#[test]
fn get_or_create_matches_service_constants_literally() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let mut storage = LinkStorage::new(temp_file.path().to_str().unwrap(), false)?;

    let existing = storage.create(0, 0);
    storage.update(existing, 7, 42)?;

    // Every `doublets` service constant, plus the external references that
    // encode to them, must be stored as an ordinary value.
    for reserved in [
        u32::MAX,
        u32::MAX - 1,
        u32::MAX - 2,
        u32::MAX - 3,
        u32::MAX - 4,
    ] {
        let created = storage.get_or_create(reserved, 42);
        assert_ne!(
            existing, created,
            "source {reserved} must not be treated as a wildcard"
        );
        assert_eq!(
            Some(&link_cli::Link::new(created, reserved, 42)),
            storage.get(created)
        );
        // A second call has to find the link it just created.
        assert_eq!(created, storage.get_or_create(reserved, 42));
    }

    Ok(())
}

/// The address a new link gets is part of what a query reports, so the store
/// hands out addresses the way `ResizableDirectMemoryLinks` does: a freed one
/// before a fresh one.
#[test]
fn test_freed_address_is_reused_before_the_store_grows() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let mut storage = LinkStorage::new(temp_file.path(), false)?;

    let first = storage.create(1, 1);
    let second = storage.create(2, 2);
    let third = storage.create(3, 3);
    assert_eq!((first, second, third), (1, 2, 3));

    storage.delete_raw(second)?;

    assert_eq!(storage.create(1, 3), second);
    assert_eq!(storage.create(3, 1), 4);

    Ok(())
}

/// Freed addresses come back in the reverse of the order they were freed: the
/// C# store pushes each one onto the head of its free list and allocates from
/// that same head.
#[test]
fn test_freed_addresses_are_reused_most_recently_freed_first() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let mut storage = LinkStorage::new(temp_file.path(), false)?;

    for part in 1..=5 {
        storage.create(part, part);
    }
    storage.delete_raw(2)?;
    storage.delete_raw(4)?;

    assert_eq!(storage.create(1, 3), 4);
    assert_eq!(storage.create(3, 1), 2);
    assert_eq!(storage.create(1, 5), 6);

    Ok(())
}

/// Freeing the highest address shrinks the store instead of leaving a hole,
/// and takes the freed addresses that have become the end with it.
#[test]
fn test_freeing_the_last_link_shrinks_the_store() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let mut storage = LinkStorage::new(temp_file.path(), false)?;

    for part in 1..=3 {
        storage.create(part, part);
    }
    storage.delete_raw(2)?;
    storage.delete_raw(3)?;

    // Both 2 and 3 are gone, but as a shrink rather than as two holes, so the
    // next links get them in ascending order.
    assert_eq!(storage.create(1, 2), 2);
    assert_eq!(storage.create(2, 1), 3);

    Ok(())
}

/// The free list decides the next address and nothing in the stored links
/// implies it, so it has to survive a save and a load.
#[test]
fn test_free_list_survives_a_reload() -> Result<()> {
    let temp_file = NamedTempFile::new()?;

    {
        let mut storage = LinkStorage::new(temp_file.path(), false)?;
        for part in 1..=5 {
            storage.create(part, part);
        }
        storage.delete_raw(2)?;
        storage.delete_raw(4)?;
        storage.save()?;
    }

    let mut storage = LinkStorage::new(temp_file.path(), false)?;
    assert_eq!(storage.create(1, 3), 4);
    assert_eq!(storage.create(3, 1), 2);
    assert_eq!(storage.create(1, 5), 6);

    Ok(())
}

/// A database written before the free list was recorded — or by hand — still
/// loads, with the addresses missing below the highest stored link recovered
/// as freed ones, lowest reused first.
#[test]
fn test_missing_addresses_are_recovered_from_a_database_without_a_free_list() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    std::fs::write(temp_file.path(), "(1 1 1)\n(3 3 3)\n(5 5 5)\n")?;

    let mut storage = LinkStorage::new(temp_file.path(), false)?;
    assert_eq!(storage.create(1, 3), 2);
    assert_eq!(storage.create(3, 1), 4);
    assert_eq!(storage.create(1, 5), 6);

    Ok(())
}

/// Reaching a requested address allocates the addresses before it, and those
/// are freed again — `EnsureCreated` deletes every link it did not ask for.
#[test]
fn test_ensure_created_frees_the_addresses_it_passed_over() -> Result<()> {
    let temp_file = NamedTempFile::new()?;
    let mut storage = LinkStorage::new(temp_file.path(), false)?;

    storage.create(1, 1);
    assert_eq!(storage.ensure_created(4), 4);

    assert!(!storage.exists(2));
    assert!(!storage.exists(3));
    assert_eq!(
        storage.get(4).map(|link| (link.source, link.target)),
        Some((0, 0))
    );

    // 2 and 3 were freed in the order they were created, leaving 3 on top.
    assert_eq!(storage.create(1, 4), 3);
    assert_eq!(storage.create(4, 1), 2);

    Ok(())
}