This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / knot2 / crates / knot-index / tests / common / mod.rs
5.7 kB 203 lines
1#![allow(dead_code)] 2 3use std::path::PathBuf; 4 5use knot_cob::{CobHome, CobId, CobStore}; 6use knot_cobs::{CollaboratorsChange, Grant, MembersChange, Registration, RegistryChange, Removal}; 7use knot_git::{Layout, Repo}; 8use knot_index::{Index, KeyBudget}; 9use knot_runtime::{K256Signer, SeededEntropy}; 10use knot_types::{AccountDid, KnotId, OwnerDid, RepoDid, RepoName, RepoRkey, UnixSeconds}; 11use tempfile::TempDir; 12 13pub fn acc(suffix: &str) -> AccountDid { 14 AccountDid::new(format!("did:plc:{suffix}")).unwrap() 15} 16 17pub fn own(suffix: &str) -> OwnerDid { 18 OwnerDid::new(format!("did:plc:{suffix}")).unwrap() 19} 20 21pub fn repo_did(suffix: &str) -> RepoDid { 22 RepoDid::new(format!("did:plc:{suffix}")).unwrap() 23} 24 25pub fn rkey(value: &str) -> RepoRkey { 26 RepoRkey::new(value).unwrap() 27} 28 29pub fn at(seconds: i64) -> UnixSeconds { 30 UnixSeconds::new(seconds) 31} 32 33pub fn meta_home() -> CobHome { 34 CobHome::from(&KnotId::new("did:web:knot.nel.pet").unwrap()) 35} 36 37pub fn grant(subject: &str, added_by: &str, seconds: i64) -> Grant { 38 Grant { 39 subject: acc(subject), 40 added_by: acc(added_by), 41 created_at: at(seconds), 42 } 43} 44 45pub fn registration(owner_id: &str, key: &str, repo: &RepoDid, seconds: i64) -> Registration { 46 Registration { 47 owner: own(owner_id), 48 rkey: rkey(key), 49 name: RepoName::new(key).unwrap(), 50 repo: repo.clone(), 51 created_at: at(seconds), 52 } 53} 54 55pub fn named_registration( 56 owner_id: &str, 57 key: &str, 58 display: &str, 59 repo: &RepoDid, 60 seconds: i64, 61) -> Registration { 62 Registration { 63 owner: own(owner_id), 64 rkey: rkey(key), 65 name: RepoName::new(display).unwrap(), 66 repo: repo.clone(), 67 created_at: at(seconds), 68 } 69} 70 71pub struct World { 72 _dir: TempDir, 73 pub meta_path: PathBuf, 74 pub layout: Layout, 75 pub signer: K256Signer, 76} 77 78impl World { 79 pub fn new() -> Self { 80 Self::seeded(1) 81 } 82 83 pub fn seeded(seed: u64) -> Self { 84 let dir = tempfile::tempdir().unwrap(); 85 let meta_path = dir.path().join("meta"); 86 Repo::create(&meta_path).unwrap(); 87 let layout = Layout::new(dir.path().join("repos")); 88 let signer = K256Signer::generate(&SeededEntropy::new(seed)); 89 Self { 90 _dir: dir, 91 meta_path, 92 layout, 93 signer, 94 } 95 } 96 97 pub fn index(&self) -> Index { 98 Index::new(&self.meta_path, self.layout.clone()) 99 } 100 101 pub fn index_within(&self, budget: KeyBudget) -> Index { 102 Index::with_key_budget(&self.meta_path, self.layout.clone(), budget) 103 } 104 105 pub fn seed_members(&self) -> CobId { 106 let meta = Repo::open(&self.meta_path).unwrap(); 107 let store = CobStore::new(&meta); 108 let created = store 109 .create( 110 &meta_home(), 111 &MembersChange::Add(grant("nel", "nel", 1)), 112 &self.signer, 113 at(1), 114 ) 115 .unwrap(); 116 store 117 .update( 118 &meta_home(), 119 created.object, 120 &MembersChange::Add(grant("olaren", "nel", 2)), 121 &self.signer, 122 at(2), 123 ) 124 .unwrap(); 125 created.object 126 } 127 128 pub fn add_member(&self, object: CobId, subject: &str, seconds: i64) { 129 let meta = Repo::open(&self.meta_path).unwrap(); 130 let store = CobStore::new(&meta); 131 store 132 .update( 133 &meta_home(), 134 object, 135 &MembersChange::Add(grant(subject, "nel", seconds)), 136 &self.signer, 137 at(seconds), 138 ) 139 .unwrap(); 140 } 141 142 pub fn seed_registry(&self, repo: &RepoDid) -> CobId { 143 let meta = Repo::open(&self.meta_path).unwrap(); 144 let store = CobStore::new(&meta); 145 store 146 .create( 147 &meta_home(), 148 &RegistryChange::Register(registration("nel", "anemone", repo, 1)), 149 &self.signer, 150 at(1), 151 ) 152 .unwrap() 153 .object 154 } 155 156 pub fn register_extra(&self, repo: &RepoDid, key: &str, registry: CobId) { 157 self.register_owned(repo, key, "nel", registry); 158 } 159 160 pub fn register_owned(&self, repo: &RepoDid, key: &str, owner: &str, registry: CobId) { 161 let meta = Repo::open(&self.meta_path).unwrap(); 162 let store = CobStore::new(&meta); 163 store 164 .update( 165 &meta_home(), 166 registry, 167 &RegistryChange::Register(registration(owner, key, repo, 2)), 168 &self.signer, 169 at(2), 170 ) 171 .unwrap(); 172 } 173 174 pub fn seed_collaborator(&self, repo: &RepoDid, subject: &str) -> CobId { 175 let git = self.layout.create(repo).unwrap(); 176 let store = CobStore::new(&git); 177 store 178 .create( 179 &CobHome::from(repo), 180 &CollaboratorsChange::Add(grant(subject, "nel", 1)), 181 &self.signer, 182 at(1), 183 ) 184 .unwrap() 185 .object 186 } 187 188 pub fn remove_collaborator(&self, repo: &RepoDid, object: CobId, subject: &str, seconds: i64) { 189 let git = self.layout.open(repo).unwrap(); 190 let store = CobStore::new(&git); 191 store 192 .update( 193 &CobHome::from(repo), 194 object, 195 &CollaboratorsChange::Remove(Removal { 196 subject: acc(subject), 197 }), 198 &self.signer, 199 at(seconds), 200 ) 201 .unwrap(); 202 } 203}