| Server IP : 85.155.190.233 / Your IP : 216.73.216.103 Web Server : nginx/1.24.0 System : Linux antigravity-cli 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64 User : wp-moonbloom ( 1001) PHP Version : 8.3.6 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /opt/Gemini_Swarm_Core/scripts/ |
Upload File : |
import asyncio
import os
import sys
from pathlib import Path
import aiosqlite
# Mock environment variables needed by tg_bridge
os.environ["TELEGRAM_BOT_TOKEN"] = "123456:mock_token"
os.environ["TELEGRAM_ALLOWED_USERS"] = "12345"
# Add parent directory to path so we can import tg_bridge
sys.path.insert(0, str(Path(__file__).resolve().parent))
import tg_bridge
# Force tg_bridge to use a test database
TEST_DB_PATH = Path(__file__).resolve().parent.parent / "test_sessions_isolation_real.db"
tg_bridge.DB_PATH = TEST_DB_PATH
async def cleanup_db():
if TEST_DB_PATH.exists():
try:
os.remove(TEST_DB_PATH)
for suffix in ["-wal", "-journal", "-shm"]:
p = Path(str(TEST_DB_PATH) + suffix)
if p.exists():
os.remove(p)
except OSError:
pass
async def test_green_state():
await cleanup_db()
# 1. Initialize DB with the real function
await tg_bridge.init_db()
chat_id = "12345"
# 2. Simulate setting repo and starting task A
async with tg_bridge.connect_db() as db:
await db.execute(
"INSERT OR REPLACE INTO app_settings (key, value) VALUES ('active_repo_path', ?)",
("/root/workspaces/project-a",)
)
cursor = await db.execute(
"INSERT INTO sessions (repo_path, task_name, conversation_id) VALUES (?, ?, ?)",
("/root/workspaces/project-a", "task-a", "conv-a")
)
session_a_id = cursor.lastrowid
await db.execute(
"INSERT OR REPLACE INTO app_settings (key, value) VALUES ('active_session_id', ?)",
(str(session_a_id),)
)
await db.commit()
print("--- Simulating Task A message insertion ---")
async def simulate_ask_ceo(session_id, text, reply):
session_db_id, repo_path, task_name, active_conversation_id = await tg_bridge.get_active_session()
async with tg_bridge.connect_db() as db:
await db.execute(
"INSERT INTO chat_history (session_id, role, text, session_db_id) VALUES (?, ?, ?, ?)",
(session_id, "user", text, session_db_id)
)
await db.execute(
"INSERT INTO chat_history (session_id, role, text, session_db_id) VALUES (?, ?, ?, ?)",
(session_id, "model", reply, session_db_id)
)
await db.commit()
await simulate_ask_ceo(chat_id, "Hello from A", "Reply from A")
# 3. Simulate switching or starting task B
async with tg_bridge.connect_db() as db:
cursor = await db.execute(
"INSERT INTO sessions (repo_path, task_name, conversation_id) VALUES (?, ?, ?)",
("/root/workspaces/project-a", "task-b", "conv-b")
)
session_b_id = cursor.lastrowid
await db.execute(
"INSERT OR REPLACE INTO app_settings (key, value) VALUES ('active_session_id', ?)",
(str(session_b_id),)
)
await db.commit()
print("--- Simulating Task B message insertion ---")
await simulate_ask_ceo(chat_id, "Hello from B", "Reply from B")
# 4. Now verify history for Task B
session_db_id, repo_path, task_name, active_conversation_id = await tg_bridge.get_active_session()
async with tg_bridge.connect_db() as db:
async with db.execute(
"SELECT role, text FROM chat_history WHERE session_id = ? AND session_db_id IS ? ORDER BY id ASC",
(chat_id, session_db_id)
) as cursor:
history_b = await cursor.fetchall()
print(f"Retrieved history for Task B: {history_b}")
# 5. Switch back to Task A and verify
async with tg_bridge.connect_db() as db:
await db.execute(
"INSERT OR REPLACE INTO app_settings (key, value) VALUES ('active_session_id', ?)",
(str(session_a_id),)
)
await db.commit()
session_db_id, repo_path, task_name, active_conversation_id = await tg_bridge.get_active_session()
async with tg_bridge.connect_db() as db:
async with db.execute(
"SELECT role, text FROM chat_history WHERE session_id = ? AND session_db_id IS ? ORDER BY id ASC",
(chat_id, session_db_id)
) as cursor:
history_a = await cursor.fetchall()
print(f"Retrieved history for Task A: {history_a}")
await cleanup_db()
# Validation
assert len(history_b) == 2, f"Expected 2 messages for B, got {len(history_b)}"
assert history_b[0][1] == "Hello from B"
assert len(history_a) == 2, f"Expected 2 messages for A, got {len(history_a)}"
assert history_a[0][1] == "Hello from A"
print("GREEN Test PASSED: Isolation verified successfully!")
sys.exit(0)
if __name__ == "__main__":
asyncio.run(test_green_state())