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
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
//! Edits of entities: values set, added, removed and cleared, deletion and restoring, and
//! search.

mod common;

use common::{library, now, this_day, today};
use nibkeep_base::EntityId;
use nibkeep_model::{FieldDef, FieldName, TypeDef, TypeName, Value, ValueKind, fields};
use nibkeep_node::{Edit, Library, NodeError};
use nibkeep_testkit::TestRng;

const BOOK: TypeName = TypeName::from_static("local.book");
const AUDIOBOOK: TypeName = TypeName::from_static("local.audiobook");

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

fn text(text: &str) -> Value {
	Value::Text(text.to_owned())
}

/// A library with a book, and the book.
fn with_a_book(test: &str, rng: &mut TestRng) -> (Library, EntityId) {
	let (_, mut library) = library(test, rng);
	let personal = library.personal_space();
	let book = TypeDef::new(BOOK, 1)
		.with_field(name("local.title"), FieldDef::new(ValueKind::Text))
		.with_field(name("local.tag"), FieldDef::new(ValueKind::Text).many());
	library
		.define_type(personal, &book, now(), rng)
		.expect("defines");
	let entity = library
		.create_entity(
			personal,
			&[BOOK],
			vec![(name("local.title"), text("Leviathan Wakes"))],
			now(),
			rng,
		)
		.expect("creates");
	(library, entity)
}

/// The values of a field of an entity, and whether they compete.
fn values(library: &Library, entity: EntityId, field: &str) -> (Vec<Value>, bool) {
	let personal = library.personal_space();
	let view = library
		.entity(personal, entity, this_day())
		.expect("a space")
		.expect("the entity");
	view.fields
		.iter()
		.find(|view| view.field == name(field))
		.map_or((Vec::new(), false), |view| {
			let values = view
				.values
				.iter()
				.map(|shown| shown.value.clone())
				.collect();
			(values, view.competing)
		})
}

#[test]
fn values_are_set_added_removed_and_cleared() {
	let mut rng = TestRng::new("values edited");
	let (mut library, book) = with_a_book("values edited", &mut rng);
	let personal = library.personal_space();
	let mut edit = |library: &mut Library, edits: Vec<Edit>| {
		library
			.edit_entity(personal, book, edits, &today(), now(), &mut rng)
			.expect("edits");
	};
	edit(
		&mut library,
		vec![
			Edit::Set(name("local.title"), vec![text("Leviathan wakes")]),
			Edit::Set(name("local.tag"), vec![text("space"), text("noir")]),
		],
	);
	assert_eq!(
		values(&library, book, "local.title"),
		(vec![text("Leviathan wakes")], false)
	);
	assert_eq!(values(&library, book, "local.tag").0.len(), 2);
	edit(
		&mut library,
		vec![Edit::Add(name("local.tag"), text("opera"))],
	);
	assert_eq!(values(&library, book, "local.tag").0.len(), 3);
	edit(
		&mut library,
		vec![Edit::Remove(name("local.tag"), text("noir"))],
	);
	let (tags, _) = values(&library, book, "local.tag");
	assert_eq!(tags.len(), 2);
	assert!(!tags.contains(&text("noir")));
	edit(&mut library, vec![Edit::Set(name("local.tag"), Vec::new())]);
	assert_eq!(values(&library, book, "local.tag").0, Vec::<Value>::new());

	// A second value of a field with one value competes, until a person chooses.
	edit(
		&mut library,
		vec![Edit::Add(name("local.title"), text("Leviathan Wakes"))],
	);
	let (titles, competing) = values(&library, book, "local.title");
	assert_eq!(titles.len(), 2);
	assert!(competing);
	edit(
		&mut library,
		vec![Edit::Set(
			name("local.title"),
			vec![text("Leviathan Wakes")],
		)],
	);
	assert_eq!(
		values(&library, book, "local.title"),
		(vec![text("Leviathan Wakes")], false)
	);
}

#[test]
fn entities_are_deleted_and_restored() {
	let mut rng = TestRng::new("deleted");
	let (mut library, book) = with_a_book("deleted", &mut rng);
	let personal = library.personal_space();
	let found = |library: &Library| {
		library
			.entities(personal, Some(&BOOK))
			.expect("a space")
			.len()
	};
	library
		.edit_entity(
			personal,
			book,
			vec![Edit::Delete],
			&today(),
			now(),
			&mut rng,
		)
		.expect("deletes");
	assert!(
		library
			.entity(personal, book, this_day())
			.expect("a space")
			.expect("kept")
			.deleted
	);
	assert_eq!(found(&library), 0);
	let deleted = library.deleted(personal).expect("a space");
	assert_eq!(
		(deleted.len(), deleted[0].id, deleted[0].name.as_deref()),
		(1, book, Some("Leviathan Wakes"))
	);
	assert_eq!(
		library
			.find_entity_or_deleted(personal, "Leviathan Wakes")
			.expect("still found by name, to restore it"),
		book
	);
	assert!(
		matches!(
			library.find_entity(personal, "Leviathan Wakes"),
			Err(NodeError::NoEntity(_))
		),
		"references never point to a deleted entity by name"
	);
	let changes = library.status().expect("reads the status").spaces[0].changes;
	library
		.edit_entity(
			personal,
			book,
			vec![Edit::Delete],
			&today(),
			now(),
			&mut rng,
		)
		.expect("deletes again");
	assert_eq!(
		library.status().expect("reads the status").spaces[0].changes,
		changes,
		"nothing to do"
	);
	library
		.edit_entity(
			personal,
			book,
			vec![Edit::Restore],
			&today(),
			now(),
			&mut rng,
		)
		.expect("restores");
	assert_eq!(found(&library), 1);
	nibkeep_testkit::assert_empty!(library.deleted(personal).expect("a space"));
}

#[test]
fn edits_that_cannot_be_made_are_refused() {
	let mut rng = TestRng::new("refused edits");
	let (mut library, book) = with_a_book("refused edits", &mut rng);
	let personal = library.personal_space();
	let changes = library.status().expect("reads the status").spaces[0].changes;
	let mut refused = |edits: Vec<Edit>| {
		library
			.edit_entity(personal, book, edits, &today(), now(), &mut rng)
			.expect_err("refused")
	};
	assert!(matches!(
		refused(vec![Edit::Set(fields::DELETED, vec![Value::Boolean(true)])]),
		NodeError::OwnField(_)
	));
	assert!(matches!(
		refused(vec![Edit::Set(
			name("local.title"),
			vec![text("a"), text("b")]
		)]),
		NodeError::OneValue(_)
	));
	assert!(matches!(
		refused(vec![Edit::Remove(name("local.tag"), text("never there"))]),
		NodeError::NoValue(_)
	));
	let nobody = EntityId::random(&mut rng);
	assert!(matches!(
		library.edit_entity(
			personal,
			nobody,
			vec![Edit::Delete],
			&today(),
			now(),
			&mut rng
		),
		Err(NodeError::NoEntity(_))
	));
	assert_eq!(
		library.status().expect("reads the status").spaces[0].changes,
		changes
	);
}

#[test]
fn search_finds_entities_by_their_words() {
	let mut rng = TestRng::new("search");
	let (mut library, book) = with_a_book("search", &mut rng);
	let personal = library.personal_space();
	let found = library.search(personal, "leviathan").expect("a space");
	assert_eq!(found.len(), 1);
	assert_eq!(found[0].id, book);
	assert_eq!(found[0].name.as_deref(), Some("Leviathan Wakes"));
	assert_eq!(found[0].types, [BOOK]);
	nibkeep_testkit::assert_empty!(library.search(personal, "caliban").expect("a space"));
	library
		.edit_entity(
			personal,
			book,
			vec![Edit::Delete],
			&today(),
			now(),
			&mut rng,
		)
		.expect("deletes");
	nibkeep_testkit::assert_empty!(library.search(personal, "leviathan").expect("a space"));
}

#[test]
fn search_finds_names_but_not_keys() {
	let mut rng = TestRng::new("names but not keys");
	let (library, _) = with_a_book("names but not keys", &mut rng);
	let personal = library.personal_space();
	let status = library.status().expect("reads the status");
	let found = |query: &str| -> Vec<EntityId> {
		library
			.search(personal, query)
			.expect("a space")
			.into_iter()
			.map(|found| found.id)
			.collect()
	};
	assert_eq!(found("laptop"), [status.device.id]);
	assert_eq!(found("nib"), [status.replica.id]);
	let view = library
		.entity(personal, status.replica.id, this_day())
		.expect("a space")
		.expect("the replica");
	let key = view
		.fields
		.iter()
		.find(|field| field.field == nibkeep_governance::SIGNING_KEY)
		.and_then(|field| field.values.first())
		.and_then(|shown| shown.value.as_text())
		.expect("the replica's key")
		.to_owned();
	nibkeep_testkit::assert_empty!(found(&key[..6]), "a replica's key is not searched");
}

#[test]
fn each_edit_sees_the_ones_before_it() {
	let mut rng = TestRng::new("in order");
	let (mut library, book) = with_a_book("in order", &mut rng);
	let personal = library.personal_space();
	library
		.edit_entity(
			personal,
			book,
			vec![
				Edit::Add(name("local.tag"), text("x")),
				Edit::Remove(name("local.tag"), text("x")),
			],
			&today(),
			now(),
			&mut rng,
		)
		.expect("adds and removes");
	nibkeep_testkit::assert_empty!(values(&library, book, "local.tag").0);
	let deleted = |library: &Library| {
		library
			.entity(personal, book, this_day())
			.expect("a space")
			.expect("kept")
			.deleted
	};
	library
		.edit_entity(
			personal,
			book,
			vec![Edit::Delete, Edit::Restore],
			&today(),
			now(),
			&mut rng,
		)
		.expect("edits");
	assert!(!deleted(&library), "restored last");
	library
		.edit_entity(
			personal,
			book,
			vec![Edit::Delete],
			&today(),
			now(),
			&mut rng,
		)
		.expect("deletes");
	library
		.edit_entity(
			personal,
			book,
			vec![Edit::Restore, Edit::Delete],
			&today(),
			now(),
			&mut rng,
		)
		.expect("edits");
	assert!(deleted(&library), "deleted last");
}

#[test]
fn setting_the_values_a_field_has_changes_nothing() {
	let mut rng = TestRng::new("same values");
	let (mut library, book) = with_a_book("same values", &mut rng);
	let personal = library.personal_space();
	let changes = library.status().expect("reads the status").spaces[0].changes;
	library
		.edit_entity(
			personal,
			book,
			vec![Edit::Set(
				name("local.title"),
				vec![text("Leviathan Wakes")],
			)],
			&today(),
			now(),
			&mut rng,
		)
		.expect("edits");
	assert_eq!(
		library.status().expect("reads the status").spaces[0].changes,
		changes
	);
}

#[test]
fn removing_the_last_value_written_clears_the_field() {
	let mut rng = TestRng::new("last writer wins");
	let (mut library, book) = with_a_book("last writer wins", &mut rng);
	let personal = library.personal_space();
	let status = TypeDef::new(BOOK, 1).with_field(
		name("local.status"),
		FieldDef::new(ValueKind::Text).last_writer_wins(),
	);
	library
		.define_type(personal, &status, now(), &mut rng)
		.expect("defines");
	for value in ["draft", "final"] {
		library
			.edit_entity(
				personal,
				book,
				vec![Edit::Add(name("local.status"), text(value))],
				&today(),
				now(),
				&mut rng,
			)
			.expect("adds");
	}
	assert_eq!(values(&library, book, "local.status").0, [text("final")]);
	let found =
		|library: &Library, word: &str| library.search(personal, word).expect("a space").len();
	assert_eq!(found(&library, "final"), 1);
	assert_eq!(
		found(&library, "draft"),
		0,
		"search finds only what the field shows"
	);
	assert!(matches!(
		library.edit_entity(
			personal,
			book,
			vec![Edit::Remove(name("local.status"), text("draft"))],
			&today(),
			now(),
			&mut rng
		),
		Err(NodeError::NoValue(_))
	));
	library
		.edit_entity(
			personal,
			book,
			vec![Edit::Remove(name("local.status"), text("final"))],
			&today(),
			now(),
			&mut rng,
		)
		.expect("removes");
	nibkeep_testkit::assert_empty!(
		values(&library, book, "local.status").0,
		"draft does not come back"
	);
}

#[test]
fn definitions_of_types_are_not_edited_as_entities() {
	use nibkeep_model::schema::FIELD_DEFINITION;

	let mut rng = TestRng::new("definitions");
	let (mut library, _) = with_a_book("definitions", &mut rng);
	let personal = library.personal_space();
	let definition = library
		.entities(personal, Some(&FIELD_DEFINITION))
		.expect("a space")
		.first()
		.expect("a field's definition")
		.id;
	assert!(matches!(
		library.edit_entity(
			personal,
			definition,
			vec![Edit::Delete],
			&today(),
			now(),
			&mut rng
		),
		Err(NodeError::OwnType(_))
	));
}

#[test]
fn types_are_given_and_taken_away() {
	let mut rng = TestRng::new("types edited");
	let (mut library, book) = with_a_book("types edited", &mut rng);
	let personal = library.personal_space();
	let types = |library: &Library| {
		library
			.entity(personal, book, this_day())
			.expect("a space")
			.expect("the book")
			.types
	};
	let mut edit = |library: &mut Library, edits: Vec<Edit>, at: u64| {
		library.edit_entity(personal, book, edits, &today(), now() + at, &mut rng)
	};
	let changes = |library: &Library| library.status().expect("a status").spaces[0].changes;

	edit(&mut library, vec![Edit::AddType(AUDIOBOOK)], 1_000).expect("gives a type");
	assert_eq!(types(&library), [AUDIOBOOK, BOOK]);
	let before = changes(&library);
	edit(&mut library, vec![Edit::AddType(AUDIOBOOK)], 2_000).expect("gives it again");
	assert_eq!(changes(&library), before, "a type it has changes nothing");
	assert_eq!(
		library.entities(personal, Some(&AUDIOBOOK)).expect("lists")[0].id,
		book,
		"it is listed with the type"
	);

	edit(&mut library, vec![Edit::RemoveType(BOOK)], 3_000).expect("takes a type away");
	assert_eq!(types(&library), [AUDIOBOOK]);
	assert_eq!(
		values(&library, book, "local.title").0,
		[text("Leviathan Wakes")],
		"the values stay"
	);
	assert_eq!(
		library.entities(personal, Some(&BOOK)).expect("lists"),
		[],
		"it is no longer listed as a book"
	);
	assert!(matches!(
		edit(&mut library, vec![Edit::RemoveType(BOOK)], 4_000),
		Err(NodeError::NotOfType(name)) if name == BOOK
	));
	assert!(matches!(
		edit(&mut library, vec![Edit::RemoveType(AUDIOBOOK)], 5_000),
		Err(NodeError::OnlyType(name)) if name == AUDIOBOOK
	));
	// A type given and the other taken away in one edit.
	edit(
		&mut library,
		vec![Edit::AddType(BOOK), Edit::RemoveType(AUDIOBOOK)],
		6_000,
	)
	.expect("swaps the types");
	assert_eq!(types(&library), [BOOK]);

	for own in [
		nibkeep_model::schema::TYPE_DEFINITION,
		nibkeep_governance::USER,
		nibkeep_governance::REPLICA,
	] {
		assert!(
			matches!(
				edit(&mut library, vec![Edit::AddType(own.clone())], 7_000),
				Err(NodeError::OwnType(refused)) if refused == own
			),
			"{own}"
		);
	}
	let user = library.status().expect("a status").user.id;
	for retyped in [
		Edit::RemoveType(nibkeep_governance::USER),
		Edit::AddType(BOOK),
	] {
		assert!(matches!(
			library.edit_entity(
				personal,
				user,
				vec![retyped],
				&today(),
				now() + 8_000,
				&mut rng
			),
			Err(NodeError::OwnType(_))
		));
	}
}