co-core-file 0.1.0

Hierarchal file structure core.
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2026 1io BRANDGUARDIAN GmbH

use anyhow::anyhow;
use cid::Cid;
use co_api::{
	co, tags, AbsolutePath, AbsolutePathOwned, BlockStorageExt, CoMap, CoSet, CoreBlockStorage, Date, Did, Link,
	OptionLink, PathExt, PathOwned, Reducer, ReducerAction, Tags,
};
use futures::{FutureExt, TryStreamExt};
use std::collections::{BTreeMap, BTreeSet, VecDeque};

#[co(state)]
pub struct File {
	pub nodes: CoMap<AbsolutePathOwned, CoSet<Node>>,
}

#[co]
pub enum Node {
	Folder(FolderNode),
	File(FileNode),
	Link(LinkNode),
}

#[co]
pub struct FileNode {
	pub name: String,
	pub create_time: Date,
	pub modify_time: Date,
	pub size: u64,
	pub mode: u32,
	pub tags: Tags,
	// #[external]
	pub contents: Cid,
	pub owner: Did,
}

#[co]
pub struct FolderNode {
	pub name: String,
	pub create_time: Date,
	pub modify_time: Date,
	pub tags: Tags,
	pub owner: Did,
	pub mode: u32,
}

#[co]
pub struct LinkNode {
	pub name: String,
	pub tags: Tags,
	pub contents: PathOwned,
}

#[co]
pub enum FileAction {
	/// Create a node.
	/// Ignored if a node with the same name already exists at path.
	Create {
		/// The parent to create the node in.
		path: AbsolutePathOwned,
		/// The node to create.
		node: Node,
		/// Whether to create parents recursively.
		recursive: bool,
	},

	/// Remove a node.
	/// If a node has children and recusive is set to false nothing will happen.
	Remove { path: AbsolutePathOwned, recursive: bool },

	/// Modify a node.
	Modify { path: AbsolutePathOwned, modifications: Vec<FileModification> },
}

#[co]
pub enum FileModification {
	/// Rename node to.
	Rename(String),

	/// Move node into path (as children).
	Move(AbsolutePathOwned),

	/// Set create time.
	SetCreateTime(Date),

	/// Set modify time.
	SetModifyTime(Date),

	/// Set mode.
	SetMode(u32),

	/// Set owner.
	SetOwner(Did),

	/// Insert tags.
	TagsInsert(Tags),

	/// Remove tags.
	TagsRemove(Tags),

	/// Set file contents.
	/// Only applicable to [`Node::File`].
	SetContents(Cid, u64),

	/// Set link target.
	/// Only applicable to [`Node::Link`].
	SetLink(PathOwned),
}

impl Reducer<FileAction> for File {
	async fn reduce(
		state: OptionLink<Self>,
		event: Link<ReducerAction<FileAction>>,
		storage: &CoreBlockStorage,
	) -> Result<Link<Self>, anyhow::Error> {
		let action = storage.get_value(&event).await?;
		let mut result = storage.get_value_or_default(&state).await?;
		match &action.payload {
			FileAction::Create { path, node, recursive } => {
				reduce_create(storage, &mut result, path, node, &action.from, action.time, *recursive)
					.boxed()
					.await?;
			},
			FileAction::Remove { path, recursive } => {
				reduce_remove(storage, &mut result, path, *recursive).boxed().await?;
			},
			FileAction::Modify { path, modifications } => {
				reduce_modify(storage, &mut result, path, modifications).boxed().await?;
			},
		}
		Ok(storage.set_value(&result).await?)
	}
}

impl Node {
	pub fn name(&self) -> &str {
		match self {
			Node::Folder(node) => &node.name,
			Node::File(node) => &node.name,
			Node::Link(node) => &node.name,
		}
	}

	pub fn is_dir(&self) -> bool {
		matches!(self, Node::Folder(_))
	}

	pub fn is_file(&self) -> bool {
		matches!(self, Node::File(_))
	}

	pub fn is_link(&self) -> bool {
		matches!(self, Node::Link(_))
	}

	pub fn modify(
		&mut self,
		context: &mut FileModificationContext,
		modification: &FileModification,
	) -> anyhow::Result<()> {
		match self {
			Node::Folder(folder_node) => folder_node.modify(context, modification),
			Node::File(file_node) => file_node.modify(context, modification),
			Node::Link(link_node) => link_node.modify(context, modification),
		}
	}
}

impl FileNode {
	pub fn modify(
		&mut self,
		_context: &mut FileModificationContext,
		modification: &FileModification,
	) -> anyhow::Result<()> {
		match modification {
			FileModification::Rename(name) => {
				self.name = name.to_owned();
			},
			FileModification::Move(_) => {
				// nothing todo (files can not have children)
			},
			FileModification::SetCreateTime(time) => {
				self.create_time = *time;
			},
			FileModification::SetModifyTime(time) => {
				self.modify_time = *time;
			},
			FileModification::SetMode(mode) => {
				self.mode = *mode;
			},
			FileModification::SetOwner(owner) => {
				self.owner = owner.to_owned();
			},
			FileModification::TagsInsert(tags) => {
				self.tags.append(&mut tags.clone());
			},
			FileModification::TagsRemove(tags) => {
				self.tags.clear(Some(tags));
			},
			FileModification::SetContents(cid, size) => {
				self.contents = *cid;
				self.size = *size;
			},
			modification => return Err(anyhow!("Unsupported modification: {:?}", modification)),
		}
		Ok(())
	}
}

impl FolderNode {
	pub fn modify(
		&mut self,
		context: &mut FileModificationContext,
		modification: &FileModification,
	) -> anyhow::Result<()> {
		match modification {
			FileModification::Rename(name) => {
				if &self.name != name {
					context.reparent(
						context.path(),
						context
							.path()
							.parent()
							.ok_or(anyhow!("No parent: {}", context.path()))?
							.join_path(name)?,
					)?;
				}
				self.name = name.to_owned();
			},
			FileModification::Move(_to) => {
				// nothing todo (handles in `reduce_modify`)
			},
			FileModification::SetCreateTime(time) => {
				self.create_time = *time;
			},
			FileModification::SetModifyTime(time) => {
				self.modify_time = *time;
			},
			FileModification::SetMode(mode) => {
				self.mode = *mode;
			},
			FileModification::SetOwner(owner) => {
				self.owner = owner.to_owned();
			},
			FileModification::TagsInsert(tags) => {
				self.tags.append(&mut tags.clone());
			},
			FileModification::TagsRemove(tags) => {
				self.tags.clear(Some(tags));
			},
			modification => return Err(anyhow!("Unsupported modification: {:?}", modification)),
		}
		Ok(())
	}
}

impl LinkNode {
	pub fn modify(
		&mut self,
		_context: &mut FileModificationContext,
		modification: &FileModification,
	) -> anyhow::Result<()> {
		match modification {
			FileModification::Rename(name) => {
				self.name = name.to_owned();
			},
			FileModification::Move(_to) => {
				// TODO: change the symlink target?
				// nothing todo as links can not have children
			},
			// TODO: should symlink have own metadata? on posix they have:
			// A symlink has its own metadata, including:
			// Mode (permissions, typically lrwxrwxrwx)
			// (Possibly) a creation time, if supported by the filesystem
			// A modification time, reflecting changes to the symlink itself
			// The symlink's metadata is separate from that of the target file it points to.
			// FileModification::SetCreateTime(time) => {
			// 	self.create_time = *time;
			// },
			// FileModification::SetModifyTime(time) => {
			// 	self.modify_time = *time;
			// },
			// FileModification::SetMode(mode) => {
			// 	self.mode = *mode;
			// },
			// FileModification::SetOwner(owner) => {
			// 	self.owner = owner.to_owned();
			// },
			FileModification::TagsInsert(tags) => {
				self.tags.append(&mut tags.clone());
			},
			FileModification::TagsRemove(tags) => {
				self.tags.clear(Some(tags));
			},
			FileModification::SetLink(path) => {
				self.contents = path.to_owned();
			},
			modification => return Err(anyhow!("Unsupported modification: {:?}", modification)),
		}
		Ok(())
	}
}

async fn reduce_create(
	storage: &CoreBlockStorage,
	state: &mut File,
	path: &AbsolutePath,
	node: &Node,
	from: &Did,
	time: Date,
	recursive: bool,
) -> Result<(), anyhow::Error> {
	let path = path.normalize()?;

	// test if node exists
	let node_path = path.join_path(node.name())?;
	if get_node(storage, &state.nodes, &node_path, true).await?.is_some() {
		return Ok(());
	}

	// implicitly create empty root on first create
	let root: AbsolutePathOwned = AbsolutePath::new_unchecked("/").to_owned();
	if !state.nodes.contains(storage, &root).await? {
		state.nodes.insert(storage, root, Default::default()).await?;
	}

	// recursive?
	if recursive {
		for parent in path.paths() {
			let parent_owned = parent.to_owned();
			if !state.nodes.contains(storage, &parent_owned).await? {
				create_folder(storage, &mut state.nodes, parent, from, time).await?;
			}
		}
	}

	// insert if name not exists already
	create_node(storage, &mut state.nodes, &path, node.clone()).await
}

async fn reduce_remove(
	storage: &CoreBlockStorage,
	state: &mut File,
	path: &AbsolutePath,
	recursive: bool,
) -> Result<(), anyhow::Error> {
	let path = path.normalize()?;
	let (parent_path, name) = path.parent_and_file_name_result()?;

	// children
	let mut stack = VecDeque::new();
	stack.push_back(path.clone());
	while let Some(current) = stack.pop_front() {
		let children = state.nodes.get(storage, &current).await?;
		if let Some(children) = children {
			// do nothing if we still have children and not delete them
			if !recursive {
				return Ok(());
			}

			// queue children
			let child_nodes: Vec<Node> = children.stream(storage).try_collect().await?;
			for child in &child_nodes {
				stack.push_back(current.join_path(child.name())?);
			}

			// remove
			state.nodes.remove(storage, current).await?;
		}
	}

	// remove node from parent
	remove_node_by_name(storage, &mut state.nodes, parent_path, name).await?;

	Ok(())
}

async fn reduce_modify(
	storage: &CoreBlockStorage,
	state: &mut File,
	path: &AbsolutePath,
	modifications: &[FileModification],
) -> Result<(), anyhow::Error> {
	let path = path.normalize()?;
	let (parent_path, name) = path.parent_and_file_name_result()?;
	let parent_path = parent_path.to_owned();
	let mut file_modification_context = FileModificationContext::new(path.clone());

	// move node
	for to_parent in modifications.iter().filter_map(|item| match item {
		FileModification::Move(path) => Some(path),
		_ => None,
	}) {
		// validate: check `to_parent` exists
		let validated_to_parent = if to_parent == "/" {
			to_parent.to_owned()
		} else if let Some((to_parent, node)) = get_node(storage, &state.nodes, to_parent, true).await? {
			if !node.is_dir() {
				return Err(anyhow!("Can only move into folders: {}", to_parent));
			}
			to_parent
		} else {
			return Err(anyhow!("Not found: {}", to_parent));
		};

		// validate: check node `name` doesnt exist in `to_parent`
		let to_path = validated_to_parent.join_path(name)?;
		if get_node(storage, &state.nodes, &to_path, true).await?.is_some() {
			return Err(anyhow!("Node exists: {}", to_path));
		}

		// remove
		let removed = remove_node_by_name(storage, &mut state.nodes, &parent_path, name).await?;

		// insert
		for node in removed {
			create_node(storage, &mut state.nodes, &validated_to_parent, node).await?;
		}

		// reparent
		file_modification_context.reparent(path.clone(), to_path)?;
	}

	// update node
	let modifications: Vec<&FileModification> = modifications
		.iter()
		.filter_map(|item| match item {
			FileModification::Move(_) => None,
			modification => Some(modification),
		})
		.collect();
	if !modifications.is_empty() {
		// get the node set for the parent
		if let Some(mut node_set) = state.nodes.get(storage, &parent_path).await? {
			// check for rename conflicts
			for modification in modifications.iter() {
				if let FileModification::Rename(new_name) = modification {
					let has_conflict = node_set
						.stream(storage)
						.try_any(|node| std::future::ready(node.name() == new_name))
						.await?;
					if has_conflict {
						return Err(anyhow!("File exists: {}", parent_path.join_path(new_name)?));
					}
				}
			}

			// find and update the node
			let nodes: Vec<Node> = node_set.stream(storage).try_collect().await?;
			let mut updated_nodes = Vec::with_capacity(nodes.len());
			for mut node in nodes {
				if node.name() == name {
					for modification in modifications.iter() {
						node.modify(&mut file_modification_context, modification)?;
					}
				}
				updated_nodes.push(node);
			}
			node_set = CoSet::from_iter(storage, updated_nodes).await?;
			state.nodes.insert(storage, parent_path.clone(), node_set).await?;
		}
	}

	// reparent children nodes
	for (from, to) in file_modification_context.reparent.iter() {
		reparent(storage, &mut state.nodes, from, to).await?;
	}

	Ok(())
}

async fn reparent(
	storage: &CoreBlockStorage,
	nodes: &mut CoMap<AbsolutePathOwned, CoSet<Node>>,
	from: &AbsolutePath,
	to: &AbsolutePath,
) -> Result<(), anyhow::Error> {
	let from_owned = from.to_owned();
	if let Some(items) = nodes.remove(storage, from_owned).await? {
		// children
		let child_nodes: Vec<Node> = items.stream(storage).try_collect().await?;
		for child in &child_nodes {
			if child.is_dir() {
				Box::pin(reparent(storage, nodes, &from.join_path(child.name())?, &to.join_path(child.name())?))
					.await?;
			}
		}

		// self
		let to_owned = to.to_owned();
		if nodes.contains(storage, &to_owned).await? {
			return Err(anyhow!("Path exists: {}", to));
		}
		nodes.insert(storage, to_owned, items).await?;
	}
	Ok(())
}

#[derive(Debug)]
pub struct FileModificationContext {
	/// Current node path.
	path: AbsolutePathOwned,

	/// Reparent from -> to.
	reparent: BTreeMap<AbsolutePathOwned, AbsolutePathOwned>,
}
impl FileModificationContext {
	pub fn new(path: AbsolutePathOwned) -> Self {
		Self { path, reparent: Default::default() }
	}

	pub fn path(&self) -> AbsolutePathOwned {
		self.path.clone()
	}

	pub fn reparent(&mut self, from: AbsolutePathOwned, to: AbsolutePathOwned) -> Result<(), anyhow::Error> {
		let from = from.normalize()?;
		let to = to.normalize()?;
		if from != to {
			self.reparent.insert(from, to);
		}
		Ok(())
	}
}

/// Returns the node and its absolute path (without links if resolve_link is true).
async fn get_node(
	storage: &CoreBlockStorage,
	nodes: &CoMap<AbsolutePathOwned, CoSet<Node>>,
	path: &AbsolutePath,
	resolve_link: bool,
) -> Result<Option<(AbsolutePathOwned, Node)>, anyhow::Error> {
	let (parent_path, name) = path.parent_and_file_name_result()?;
	let parent_owned = parent_path.to_owned();
	let Some(node_set) = nodes.get(storage, &parent_owned).await? else {
		return Ok(None);
	};

	let all_nodes: Vec<Node> = node_set.stream(storage).try_collect().await?;
	let node = all_nodes.into_iter().find(|node| node.name() == name);

	// resolve_link
	if let Some(node) = &node {
		if resolve_link {
			if let Node::Link(link) = node {
				let target = parent_path.join(&link.contents)?;
				return Box::pin(get_node(storage, nodes, &target, resolve_link)).await;
			}
		}
	}

	Ok(node.map(|node| (path.to_owned(), node)))
}

async fn create_node(
	storage: &CoreBlockStorage,
	nodes: &mut CoMap<AbsolutePathOwned, CoSet<Node>>,
	parent_path: &AbsolutePath,
	node: Node,
) -> Result<(), anyhow::Error> {
	// validate parent exists
	let validated_parent_path = match parent_path.as_str() {
		// root always exists
		"/" => parent_path.to_owned(),
		// check if node exists
		_ => {
			get_node(storage, nodes, parent_path, true)
				.await?
				.ok_or(anyhow!("No such directory: {}", parent_path))?
				.0
		},
	};

	// get or create node set
	let mut node_set = nodes.get(storage, &validated_parent_path).await?.unwrap_or_default();

	// insert node if name not exists yet
	let all_nodes: Vec<Node> = node_set.stream(storage).try_collect().await?;
	let name_exists = all_nodes.iter().any(|existing| existing.name() == node.name());
	if !name_exists {
		node_set.insert(storage, node).await?;
		nodes.insert(storage, validated_parent_path, node_set).await?;
	}

	Ok(())
}

/// Remove node from set.
async fn remove_node_by_name(
	storage: &CoreBlockStorage,
	nodes: &mut CoMap<AbsolutePathOwned, CoSet<Node>>,
	parent_path: &AbsolutePath,
	name: &str,
) -> Result<BTreeSet<Node>, anyhow::Error> {
	let parent_owned = parent_path.to_owned();
	let node_set = nodes.get(storage, &parent_owned).await?.unwrap_or_default();

	let all_nodes: Vec<Node> = node_set.stream(storage).try_collect().await?;
	let mut kept = Vec::new();
	let mut removed = BTreeSet::new();
	for node in all_nodes {
		if node.name() == name {
			removed.insert(node);
		} else {
			kept.push(node);
		}
	}

	// store
	if kept.is_empty() && parent_path != "/" {
		nodes.remove(storage, parent_owned).await?;
	} else {
		let new_set = CoSet::from_iter(storage, kept).await?;
		nodes.insert(storage, parent_owned, new_set).await?;
	}

	Ok(removed)
}

async fn create_folder(
	storage: &CoreBlockStorage,
	nodes: &mut CoMap<AbsolutePathOwned, CoSet<Node>>,
	path: &AbsolutePath,
	from: &Did,
	time: Date,
) -> Result<(), anyhow::Error> {
	let (parent_path, name) = path.parent_and_file_name_result()?;
	let node = Node::Folder(FolderNode {
		name: name.to_owned(),
		create_time: time,
		modify_time: time,
		tags: tags!(),
		owner: from.to_owned(),
		mode: 0o665,
	});
	create_node(storage, nodes, parent_path, node).await
}

#[cfg(test)]
mod tests {
	use crate::{File, FileAction, FileModification, FileNode, Node};
	use co_api::{
		AbsolutePath, AbsolutePathOwned, BlockSerializer, BlockStorage, BlockStorageExt, CoreBlockStorage, Link,
		OptionLink, Reducer, ReducerAction,
	};
	use co_storage::MemoryBlockStorage;
	use futures::TryStreamExt;

	fn new_storage() -> MemoryBlockStorage {
		MemoryBlockStorage::default()
	}

	fn core_storage(storage: &MemoryBlockStorage) -> CoreBlockStorage {
		CoreBlockStorage::new(storage.clone(), false)
	}

	async fn create_test_file_state() -> (MemoryBlockStorage, Link<File>) {
		let storage = new_storage();

		// create
		let block = BlockSerializer::default().serialize(&"hello world").unwrap();
		let contents = *block.cid();
		storage.set(block).await.unwrap();
		let node = Node::File(FileNode {
			contents,
			create_time: 123,
			modify_time: 123,
			mode: 0o655,
			name: "test.txt".to_owned(),
			owner: "did:local:test".to_owned(),
			size: 11,
			tags: Default::default(),
		});
		let action = ReducerAction {
			from: "did:local:test".to_owned(),
			core: "file".to_owned(),
			time: 123,
			payload: FileAction::Create { path: "/hello/world".try_into().unwrap(), node, recursive: true },
		};
		let action_link: Link<ReducerAction<FileAction>> = storage.set_value(&action).await.unwrap();
		let state_link: OptionLink<File> = OptionLink::none();
		let cs = core_storage(&storage);
		let state_link = File::reduce(state_link, action_link, &cs).await.unwrap();
		let state: File = storage.get_value(&state_link).await.unwrap();

		// verify
		let paths = collect_paths(&storage, &state).await;
		assert_eq!(paths.len(), 3); // "/", "/hello", "/hello/world"
		assert_eq!(nodes_at(&storage, &state, "/").await.len(), 1); // "hello"
		assert_eq!(nodes_at(&storage, &state, "/hello").await.len(), 1); // "world"
		assert_eq!(nodes_at(&storage, &state, "/hello/world").await.len(), 1); // "test.txt"

		(storage, state_link)
	}

	async fn collect_paths(storage: &MemoryBlockStorage, state: &File) -> Vec<AbsolutePathOwned> {
		state
			.nodes
			.stream(storage)
			.map_ok(|(key, _): (AbsolutePathOwned, _)| key)
			.try_collect::<Vec<AbsolutePathOwned>>()
			.await
			.unwrap()
	}

	async fn nodes_at(storage: &MemoryBlockStorage, state: &File, path: &str) -> Vec<Node> {
		let path_owned = AbsolutePath::new_unchecked(path).to_owned();
		match state.nodes.get(storage, &path_owned).await.unwrap() {
			Some(set) => set.stream(storage).try_collect().await.unwrap(),
			None => vec![],
		}
	}

	async fn names(storage: &MemoryBlockStorage, state: &File, path: &str) -> Vec<String> {
		nodes_at(storage, state, path)
			.await
			.iter()
			.map(|node| node.name().to_owned())
			.collect()
	}

	async fn reduce_action(
		storage: &MemoryBlockStorage,
		state_link: Link<File>,
		action: ReducerAction<FileAction>,
	) -> (File, Link<File>) {
		let action_link: Link<ReducerAction<FileAction>> = storage.set_value(&action).await.unwrap();
		let cs = core_storage(storage);
		let next_link = File::reduce(state_link.into(), action_link, &cs).await.unwrap();
		let state: File = storage.get_value(&next_link).await.unwrap();
		(state, next_link)
	}

	#[tokio::test]
	async fn test_create() {
		let (_storage, _state_link) = create_test_file_state().await;
	}

	#[tokio::test]
	async fn test_delete_recursive() {
		let (storage, state_link) = create_test_file_state().await;

		let action = ReducerAction {
			from: "did:local:test".to_owned(),
			core: "file".to_owned(),
			time: 456,
			payload: FileAction::Remove { path: "/hello".try_into().unwrap(), recursive: true },
		};
		let (state, _) = reduce_action(&storage, state_link, action).await;
		let paths = collect_paths(&storage, &state).await;
		assert_eq!(paths.len(), 1); // "/"
		assert_eq!(nodes_at(&storage, &state, "/").await.len(), 0);
	}

	#[tokio::test]
	async fn test_modify_rename() {
		let (storage, state_link) = create_test_file_state().await;

		let action = ReducerAction {
			from: "did:local:test".to_owned(),
			core: "file".to_owned(),
			time: 456,
			payload: FileAction::Modify {
				path: "/hello/world/test.txt".try_into().unwrap(),
				modifications: vec![FileModification::Rename("welcome.txt".to_owned())],
			},
		};
		let (state, _) = reduce_action(&storage, state_link, action).await;
		let files = nodes_at(&storage, &state, "/hello/world").await;
		assert_eq!(files.len(), 1);
		assert_eq!(files.first().unwrap().name(), "welcome.txt");
	}

	#[tokio::test]
	async fn test_modify_rename_with_children() {
		let (storage, state_link) = create_test_file_state().await;

		let action = ReducerAction {
			from: "did:local:test".to_owned(),
			core: "file".to_owned(),
			time: 456,
			payload: FileAction::Modify {
				path: "/hello".try_into().unwrap(),
				modifications: vec![FileModification::Rename("test".to_owned())],
			},
		};
		let (state, _) = reduce_action(&storage, state_link, action).await;
		let mut paths: Vec<String> = collect_paths(&storage, &state).await.iter().map(|p| p.to_string()).collect();
		paths.sort();
		assert_eq!(paths, vec!["/", "/test", "/test/world"]);
		assert_eq!(names(&storage, &state, "/").await, vec!["test"]);
		assert_eq!(names(&storage, &state, "/test").await, vec!["world"]);
		assert_eq!(names(&storage, &state, "/test/world").await, vec!["test.txt"]);
	}

	#[tokio::test]
	async fn test_modify_move() {
		let (storage, state_link) = create_test_file_state().await;

		let action = ReducerAction {
			from: "did:local:test".to_owned(),
			core: "file".to_owned(),
			time: 456,
			payload: FileAction::Modify {
				path: "/hello/world".try_into().unwrap(),
				modifications: vec![FileModification::Move("/".try_into().unwrap())],
			},
		};
		let (state, _) = reduce_action(&storage, state_link, action).await;
		let mut paths: Vec<String> = collect_paths(&storage, &state).await.iter().map(|p| p.to_string()).collect();
		paths.sort();
		assert_eq!(paths, vec!["/", "/world"]); // "/hello" is empty now
		let mut root_names = names(&storage, &state, "/").await;
		root_names.sort();
		assert_eq!(root_names, vec!["hello", "world"]);
		assert!(names(&storage, &state, "/hello").await.is_empty());
		assert_eq!(names(&storage, &state, "/world").await, vec!["test.txt"]);
	}

	#[tokio::test]
	async fn test_modify_move_file() {
		let (storage, state_link) = create_test_file_state().await;

		let action = ReducerAction {
			from: "did:local:test".to_owned(),
			core: "file".to_owned(),
			time: 456,
			payload: FileAction::Modify {
				path: "/hello/world/test.txt".try_into().unwrap(),
				modifications: vec![FileModification::Move("/hello".try_into().unwrap())],
			},
		};
		let (state, _) = reduce_action(&storage, state_link, action).await;
		let mut paths: Vec<String> = collect_paths(&storage, &state).await.iter().map(|p| p.to_string()).collect();
		paths.sort();
		assert_eq!(paths, vec!["/", "/hello"]); // "/world" is empty now
		assert_eq!(names(&storage, &state, "/").await, vec!["hello"]);
		let mut hello_names = names(&storage, &state, "/hello").await;
		hello_names.sort();
		assert_eq!(hello_names, vec!["test.txt", "world"]);
		assert!(names(&storage, &state, "/hello/world").await.is_empty());
	}
}