spr migrated books, and tester

This commit is contained in:
buenosairesam
2025-12-31 09:07:27 -03:00
parent 21b8eab3cb
commit cccc6b5a93
136 changed files with 15763 additions and 472 deletions

18
artery/shunts/__init__.py Normal file
View File

@@ -0,0 +1,18 @@
"""
Shunts - Fake connectors for testing.
A shunt redirects flow when the real service isn't available.
Each shunt mimics a vein's interface with configurable responses.
Structure:
shunts/<service>/
├── main.py # FastAPI with config UI
├── depot/
│ └── responses.json # Configurable fake responses
└── README.md
Usage:
1. Start the shunt instead of the real vein
2. Configure responses via the UI or responses.json
3. Run tests against the shunt
"""

View File

@@ -0,0 +1,37 @@
# Example Shunt
Template for creating fake service connectors for testing.
## Usage
```bash
# Run the shunt
python main.py
# Or with uvicorn
uvicorn main:app --port 8099 --reload
```
## Creating a New Shunt
1. Copy this directory:
```bash
cp -r shunts/example shunts/mercadopago
```
2. Edit `depot/responses.json` with fake responses
3. Update `main.py` to match the real vein's API endpoints
## Configuration
Edit `depot/responses.json` to configure fake responses:
```json
{
"GET /endpoint": {"response": "data"},
"POST /endpoint": {"success": true}
}
```
The shunt UI at `/` shows current configuration.

View File

@@ -0,0 +1,15 @@
{
"GET /users": [
{"id": 1, "name": "Test User", "email": "test@example.com"}
],
"GET /users/1": {
"id": 1,
"name": "Test User",
"email": "test@example.com"
},
"POST /users": {
"id": 2,
"name": "Created User",
"success": true
}
}

View File

@@ -0,0 +1,93 @@
"""
Example Shunt - Template for creating fake service connectors.
Copy this to create a new shunt:
cp -r shunts/example shunts/mercadopago
Then customize:
- Update responses.json with fake responses
- Add endpoints matching the real vein's API
"""
import json
import os
from pathlib import Path
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, JSONResponse
app = FastAPI(title="Example Shunt", description="Fake service for testing")
BASE_DIR = Path(__file__).parent
DEPOT_DIR = Path(os.getenv("SHUNT_DEPOT", str(BASE_DIR / "depot")))
# Load responses
RESPONSES_FILE = DEPOT_DIR / "responses.json"
responses = {}
if RESPONSES_FILE.exists():
responses = json.loads(RESPONSES_FILE.read_text())
@app.get("/health")
def health():
return {"status": "ok", "shunt": "example"}
@app.get("/", response_class=HTMLResponse)
def config_ui():
"""Simple UI to view/edit responses."""
return f"""
<!DOCTYPE html>
<html>
<head>
<title>Example Shunt</title>
<style>
body {{ font-family: system-ui; background: #111827; color: #f3f4f6; padding: 2rem; }}
h1 {{ color: #60a5fa; }}
pre {{ background: #1f2937; padding: 1rem; border-radius: 8px; overflow-x: auto; }}
.info {{ color: #9ca3af; }}
</style>
</head>
<body>
<h1>Example Shunt</h1>
<p class="info">This shunt returns configurable fake responses for testing.</p>
<h2>Current Responses</h2>
<pre>{json.dumps(responses, indent=2)}</pre>
<p class="info">Edit {RESPONSES_FILE} to change responses.</p>
</body>
</html>
"""
@app.get("/api/{endpoint:path}")
def fake_get(endpoint: str):
"""Return configured response for GET requests."""
key = f"GET /{endpoint}"
if key in responses:
return responses[key]
return {"error": "not configured", "endpoint": endpoint, "method": "GET"}
@app.post("/api/{endpoint:path}")
async def fake_post(endpoint: str, request: Request):
"""Return configured response for POST requests."""
key = f"POST /{endpoint}"
if key in responses:
return responses[key]
body = (
await request.json()
if request.headers.get("content-type") == "application/json"
else {}
)
return {
"error": "not configured",
"endpoint": endpoint,
"method": "POST",
"received": body,
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8099)