CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (pull_request) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Successful in 1m6s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Successful in 4s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (push) Successful in 4s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Failing after 21s
CI / Web (dms-browser) (push) Failing after 1m23s
CI / Web (pdf-viewer) (push) Failing after 1m2s
CI / Web (shl) (push) Failing after 16s
Supply-Chain-Scan (Go) / govulncheck (push) Failing after 1m2s
AI-Pflichttest-Gate / pflichttest-gate (pull_request) Successful in 24m42s
CI / Core-Modul (Build/Vet/Test) (pull_request) Successful in 1m0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (pull_request) Successful in 4s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (pull_request) Successful in 5s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (pull_request) Failing after 25s
CI / Web (dms-browser) (pull_request) Failing after 1m32s
CI / Web (pdf-viewer) (pull_request) Failing after 55s
CI / Web (shl) (pull_request) Failing after 13s
Core-Schnittstellen-Vertragstests / contract-tests (pull_request) Successful in 24m26s
Supply-Chain-Scan (Go) / govulncheck (pull_request) Failing after 1m2s
Supply-Chain-Scan (npm) / npm-audit (pull_request) Failing after 1m52s
Core-Pflichttest-Gate / pflichttest-gate (pull_request) Successful in 24m43s
RBAC-Rollenhierarchie-Pflichtgate / rbac-policy-invariant (pull_request) Successful in 25m10s
Workflow-Pflichttest-Gate / pflichttest-gate (pull_request) Successful in 24m45s
DB-Integrationstests / db-tests (pull_request) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (pull_request) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
workflow bearerauth setzt X-User-Role nur aus den Claims (Client-Header werden entfernt); taskapi und connectorsetupapi fragen mit dieser Rolle, Default user. Migration 0029: connect.setup.write nur fuer tenant_admin. ai-app, workflow-app, connect-app starten ohne POLICY_URL nicht mehr, ausser mit NEXARCH_<MODUL>_APP_POLICY_DISABLED=1. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
228 lines
8.0 KiB
Go
228 lines
8.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"gitea.perlbach24.de/scripte/nexarch/ai/internal/migrate"
|
|
"gitea.perlbach24.de/scripte/nexarch/ai/internal/provider"
|
|
"gitea.perlbach24.de/scripte/nexarch/ai/internal/provider/mock"
|
|
"gitea.perlbach24.de/scripte/nexarch/ai/internal/queue"
|
|
"gitea.perlbach24.de/scripte/nexarch/common/testdb"
|
|
)
|
|
|
|
// AK3 + AK4: eine fehlende Pflicht-Umgebungsvariable liefert einen klaren
|
|
// Fehler statt eines stillen Abbruchs, bevor irgendein Port geoeffnet wird.
|
|
func TestLoadConfig_MissingRequiredVars(t *testing.T) {
|
|
empty := func(string) string { return "" }
|
|
|
|
if _, err := loadConfig(empty); err == nil {
|
|
t.Fatal("erwartete fehler bei fehlender NEXARCH_AI_APP_TENANT_DSN")
|
|
} else if !strings.Contains(err.Error(), "NEXARCH_AI_APP_TENANT_DSN") {
|
|
t.Fatalf("fehlermeldung nennt nicht die fehlende variable: %v", err)
|
|
}
|
|
|
|
withDSN := func(k string) string {
|
|
if k == "NEXARCH_AI_APP_TENANT_DSN" {
|
|
return "postgres://x"
|
|
}
|
|
return ""
|
|
}
|
|
if _, err := loadConfig(withDSN); err == nil {
|
|
t.Fatal("erwartete fehler bei fehlender NEXARCH_AI_APP_SERVICE_TOKEN")
|
|
} else if !strings.Contains(err.Error(), "NEXARCH_AI_APP_SERVICE_TOKEN") {
|
|
t.Fatalf("fehlermeldung nennt nicht die fehlende variable: %v", err)
|
|
}
|
|
}
|
|
|
|
// Issue #369: Policy-URL ohne Service-Token darf nicht still ohne
|
|
// Rechtepruefung starten.
|
|
func TestLoadConfig_PolicyURLRequiresServiceToken(t *testing.T) {
|
|
env := map[string]string{
|
|
"NEXARCH_AI_APP_TENANT_DSN": "postgres://x",
|
|
"NEXARCH_AI_APP_SERVICE_TOKEN": "tok",
|
|
"NEXARCH_AI_APP_POLICY_URL": "http://127.0.0.1:8093",
|
|
}
|
|
get := func(k string) string { return env[k] }
|
|
if _, err := loadConfig(get); err == nil || !strings.Contains(err.Error(), "NEXARCH_AI_APP_POLICY_SERVICE_TOKEN") {
|
|
t.Fatalf("erwartete fehler zur fehlenden NEXARCH_AI_APP_POLICY_SERVICE_TOKEN, bekam %v", err)
|
|
}
|
|
env["NEXARCH_AI_APP_POLICY_SERVICE_TOKEN"] = "policy-tok"
|
|
cfg, err := loadConfig(get)
|
|
if err != nil {
|
|
t.Fatalf("unerwarteter fehler: %v", err)
|
|
}
|
|
if cfg.policyClient() == nil {
|
|
t.Fatal("policy-client muss bei gesetzter URL aktiv sein")
|
|
}
|
|
if (config{}).policyClient() != nil {
|
|
t.Fatal("ohne URL darf kein policy-client entstehen")
|
|
}
|
|
}
|
|
|
|
func TestLoadConfig_Defaults(t *testing.T) {
|
|
env := map[string]string{
|
|
"NEXARCH_AI_APP_TENANT_DSN": "postgres://x",
|
|
"NEXARCH_AI_APP_SERVICE_TOKEN": "tok",
|
|
"NEXARCH_AI_APP_POLICY_DISABLED": "1",
|
|
}
|
|
cfg, err := loadConfig(func(k string) string { return env[k] })
|
|
if err != nil {
|
|
t.Fatalf("unerwarteter fehler: %v", err)
|
|
}
|
|
if cfg.addr != defaultListenAddr {
|
|
t.Errorf("addr = %q, want default %q", cfg.addr, defaultListenAddr)
|
|
}
|
|
if cfg.model != defaultModel {
|
|
t.Errorf("model = %q, want default %q", cfg.model, defaultModel)
|
|
}
|
|
}
|
|
|
|
func newTestPool(t *testing.T) *pgxpool.Pool {
|
|
t.Helper()
|
|
dsn := os.Getenv("TEST_TENANT_DSN")
|
|
if dsn == "" {
|
|
t.Skip("TEST_TENANT_DSN nicht gesetzt, Integrationstest uebersprungen")
|
|
}
|
|
pool := testdb.Pool(t, dsn)
|
|
|
|
root, err := filepath.Abs("../../migrations")
|
|
if err != nil {
|
|
t.Fatalf("migrationsverzeichnis: %v", err)
|
|
}
|
|
migrations, err := migrate.Load(root)
|
|
if err != nil {
|
|
t.Fatalf("migrationen laden: %v", err)
|
|
}
|
|
if _, err := migrate.Up(context.Background(), pool, migrations); err != nil {
|
|
t.Fatalf("migrationen anwenden: %v", err)
|
|
}
|
|
return pool
|
|
}
|
|
|
|
// AK1 + Pruefung 1: der real gestartete Dienst beantwortet mindestens einen
|
|
// Endpunkt je eingebundenem AI-API-Paket ueber denselben ServeMux.
|
|
func TestBuild_AllPackagesRegistered(t *testing.T) {
|
|
pool := newTestPool(t)
|
|
cfg := config{serviceToken: "test-token", model: "mock"}
|
|
prov := &mock.Provider{}
|
|
w := build(pool, cfg, prov, "test-worker")
|
|
|
|
srv := httptest.NewServer(w.mux)
|
|
defer srv.Close()
|
|
|
|
get := func(method, path string, body string) *http.Response {
|
|
req, err := http.NewRequest(method, srv.URL+path, strings.NewReader(body))
|
|
if err != nil {
|
|
t.Fatalf("request aufbauen: %v", err)
|
|
}
|
|
req.Header.Set("X-Service-Token", "test-token")
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("%s %s: %v", method, path, err)
|
|
}
|
|
return resp
|
|
}
|
|
|
|
// healthz: kein API-Paket, aber Nachweis, dass der Mux ueberhaupt bedient wird.
|
|
if resp := get("GET", "/healthz", ""); resp.StatusCode != http.StatusOK {
|
|
t.Errorf("/healthz status = %d, want 200", resp.StatusCode)
|
|
}
|
|
|
|
// residencyapi (GOV-03): unkonfigurierter Tenant liefert einen definierten
|
|
// Default statt eines Fehlers, das Paket ist also erreichbar verdrahtet.
|
|
if resp := get("GET", "/tenants/acme/residency", ""); resp.StatusCode != http.StatusOK {
|
|
t.Errorf("residencyapi status = %d, want 200", resp.StatusCode)
|
|
}
|
|
|
|
// routingsuggestapi (WFS-02): leere Historie ist gueltig (low_data-Antwort).
|
|
if resp := get("POST", "/workflow/routing-suggestions", `{"history":[]}`); resp.StatusCode != http.StatusOK {
|
|
t.Errorf("routingsuggestapi status = %d, want 200", resp.StatusCode)
|
|
}
|
|
|
|
// semanticsearchapi (SEM-03): fehlende freigegebene Referenzen sind ein
|
|
// definierter 400er, kein 404/501 -- die Route ist also registriert.
|
|
if resp := get("POST", "/search", `{"tenant_slug":"acme","query":"x","top_k":3}`); resp.StatusCode != http.StatusBadRequest {
|
|
t.Errorf("semanticsearchapi status = %d, want 400 (keine freigegebenen referenzen)", resp.StatusCode)
|
|
}
|
|
|
|
// summaryapi und extractapi rufen tatsaechlich den Provider auf (kein
|
|
// synthetischer Leerfall) -- die HTTP-Bindung wird separat unten mit
|
|
// einem Mock-Provider durchlaufen, hier nur die Auth-/Routing-Ebene.
|
|
if resp := get("POST", "/documents/summarize", `{"text":""}`); resp.StatusCode == http.StatusNotFound {
|
|
t.Errorf("summaryapi route nicht registriert (404)")
|
|
}
|
|
if resp := get("POST", "/invoices/extract", `{"text":""}`); resp.StatusCode == http.StatusNotFound {
|
|
t.Errorf("extractapi route nicht registriert (404)")
|
|
}
|
|
}
|
|
|
|
// AK2 + Pruefung 2: ein echter Inferenz-Auftrag durchlaeuft die Warteschlange
|
|
// real bis zum Abschluss und die Verbrauchszaehlung (GOV-04) wird dabei
|
|
// tatsaechlich erhoeht -- Nachweis, dass der Worker laeuft, nicht nur die
|
|
// HTTP-Handler ohne Hintergrundprozess.
|
|
func TestBuild_QueueWorkerProcessesJobWithQuota(t *testing.T) {
|
|
pool := newTestPool(t)
|
|
cfg := config{serviceToken: "test-token", model: "mock"}
|
|
prov := &mock.Provider{ChatResponse: provider.ChatResponse{Message: provider.Message{Content: "ok"}}}
|
|
w := build(pool, cfg, prov, "test-worker")
|
|
|
|
ctx := context.Background()
|
|
jobID, err := w.queue.Enqueue(ctx, queue.KindChat, provider.ChatRequest{
|
|
Messages: []provider.Message{{Role: "user", Content: "hallo"}},
|
|
}, queue.EnqueueOptions{TenantSlug: "acme-eng08"})
|
|
if err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
if err := w.worker.ProcessOne(ctx); err != nil && !errors.Is(err, queue.ErrNoJobAvailable) {
|
|
t.Fatalf("worker.ProcessOne: %v", err)
|
|
}
|
|
job, err := w.queue.Status(ctx, jobID)
|
|
if err != nil {
|
|
t.Fatalf("status: %v", err)
|
|
}
|
|
if job.Status == queue.StatusSucceeded {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("job nicht innerhalb der frist abgeschlossen, status=%s", job.Status)
|
|
}
|
|
}
|
|
|
|
value, err := w.quotaStore.Get(ctx, "acme-eng08", "ai_inference_calls")
|
|
if err != nil {
|
|
t.Fatalf("quota-zaehlerstand lesen: %v", err)
|
|
}
|
|
if value != 1 {
|
|
t.Errorf("quota-zaehlerstand = %d, want 1 (GOV-04-Verbrauchszaehlung nicht verdrahtet)", value)
|
|
}
|
|
}
|
|
|
|
func TestLoadConfig_PolicyRequiredUnlessDisabled(t *testing.T) {
|
|
env := map[string]string{
|
|
"NEXARCH_AI_APP_TENANT_DSN": "postgres://x",
|
|
"NEXARCH_AI_APP_SERVICE_TOKEN": "tok",
|
|
}
|
|
get := func(k string) string { return env[k] }
|
|
if _, err := loadConfig(get); err == nil || !strings.Contains(err.Error(), "NEXARCH_AI_APP_POLICY_URL") {
|
|
t.Fatalf("erwartete fehler zur fehlenden NEXARCH_AI_APP_POLICY_URL, bekam %v", err)
|
|
}
|
|
env["NEXARCH_AI_APP_POLICY_DISABLED"] = "1"
|
|
if _, err := loadConfig(get); err != nil {
|
|
t.Fatalf("mit POLICY_DISABLED=1 darf der Start nicht scheitern: %v", err)
|
|
}
|
|
}
|