File size: 1,236 Bytes
1da7ac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# tests/test_spawn.py
from fractus_vorax.hv import bundle, hash_hv
from fractus_vorax.organs.spawn import ExpertRegistry

CAP_TOKENS = ["what", "is", "the", "capital", "of"]
BOOK_TOKENS = ["who", "wrote"]


def _sig(tokens, D=2048):
    return bundle([hash_hv(t, D) for t in tokens])


def test_spawn_and_route():
    reg = ExpertRegistry()
    reg.spawn("capitals", _sig(CAP_TOKENS), 0, 4)
    reg.spawn("books", _sig(BOOK_TOKENS), 5, 9)
    assert len(reg) == 2
    hits = reg.route(_sig(["what", "is", "the", "capital", "of", "germany"]))
    assert hits[0][0] == "capitals"
    assert hits[0][1] > 0.5


def test_route_empty():
    assert ExpertRegistry().route(_sig(CAP_TOKENS)) == []


def test_get():
    reg = ExpertRegistry()
    e = reg.spawn("capitals", _sig(CAP_TOKENS), 0, 4)
    assert reg.get("capitals") == e
    assert reg.get("nope") is None


def test_save_load_roundtrip(tmp_path):
    reg = ExpertRegistry()
    reg.spawn("capitals", _sig(CAP_TOKENS), 0, 4)
    reg.spawn("books", _sig(BOOK_TOKENS), 5, 9)
    reg.save(tmp_path / "experts")
    loaded = ExpertRegistry.load(tmp_path / "experts")
    assert len(loaded) == 2
    hits = loaded.route(_sig(["who", "wrote", "hamlet"]))
    assert hits[0][0] == "books"