-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
355 lines (298 loc) · 11 KB
/
Copy pathtests.py
File metadata and controls
355 lines (298 loc) · 11 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
"""
tests.py — Automated tests for the Persistent AI Telegram workshop.
Verifies that all 10 modules are properly structured and functional:
1. Required files exist in each module folder
2. Python files have valid syntax (compile check)
3. README/GUIDE/EXERCISES files are non-empty and have expected sections
4. Makefile has all required targets
5. Top-level files exist
6. Module code compiles without syntax errors
Run: make test
or: uv run python tests.py
"""
import ast
from pathlib import Path
WORKSHOP_DIR = Path(__file__).parent
PASS = "\033[32m✓\033[0m"
FAIL = "\033[31m✗\033[0m"
results = {"passed": 0, "failed": 0, "errors": []}
def check(description: str, condition: bool, detail: str = ""):
"""Record a test result."""
if condition:
results["passed"] += 1
print(f" {PASS} {description}")
else:
results["failed"] += 1
msg = f"{description}: {detail}" if detail else description
results["errors"].append(msg)
print(f" {FAIL} {description}" + (f" — {detail}" if detail else ""))
# --- Module definitions ---
MODULES = [
{
"dir": "01-simplest-bot",
"main_file": "bot.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": [],
},
{
"dir": "02-persistent-sessions",
"main_file": "bot.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": [],
},
{
"dir": "03-personality-soul",
"main_file": "bot.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": ["SOUL.md"],
},
{
"dir": "04-tools-agent-loop",
"main_file": "bot.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": [],
},
{
"dir": "05-permission-controls",
"main_file": "bot.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": [],
},
{
"dir": "06-gateway",
"main_file": "bot.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": [],
},
{
"dir": "07-context-compaction",
"main_file": "bot.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": [],
},
{
"dir": "08-long-term-memory",
"main_file": "bot.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": [],
},
{
"dir": "09-concurrency-scheduling",
"main_file": "bot.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": [],
},
{
"dir": "10-multi-agent-integration",
"main_file": "mini-openclaw.py",
"docs": ["README.md", "GUIDE.md", "EXERCISES.md"],
"extra_files": [],
},
]
def test_top_level_files():
"""Test that top-level workshop files exist."""
print("\nTop-Level Files")
required = ["README.md", "Makefile", "pyproject.toml", ".env.example", ".gitignore", "tests.py"]
for filename in required:
path = WORKSHOP_DIR / filename
check(f"{filename} exists", path.exists())
if path.exists():
check(f"{filename} is non-empty", path.stat().st_size > 0)
def test_module_structure():
"""Test that all 10 module folders have the required files."""
print("\nModule Structure")
for module in MODULES:
module_dir = WORKSHOP_DIR / module["dir"]
check(f"{module['dir']}/ exists", module_dir.is_dir())
if not module_dir.is_dir():
continue
# Main Python file
main_file = module_dir / module["main_file"]
check(f"{module['dir']}/{module['main_file']} exists", main_file.exists())
# Documentation files
for doc in module["docs"]:
doc_path = module_dir / doc
check(f"{module['dir']}/{doc} exists", doc_path.exists())
# Extra files
for extra in module["extra_files"]:
extra_path = module_dir / extra
check(f"{module['dir']}/{extra} exists", extra_path.exists())
def test_python_syntax():
"""Test that all Python files have valid syntax."""
print("\nPython Syntax")
for module in MODULES:
main_file = WORKSHOP_DIR / module["dir"] / module["main_file"]
if not main_file.exists():
continue
try:
source = main_file.read_text()
ast.parse(source)
check(f"{module['dir']}/{module['main_file']} compiles", True)
except SyntaxError as e:
check(f"{module['dir']}/{module['main_file']} compiles", False, str(e))
def test_doc_content():
"""Test that documentation files have expected sections."""
print("\nDocumentation Content")
for module in MODULES:
module_dir = WORKSHOP_DIR / module["dir"]
if not module_dir.is_dir():
continue
# README should have key sections
readme = module_dir / "README.md"
if readme.exists():
content = readme.read_text()
check(
f"{module['dir']}/README.md has title",
content.startswith("#"),
"Should start with a heading",
)
check(
f"{module['dir']}/README.md is substantial",
len(content) > 200,
f"Only {len(content)} chars",
)
# GUIDE should be substantial
guide = module_dir / "GUIDE.md"
if guide.exists():
content = guide.read_text()
check(
f"{module['dir']}/GUIDE.md is substantial",
len(content) > 500,
f"Only {len(content)} chars",
)
# EXERCISES should have multiple exercises
exercises = module_dir / "EXERCISES.md"
if exercises.exists():
content = exercises.read_text()
exercise_count = content.count("## Exercise")
check(
f"{module['dir']}/EXERCISES.md has 3+ exercises",
exercise_count >= 3,
f"Found {exercise_count}",
)
# Check for checkpoints
checkpoint_count = content.lower().count("checkpoint")
check(
f"{module['dir']}/EXERCISES.md has checkpoints",
checkpoint_count >= 2,
f"Found {checkpoint_count}",
)
def test_makefile_targets():
"""Test that the Makefile has all required targets."""
print("\nMakefile Targets")
makefile = WORKSHOP_DIR / "Makefile"
if not makefile.exists():
check("Makefile exists", False)
return
content = makefile.read_text()
required_targets = [
"install",
"01-simplest-bot",
"02-persistent-sessions",
"03-personality-soul",
"04-tools-agent-loop",
"05-permission-controls",
"06-gateway",
"07-context-compaction",
"08-long-term-memory",
"09-concurrency-scheduling",
"10-multi-agent-integration",
"test",
"clean",
]
for target in required_targets:
check(
f"Makefile has '{target}' target",
f"{target}:" in content,
)
def test_pyproject():
"""Test pyproject.toml has required dependencies."""
print("\nDependencies")
pyproject = WORKSHOP_DIR / "pyproject.toml"
if not pyproject.exists():
check("pyproject.toml exists", False)
return
content = pyproject.read_text()
required_deps = ["anthropic", "python-telegram-bot", "flask", "schedule", "python-dotenv"]
for dep in required_deps:
check(
f"pyproject.toml includes {dep}",
dep in content,
)
def test_code_progression():
"""Test that code complexity increases across modules."""
print("\nCode Progression")
sizes = []
for module in MODULES:
main_file = WORKSHOP_DIR / module["dir"] / module["main_file"]
if main_file.exists():
size = len(main_file.read_text().splitlines())
sizes.append((module["dir"], size))
if len(sizes) >= 2:
# Module 01 should be smallest
check(
f"01 is smallest ({sizes[0][1]} lines)",
sizes[0][1] <= sizes[1][1],
f"{sizes[0][1]} > {sizes[1][1]}",
)
# General trend should be increasing (allow some variation)
check(
f"Code grows from ~{sizes[0][1]} to ~{sizes[-1][1]} lines",
sizes[-1][1] > sizes[0][1] * 2,
"Final module should be significantly larger than first",
)
def test_key_concepts():
"""Test that key concepts appear in the right modules."""
print("\nKey Concepts in Code")
concept_checks = [
("01-simplest-bot", "bot.py", "client.messages.create", "Anthropic API call"),
("02-persistent-sessions", "bot.py", "jsonl", "JSONL format"),
("02-persistent-sessions", "bot.py", "load_session", "Session loading"),
("03-personality-soul", "bot.py", "SOUL", "SOUL system prompt"),
("04-tools-agent-loop", "bot.py", "execute_tool", "Tool execution"),
("04-tools-agent-loop", "bot.py", "while True", "Agent loop"),
("05-permission-controls", "bot.py", "check_command_safety", "Safety checks"),
("05-permission-controls", "bot.py", "DANGEROUS_PATTERNS", "Dangerous patterns"),
("06-gateway", "bot.py", "Flask", "Flask HTTP"),
("06-gateway", "bot.py", "threading", "Threading"),
("07-context-compaction", "bot.py", "estimate_tokens", "Token estimation"),
("07-context-compaction", "bot.py", "compact_session", "Compaction"),
("08-long-term-memory", "bot.py", "save_memory", "Memory saving"),
("08-long-term-memory", "bot.py", "memory_search", "Memory search"),
("09-concurrency-scheduling", "bot.py", "session_locks", "Session locks"),
("09-concurrency-scheduling", "bot.py", "schedule", "Scheduling"),
("10-multi-agent-integration", "mini-openclaw.py", "AGENTS", "Agent config"),
("10-multi-agent-integration", "mini-openclaw.py", "resolve_agent", "Agent routing"),
]
for module_dir, filename, concept, label in concept_checks:
filepath = WORKSHOP_DIR / module_dir / filename
if filepath.exists():
content = filepath.read_text()
check(
f"{module_dir} has {label}",
concept in content,
)
# --- Run all tests ---
if __name__ == "__main__":
print("=" * 60)
print(" Persistent AI Telegram — Automated Tests")
print("=" * 60)
test_top_level_files()
test_module_structure()
test_python_syntax()
test_doc_content()
test_makefile_targets()
test_pyproject()
test_code_progression()
test_key_concepts()
# Summary
total = results["passed"] + results["failed"]
print(f"\n{'=' * 60}")
print(f" Results: {results['passed']}/{total} passed")
if results["errors"]:
print(f"\n Failures:")
for err in results["errors"]:
print(f" - {err}")
print(f"{'=' * 60}")
exit(0 if results["failed"] == 0 else 1)