| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Helpers for the unit tests: an in-memory repository with short ways to |
| 5 | //! write files, trees and changesets, and to read them back. |
| 6 | |
| 7 | use std::cell::RefCell; |
| 8 | use std::collections::BTreeMap; |
| 9 | use std::convert::Infallible; |
| 10 | |
| 11 | use braid_format::{ |
| 12 | Change, ChangeOp, EntryKind, GitId, Kind, MemoryObjects, ObjectId, Objects, Signature, |
| 13 | }; |
| 14 | |
| 15 | /// Objects in memory that remember the hints they get: the id stored and |
| 16 | /// the id it was said to be similar to. |
| 17 | #[derive(Default)] |
| 18 | pub(crate) struct Hinted { |
| 19 | pub(crate) objects: MemoryObjects, |
| 20 | pub(crate) hints: RefCell<Vec<(ObjectId, ObjectId)>>, |
| 21 | } |
| 22 | |
| 23 | impl Objects for Hinted { |
| 24 | type Error = Infallible; |
| 25 | |
| 26 | fn get(&self, id: &ObjectId) -> Result<Option<(Kind, Vec<u8>)>, Self::Error> { |
| 27 | self.objects.get(id) |
| 28 | } |
| 29 | |
| 30 | fn put(&self, kind: Kind, payload: &[u8]) -> Result<ObjectId, Self::Error> { |
| 31 | self.objects.put(kind, payload) |
| 32 | } |
| 33 | |
| 34 | fn put_similar( |
| 35 | &self, |
| 36 | kind: Kind, |
| 37 | payload: &[u8], |
| 38 | similar: &ObjectId, |
| 39 | ) -> Result<ObjectId, Self::Error> { |
| 40 | let id = self.objects.put(kind, payload)?; |
| 41 | self.hints.borrow_mut().push((id, *similar)); |
| 42 | Ok(id) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | use crate::access::{EMPTY_TREE, read_blob, read_changeset, read_tree}; |
| 47 | use crate::apply::apply; |
| 48 | use crate::commit::commit; |
| 49 | use crate::path::join; |
| 50 | |
| 51 | pub(crate) fn signature(name: &str) -> Signature { |
| 52 | Signature { |
| 53 | name: name.as_bytes().to_vec(), |
| 54 | email: format!("{name}@example.org").into_bytes(), |
| 55 | seconds: 1_700_000_000, |
| 56 | offset_minutes: 60, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | pub(crate) fn delete(path: &str) -> Change { |
| 61 | Change { |
| 62 | path: path.as_bytes().to_vec(), |
| 63 | op: ChangeOp::Delete, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | pub(crate) fn gitlink(path: &str, byte: u8) -> Change { |
| 68 | Change { |
| 69 | path: path.as_bytes().to_vec(), |
| 70 | op: ChangeOp::Gitlink { |
| 71 | commit: GitId::Sha1([byte; 20]), |
| 72 | }, |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub(crate) struct Repo { |
| 77 | pub(crate) objects: MemoryObjects, |
| 78 | } |
| 79 | |
| 80 | impl Repo { |
| 81 | pub(crate) fn new() -> Self { |
| 82 | Repo { |
| 83 | objects: MemoryObjects::new(), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub(crate) fn blob(&self, content: &str) -> ObjectId { |
| 88 | self.objects.put(Kind::Blob, content.as_bytes()).unwrap() |
| 89 | } |
| 90 | |
| 91 | pub(crate) fn file_kind(&self, content: &str) -> EntryKind { |
| 92 | EntryKind::File { |
| 93 | blob: self.blob(content), |
| 94 | size: content.len() as u64, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub(crate) fn set(&self, path: &str, content: &str) -> Change { |
| 99 | Change { |
| 100 | path: path.as_bytes().to_vec(), |
| 101 | op: ChangeOp::File { |
| 102 | blob: self.blob(content), |
| 103 | size: content.len() as u64, |
| 104 | }, |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub(crate) fn exec(&self, path: &str, content: &str) -> Change { |
| 109 | Change { |
| 110 | path: path.as_bytes().to_vec(), |
| 111 | op: ChangeOp::Executable { |
| 112 | blob: self.blob(content), |
| 113 | size: content.len() as u64, |
| 114 | }, |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub(crate) fn symlink(&self, path: &str, target: &str) -> Change { |
| 119 | Change { |
| 120 | path: path.as_bytes().to_vec(), |
| 121 | op: ChangeOp::Symlink { |
| 122 | blob: self.blob(target), |
| 123 | size: target.len() as u64, |
| 124 | }, |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// A tree with these regular files. |
| 129 | pub(crate) fn tree(&self, files: &[(&str, &str)]) -> ObjectId { |
| 130 | let changes: Vec<Change> = files |
| 131 | .iter() |
| 132 | .map(|(path, content)| self.set(path, content)) |
| 133 | .collect(); |
| 134 | apply(&self.objects, EMPTY_TREE, &changes).unwrap() |
| 135 | } |
| 136 | |
| 137 | pub(crate) fn commit(&self, parents: &[ObjectId], changes: Vec<Change>) -> ObjectId { |
| 138 | commit( |
| 139 | &self.objects, |
| 140 | parents.to_vec(), |
| 141 | changes, |
| 142 | signature("author"), |
| 143 | signature("author"), |
| 144 | b"change\n".to_vec(), |
| 145 | Vec::new(), |
| 146 | ) |
| 147 | .unwrap() |
| 148 | } |
| 149 | |
| 150 | pub(crate) fn root(&self, changeset: ObjectId) -> ObjectId { |
| 151 | read_changeset(&self.objects, changeset).unwrap().root |
| 152 | } |
| 153 | |
| 154 | /// Every file under `root`, as text: regular files by content, |
| 155 | /// executables as `exec:<content>`, symlinks as `link:<target>` and |
| 156 | /// gitlinks as `gitlink:<first byte>`. |
| 157 | pub(crate) fn files(&self, root: ObjectId) -> BTreeMap<String, String> { |
| 158 | let mut files = BTreeMap::new(); |
| 159 | let mut pending = vec![(Vec::new(), root)]; |
| 160 | while let Some((dir, tree)) = pending.pop() { |
| 161 | for entry in read_tree(&self.objects, tree).unwrap() { |
| 162 | let path = join(&dir, &entry.name); |
| 163 | let text = |blob: ObjectId| { |
| 164 | String::from_utf8(read_blob(&self.objects, blob).unwrap()).unwrap() |
| 165 | }; |
| 166 | let value = match entry.kind { |
| 167 | EntryKind::Tree { tree } => { |
| 168 | pending.push((path, tree)); |
| 169 | continue; |
| 170 | } |
| 171 | EntryKind::File { blob, .. } => text(blob), |
| 172 | EntryKind::Executable { blob, .. } => format!("exec:{}", text(blob)), |
| 173 | EntryKind::Symlink { blob, .. } => format!("link:{}", text(blob)), |
| 174 | EntryKind::Gitlink { commit } => format!("gitlink:{}", commit.as_bytes()[0]), |
| 175 | }; |
| 176 | files.insert(String::from_utf8(path).unwrap(), value); |
| 177 | } |
| 178 | } |
| 179 | files |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /// A path with `depth` names: `d/d/…/d/file`. |
| 184 | pub(crate) fn deep_path(depth: usize) -> String { |
| 185 | let mut names = vec!["d"; depth - 1]; |
| 186 | names.push("file"); |
| 187 | names.join("/") |
| 188 | } |
| 189 | |
| 190 | /// A map from path to text, for comparing with [`Repo::files`]. |
| 191 | pub(crate) fn text_map(files: &[(&str, &str)]) -> BTreeMap<String, String> { |
| 192 | files |
| 193 | .iter() |
| 194 | .map(|(path, value)| (path.to_string(), value.to_string())) |
| 195 | .collect() |
| 196 | } |