forked from jsturtevant/copilot-codeact-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-workspace.sh
More file actions
executable file
·1083 lines (789 loc) · 25.8 KB
/
Copy pathsetup-workspace.sh
File metadata and controls
executable file
·1083 lines (789 loc) · 25.8 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# setup-workspace.sh — Create a temp workspace for codeact tests
# Outputs the workspace path on stdout
set -euo pipefail
WORKSPACE=$(mktemp -d /tmp/codeact-test-XXXX)
# --- src/ directory with Python files ---
mkdir -p "$WORKSPACE/src"
cat > "$WORKSPACE/src/app.py" << 'PYEOF'
"""Main application module."""
import os
import sys
from pathlib import Path
# TODO: Add proper logging configuration
def main():
"""Entry point for the application."""
config = load_config()
# TODO: Validate config before using
server = create_server(config)
server.run()
def load_config():
"""Load configuration from environment."""
return {
"host": os.environ.get("APP_HOST", "0.0.0.0"),
"port": int(os.environ.get("APP_PORT", "8080")),
"debug": os.environ.get("APP_DEBUG", "false").lower() == "true",
}
def create_server(config):
"""Create and configure the server."""
# TODO: Support HTTPS configuration
return Server(config)
class Server:
def __init__(self, config):
self.config = config
self.routes = {}
def add_route(self, path, handler):
self.routes[path] = handler
def run(self):
# TODO: Implement graceful shutdown
print(f"Server running on {self.config['host']}:{self.config['port']}")
def health_check(self):
return {"status": "ok"}
def parse_request(raw):
"""Parse an HTTP request string."""
lines = raw.split("\n")
method, path, _ = lines[0].split(" ")
return {"method": method, "path": path}
def format_response(status, body):
"""Format an HTTP response."""
return f"HTTP/1.1 {status}\r\nContent-Length: {len(body)}\r\n\r\n{body}"
# TODO: Add request rate limiting
def handle_request(request):
"""Handle an incoming request."""
if request["method"] == "GET":
return format_response("200 OK", '{"message": "hello"}')
return format_response("405 Method Not Allowed", "")
if __name__ == "__main__":
main()
PYEOF
cat > "$WORKSPACE/src/utils.py" << 'PYEOF'
"""Utility functions."""
import re
import json
from datetime import datetime
def slugify(text):
text = text.lower().strip()
text = re.sub(r'[^\w\s-]', '', text)
text = re.sub(r'[\s_-]+', '-', text)
return text
def timestamp():
return datetime.now().isoformat()
def deep_merge(base, override):
result = base.copy()
for key, value in override.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = deep_merge(result[key], value)
else:
result[key] = value
return result
def truncate(text, max_length=100):
if len(text) <= max_length:
return text
return text[:max_length - 3] + "..."
def parse_csv_line(line):
return [field.strip() for field in line.split(",")]
def validate_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return bool(re.match(pattern, email))
def chunk_list(lst, size):
return [lst[i:i + size] for i in range(0, len(lst), size)]
PYEOF
cat > "$WORKSPACE/src/models.py" << 'PYEOF'
"""Data models."""
import os
from dataclasses import dataclass, field
from typing import Optional, List
@dataclass
class User:
id: int
name: str
email: str
role: str = "user"
active: bool = True
def display_name(self):
return f"{self.name} ({self.role})"
@dataclass
class Project:
id: int
name: str
owner: User
members: List[User] = field(default_factory=list)
description: Optional[str] = None
def add_member(self, user):
if user not in self.members:
self.members.append(user)
def member_count(self):
return len(self.members) + 1 # +1 for owner
@dataclass
class Task:
id: int
title: str
project: Project
assignee: Optional[User] = None
status: str = "open"
priority: int = 0
def assign(self, user):
self.assignee = user
def close(self):
self.status = "closed"
def is_overdue(self):
return False # TODO: implement with due dates
PYEOF
cat > "$WORKSPACE/src/api.py" << 'PYEOF'
"""API endpoint handlers."""
import os
import json
# TODO: Add authentication middleware
def get_users(db):
"""Get all users from the database."""
users = db.query("SELECT * FROM users WHERE active = 1")
return json.dumps(users)
def create_user(db, data):
"""Create a new user."""
required = ["name", "email"]
for field in required:
if field not in data:
return {"error": f"Missing field: {field}"}, 400
db.insert("users", data)
return {"status": "created"}, 201
def get_projects(db, user_id=None):
"""Get projects, optionally filtered by user."""
if user_id:
return db.query(f"SELECT * FROM projects WHERE owner_id = {user_id}")
return db.query("SELECT * FROM projects")
def health(db):
"""Health check endpoint."""
try:
db.query("SELECT 1")
return {"status": "healthy", "database": "connected"}
except Exception:
return {"status": "unhealthy", "database": "disconnected"}
def search(db, query, limit=10):
"""Search across all resources."""
results = []
for table in ["users", "projects", "tasks"]:
rows = db.query(f"SELECT * FROM {table} WHERE name LIKE '%{query}%' LIMIT {limit}")
results.extend(rows)
return results
PYEOF
cat > "$WORKSPACE/src/config.py" << 'PYEOF'
"""Configuration management."""
import os
import json
from pathlib import Path
DEFAULT_CONFIG = {
"app": {
"name": "myapp",
"version": "1.0.0",
"debug": False,
},
"server": {
"host": "0.0.0.0",
"port": 8080,
"workers": 4,
},
"database": {
"url": "sqlite:///app.db",
"pool_size": 5,
},
"logging": {
"level": "INFO",
"format": "%(asctime)s %(levelname)s %(message)s",
},
}
def load_config(path=None):
"""Load config from file, falling back to defaults."""
config = DEFAULT_CONFIG.copy()
if path and Path(path).exists():
with open(path) as f:
overrides = json.load(f)
config.update(overrides)
# Environment overrides
if os.environ.get("APP_DEBUG"):
config["app"]["debug"] = True
if os.environ.get("APP_PORT"):
config["server"]["port"] = int(os.environ["APP_PORT"])
return config
def save_config(config, path):
"""Save config to file."""
Path(path).parent.mkdir(parents=True, exist_ok=True)
with open(path, "w") as f:
json.dump(config, f, indent=2)
PYEOF
# --- config/ directory with JSON files ---
mkdir -p "$WORKSPACE/config"
cat > "$WORKSPACE/config/settings.json" << 'EOF'
{
"app_name": "test-app",
"version": "2.1.0",
"features": {
"dark_mode": true,
"notifications": true,
"beta_features": false
}
}
EOF
cat > "$WORKSPACE/config/database.json" << 'EOF'
{
"host": "localhost",
"port": 5432,
"name": "testdb",
"pool_size": 10,
"ssl": true
}
EOF
cat > "$WORKSPACE/config/broken.json" << 'EOF'
{
"key": "value",
"missing_closing_bracket": [1, 2, 3
"another_key": true
}
EOF
# --- tests/ directory ---
mkdir -p "$WORKSPACE/tests"
cat > "$WORKSPACE/tests/test_app.py" << 'PYEOF'
"""Tests for app module."""
import pytest
def test_load_config():
from src.app import load_config
config = load_config()
assert "host" in config
assert "port" in config
def test_parse_request():
from src.app import parse_request
req = parse_request("GET /api/users HTTP/1.1\nHost: localhost")
assert req["method"] == "GET"
assert req["path"] == "/api/users"
def test_format_response():
from src.app import format_response
resp = format_response("200 OK", "hello")
assert "200 OK" in resp
assert "hello" in resp
PYEOF
cat > "$WORKSPACE/tests/test_utils.py" << 'PYEOF'
"""Tests for utils module."""
def test_slugify():
from src.utils import slugify
assert slugify("Hello World") == "hello-world"
assert slugify(" spaces ") == "spaces"
def test_truncate():
from src.utils import truncate
assert truncate("short") == "short"
assert len(truncate("a" * 200)) <= 100
def test_validate_email():
from src.utils import validate_email
assert validate_email("user@example.com")
assert not validate_email("invalid")
PYEOF
# --- README.md ---
cat > "$WORKSPACE/README.md" << 'EOF'
# Test Project
A sample project for testing codeact functionality.
## Structure
- `src/` — Source code
- `config/` — Configuration files
- `tests/` — Test files
## Setup
```bash
pip install -r requirements.txt
python -m src.app
```
EOF
# --- src/handlers/ ---
mkdir -p "$WORKSPACE/src/handlers"
cat > "$WORKSPACE/src/handlers/__init__.py" << 'PYEOF'
"""HTTP request handlers."""
PYEOF
cat > "$WORKSPACE/src/handlers/user_handler.py" << 'PYEOF'
"""User endpoint handlers."""
from src.models import User
from src.services.user_service import get_user, list_users
def handle_get_users(request):
"""List all active users."""
users = list_users(active_only=True)
return {"users": users, "count": len(users)}
def handle_get_user(request, user_id):
user = get_user(user_id)
if not user:
return {"error": "Not found"}, 404
return user
def handle_create_user(request):
"""Create a new user from request data."""
data = request.get("body", {})
# TODO: Add input validation
if not data.get("email"):
return {"error": "Email required"}, 400
user = User(id=0, name=data["name"], email=data["email"])
return {"id": user.id, "status": "created"}, 201
def handle_update_user(request, user_id):
data = request.get("body", {})
# TODO: Validate update fields
return {"id": user_id, "status": "updated"}
def handle_delete_user(request, user_id):
"""Delete a user by ID."""
# TODO: Add soft-delete support
return {"status": "deleted"}, 204
PYEOF
cat > "$WORKSPACE/src/handlers/project_handler.py" << 'PYEOF'
"""Project endpoint handlers."""
from src.models import Project
from src.services.project_service import get_project, list_projects
def handle_list_projects(request):
"""Return all projects."""
return {"projects": list_projects()}
def handle_get_project(request, project_id):
project = get_project(project_id)
if not project:
return {"error": "Not found"}, 404
return project
def handle_create_project(request):
"""Create a new project."""
data = request.get("body", {})
# TODO: Validate required fields
return {"status": "created"}, 201
def handle_update_project(request, project_id):
data = request.get("body", {})
return {"id": project_id, "status": "updated"}
def handle_archive_project(request, project_id):
"""Archive a project instead of deleting."""
# TODO: Notify project members on archive
return {"status": "archived"}
PYEOF
cat > "$WORKSPACE/src/handlers/task_handler.py" << 'PYEOF'
"""Task management handlers."""
from src.models import Task
def handle_list_tasks(request, project_id):
"""List tasks for a project."""
# TODO: Add pagination support
return {"tasks": [], "project_id": project_id}
def handle_get_task(request, task_id):
return {"task_id": task_id}
def handle_create_task(request, project_id):
data = request.get("body", {})
# TODO: Validate priority range 0-5
return {"status": "created"}, 201
def handle_assign_task(request, task_id, user_id):
"""Assign a task to a user."""
return {"task_id": task_id, "assignee": user_id}
def handle_close_task(request, task_id):
# TODO: Check if task has open subtasks before closing
return {"task_id": task_id, "status": "closed"}
PYEOF
cat > "$WORKSPACE/src/handlers/auth_handler.py" << 'PYEOF'
"""Authentication handlers."""
import os
import json
def handle_login(request):
"""Authenticate user and return token."""
data = request.get("body", {})
if not data.get("email") or not data.get("password"):
return {"error": "Credentials required"}, 401
# TODO: Implement proper password hashing
return {"token": "fake-jwt-token"}
def handle_logout(request):
return {"status": "logged out"}
def handle_refresh_token(request):
"""Refresh an expired JWT token."""
# TODO: Validate refresh token expiry
return {"token": "new-fake-jwt-token"}
def handle_reset_password(request):
data = request.get("body", {})
if not data.get("email"):
return {"error": "Email required"}, 400
# TODO: Send actual reset email
return {"status": "reset email sent"}
PYEOF
# --- src/middleware/ ---
mkdir -p "$WORKSPACE/src/middleware"
cat > "$WORKSPACE/src/middleware/__init__.py" << 'PYEOF'
"""Request/response middleware."""
PYEOF
cat > "$WORKSPACE/src/middleware/auth.py" << 'PYEOF'
"""Authentication middleware."""
import os
SECRET_KEY = os.environ.get("JWT_SECRET", "dev-secret")
def authenticate(request):
"""Verify JWT token in Authorization header."""
token = request.get("headers", {}).get("Authorization", "")
if not token.startswith("Bearer "):
return None
# TODO: Actually verify JWT signature
return {"user_id": 1, "role": "user"}
def require_role(role):
def decorator(handler):
def wrapper(request, *args, **kwargs):
user = authenticate(request)
if not user or user.get("role") != role:
return {"error": "Forbidden"}, 403
return handler(request, *args, **kwargs)
return wrapper
return decorator
def hash_password(password):
# TODO: Use bcrypt instead of this placeholder
return f"hashed_{password}"
PYEOF
cat > "$WORKSPACE/src/middleware/logging_mw.py" << 'PYEOF'
"""Request logging middleware."""
import time
from datetime import datetime
def log_request(request):
"""Log incoming request details."""
method = request.get("method", "?")
path = request.get("path", "?")
timestamp = datetime.now().isoformat()
print(f"[{timestamp}] {method} {path}")
def log_response(request, response, duration_ms):
status = response[1] if isinstance(response, tuple) else 200
print(f" -> {status} ({duration_ms}ms)")
def timing_middleware(handler):
def wrapper(request, *args, **kwargs):
start = time.time()
response = handler(request, *args, **kwargs)
duration = (time.time() - start) * 1000
log_response(request, response, duration)
return response
return wrapper
PYEOF
cat > "$WORKSPACE/src/middleware/cors.py" << 'PYEOF'
"""CORS handling middleware."""
ALLOWED_ORIGINS = ["http://localhost:3000", "https://app.example.com"]
def cors_headers(origin):
"""Generate CORS headers for the given origin."""
if origin in ALLOWED_ORIGINS:
return {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
}
return {}
def cors_middleware(handler):
def wrapper(request, *args, **kwargs):
origin = request.get("headers", {}).get("Origin", "")
response = handler(request, *args, **kwargs)
headers = cors_headers(origin)
return response, headers
return wrapper
PYEOF
cat > "$WORKSPACE/src/middleware/rate_limit.py" << 'PYEOF'
"""Rate limiting middleware."""
import time
# In-memory store (not suitable for production)
_request_counts = {}
def check_rate_limit(client_ip, max_requests=100, window_seconds=60):
"""Check if client has exceeded rate limit."""
now = time.time()
key = f"{client_ip}:{int(now / window_seconds)}"
_request_counts[key] = _request_counts.get(key, 0) + 1
return _request_counts[key] <= max_requests
def rate_limit_middleware(handler):
def wrapper(request, *args, **kwargs):
ip = request.get("client_ip", "unknown")
if not check_rate_limit(ip):
return {"error": "Too many requests"}, 429
return handler(request, *args, **kwargs)
return wrapper
def reset_counts():
# TODO: Add automatic cleanup of old window entries
_request_counts.clear()
PYEOF
# --- src/services/ ---
mkdir -p "$WORKSPACE/src/services"
cat > "$WORKSPACE/src/services/__init__.py" << 'PYEOF'
"""Business logic services."""
PYEOF
cat > "$WORKSPACE/src/services/user_service.py" << 'PYEOF'
"""User business logic."""
from src.models import User
_users_db = []
def get_user(user_id):
for u in _users_db:
if u.id == user_id:
return u
return None
def list_users(active_only=False):
"""Return all users, optionally filtered by active status."""
if active_only:
return [u for u in _users_db if u.active]
return list(_users_db)
def create_user(name, email, role="user"):
"""Create and store a new user."""
user = User(id=len(_users_db) + 1, name=name, email=email, role=role)
_users_db.append(user)
return user
def deactivate_user(user_id):
user = get_user(user_id)
if user:
user.active = False
return user
def search_users(query):
# TODO: Add fuzzy matching
return [u for u in _users_db if query.lower() in u.name.lower()]
PYEOF
cat > "$WORKSPACE/src/services/project_service.py" << 'PYEOF'
"""Project business logic."""
from src.models import Project
_projects_db = []
def get_project(project_id):
"""Fetch a single project by ID."""
for p in _projects_db:
if p.id == project_id:
return p
return None
def list_projects(owner_id=None):
if owner_id:
return [p for p in _projects_db if p.owner.id == owner_id]
return list(_projects_db)
def create_project(name, owner, description=None):
"""Create a new project."""
project = Project(
id=len(_projects_db) + 1,
name=name, owner=owner, description=description,
)
_projects_db.append(project)
return project
def archive_project(project_id):
# TODO: Cascade archive to project tasks
project = get_project(project_id)
return project
PYEOF
cat > "$WORKSPACE/src/services/email_service.py" << 'PYEOF'
"""Email sending service."""
import os
SMTP_HOST = os.environ.get("SMTP_HOST", "localhost")
SMTP_PORT = int(os.environ.get("SMTP_PORT", "587"))
def send_email(to, subject, body):
"""Send an email message."""
# TODO: Implement actual SMTP connection
print(f"Sending to {to}: {subject}")
return True
def send_welcome_email(user):
return send_email(
user.email,
"Welcome!",
f"Hello {user.name}, welcome to the platform.",
)
def send_password_reset(email, reset_url):
# TODO: Add email template rendering
return send_email(email, "Password Reset", f"Reset here: {reset_url}")
def send_notification(user, message):
"""Send a notification email."""
return send_email(user.email, "Notification", message)
PYEOF
cat > "$WORKSPACE/src/services/cache_service.py" << 'PYEOF'
"""In-memory cache service."""
import time
_cache = {}
def get(key):
entry = _cache.get(key)
if entry is None:
return None
if entry["expires"] and time.time() > entry["expires"]:
del _cache[key]
return None
return entry["value"]
def set(key, value, ttl_seconds=300):
"""Store a value with optional TTL."""
_cache[key] = {
"value": value,
"expires": time.time() + ttl_seconds if ttl_seconds else None,
}
def delete(key):
_cache.pop(key, None)
def clear():
"""Clear the entire cache."""
# TODO: Add cache statistics tracking before clear
_cache.clear()
def cache_size():
return len(_cache)
PYEOF
# --- src/db/ ---
mkdir -p "$WORKSPACE/src/db"
cat > "$WORKSPACE/src/db/__init__.py" << 'PYEOF'
"""Database layer."""
PYEOF
cat > "$WORKSPACE/src/db/connection.py" << 'PYEOF'
"""Database connection management."""
import os
DATABASE_URL = os.environ.get("DATABASE_URL", "sqlite:///app.db")
_pool = []
def get_connection():
"""Get a database connection from the pool."""
# TODO: Implement actual connection pooling
if _pool:
return _pool.pop()
return _create_connection()
def _create_connection():
return {"url": DATABASE_URL, "active": True}
def release_connection(conn):
"""Return a connection to the pool."""
_pool.append(conn)
PYEOF
cat > "$WORKSPACE/src/db/migrations.py" << 'PYEOF'
"""Schema migration utilities."""
import json
from pathlib import Path
MIGRATIONS_DIR = Path("migrations")
def list_migrations():
"""List all available migration files."""
if not MIGRATIONS_DIR.exists():
return []
return sorted(MIGRATIONS_DIR.glob("*.sql"))
def get_current_version():
# TODO: Read from _schema_version table
return 0
def apply_migration(migration_path):
"""Apply a single migration file."""
content = Path(migration_path).read_text()
print(f"Applying: {migration_path}")
return True
def migrate_up():
"""Run all pending migrations."""
current = get_current_version()
pending = [m for m in list_migrations() if _version_from(m) > current]
# TODO: Wrap in transaction
for m in pending:
apply_migration(m)
return len(pending)
def _version_from(path):
return int(Path(path).stem.split("_")[0])
PYEOF
cat > "$WORKSPACE/src/db/queries.py" << 'PYEOF'
"""Common database queries."""
def find_users_by_role(conn, role):
"""Find all users with a given role."""
return conn.execute(
"SELECT * FROM users WHERE role = ?", (role,)
).fetchall()
def find_active_projects(conn):
return conn.execute(
"SELECT * FROM projects WHERE archived = 0"
).fetchall()
def count_tasks_by_status(conn, project_id):
"""Count tasks grouped by status for a project."""
return conn.execute(
"SELECT status, COUNT(*) FROM tasks WHERE project_id = ? GROUP BY status",
(project_id,),
).fetchall()
def search_all(conn, query):
results = {}
for table in ["users", "projects", "tasks"]:
results[table] = conn.execute(
f"SELECT * FROM {table} WHERE name LIKE ?",
(f"%{query}%",),
).fetchall()
return results
def recent_activity(conn, limit=20):
"""Get recent activity log entries."""
return conn.execute(
"SELECT * FROM activity_log ORDER BY created_at DESC LIMIT ?",
(limit,),
).fetchall()
PYEOF
# --- Additional test files ---
cat > "$WORKSPACE/tests/test_handlers.py" << 'PYEOF'
"""Tests for request handlers."""
def test_handle_get_users():
from src.handlers.user_handler import handle_get_users
result = handle_get_users({})
assert "users" in result
assert "count" in result
def test_handle_login_missing_credentials():
from src.handlers.auth_handler import handle_login
result = handle_login({"body": {}})
assert result[1] == 401
def test_handle_list_projects():
from src.handlers.project_handler import handle_list_projects
result = handle_list_projects({})
assert "projects" in result
def test_handle_create_task():
from src.handlers.task_handler import handle_create_task
result = handle_create_task({"body": {"title": "Test"}}, project_id=1)
assert result[1] == 201
PYEOF
cat > "$WORKSPACE/tests/test_services.py" << 'PYEOF'
"""Tests for service layer."""
def test_create_user():
from src.services.user_service import create_user
user = create_user("Test", "test@example.com")
assert user.name == "Test"
assert user.email == "test@example.com"
def test_cache_set_get():
from src.services.cache_service import set, get, clear
clear()
set("key", "value")
assert get("key") == "value"
def test_send_email():
from src.services.email_service import send_email
assert send_email("test@test.com", "Subject", "Body") is True
PYEOF
cat > "$WORKSPACE/tests/test_middleware.py" << 'PYEOF'
"""Tests for middleware."""
def test_authenticate_no_token():
from src.middleware.auth import authenticate
result = authenticate({})
assert result is None