⎇ 53dbdf0f1deea10d6fc567ea2b43e1e75d6d60cd
crates/modules/semantic_dht/minga-store/tests/repo_integration.rs
History
Who touched what
raw
chore: monorepo inicial con arje + minga + yahweh absorbidos
1//! Test de integración del `PersistentRepo`: los tres stores conviven
2//! en una misma `sled::Db`, escritos en una sesión y recuperados
3//! intactos en la siguiente.
4
5use minga_core::{parse, Attestation, ContentHash, Keypair};
6use minga_store::PersistentRepo;
7use tempfile::TempDir;
8
9#[test]
10fn three_stores_persist_together_across_reopen() {
11 let dir = TempDir::new().unwrap();
12 let path = dir.path();
13 let alice = Keypair::from_seed(&[1; 32]);
14
15 // ── Sesión 1: poblamos el repo ──────────────────────────────────
16 let function_hash;
17 let target_root_hash;
18 {
19 let repo = PersistentRepo::open(path).unwrap();
20 let n = parse::rust("fn add(x: i32, y: i32) -> i32 { x + y }").unwrap();
21 function_hash = repo.nodes.put(&n).unwrap();
22 repo.mst.insert(function_hash).unwrap();
23 repo.attestations
24 .add(Attestation::create(&alice, function_hash))
25 .unwrap();
26
27 target_root_hash = repo.mst.to_in_memory().unwrap().root_hash();
28 repo.flush().unwrap();
29 }
30
31 // ── Sesión 2: reabrimos y verificamos integridad ────────────────
32 {
33 let repo = PersistentRepo::open(path).unwrap();
34
35 // Nodo recuperable.
36 let stored = repo.nodes.get(&function_hash).unwrap().unwrap();
37 assert_eq!(stored.kind, "source_file");
38
39 // Reconstrucción completa idéntica al original.
40 let reconstructed = repo.nodes.reconstruct(&function_hash).unwrap().unwrap();
41 let original = parse::rust("fn add(x: i32, y: i32) -> i32 { x + y }").unwrap();
42 assert_eq!(reconstructed, original);
43
44 // MST: misma raíz tras reconstruir.
45 assert_eq!(
46 repo.mst.to_in_memory().unwrap().root_hash(),
47 target_root_hash
48 );
49
50 // Atestación: sigue ahí, sigue verificable.
51 let authors = repo.attestations.authors_of(&function_hash).unwrap();
52 assert_eq!(authors, vec![alice.did()]);
53 let atts = repo.attestations.get(&function_hash).unwrap();
54 assert!(atts[0].verify());
55 }
56}
57
58#[test]
59fn repo_supports_multiple_functions_and_authors() {
60 let dir = TempDir::new().unwrap();
61 let repo = PersistentRepo::open(dir.path()).unwrap();
62
63 let alice = Keypair::from_seed(&[1; 32]);
64 let bob = Keypair::from_seed(&[2; 32]);
65
66 let mut hashes: Vec<ContentHash> = Vec::new();
67 for src in &[
68 "fn one() -> i32 { 1 }",
69 "fn two() -> i32 { 2 }",
70 "fn three(x: i32) -> i32 { x + 1 }",
71 ] {
72 let n = parse::rust(src).unwrap();
73 let h = repo.nodes.put(&n).unwrap();
74 repo.mst.insert(h).unwrap();
75 hashes.push(h);
76 }
77
78 // Alice firma las tres; Bob firma solo la primera.
79 for h in &hashes {
80 repo.attestations
81 .add(Attestation::create(&alice, *h))
82 .unwrap();
83 }
84 repo.attestations
85 .add(Attestation::create(&bob, hashes[0]))
86 .unwrap();
87
88 repo.flush().unwrap();
89
90 assert_eq!(repo.mst.len(), 3);
91 assert_eq!(repo.attestations.len(), 4);
92
93 // La función firmada por ambos tiene dos autores.
94 let authors_first = repo.attestations.authors_of(&hashes[0]).unwrap();
95 assert_eq!(authors_first.len(), 2);
96 assert!(authors_first.contains(&alice.did()));
97 assert!(authors_first.contains(&bob.did()));
98
99 // Las otras dos solo tienen a Alice.
100 assert_eq!(
101 repo.attestations.authors_of(&hashes[1]).unwrap(),
102 vec![alice.did()]
103 );
104}