Repository navigation
Expand file tree
/
Copy pathtest_memory.py
More file actions
77 lines (50 loc) · 1.9 KB
/
Copy pathtest_memory.py
File metadata and controls
77 lines (50 loc) · 1.9 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Memory V1 — service round trip and inject-all behaviour (★②)."""
from __future__ import annotations
import asyncio
from lang2sql.memory import (
InMemoryStore,
InjectAllRecall,
ManualExtractor,
MemoryService,
)
def _service() -> MemoryService:
return MemoryService(InMemoryStore(), InjectAllRecall(), ManualExtractor())
def test_remember_then_recall_round_trip() -> None:
svc = _service()
async def run() -> None:
fact = await svc.remember("u1", "revenue excludes cancelled orders")
assert fact.id
assert fact.source == "manual"
assert fact.ts > 0
facts = await svc.recall("u1", "anything")
assert [f.text for f in facts] == ["revenue excludes cancelled orders"]
asyncio.run(run())
def test_inject_all_returns_every_fact_for_owner() -> None:
svc = _service()
async def run() -> None:
await svc.remember("u1", "fact a")
await svc.remember("u1", "fact b")
await svc.remember("u2", "other owner")
facts = await svc.recall("u1", "ignored query")
assert {f.text for f in facts} == {"fact a", "fact b"}
asyncio.run(run())
def test_recall_empty_owner_is_empty_and_renders_blank() -> None:
svc = _service()
async def run() -> None:
facts = await svc.recall("nobody", "q")
assert facts == []
assert svc.render(facts) == ""
asyncio.run(run())
def test_render_produces_markdown_block() -> None:
svc = _service()
async def run() -> None:
await svc.remember("u1", "fact a")
rendered = svc.render(await svc.recall("u1", "q"))
assert "## Remembered facts" in rendered
assert "- fact a" in rendered
asyncio.run(run())
def test_manual_extractor_yields_nothing() -> None:
extractor = ManualExtractor()
async def run() -> None:
assert await extractor.extract("u1", []) == []
asyncio.run(run())