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.0 kB 179 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; 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 struct World { 56 _dir: TempDir, 57 pub meta_path: PathBuf, 58 pub layout: Layout, 59 pub signer: K256Signer, 60} 61 62impl World { 63 pub fn new() -> Self { 64 Self::seeded(1) 65 } 66 67 pub fn seeded(seed: u64) -> Self { 68 let dir = tempfile::tempdir().unwrap(); 69 let meta_path = dir.path().join("meta"); 70 Repo::create(&meta_path).unwrap(); 71 let layout = Layout::new(dir.path().join("repos")); 72 let signer = K256Signer::generate(&SeededEntropy::new(seed)); 73 Self { 74 _dir: dir, 75 meta_path, 76 layout, 77 signer, 78 } 79 } 80 81 pub fn index(&self) -> Index { 82 Index::new(&self.meta_path, self.layout.clone()) 83 } 84 85 pub fn seed_members(&self) -> CobId { 86 let meta = Repo::open(&self.meta_path).unwrap(); 87 let store = CobStore::new(&meta); 88 let created = store 89 .create( 90 &meta_home(), 91 &MembersChange::Add(grant("nel", "nel", 1)), 92 &self.signer, 93 at(1), 94 ) 95 .unwrap(); 96 store 97 .update( 98 &meta_home(), 99 created.object, 100 &MembersChange::Add(grant("olaren", "nel", 2)), 101 &self.signer, 102 at(2), 103 ) 104 .unwrap(); 105 created.object 106 } 107 108 pub fn add_member(&self, object: CobId, subject: &str, seconds: i64) { 109 let meta = Repo::open(&self.meta_path).unwrap(); 110 let store = CobStore::new(&meta); 111 store 112 .update( 113 &meta_home(), 114 object, 115 &MembersChange::Add(grant(subject, "nel", seconds)), 116 &self.signer, 117 at(seconds), 118 ) 119 .unwrap(); 120 } 121 122 pub fn seed_registry(&self, repo: &RepoDid) -> CobId { 123 let meta = Repo::open(&self.meta_path).unwrap(); 124 let store = CobStore::new(&meta); 125 store 126 .create( 127 &meta_home(), 128 &RegistryChange::Register(registration("nel", "anemone", repo, 1)), 129 &self.signer, 130 at(1), 131 ) 132 .unwrap() 133 .object 134 } 135 136 pub fn register_extra(&self, repo: &RepoDid, key: &str, registry: CobId) { 137 let meta = Repo::open(&self.meta_path).unwrap(); 138 let store = CobStore::new(&meta); 139 store 140 .update( 141 &meta_home(), 142 registry, 143 &RegistryChange::Register(registration("nel", key, repo, 2)), 144 &self.signer, 145 at(2), 146 ) 147 .unwrap(); 148 } 149 150 pub fn seed_collaborator(&self, repo: &RepoDid, subject: &str) -> CobId { 151 let git = self.layout.create(repo).unwrap(); 152 let store = CobStore::new(&git); 153 store 154 .create( 155 &CobHome::from(repo), 156 &CollaboratorsChange::Add(grant(subject, "nel", 1)), 157 &self.signer, 158 at(1), 159 ) 160 .unwrap() 161 .object 162 } 163 164 pub fn remove_collaborator(&self, repo: &RepoDid, object: CobId, subject: &str, seconds: i64) { 165 let git = self.layout.open(repo).unwrap(); 166 let store = CobStore::new(&git); 167 store 168 .update( 169 &CobHome::from(repo), 170 object, 171 &CollaboratorsChange::Remove(Removal { 172 subject: acc(subject), 173 }), 174 &self.signer, 175 at(seconds), 176 ) 177 .unwrap(); 178 } 179}