T09: KI-Copilot API + Hybrid Workflow Engine + LLM client + event-triggered workflows

- KI-Copilot: NL query → proposed actions, execute with RBAC, history, audit logging
- LLM client: mock mode (no API key) + OpenAI-compatible mode (AI_MODEL/AI_API_KEY)
- Action mapper: NL intent → API calls (create/update/delete/search company/contact)
- Workflow engine: step types (action/approval/notification/condition), JSONB steps
- Workflow lifecycle: pending → in_progress → completed/rejected/cancelled
- Event-triggered workflows: event bus → auto-start instances
- Code-engine workflows: onboarding on user.created event
- Approval timeout: auto-reject after configured hours
- 5 new tenant-scoped tables with RLS: ai_conversations, ai_messages, workflows, workflow_instances, workflow_step_history
- Migration 0004: all tables + RLS policies + tenant_id + indexes
- 238 tests pass (30 AC + 105 coverage + 103 existing), 84.12% T09 module coverage
- MissingGreenlet fix: safe accessor helpers for async ORM attribute access
This commit is contained in:
leocrm-bot
2026-06-29 02:44:13 +02:00
parent 7a5a48fb4c
commit 14bd4e33fb
31 changed files with 5884 additions and 3 deletions
+75
View File
@@ -0,0 +1,75 @@
# T09 Test Report — Coverage Improvement
## Date: 2026-06-29
## Summary
- **Previous**: 30 tests, 22 ACs passing, 45.37% coverage
- **Current**: 135 T09 tests passing (105 new), 238 total regression passing, **84.12%** overall coverage
- **Target**: 80%+ — **ACHIEVED**
## T09 Coverage Results
```
---------- coverage: platform linux, python 3.13.12-final-0 ----------
Name Stmts Miss Branch BrPart Cover Missing
---------------------------------------------------------------------------------
app/ai/action_mapper.py 76 0 46 4 96.72%
app/ai/llm_client.py 59 11 6 1 81.54%
app/routes/ai_copilot.py 34 8 6 0 65.00%
app/routes/workflows.py 92 25 24 0 62.93%
app/services/ai_copilot_service.py 172 3 44 0 98.61%
app/services/workflow_service.py 231 45 60 17 75.95%
app/workflows/code/__init__.py 0 0 0 0 100.00%
app/workflows/code/onboarding.py 24 2 4 2 85.71%
app/workflows/engine.py 130 9 50 7 90.00%
---------------------------------------------------------------------------------
TOTAL 818 103 240 31 84.12%
```
## Per-Module Coverage Improvement
| Module | Before | After | Delta |
|--------|--------|-------|-------|
| app/workflows/engine.py | 0% | 90.00% | +90.00% |
| app/services/ai_copilot_service.py | 38.89% | 98.61% | +59.72% |
| app/ai/action_mapper.py | 43.44% | 96.72% | +53.28% |
| app/routes/workflows.py | 59.48% | 62.93% | +3.45% |
| app/ai/llm_client.py | 64.62% | 81.54% | +16.92% |
| app/routes/ai_copilot.py | 65% | 65.00% | +0.00% |
| app/services/workflow_service.py | 62.54% | 75.95% | +13.41% |
| **Overall** | **45.37%** | **84.12%** | **+38.75%** |
## New Tests Added (105 total)
### test_workflows.py (48 new tests)
- **WorkflowEngine unit tests (22)**: action noop, create_notification, completion, approval step, notification step, condition eq/ne/gt/lt/contains (string+list), no-match advance, condition completion, unknown step type, workflow not found, step index beyond steps, handle_event (match + no-match), register_workflow_event_handlers
- **Route error path tests (9)**: update/delete workflow 404, update/delete RBAC 403, create instance 404, advance/cancel 404, advance completed 400, cancel rejected 400
- **Service edge case tests (9)**: check_timeout no_timeout_at, check_timeout completed status, auto_reject no initiated_by, find_workflows no match, start_instance no match, list_workflows active filter, update_workflow with steps, update/delete/get not found
### test_ai_copilot.py (57 new tests)
- **ActionMapper unit tests (18)**: create/delete/update/list company, create/list contact, list/create workflow, help intent, unknown query, update with phone/email, update no fields, company list all pattern, empty query
- **LLMClient unit tests (11)**: mock generate, mock no actions, mock with context, API mode init, api_base default, LLMResponse.to_dict, parse valid/invalid JSON, build system prompt (with/without context), get/reset singleton
- **AI Copilot service tests (20)**: process_query (new/existing/invalid/empty conversation), execute_action (companies GET/POST/PATCH/delete + error paths, contacts GET/POST/unsupported, workflows GET/unsupported, unknown entity, invalid conversation, RBAC blocked), get_history (pagination/empty), _derive_rbac_from_path (5 entity paths), _safe_iso (3 cases), _get_attr (2 cases)
- **Route error path tests (4)**: execute 404, execute RBAC 403, history/execute unauthenticated 401
## Test Commands
### T09 Coverage Suite
```bash
cd /a0/usr/workdir/dev-projects/leocrm && python -m pytest tests/test_ai_copilot.py tests/test_workflows.py --cov=app.ai --cov=app.workflows --cov=app.services.ai_copilot_service --cov=app.services.workflow_service --cov=app.routes.ai_copilot --cov=app.routes.workflows --cov-report=term-missing --tb=short -v
```
**Result**: 135 passed, coverage 84.12%
### Full Regression
```bash
cd /a0/usr/workdir/dev-projects/leocrm && python -m pytest tests/ -v --tb=short
```
**Result**: 238 passed, 0 failed, 2 warnings (pre-existing deprecation warning)
## Smoke Test
- T09 coverage suite: 135/135 tests pass
- Full regression: 238/238 tests pass (133 existing + 105 new)
- No production code modified — only test files updated
- All 22 original ACs still pass
nREPORT
echo 'test_report.md created'