nibkeep-node 0.1.0

The NibKeep engine: everything an app needs to work with a library
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
//! Merging entities that turn out to be the same, and undoing a merge (`docs/spec/model.md`,
//! section 7).

mod common;

use common::{library, now, this_day};
use nibkeep_base::EntityId;
use nibkeep_model::{FieldDef, FieldName, Name, TypeDef, Value, ValueKind, fields};
use nibkeep_node::{Library, NodeError};
use nibkeep_std::{AUTHOR, BOOK};
use nibkeep_testkit::TestRng;

const PERSON: nibkeep_model::TypeName = nibkeep_governance::PERSON;

fn field(text: &str) -> FieldName {
	text.parse().expect("a field name")
}

fn name(text: &str) -> Value {
	Value::Name(Name::new(text))
}

/// A library with a person, the same person recorded again under another name with a note,
/// and a book credited to the second.
struct Duplicates {
	library: Library,
	main: EntityId,
	duplicate: EntityId,
	book: EntityId,
}

fn duplicates(test: &str, rng: &mut TestRng) -> Duplicates {
	let (_, mut library) = library(test, rng);
	let personal = library.personal_space();
	library
		.define_type(
			personal,
			&TypeDef::new(PERSON, 1)
				.with_field(field("local.note"), FieldDef::new(ValueKind::Text).many()),
			now(),
			rng,
		)
		.expect("defines");
	let mut add = |library: &mut Library, values: Vec<(FieldName, Value)>, kind| {
		library
			.create_entity(personal, &[kind], values, now(), rng)
			.expect("creates")
	};
	let main = add(
		&mut library,
		vec![(fields::NAME, name("Daniel Abraham"))],
		PERSON,
	);
	let duplicate = add(
		&mut library,
		vec![
			(fields::NAME, name("D. Abraham")),
			(
				field("local.note"),
				Value::Text("writes fantasy".to_owned()),
			),
		],
		PERSON,
	);
	let book = add(
		&mut library,
		vec![
			(fields::NAME, name("The Dragon's Path")),
			(AUTHOR, Value::Reference(duplicate)),
		],
		BOOK,
	);
	Duplicates {
		library,
		main,
		duplicate,
		book,
	}
}

#[test]
fn a_merged_entity_shows_under_the_one_it_was_merged_into() {
	let mut rng = TestRng::new("merge");
	let Duplicates {
		mut library,
		main,
		duplicate,
		book,
	} = duplicates("merge", &mut rng);
	let personal = library.personal_space();
	library
		.merge_entities(personal, duplicate, main, now(), &mut rng)
		.expect("merges");

	// The entity shows what both say: two names compete until a person chooses one.
	let view = library
		.entity(personal, main, this_day())
		.expect("a space")
		.expect("the person");
	let names = view
		.fields
		.iter()
		.find(|shown| shown.field == fields::NAME)
		.expect("names");
	assert!(names.competing);
	assert_eq!(names.values.len(), 2);
	assert!(
		view.fields
			.iter()
			.any(|shown| shown.field == field("local.note"))
	);
	// The duplicate leads to it, by ID and by name.
	assert_eq!(
		library
			.entity(personal, duplicate, this_day())
			.expect("a space")
			.map(|view| view.id),
		Some(main)
	);
	assert_eq!(
		library.find_entity(personal, "D. Abraham").expect("finds"),
		main
	);
	// What referred to the duplicate links to it.
	let links = library
		.links_to(personal, main, this_day(), 10)
		.expect("a space");
	assert_eq!(links[0].entities[0].id, book);
	// A search finds it once.
	let found = library.search(personal, "abraham").expect("a space");
	assert_eq!(found.iter().filter(|listed| listed.id == main).count(), 1);
	assert!(found.iter().all(|listed| listed.id != duplicate));
	// It lists the entity merged into it, by that entity's own name.
	let merged = library.merged(personal, main).expect("a space");
	assert_eq!(merged.len(), 1);
	assert_eq!(merged[0].id, duplicate);
	assert_eq!(merged[0].name.as_deref(), Some("D. Abraham"));
	nibkeep_testkit::assert_empty!(library.merged(personal, book).expect("a space"));
}

#[test]
fn a_merge_is_undone() {
	let mut rng = TestRng::new("undo merge");
	let Duplicates {
		mut library,
		main,
		duplicate,
		book,
	} = duplicates("undo merge", &mut rng);
	let personal = library.personal_space();
	library
		.merge_entities(personal, duplicate, main, now(), &mut rng)
		.expect("merges");
	library
		.undo_merge(personal, duplicate, now(), &mut rng)
		.expect("undoes");
	assert_eq!(
		library
			.entity(personal, duplicate, this_day())
			.expect("a space")
			.map(|view| view.id),
		Some(duplicate)
	);
	nibkeep_testkit::assert_empty!(
		library
			.links_to(personal, main, this_day(), 10)
			.expect("a space")
	);
	assert_eq!(
		library
			.links_to(personal, duplicate, this_day(), 10)
			.expect("a space")[0]
			.entities[0]
			.id,
		book
	);
	nibkeep_testkit::assert_empty!(library.merged(personal, main).expect("a space"));
}

#[test]
fn merging_again_moves_the_entity_and_merges_join() {
	let mut rng = TestRng::new("merge again");
	let Duplicates {
		mut library,
		main,
		duplicate,
		book,
	} = duplicates("merge again", &mut rng);
	let personal = library.personal_space();
	let canonical = |library: &Library, entity| {
		library
			.entity(personal, entity, this_day())
			.expect("a space")
			.map(|view| view.id)
	};
	library
		.merge_entities(personal, duplicate, book, now(), &mut rng)
		.expect("merges, by mistake");
	// Merging it again moves it: one merge replaces the other.
	library
		.merge_entities(personal, duplicate, main, now(), &mut rng)
		.expect("merges again");
	assert_eq!(canonical(&library, duplicate), Some(main));
	assert_eq!(canonical(&library, book), Some(book));
	// Merging the entity that others were merged into takes them along.
	let third = library
		.create_entity(
			personal,
			&[PERSON],
			vec![(fields::NAME, name("Abraham, Daniel"))],
			now(),
			&mut rng,
		)
		.expect("creates");
	library
		.merge_entities(personal, main, third, now(), &mut rng)
		.expect("merges");
	assert_eq!(canonical(&library, duplicate), Some(third));
	let merged: Vec<EntityId> = library
		.merged(personal, third)
		.expect("a space")
		.iter()
		.map(|listed| listed.id)
		.collect();
	assert_eq!(merged.len(), 2);
	assert!(merged.contains(&main) && merged.contains(&duplicate));
}

#[test]
fn merges_that_cannot_be_are_refused() {
	let mut rng = TestRng::new("bad merges");
	let Duplicates {
		mut library,
		main,
		duplicate,
		..
	} = duplicates("bad merges", &mut rng);
	let personal = library.personal_space();
	let refused = |library: &mut Library, from, into, rng: &mut TestRng| {
		library
			.merge_entities(personal, from, into, now(), rng)
			.expect_err("refused")
	};
	assert!(matches!(
		refused(&mut library, main, main, &mut rng),
		NodeError::SameEntity(_)
	));
	library
		.merge_entities(personal, duplicate, main, now(), &mut rng)
		.expect("merges");
	// They are one already, whichever way round.
	for (from, into) in [(main, duplicate), (duplicate, main)] {
		assert!(matches!(
			refused(&mut library, from, into, &mut rng),
			NodeError::SameEntity(_)
		));
	}
	let unknown = EntityId::random(&mut rng);
	assert!(matches!(
		refused(&mut library, unknown, main, &mut rng),
		NodeError::NoEntity(_)
	));
	assert!(matches!(
		refused(&mut library, main, unknown, &mut rng),
		NodeError::NoEntity(_)
	));
	// Types are defined by entities, which are not merged.
	let definition = library
		.entities(personal, Some(&nibkeep_model::schema::TYPE_DEFINITION))
		.expect("a space");
	let definition = definition.first().expect("a type definition").id;
	assert!(matches!(
		refused(&mut library, definition, main, &mut rng),
		NodeError::DefinesTypes(_)
	));
	// Only a merged entity's merge is undone.
	assert!(matches!(
		library.undo_merge(personal, main, now(), &mut rng),
		Err(NodeError::NotMerged(_))
	));
}

#[test]
fn a_merged_entity_is_found_by_either_name_after_opening_again() {
	let mut rng = TestRng::new("merge reopened");
	let (directory, mut library) = library("merge reopened", &mut rng);
	let personal = library.personal_space();
	let add = |library: &mut Library, text: &str, rng: &mut TestRng| {
		library
			.create_entity(
				personal,
				&[PERSON],
				vec![(fields::NAME, name(text))],
				now(),
				rng,
			)
			.expect("creates")
	};
	let main = add(&mut library, "Daniel Abraham", &mut rng);
	let duplicate = add(&mut library, "D. Abraham", &mut rng);
	library
		.merge_entities(personal, duplicate, main, now(), &mut rng)
		.expect("merges");
	for text in ["Daniel Abraham", "D. Abraham"] {
		assert_eq!(
			library.find_entity_or_deleted(personal, text).expect(text),
			main
		);
	}
	drop(library);
	let library = Library::open(
		&directory,
		&nibkeep_node::Unlocker::Passphrase("correct horse"),
	)
	.expect("opens");
	for text in ["Daniel Abraham", "D. Abraham"] {
		assert_eq!(
			library.find_entity_or_deleted(personal, text).expect(text),
			main
		);
	}
}

#[test]
fn a_merged_entity_is_found_among_those_merged_by_its_own_names() {
	let mut rng = TestRng::new("find merged");
	let Duplicates {
		mut library,
		main,
		duplicate,
		..
	} = duplicates("find merged", &mut rng);
	let personal = library.personal_space();
	library
		.merge_entities(personal, duplicate, main, now(), &mut rng)
		.expect("merges");
	for reference in [
		"D. Abraham",
		"d. abraham",
		&duplicate.to_string(),
		&duplicate.to_string()[..8],
	] {
		assert_eq!(
			library
				.find_merged(personal, main, reference)
				.expect(reference),
			duplicate
		);
	}
	// The entity merged into is not among those merged into it, nor is a short prefix.
	for reference in ["Daniel Abraham", "Someone", &duplicate.to_string()[..2]] {
		assert!(matches!(
			library.find_merged(personal, main, reference),
			Err(NodeError::NotMerged(_))
		));
	}
}

#[test]
fn merged_entities_named_in_other_fields_are_found_by_those_names() {
	let mut rng = TestRng::new("other naming fields");
	let (_, mut library) = library("other naming fields", &mut rng);
	let personal = library.personal_space();
	let note = nibkeep_model::TypeName::from_static("local.note");
	library
		.define_type(
			personal,
			&TypeDef::new(note.clone(), 1)
				.with_field(field("local.title"), FieldDef::new(ValueKind::Text)),
			now(),
			&mut rng,
		)
		.expect("defines");
	let titled = library
		.create_entity(
			personal,
			&[note],
			vec![(
				field("local.title"),
				Value::Text("Notes on Abraham".to_owned()),
			)],
			now(),
			&mut rng,
		)
		.expect("creates");
	let named = library
		.create_entity(
			personal,
			&[PERSON],
			vec![(fields::NAME, name("Daniel Abraham"))],
			now(),
			&mut rng,
		)
		.expect("creates");
	library
		.merge_entities(personal, titled, named, now(), &mut rng)
		.expect("merges");
	for text in ["Daniel Abraham", "Notes on Abraham"] {
		assert_eq!(library.find_entity(personal, text).expect(text), named);
	}
}

#[test]
fn records_of_who_uses_the_library_and_deleted_entities_are_not_merged() {
	let mut rng = TestRng::new("own records");
	let Duplicates {
		mut library,
		main,
		duplicate,
		..
	} = duplicates("own records", &mut rng);
	let personal = library.personal_space();
	let user = library.status().expect("a status").user.id;
	assert!(matches!(
		library.merge_entities(personal, user, main, now(), &mut rng),
		Err(NodeError::OwnRecord(_))
	));
	assert!(matches!(
		library.merge_entities(personal, main, user, now(), &mut rng),
		Err(NodeError::OwnRecord(_))
	));
	library
		.edit_entity(
			personal,
			duplicate,
			vec![nibkeep_node::Edit::Delete],
			&common::today(),
			now(),
			&mut rng,
		)
		.expect("deletes");
	assert!(matches!(
		library.merge_entities(personal, duplicate, main, now(), &mut rng),
		Err(NodeError::Deleted(_))
	));
	assert!(matches!(
		library.merge_entities(personal, main, duplicate, now(), &mut rng),
		Err(NodeError::Deleted(_))
	));
}

#[test]
fn entities_merged_by_a_moment_are_listed_as_of_it() {
	let mut rng = TestRng::new("merged as of");
	let Duplicates {
		mut library,
		main,
		duplicate,
		..
	} = duplicates("merged as of", &mut rng);
	let personal = library.personal_space();
	let later = now() + 60_000;
	library
		.merge_entities(personal, duplicate, main, later, &mut rng)
		.expect("merges");
	nibkeep_testkit::assert_empty!(
		library
			.merged_as_of(personal, main, now())
			.expect("a space")
	);
	assert_eq!(
		library
			.merged_as_of(personal, main, later)
			.expect("a space")[0]
			.id,
		duplicate
	);
}

/// Each edit of a write sees what the edits before it did, also to the values of an entity
/// merged into the one edited, whose statements each change names apart.
#[test]
fn an_edit_sees_what_the_edit_before_it_took_from_a_merged_entity() {
	let mut rng = TestRng::new("edits of merged entities");
	let Duplicates {
		mut library,
		main,
		duplicate,
		..
	} = duplicates("edits of merged entities", &mut rng);
	let personal = library.personal_space();
	library
		.merge_entities(personal, duplicate, main, now(), &mut rng)
		.expect("merges");
	let note = || {
		nibkeep_node::Edit::Remove(
			field("local.note"),
			Value::Text("writes fantasy".to_owned()),
		)
	};
	assert!(matches!(
		library.edit_entity(
			personal,
			main,
			vec![note(), note()],
			&common::today(),
			now(),
			&mut rng
		),
		Err(NodeError::NoValue(_))
	));
	library
		.edit_entity(
			personal,
			main,
			vec![note()],
			&common::today(),
			now(),
			&mut rng,
		)
		.expect("removes the duplicate's note");
	let shown = library
		.entity(personal, main, this_day())
		.expect("reads")
		.expect("an entity");
	assert!(
		!shown
			.fields
			.iter()
			.any(|field| field.field.as_str() == "local.note"),
		"{shown:?}"
	);
}