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) Failing after 1m0s
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 24s
CI / Web (dms-browser) (push) Failing after 1m23s
CI / Web (pdf-viewer) (push) Failing after 59s
CI / Web (shl) (push) Failing after 13s
Supply-Chain-Scan (Go) / govulncheck (push) Failing after 56s
Workflow-Schnittstellen-Vertragstests / contract-tests (push) Failing after 59s
CI / Core-Modul (Build/Vet/Test) (pull_request) Failing after 1m1s
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 3s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (pull_request) Failing after 21s
CI / Web (dms-browser) (pull_request) Failing after 1m17s
CI / Web (pdf-viewer) (pull_request) Failing after 59s
CI / Web (shl) (pull_request) Failing after 13s
Core-Schnittstellen-Vertragstests / contract-tests (pull_request) Successful in 3m51s
Supply-Chain-Scan (Go) / govulncheck (pull_request) Failing after 56s
Supply-Chain-Scan (npm) / npm-audit (pull_request) Failing after 2m6s
Core-Pflichttest-Gate / pflichttest-gate (pull_request) Successful in 11m50s
RBAC-Rollenhierarchie-Pflichtgate / rbac-policy-invariant (pull_request) Successful in 11m41s
Workflow-Schnittstellen-Vertragstests / contract-tests (pull_request) Failing after 41s
Workflow-Pflichttest-Gate / pflichttest-gate (pull_request) Successful in 11m18s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Failing after 3s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (pull_request) Failing after 3s
DB-Integrationstests / db-tests (pull_request) Canceled after 0s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
195 lines
6.5 KiB
Go
195 lines
6.5 KiB
Go
package routingengine
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"gitea.perlbach24.de/scripte/nexarch/common/testdb"
|
|
"gitea.perlbach24.de/scripte/nexarch/workflow/internal/migrate"
|
|
"gitea.perlbach24.de/scripte/nexarch/workflow/internal/routingrule"
|
|
)
|
|
|
|
func findModuleRoot(t *testing.T) string {
|
|
t.Helper()
|
|
dir, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("getwd: %v", err)
|
|
}
|
|
for {
|
|
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
|
return dir
|
|
}
|
|
parent := filepath.Dir(dir)
|
|
if parent == dir {
|
|
t.Fatal("go.mod nicht gefunden")
|
|
}
|
|
dir = parent
|
|
}
|
|
}
|
|
|
|
func setup(t *testing.T) (*Engine, *routingrule.Store, *pgxpool.Pool) {
|
|
t.Helper()
|
|
dsn := os.Getenv("TEST_TENANT_DSN")
|
|
if dsn == "" {
|
|
t.Skip("TEST_TENANT_DSN nicht gesetzt, Integrationstest uebersprungen")
|
|
}
|
|
ctx := context.Background()
|
|
pool := testdb.Pool(t, dsn)
|
|
|
|
migrations, err := migrate.Load(filepath.Join(findModuleRoot(t), "migrations", "tenant"))
|
|
if err != nil {
|
|
t.Fatalf("migrationen laden: %v", err)
|
|
}
|
|
if _, err := migrate.Up(ctx, pool, migrations); err != nil {
|
|
t.Fatalf("migrationen anwenden: %v", err)
|
|
}
|
|
|
|
store := routingrule.NewStore(pool)
|
|
return NewEngine(store, pool), store, pool
|
|
}
|
|
|
|
// Akzeptanzkriterium 1: nicht zuordenbares Objekt landet im Klaerungskorb,
|
|
// geht also nicht verloren (Pruefung 2).
|
|
func TestExecute_NoMatch_LandsInClarificationBasket(t *testing.T) {
|
|
ctx := context.Background()
|
|
engine, store, _ := setup(t)
|
|
|
|
_, err := store.Create(ctx, "nur rechnungen", 10, []routingrule.Condition{
|
|
{Field: "object_type", Operator: "equals", Value: "invoice"},
|
|
}, []routingrule.Action{{Type: "route_to_process", Target: "approval"}})
|
|
if err != nil {
|
|
t.Fatalf("regel anlegen: %v", err)
|
|
}
|
|
|
|
decision, err := engine.Execute(ctx, "document", "doc-no-match-1", map[string]any{"object_type": "contract"})
|
|
if err != nil {
|
|
t.Fatalf("execute: %v", err)
|
|
}
|
|
if decision.Matched {
|
|
t.Fatal("erwartet: kein Treffer, aber Matched=true")
|
|
}
|
|
if !decision.InBasket || decision.BasketID == "" {
|
|
t.Fatal("erwartet: Objekt landet im Klaerungskorb")
|
|
}
|
|
|
|
entries, err := engine.UnresolvedBasketEntries(ctx)
|
|
if err != nil {
|
|
t.Fatalf("basket lesen: %v", err)
|
|
}
|
|
found := false
|
|
for _, e := range entries {
|
|
if e.ID == decision.BasketID && e.ObjectID == "doc-no-match-1" {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("Objekt nicht im Klaerungskorb wiederauffindbar -- waere verloren gegangen")
|
|
}
|
|
}
|
|
|
|
// Akzeptanzkriterium: erste zutreffende Regel nach Prioritaet gewinnt.
|
|
func TestExecute_FirstMatchingRuleByPriorityWins(t *testing.T) {
|
|
ctx := context.Background()
|
|
engine, store, _ := setup(t)
|
|
|
|
_, err := store.Create(ctx, "niedrige prioritaet", 1, []routingrule.Condition{
|
|
{Field: "object_type", Operator: "equals", Value: "invoice"},
|
|
}, []routingrule.Action{{Type: "route_to_process", Target: "low"}})
|
|
if err != nil {
|
|
t.Fatalf("niedrige regel anlegen: %v", err)
|
|
}
|
|
_, err = store.Create(ctx, "hohe prioritaet", 100, []routingrule.Condition{
|
|
{Field: "object_type", Operator: "equals", Value: "invoice"},
|
|
}, []routingrule.Action{{Type: "route_to_process", Target: "high"}})
|
|
if err != nil {
|
|
t.Fatalf("hohe regel anlegen: %v", err)
|
|
}
|
|
|
|
decision, err := engine.Execute(ctx, "document", "doc-priority-1", map[string]any{"object_type": "invoice"})
|
|
if err != nil {
|
|
t.Fatalf("execute: %v", err)
|
|
}
|
|
if !decision.Matched {
|
|
t.Fatal("erwartet: Treffer")
|
|
}
|
|
if decision.Action.Target != "high" {
|
|
t.Fatalf("erwartet Ziel 'high' (hoehere Prioritaet), bekam %q", decision.Action.Target)
|
|
}
|
|
}
|
|
|
|
// Akzeptanzkriterium 2: jede Weiterleitung ist nachvollziehbar protokolliert
|
|
// -- sowohl Treffer als auch Klaerungskorb-Faelle.
|
|
func TestExecute_EveryDecisionIsLogged(t *testing.T) {
|
|
ctx := context.Background()
|
|
engine, store, pool := setup(t)
|
|
|
|
rule, err := store.Create(ctx, "log-test-regel", 5, []routingrule.Condition{
|
|
{Field: "object_type", Operator: "equals", Value: "einvoice"},
|
|
}, []routingrule.Action{{Type: "route_to_process", Target: "approval"}})
|
|
if err != nil {
|
|
t.Fatalf("regel anlegen: %v", err)
|
|
}
|
|
|
|
if _, err := engine.Execute(ctx, "document", "doc-log-1", map[string]any{"object_type": "einvoice"}); err != nil {
|
|
t.Fatalf("execute (treffer): %v", err)
|
|
}
|
|
if _, err := engine.Execute(ctx, "document", "doc-log-2", map[string]any{"object_type": "unbekannt"}); err != nil {
|
|
t.Fatalf("execute (kein treffer): %v", err)
|
|
}
|
|
|
|
var matchedCount, unmatchedCount int
|
|
if err := pool.QueryRow(ctx, `SELECT count(*) FROM routing_decisions WHERE object_id = 'doc-log-1' AND matched_rule_id = $1`, rule.ID).Scan(&matchedCount); err != nil {
|
|
t.Fatalf("protokoll (treffer) lesen: %v", err)
|
|
}
|
|
if matchedCount != 1 {
|
|
t.Fatalf("erwartet 1 protokollierte Treffer-Entscheidung, bekam %d", matchedCount)
|
|
}
|
|
if err := pool.QueryRow(ctx, `SELECT count(*) FROM routing_decisions WHERE object_id = 'doc-log-2' AND matched_rule_id IS NULL`).Scan(&unmatchedCount); err != nil {
|
|
t.Fatalf("protokoll (kein treffer) lesen: %v", err)
|
|
}
|
|
if unmatchedCount != 1 {
|
|
t.Fatalf("erwartet 1 protokollierte Nicht-Treffer-Entscheidung, bekam %d", unmatchedCount)
|
|
}
|
|
}
|
|
|
|
// Akzeptanzkriterium 3 / Pruefung 3: eine Regeländerung wirkt ohne Neustart,
|
|
// da Execute bei jedem Aufruf frisch aus der Datenbank liest.
|
|
func TestExecute_RuleChangeTakesEffectWithoutRestart(t *testing.T) {
|
|
ctx := context.Background()
|
|
engine, store, _ := setup(t)
|
|
|
|
rule, err := store.Create(ctx, "aenderbare regel", 5, []routingrule.Condition{
|
|
{Field: "object_type", Operator: "equals", Value: "mail"},
|
|
}, []routingrule.Action{{Type: "route_to_process", Target: "process-a"}})
|
|
if err != nil {
|
|
t.Fatalf("regel anlegen: %v", err)
|
|
}
|
|
|
|
decision1, err := engine.Execute(ctx, "message", "msg-1", map[string]any{"object_type": "mail"})
|
|
if err != nil {
|
|
t.Fatalf("execute vor aenderung: %v", err)
|
|
}
|
|
if decision1.Action.Target != "process-a" {
|
|
t.Fatalf("erwartet 'process-a' vor Aenderung, bekam %q", decision1.Action.Target)
|
|
}
|
|
|
|
// Aenderung der Regel zur Laufzeit -- dieselbe Engine-Instanz, kein Neustart.
|
|
if _, err := store.Update(ctx, rule.ID, []routingrule.Condition{
|
|
{Field: "object_type", Operator: "equals", Value: "mail"},
|
|
}, []routingrule.Action{{Type: "route_to_process", Target: "process-b"}}); err != nil {
|
|
t.Fatalf("regel aendern: %v", err)
|
|
}
|
|
|
|
decision2, err := engine.Execute(ctx, "message", "msg-2", map[string]any{"object_type": "mail"})
|
|
if err != nil {
|
|
t.Fatalf("execute nach aenderung: %v", err)
|
|
}
|
|
if decision2.Action.Target != "process-b" {
|
|
t.Fatalf("erwartet 'process-b' nach Aenderung ohne Neustart, bekam %q", decision2.Action.Target)
|
|
}
|
|
}
|