返回 ViMax
test_agent_session_index.py
根目录 / tests / test_agent_session_index.py
1 import tempfile
2 import unittest
3 from pathlib import Path
4
5 from agent_runtime.session_index import SessionIndex
6
7
8 class SessionIndexTests(unittest.TestCase):
9 def test_generated_session_id_round_trips_after_slug_truncation(self):
10 with tempfile.TemporaryDirectory() as tmp:
11 index = SessionIndex(tmp)
12 record = index.create(idea="A red ball rolls across a white table.")
13 self.assertIsNotNone(index.get(record["session_id"]))
14 self.assertEqual(index.working_dir(record["session_id"]).name, record["session_id"])
15
16 def test_create_session_and_checklist(self):
17 with tempfile.TemporaryDirectory() as tmp:
18 index = SessionIndex(tmp)
19 record = index.create(idea="Moon cat", user_requirement="short", style="anime")
20 self.assertEqual(index.active()["session_id"], record["session_id"])
21 working_dir = Path(tmp) / record["working_dir"]
22 self.assertTrue((working_dir / "idea2video").exists())
23 self.assertTrue((working_dir / "script2video").exists())
24 checklist = index.artifact_checklist(record["session_id"])
25 self.assertFalse(checklist["script2video/storyboard.json"])
26 self.assertFalse(checklist["idea2video/scene_*/storyboard.json"])
27 self.assertEqual(record["compacted_summary"], "")
28 self.assertEqual(record["compaction_snapshots"], [])
29
30 def test_create_session_preserves_project_name(self):
31 with tempfile.TemporaryDirectory() as tmp:
32 index = SessionIndex(tmp)
33 record = index.create(project_name="Ocean campaign")
34 self.assertEqual(record["project_name"], "Ocean campaign")
35 self.assertIn("ocean-campaign", record["session_id"])
36 self.assertEqual(index.get(record["session_id"])["project_name"], "Ocean campaign")
37
38
39 def test_session_id_is_sanitized_and_stays_under_working_dir(self):
40 with tempfile.TemporaryDirectory() as tmp:
41 index = SessionIndex(tmp)
42 record = index.create(session_id="../../escaped-review")
43 self.assertEqual(record["session_id"], "escaped-review")
44 working_dir = (Path(tmp) / record["working_dir"]).resolve()
45 self.assertTrue(str(working_dir).startswith(str((Path(tmp) / ".working_dir").resolve())))
46 self.assertFalse((Path(tmp).parent / "escaped-review").exists())
47
48
49 def test_update_compaction_writes_session_state_not_memory(self):
50 with tempfile.TemporaryDirectory() as tmp:
51 index = SessionIndex(tmp)
52 record = index.create(idea="compact")
53 index.update_compaction(record["session_id"], {
54 "summary": "## Reference Context Only\n- old context",
55 "compacted_message_count": 4,
56 "preserved_message_count": 2,
57 "estimated_tokens_before": 1000,
58 "estimated_tokens_after": 300,
59 "reason": "manual",
60 "mode": "fallback-local",
61 })
62 session = index.get(record["session_id"])
63 self.assertIn("old context", session["compacted_summary"])
64 self.assertEqual(session["compacted_turns"], 2)
65 self.assertEqual(session["last_compaction_reason"], "manual")
66 self.assertTrue(session["compaction_snapshots"])
67 self.assertNotIn("old context", index.memory_text())
68
69 def test_memory_and_turn_record_boundaries(self):
70 with tempfile.TemporaryDirectory() as tmp:
71 index = SessionIndex(tmp)
72 record = index.create()
73 index.write_memory("# User Preferences\n- 16:9\n")
74 self.assertIn("16:9", index.memory_text())
75 index.append_turn_record(record["session_id"], {"turn_id": "t1", "status": "completed", "tool_rounds": [], "final_assistant_text": "done"})
76 self.assertTrue((Path(tmp) / ".vimax" / "logs" / "loop_history.jsonl").exists())
77 self.assertEqual(len(index.get(record["session_id"])["recent_turn_records"]), 1)
78
78 lines PYTHON