P-0000: Register-Dienst (Minimalfassung) mit FastAPI und SQLite, Sendebefehl projekt_id.sh, Tests projekt2id.2 und .3

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-03 10:48:39 +01:00
parent 8453e77153
commit c7f33b46bc
12 changed files with 848 additions and 3 deletions

119
server/tests/projekt2id.py Normal file
View File

@ -0,0 +1,119 @@
#!/usr/bin/env python3
"""Automatische Tests projekt2id.2 und .3 (Übergangsweg über skill/scripts/projekt_id.sh, bis es den Agenten gibt).
Startet einen echten Register-Dienst (run.py) auf 127.0.0.1 mit frischer Datenbank in einem Temp-Ordner und ruft
projekt_id.sh, init.sh und check_format.py auf. Aufruf (aus dem Projektordner): python3 server/tests/projekt2id.py
Ausgabe je Test `- id: ✓|✗ Notiz`, Exit-Code 1 wenn einer ✗ ist. Test .1 (Agent holt die ID nach) ist hier NICHT abgedeckt.
"""
import json
import os
import shutil
import socket
import subprocess
import sys
import tempfile
import time
import urllib.request
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
PY = os.path.join(ROOT, "server", ".venv", "bin", "python")
PID_SH = os.path.join(ROOT, "skill", "scripts", "projekt_id.sh")
INIT = os.path.join(ROOT, "skill", "scripts", "init.sh")
LINT = os.path.join(ROOT, "skill", "scripts", "check_format.py")
alle = []
def melde(tid, ok, notiz):
alle.append(ok)
print(f"- {tid}: {'✓' if ok else '✗'} {notiz}")
def freier_port():
s = socket.socket()
s.bind(("127.0.0.1", 0))
p = s.getsockname()[1]
s.close()
return p
def projekt(basis, name, remote):
d = os.path.join(basis, name)
os.makedirs(d)
subprocess.run(["git", "init", "-q", d], check=True)
subprocess.run(["git", "-C", d, "remote", "add", "origin", remote], check=True)
return d
def holen(url, pfad):
with urllib.request.urlopen(url + pfad, timeout=5) as r:
return json.loads(r.read())
def main():
basis = tempfile.mkdtemp(prefix="projekt2id-")
port = freier_port()
url = f"http://127.0.0.1:{port}"
env = dict(os.environ, REGISTER_PORT=str(port), REGISTER_BIND="127.0.0.1", REGISTER_DB=os.path.join(basis, "daten", "r.db"))
dienst = subprocess.Popen([PY, os.path.join(ROOT, "server", "run.py")], env=env, cwd=os.path.join(ROOT, "server"),
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
try:
for _ in range(50):
try:
if holen(url, "/api/health").get("ok"):
break
except Exception:
time.sleep(0.1)
else:
print("Dienst startete nicht:", dienst.stdout.read() if dienst.poll() is not None else "keine Antwort")
sys.exit(1)
e = dict(os.environ, REGISTER_URL=url)
# .2 ID holen, Projekt mit init.sh anlegen, Register enthält Name und Git-Adresse
d1 = projekt(basis, "erstes", "ssh://git@git.example.org:2222/test/erstes.git")
p = subprocess.run(["bash", PID_SH, "Erstes Projekt", d1], env=dict(e, RECHNER="rechner-a"), capture_output=True, text=True)
pid = p.stdout.strip()
i = subprocess.run(["bash", INIT, "Erstes Projekt", d1, pid], capture_output=True, text=True)
l = subprocess.run([sys.executable, "-I", LINT, os.path.join(d1, "ROADMAP.md")], capture_output=True, text=True)
reg = {x["id"]: x for x in holen(url, "/api/projekte")}
ok = (p.returncode == 0 and pid == "P-0001" and i.returncode == 0 and l.returncode == 0 and f"Projekt-ID: {pid}" in l.stdout
and pid in reg and reg[pid]["name"] == "Erstes Projekt"
and reg[pid]["git_adresse"] == "git.example.org:2222/test/erstes" and reg[pid]["rechner"] == ["rechner-a"])
melde("projekt2id.2", ok, "nächste freie ID P-0001, init.sh legt Projekt an, Prüfskript OK, Register hat Name, Git-Adresse und Rechner"
if ok else f"Sendebefehl Exit {p.returncode} {p.stderr[-200:]!r}, ID {pid!r}, init {i.returncode}, lint {l.returncode} {l.stdout[-200:]!r}")
# .3 dasselbe Repo auf zweitem Rechner (andere Schreibweise der Adresse): vorhandene ID, keine neue
d2 = projekt(basis, "zweiter-rechner", "ssh://git@GIT.example.org:2222/test/erstes/")
p2 = subprocess.run(["bash", PID_SH, "Erstes Projekt", d2], env=dict(e, RECHNER="rechner-b"), capture_output=True, text=True)
d3 = projekt(basis, "anderes", "ssh://git@git.example.org:2222/test/anderes.git")
p3 = subprocess.run(["bash", PID_SH, "Anderes", d3], env=dict(e, RECHNER="rechner-b"), capture_output=True, text=True)
reg = {x["id"]: x for x in holen(url, "/api/projekte")}
ok = (p2.returncode == 0 and p2.stdout.strip() == pid and reg[pid]["rechner"] == ["rechner-a", "rechner-b"]
and p3.stdout.strip() == "P-0002" and len(reg) == 3)
melde("projekt2id.3", ok, "zweiter Rechner bekommt dieselbe ID P-0001 (Rechner a und b vermerkt), ein weiteres Projekt P-0002, im Register 3 Projekte"
if ok else f"zweiter Rechner: {p2.stdout.strip()!r} {p2.stderr[-200:]!r}, drittes: {p3.stdout.strip()!r}, Register: {list(reg)}")
# Zusatz: Dienst nicht erreichbar -> klare Meldung, Exit 1, keine ID
p4 = subprocess.run(["bash", PID_SH, "X", d1], env=dict(e, REGISTER_URL=f"http://127.0.0.1:{freier_port()}"), capture_output=True, text=True)
ok = p4.returncode == 1 and p4.stdout.strip() == "" and "nicht erreichbar" in p4.stderr and "offen" in p4.stderr
melde("projekt2id.2z", ok, "Dienst nicht erreichbar: Exit 1, keine ID, Hinweis auf 'offen'" if ok else f"Exit {p4.returncode}, {p4.stderr[-200:]!r}")
# Zusatz: Projekt ohne Remote origin -> Fehler, nichts wird vergeben
d5 = os.path.join(basis, "ohne-remote")
os.makedirs(d5)
subprocess.run(["git", "init", "-q", d5], check=True)
p5 = subprocess.run(["bash", PID_SH, "Y", d5], env=e, capture_output=True, text=True)
n_nachher = len(holen(url, "/api/projekte"))
ok = p5.returncode == 1 and "origin" in p5.stderr and n_nachher == 3
melde("projekt2id.2y", ok, "ohne Remote origin: Fehler, im Register nichts Neues" if ok else f"Exit {p5.returncode}, Register {n_nachher}")
finally:
dienst.terminate()
try:
dienst.wait(timeout=5)
except subprocess.TimeoutExpired:
dienst.kill()
shutil.rmtree(basis, ignore_errors=True)
sys.exit(0 if all(alle) else 1)
if __name__ == "__main__":
main()