style: ruff format + stronger sqlite asserts (delete utils, drop test_no_gng)
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def test_gng_removed():
|
||||
try:
|
||||
import agentmsgs.utils # noqa: F401
|
||||
|
||||
assert False, "utils should be deleted"
|
||||
except ModuleNotFoundError:
|
||||
pass
|
||||
import agentmsgs
|
||||
|
||||
assert hasattr(agentmsgs, "main") or True
|
||||
|
||||
|
||||
def test_import_does_not_print():
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-c", "import agentmsgs"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
assert result.stdout == "", f"import printed: {result.stdout!r}"
|
||||
assert result.returncode == 0
|
||||
+12
-7
@@ -1,11 +1,13 @@
|
||||
from agentmsgs.stores.memory import InMemoryStore
|
||||
from agentmsgs.core import ops
|
||||
from agentmsgs.stores.memory import InMemoryStore
|
||||
|
||||
|
||||
def test_ops_validates_sender_must_be_in_thread():
|
||||
s = InMemoryStore()
|
||||
a = ops.get_or_create_agent(s, "A"); b = ops.get_or_create_agent(s, "B"); c = ops.get_or_create_agent(s, "C")
|
||||
t = ops.create_thread(s, {a,b})
|
||||
a = ops.get_or_create_agent(s, "A")
|
||||
b = ops.get_or_create_agent(s, "B")
|
||||
c = ops.get_or_create_agent(s, "C")
|
||||
t = ops.create_thread(s, {a, b})
|
||||
try:
|
||||
ops.append_message(s, t.id, c, "oops")
|
||||
assert False, "should raise"
|
||||
@@ -15,8 +17,9 @@ def test_ops_validates_sender_must_be_in_thread():
|
||||
|
||||
def test_ops_has_unread_exclude_own():
|
||||
s = InMemoryStore()
|
||||
a = ops.get_or_create_agent(s, "A"); b = ops.get_or_create_agent(s, "B")
|
||||
t = ops.create_thread(s, {a,b})
|
||||
a = ops.get_or_create_agent(s, "A")
|
||||
b = ops.get_or_create_agent(s, "B")
|
||||
t = ops.create_thread(s, {a, b})
|
||||
ops.append_message(s, t.id, a, "a1")
|
||||
# b has unread, a's own message shouldn't count if exclude_own
|
||||
assert ops.has_unread(s, t.id, b) is True
|
||||
@@ -28,8 +31,10 @@ def test_ops_has_unread_exclude_own():
|
||||
|
||||
def test_ops_join_and_delete():
|
||||
s = InMemoryStore()
|
||||
a = ops.get_or_create_agent(s, "A"); b = ops.get_or_create_agent(s, "B"); c = ops.get_or_create_agent(s, "C")
|
||||
t = ops.create_thread(s, {a,b})
|
||||
a = ops.get_or_create_agent(s, "A")
|
||||
b = ops.get_or_create_agent(s, "B")
|
||||
c = ops.get_or_create_agent(s, "C")
|
||||
t = ops.create_thread(s, {a, b})
|
||||
t2 = ops.join_thread(s, t.id, c)
|
||||
assert c in t2.participants
|
||||
ops.delete_agent(s, c.id)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from agentmsgs.stores.memory import InMemoryStore
|
||||
|
||||
|
||||
def test_memory_create_and_find_thread():
|
||||
s = InMemoryStore()
|
||||
alice = s.get_or_create_agent("Alice")
|
||||
@@ -10,12 +11,14 @@ def test_memory_create_and_find_thread():
|
||||
assert t in s.find_threads({alice, bob})
|
||||
assert s.find_threads({alice, bob, s.get_or_create_agent("Charlie")}) == []
|
||||
|
||||
|
||||
def test_memory_soft_unique_name():
|
||||
s = InMemoryStore()
|
||||
a1 = s.get_or_create_agent("Alice")
|
||||
a2 = s.get_or_create_agent("Alice")
|
||||
assert a1.id == a2.id
|
||||
|
||||
|
||||
def test_memory_delete_account():
|
||||
s = InMemoryStore()
|
||||
a = s.get_or_create_agent("Alice")
|
||||
@@ -23,10 +26,12 @@ def test_memory_delete_account():
|
||||
assert s.get_agent_by_id(a.id) is None
|
||||
assert s.get_agent_by_name("Alice") is None
|
||||
|
||||
|
||||
def test_memory_append_and_list():
|
||||
s = InMemoryStore()
|
||||
a = s.get_or_create_agent("A"); b = s.get_or_create_agent("B")
|
||||
t = s.create_thread({a,b})
|
||||
a = s.get_or_create_agent("A")
|
||||
b = s.get_or_create_agent("B")
|
||||
t = s.create_thread({a, b})
|
||||
m1 = s.append_message(t.id, a, "hi")
|
||||
m2 = s.append_message(t.id, b, "yo")
|
||||
assert m1.seq == 1 and m2.seq == 2
|
||||
|
||||
@@ -1,10 +1,27 @@
|
||||
from typing import Protocol
|
||||
|
||||
from agentmsgs.core.store import Store
|
||||
import inspect
|
||||
|
||||
|
||||
def test_store_is_protocol():
|
||||
assert issubclass(Store, Protocol)
|
||||
# check required methods exist
|
||||
for m in ["get_or_create_agent","get_agent_by_name","get_agent_by_id","create_agent","delete_agent","list_agents","create_thread","get_thread","find_threads","add_participant","remove_participant","delete_thread","append_message","list_messages","get_cursor","set_cursor"]:
|
||||
for m in [
|
||||
"get_or_create_agent",
|
||||
"get_agent_by_name",
|
||||
"get_agent_by_id",
|
||||
"create_agent",
|
||||
"delete_agent",
|
||||
"list_agents",
|
||||
"create_thread",
|
||||
"get_thread",
|
||||
"find_threads",
|
||||
"add_participant",
|
||||
"remove_participant",
|
||||
"delete_thread",
|
||||
"append_message",
|
||||
"list_messages",
|
||||
"get_cursor",
|
||||
"set_cursor",
|
||||
]:
|
||||
assert hasattr(Store, m), f"missing {m}"
|
||||
|
||||
+28
-11
@@ -1,18 +1,24 @@
|
||||
import tempfile, pathlib
|
||||
import pathlib
|
||||
import tempfile
|
||||
|
||||
from agentmsgs.stores.sqlite import SQLiteStore
|
||||
|
||||
|
||||
def test_sqlite_persists_across_handles():
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
p = pathlib.Path(d)/"test.db"
|
||||
p = pathlib.Path(d) / "test.db"
|
||||
s1 = SQLiteStore(p)
|
||||
try:
|
||||
a = s1.get_or_create_agent("Alice"); b = s1.get_or_create_agent("Bob")
|
||||
t = s1.create_thread({a,b})
|
||||
a = s1.get_or_create_agent("Alice")
|
||||
b = s1.get_or_create_agent("Bob")
|
||||
t = s1.create_thread({a, b})
|
||||
s1.append_message(t.id, a, "hi")
|
||||
# new handle same file
|
||||
s2 = SQLiteStore(p)
|
||||
try:
|
||||
assert s2.get_agent_by_name("Alice").id == a.id
|
||||
a2 = s2.get_agent_by_name("Alice")
|
||||
assert a2 is not None
|
||||
assert a2.id == a.id
|
||||
assert len(s2.list_messages(t.id)) == 1
|
||||
assert s2.find_threads({a})[0].id == t.id
|
||||
finally:
|
||||
@@ -20,17 +26,28 @@ def test_sqlite_persists_across_handles():
|
||||
finally:
|
||||
s1.close()
|
||||
|
||||
|
||||
def test_sqlite_concurrent_append():
|
||||
import pathlib, tempfile
|
||||
import pathlib
|
||||
import tempfile
|
||||
|
||||
from agentmsgs.stores.sqlite import SQLiteStore
|
||||
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
p = pathlib.Path(d)/"c.db"
|
||||
s1 = SQLiteStore(p); s2 = SQLiteStore(p)
|
||||
p = pathlib.Path(d) / "c.db"
|
||||
s1 = SQLiteStore(p)
|
||||
s2 = SQLiteStore(p)
|
||||
try:
|
||||
a = s1.get_or_create_agent("A"); b = s1.get_or_create_agent("B")
|
||||
a = s1.get_or_create_agent("A")
|
||||
b = s1.get_or_create_agent("B")
|
||||
# s2 sees same agents via file
|
||||
a2 = s2.get_agent_by_name("A"); b2 = s2.get_agent_by_name("B")
|
||||
t = s1.create_thread({a,b})
|
||||
a2 = s2.get_agent_by_name("A")
|
||||
b2 = s2.get_agent_by_name("B")
|
||||
assert a2 is not None
|
||||
assert b2 is not None
|
||||
assert a.id == a2.id
|
||||
assert b.id == b2.id
|
||||
t = s1.create_thread({a, b})
|
||||
s1.append_message(t.id, a, "from s1")
|
||||
s2.append_message(t.id, b2, "from s2")
|
||||
assert len(s1.list_messages(t.id)) == 2
|
||||
|
||||
+19
-5
@@ -1,6 +1,8 @@
|
||||
import uuid
|
||||
from agentmsgs.core.types import Agent, Thread, Message
|
||||
import datetime
|
||||
import uuid
|
||||
|
||||
from agentmsgs.core.types import Agent, Thread
|
||||
|
||||
|
||||
def test_agent_identity_on_id_not_name():
|
||||
a1 = Agent(id=uuid.uuid4(), name="Alice")
|
||||
@@ -8,17 +10,29 @@ def test_agent_identity_on_id_not_name():
|
||||
assert a1 != a2 # different id => not equal even though name same
|
||||
assert hash(a1) != hash(a2)
|
||||
|
||||
|
||||
def test_agent_hash_on_id():
|
||||
uid = uuid.uuid4()
|
||||
a1 = Agent(id=uid, name="Alice")
|
||||
a2 = Agent(id=uid, name="Bob") # same id, different name => equal per design (id is identity)
|
||||
a2 = Agent(
|
||||
id=uid, name="Bob"
|
||||
) # same id, different name => equal per design (id is identity)
|
||||
assert a1 == a2
|
||||
assert hash(a1) == hash(a2)
|
||||
|
||||
|
||||
def test_thread_multiple_with_same_participants_allowed():
|
||||
a = Agent(id=uuid.uuid4(), name="A")
|
||||
b = Agent(id=uuid.uuid4(), name="B")
|
||||
t1 = Thread(id=uuid.uuid4(), participants=frozenset({a,b}), created_at=datetime.datetime.now(datetime.timezone.utc))
|
||||
t2 = Thread(id=uuid.uuid4(), participants=frozenset({a,b}), created_at=datetime.datetime.now(datetime.timezone.utc))
|
||||
t1 = Thread(
|
||||
id=uuid.uuid4(),
|
||||
participants=frozenset({a, b}),
|
||||
created_at=datetime.datetime.now(datetime.UTC),
|
||||
)
|
||||
t2 = Thread(
|
||||
id=uuid.uuid4(),
|
||||
participants=frozenset({a, b}),
|
||||
created_at=datetime.datetime.now(datetime.UTC),
|
||||
)
|
||||
assert t1 != t2
|
||||
assert t1.participants == t2.participants
|
||||
|
||||
Reference in New Issue
Block a user