FORGE-F008 Add unified testing toolkit for generated projects

This commit is contained in:
rob
2026-11-19 14:15:07 +00:00
parent b9cc14333a
commit 9dc4420373
20 changed files with 883 additions and 5 deletions
@@ -0,0 +1,22 @@
{
"version": 1,
"manual": {
"kind": "compose",
"environment": "development",
"file": "docker-compose.yml",
"env_file": ".env.example",
"project": "__APP_SLUG__",
"ports": {"BORTSTACK_API_PORT": 5001, "BORTSTACK_WEB_PORT": 8081},
"readiness": ["http://127.0.0.1:5001/api/health/ready", "http://127.0.0.1:8081/"],
"urls": ["http://127.0.0.1:8081/"],
"authentication": {"required": true, "provision": ["python3", "scripts/provision-test-user.py"]}
},
"automated": {
"default": ["unit"],
"suites": {
"unit": {"command": ["./scripts/run-unit-tests.sh"], "cwd": "."},
"integration": {"command": ["./scripts/run-integration-tests.sh"], "cwd": "."}
},
"act": []
}
}
@@ -20,6 +20,38 @@ Windows PowerShell: create the virtual environment with `py -3 -m venv api/.venv
## Validation
The generated testing toolkit is available without Forge at runtime:
```sh
./scripts/testtask.sh --start # Compose development services and local account
./scripts/testtask.sh --status
./scripts/testtask.sh --stop # stops only this worktree's Compose project
./scripts/testrunner.sh --unit
./scripts/testrunner.sh --integration
```
The toolkit uses Git worktree metadata. Generate with `--git-init` or initialise
Git locally before using the commands; an initial commit is not required for
the current worktree. Explicit branch targets require a local branch ref and
an existing checkout.
Configure a private local test account in `~/.config/bortstack/testing.env`
with `BORTSTACK_TEST_EMAIL` and `BORTSTACK_TEST_PASSWORD`, then `chmod 600`
that file. `testtask.sh` asks before using it and permits a one-session hidden
password override. Non-interactive use requires `--use-default-credentials`.
The generated development hook creates a verified account only in this
worktree's Compose development database; an existing account is accepted only
when its password matches. Compose uses ports 5001 and 8081 by default;
adjust `.bortstack/testing.json` if they are occupied. Keep the same file's
URLs in sync. The toolkit never removes the persistent SQLite volume.
`testrunner.sh` requires the documented Python virtual environment and
`react/node_modules`; it does not install dependencies. `--act` is unavailable
until a reviewed local-safe GitHub workflow/job is explicitly allowlisted in
`.bortstack/testing.json`. The optional deployment workflow is not allowlisted.
See the generated config and Forge's toolkit README for the schema and Windows
requirements.
```sh
cd api && .venv/bin/pytest
cd react && npm run lint && npm test && npm run build
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
"""Provision only this generated project's local Compose development account."""
import json
import os
import subprocess
import sys
from pathlib import Path
project = os.environ.get("BORTSTACK_COMPOSE_PROJECT", "")
compose_file = Path(os.environ.get("BORTSTACK_COMPOSE_FILE", ""))
secret_file = Path(os.environ.get("BORTSTACK_CREDENTIALS_FILE", ""))
if not project.startswith("bortstack-") or not compose_file.is_file() or not secret_file.is_file():
raise SystemExit("local Compose project and temporary credentials are required")
if os.environ.get("APP_ENV", "development") != "development":
raise SystemExit("refusing account provisioning outside development")
secret = json.loads(secret_file.read_text(encoding="utf-8"))
if not secret.get("BORTSTACK_TEST_EMAIL") or not secret.get("BORTSTACK_TEST_PASSWORD"):
raise SystemExit("missing local test credentials")
base = ["docker", "compose", "--project-name", project, "--file", str(compose_file)]
if subprocess.run(base + ["exec", "-T", "api", "flask", "--app", "api.py", "db", "upgrade"], check=False).returncode:
raise SystemExit("local development migration failed")
code = '''
import json, sys
from datetime import datetime
from sqlalchemy import func
from settings import app, db
from models.user import User, normalize_email
values = json.load(sys.stdin)
email = normalize_email(values["BORTSTACK_TEST_EMAIL"])
password = values["BORTSTACK_TEST_PASSWORD"]
if len(password) < 8:
raise SystemExit("test password must have at least eight characters")
with app.app_context():
existing = User.query.filter(func.lower(User.email) == email).first()
if existing:
if not existing.check_password(password) or existing.email_verified_at is None:
raise SystemExit("test account exists with different credentials or is unverified; no account was changed")
else:
user = User(email=email, username="Local Test User", email_verified_at=datetime.utcnow())
user.set_password(password)
db.session.add(user)
db.session.commit()
'''
result = subprocess.run(base + ["exec", "-T", "api", "python", "-c", code],
input=json.dumps(secret), text=True, check=False)
sys.exit(result.returncode)
@@ -0,0 +1,8 @@
#!/bin/sh
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
if [ ! -d "$root/react/node_modules" ]; then
echo 'integration tests unavailable: install react/node_modules first' >&2
exit 3
fi
(cd "$root/react" && npm run test:e2e)
@@ -0,0 +1,9 @@
#!/bin/sh
set -eu
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
if [ ! -x "$root/api/.venv/bin/python" ] || [ ! -d "$root/react/node_modules" ]; then
echo 'unit tests unavailable: install api/.venv and react/node_modules first' >&2
exit 3
fi
(cd "$root/api" && APP_ENV=testing .venv/bin/python -m pytest -c pytest.ini tests)
(cd "$root/react" && npm test -- --run)