* feat: AI-assisted flow builder with 4-stage wizard Implements the complete AI flow builder feature using a guided 4-stage wizard (Foundation → Scaffold → Branch Detail → Review & Assemble). AI assists at bounded points using Claude Haiku for cost-efficient structured JSON generation (~$0.01-0.03/flow). Backend: new models (ai_conversations, ai_usage), Alembic migration, quota enforcement with billing anchor, Anthropic API integration with prompt caching, tree validation, conversation CRUD with 24h TTL, APScheduler cleanup job, 5 API endpoints, Pydantic schemas. Frontend: TypeScript types, API client, Zustand store for wizard state, 7 components (modal, step indicator, foundation form, branch selector, branch detail view, tree preview, quota display), MyTreesPage integration with "Build with AI" button (hidden when AI not configured). Tests: 14 validator unit tests + 11 endpoint integration tests with mocked Anthropic (zero real API spend). All 25 tests passing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: dashboard design doc and implementation plan Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 1 — pinnedFlowsStore, pagination hook, cached quota hook, sidebar refactor - Add pin() to pinnedFlowsApi - Create pinnedFlowsStore (Zustand) — single source of truth for pin state - Add dashboardMyFlowsView preference to userPreferencesStore - Create usePaginationParams hook (URL-synced) - Create useCachedQuota hook (5-min TTL) - Sidebar uses pinnedFlowsStore instead of local state Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 2 — pin/favorite buttons on all library view components - TreeGridView: star in top-right corner of cards - TreeListView: star at end of each row - TreeTableView: dedicated leftmost Favorite column - All with proper a11y (aria-label), event isolation, loading states Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 3 — Library page create dropdown + AI Builder + pin wiring - Replace single Create link with dropdown menu (3 flow types + AI Builder) - Wire pinnedFlowsStore to all view components - AI Builder modal integration via useCachedQuota hook Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 4 — Dashboard refactor with Favorites grid + paginated My Flows - Favorites section: compact grid from pinnedFlowsStore, max 2 rows, expandable - My Flows: author_id filter, URL-synced pagination (10/25/50/All) - View toggle (grid/list/table) with independent preference - Skeleton loaders, empty states with CTAs - Create dropdown with AI Builder option - 500-item ceiling for "Show All" mode Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: Phase 5 — Sidebar pinned section dual collapse + show more/less - Header collapse hides entire section, resets to 5 items on re-expand - List truncation: show first 5, "Show more (N)" expands to all - Clicking a flow auto-collapses back to 5 - Smooth max-height CSS transition (250ms ease-out) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: stabilize usePaginationParams to prevent infinite re-render loop allowedPageSizes array was recreated every render as a useMemo dep, causing infinite updates. Use useRef to stabilize the reference. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: remove Set-based Zustand selectors causing infinite re-render loop Zustand selectors returning new Set() on every call fail Object.is equality check, triggering continuous re-renders. Replaced with useMemo-derived Sets in consuming components. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: pin route ordering and star icon overlap in grid view Move GET /pinned and PATCH /pinned/reorder before GET /{tree_id} to prevent FastAPI from matching "pinned" as a UUID path parameter (422). Relocate star button from absolute positioning into the header row to avoid overlapping privacy icons and category badges. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: code review fixes — date calc, input validation, rate limits, shared components - Fix monthly_reset_at crash when billing anchor day exceeds next month's length - Add environment_tags sanitization (max 20 tags, 100 chars each) to prevent prompt injection - Add @limiter.limit("10/minute") rate limiting to all AI endpoints - Use getTreeNavigatePath() routing helper instead of hardcoded paths - Extract shared CreateFlowDropdown component from QuickStartPage and TreeLibraryPage - Clear useCachedQuota on logout to prevent stale data across user sessions - Add useRef guard to scaffold useEffect to prevent potential double-fire - Use node.id as React key instead of array index in BranchDetailView - Remove redundant dead logic in ai_tree_validator Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: correct Anthropic model ID to full dated version claude-haiku-4-5 is not a valid model alias — Anthropic requires the full dated model ID claude-haiku-4-5-20251001. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: strip markdown code fences from AI JSON responses Haiku sometimes wraps its JSON in ```json ... ``` despite the prompt instructing otherwise. Strip fences before parsing to avoid JSONDecodeError at char 0. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: increase branch_detail max_tokens to 8192 and add response logging Truncated output at 4096 tokens produces invalid JSON mid-generation. Also logs stop_reason and output_tokens per attempt to diagnose failures. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: pass explicit status='draft' when creating AI-generated flow Tree model defaults to 'published' in the DB schema, but passing status=None from the constructor overrides that default, causing a nullable=False violation and a 500 on save. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: auto-advance branch detail and pin navigation bar - Auto-advance to next undetailed branch after generation completes, using a useEffect that watches the count of detailed branches - Cap tree preview at max-h-48 with internal scroll so the nav bar is never pushed off screen - Make nav bar sticky bottom-0 with bg-card so it stays visible regardless of content height Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: increase branch retries to 3 and relax cross-reference validation on final attempt next_node_id mismatches are a common model hallucination that the retry prompt doesn't reliably fix. On the final (3rd) attempt, accept the branch with strict=False so only truly fatal errors (missing fields, dead ends, bad JSON) cause a hard failure. Cross-reference issues are minor and fixable in the tree editor. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: strengthen prompt to prevent next_node_id mismatches, keep strict validation Rather than lowering the validation bar, improve the system prompt: - Rule 6 now explicitly states next_node_id must match a direct child's id - Added rule 10: build tree bottom-up to avoid forward-reference errors - Corrective prompt now calls out the ID mismatch constraint specifically Reverts the strict=False fallback — flows must be correct before saving. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: persist branch viewing index in store to survive phase remounts Local useState resets to 0 every time phase transitions from 'generating' back to 'detailing', causing the view to snap back to branch 1. Move viewingIndex to store's currentBranchIndex (already existed) and advance it in generateBranchDetail after success. Component reads from store so remounts no longer lose position. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: correct publish validation to check title instead of action/solution fields The publish validator was checking for an 'action' field on action nodes and a 'solution' field on solution nodes, but the actual node schema (confirmed from seed data and frontend types) uses 'title'/'description'. This caused all AI-generated trees to fail publish validation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: correct action node schema and improve AI flow quality - Fix action nodes to use next_node_id (not children) for continuation, matching how TreeNavigationPage.tsx navigates action nodes - Validator now requires next_node_id on all action nodes and flags missing ones as broken dead ends - Update _check_branch_termination: action nodes are not dead ends since they continue via next_node_id (validated separately) - Improve scaffold prompt: branch names must describe observable symptoms users can self-identify, not internal category names - Update branch_detail prompt with clearer action node schema, corrected few-shot example showing proper next_node_id on action nodes - Improve assemble_tree root question to be more user-facing Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * docs: add AI flow builder gotchas to CLAUDE.md (#23-25) - Action nodes use next_node_id (not children) for navigation - Anthropic model IDs require full dated version string - Claude API may wrap JSON in markdown fences Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: resolve CI lint errors and httpx dependency conflict - Fix httpx version conflict: requirements-dev.txt now uses >=0.27.0 to match requirements.txt - Extract CSAT helper functions to csatUtils.ts to fix react-refresh/only-export-components - Remove default export from admin/EmptyState.tsx shim (same rule) - Fix empty catch block in Modal.tsx (no-empty) - Add eslint-disable comments for intentional setState-in-effect patterns in FlowAnalyticsPanel, QuickLaunch, NodeEditorPanel, useCachedQuota, MyAnalyticsPage, TeamAnalyticsPage - Add eslint-disable comments for intentional _children destructure in NodeEditorPanel - Fix _parentId unused var in useTreeLayout.ts - Rewrite usePaginationParams.ts to avoid reading refs during render Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: update tests to match action node schema (next_node_id, not children) - Update _make_valid_tree() in test_ai_tree_validator to use next_node_id on action nodes (solution is a sibling, not a child) - Fix test_dead_end_action_node → test_dead_end_decision_node (action nodes don't have child-based dead ends; dead ends are decision nodes with no children) - Add test_action_missing_next_node_id for the new validation rule - Update BRANCH_DETAIL_JSON in test_ai_endpoints to use next_node_id pattern - Update test_draft_trees.py to use "title" field for action/solution nodes (tree_validation.py was updated this branch to require "title" not "action"/"solution") Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: update remaining tests and session_to_tree for title field rename - test_tree_validation.py: replace "action"/"solution" content fields with "title" - test_procedural_flows.py: update solution node fixtures to use "title" - test_save_session_as_tree.py: update fixtures and assertions for "title" field - session_to_tree.py: generate "title" instead of "action"/"solution" on converted nodes; fall back to legacy field names when reading from old tree snapshots for compatibility Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
711 lines
24 KiB
Python
711 lines
24 KiB
Python
"""Tests for procedural flows feature (tree_type='procedural')."""
|
|
import pytest
|
|
from app.core.tree_validation import (
|
|
validate_procedural_structure,
|
|
can_publish_tree,
|
|
)
|
|
from app.schemas.tree import IntakeFormField, TreeCreate
|
|
|
|
|
|
# --- Helper Data ---
|
|
|
|
def make_valid_procedural_steps():
|
|
"""Return a valid procedural step array."""
|
|
return {
|
|
"steps": [
|
|
{
|
|
"id": "step-1",
|
|
"type": "procedure_step",
|
|
"title": "Configure Static IP",
|
|
"description": "Set the IP to [VAR:ip_address]",
|
|
"content_type": "action",
|
|
},
|
|
{
|
|
"id": "step-2",
|
|
"type": "procedure_step",
|
|
"title": "Install AD DS Role",
|
|
"description": "Add the AD DS server role",
|
|
"content_type": "action",
|
|
},
|
|
{
|
|
"id": "step-end",
|
|
"type": "procedure_end",
|
|
"title": "Procedure Complete",
|
|
},
|
|
]
|
|
}
|
|
|
|
|
|
def make_valid_intake_form():
|
|
"""Return valid intake form field dicts."""
|
|
return [
|
|
{
|
|
"variable_name": "server_name",
|
|
"label": "Server Name",
|
|
"field_type": "text",
|
|
"required": True,
|
|
"display_order": 1,
|
|
},
|
|
{
|
|
"variable_name": "ip_address",
|
|
"label": "IP Address",
|
|
"field_type": "ip_address",
|
|
"required": True,
|
|
"display_order": 2,
|
|
},
|
|
{
|
|
"variable_name": "notes",
|
|
"label": "Additional Notes",
|
|
"field_type": "textarea",
|
|
"required": False,
|
|
"display_order": 3,
|
|
},
|
|
]
|
|
|
|
|
|
# --- Procedural Validation Unit Tests ---
|
|
|
|
class TestValidateProceduralStructure:
|
|
"""Unit tests for validate_procedural_structure()."""
|
|
|
|
def test_valid_procedural_tree(self):
|
|
is_valid, errors = validate_procedural_structure(make_valid_procedural_steps())
|
|
assert is_valid
|
|
assert errors == []
|
|
|
|
def test_empty_structure(self):
|
|
is_valid, errors = validate_procedural_structure({})
|
|
assert not is_valid
|
|
assert any("empty" in e["message"].lower() for e in errors)
|
|
|
|
def test_none_structure(self):
|
|
is_valid, errors = validate_procedural_structure(None)
|
|
assert not is_valid
|
|
|
|
def test_missing_steps_array(self):
|
|
is_valid, errors = validate_procedural_structure({"name": "test"})
|
|
assert not is_valid
|
|
assert any("steps" in e["message"] for e in errors)
|
|
|
|
def test_empty_steps_array(self):
|
|
is_valid, errors = validate_procedural_structure({"steps": []})
|
|
assert not is_valid
|
|
|
|
def test_step_missing_id(self):
|
|
structure = {
|
|
"steps": [
|
|
{"type": "procedure_step", "title": "Step 1"},
|
|
{"id": "end", "type": "procedure_end", "title": "Done"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert not is_valid
|
|
assert any("id" in e["field"] for e in errors)
|
|
|
|
def test_step_missing_type(self):
|
|
structure = {
|
|
"steps": [
|
|
{"id": "s1", "title": "Step 1"},
|
|
{"id": "end", "type": "procedure_end", "title": "Done"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert not is_valid
|
|
assert any("type" in e["field"] for e in errors)
|
|
|
|
def test_step_missing_title(self):
|
|
structure = {
|
|
"steps": [
|
|
{"id": "s1", "type": "procedure_step"},
|
|
{"id": "end", "type": "procedure_end", "title": "Done"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert not is_valid
|
|
assert any("title" in e["field"] for e in errors)
|
|
|
|
def test_invalid_step_type(self):
|
|
structure = {
|
|
"steps": [
|
|
{"id": "s1", "type": "decision", "title": "Bad Type"},
|
|
{"id": "end", "type": "procedure_end", "title": "Done"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert not is_valid
|
|
assert any("Invalid step type" in e["message"] for e in errors)
|
|
|
|
def test_no_end_step(self):
|
|
structure = {
|
|
"steps": [
|
|
{"id": "s1", "type": "procedure_step", "title": "Step 1"},
|
|
{"id": "s2", "type": "procedure_step", "title": "Step 2"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert not is_valid
|
|
assert any("procedure_end" in e["message"] for e in errors)
|
|
|
|
def test_multiple_end_steps(self):
|
|
structure = {
|
|
"steps": [
|
|
{"id": "s1", "type": "procedure_step", "title": "Step 1"},
|
|
{"id": "end1", "type": "procedure_end", "title": "End 1"},
|
|
{"id": "end2", "type": "procedure_end", "title": "End 2"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert not is_valid
|
|
|
|
def test_end_step_not_last(self):
|
|
structure = {
|
|
"steps": [
|
|
{"id": "end", "type": "procedure_end", "title": "End"},
|
|
{"id": "s1", "type": "procedure_step", "title": "Step 1"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert not is_valid
|
|
assert any("last step" in e["message"] for e in errors)
|
|
|
|
def test_duplicate_step_ids(self):
|
|
structure = {
|
|
"steps": [
|
|
{"id": "s1", "type": "procedure_step", "title": "Step 1"},
|
|
{"id": "s1", "type": "procedure_step", "title": "Step 2"},
|
|
{"id": "end", "type": "procedure_end", "title": "Done"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert not is_valid
|
|
assert any("Duplicate" in e["message"] for e in errors)
|
|
|
|
def test_invalid_content_type(self):
|
|
structure = {
|
|
"steps": [
|
|
{"id": "s1", "type": "procedure_step", "title": "Step 1", "content_type": "bad_type"},
|
|
{"id": "end", "type": "procedure_end", "title": "Done"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert not is_valid
|
|
assert any("content_type" in e["field"] for e in errors)
|
|
|
|
def test_valid_content_types(self):
|
|
structure = {
|
|
"steps": [
|
|
{"id": "s1", "type": "procedure_step", "title": "Step 1", "content_type": "action"},
|
|
{"id": "s2", "type": "procedure_step", "title": "Step 2", "content_type": "informational"},
|
|
{"id": "s3", "type": "procedure_step", "title": "Step 3", "content_type": "verification"},
|
|
{"id": "s4", "type": "procedure_step", "title": "Step 4", "content_type": "warning"},
|
|
{"id": "end", "type": "procedure_end", "title": "Done"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert is_valid
|
|
|
|
def test_single_step_with_end(self):
|
|
"""Minimal valid procedural tree: one step + end."""
|
|
structure = {
|
|
"steps": [
|
|
{"id": "s1", "type": "procedure_step", "title": "Only Step"},
|
|
{"id": "end", "type": "procedure_end", "title": "Done"},
|
|
]
|
|
}
|
|
is_valid, errors = validate_procedural_structure(structure)
|
|
assert is_valid
|
|
|
|
|
|
# --- can_publish_tree dispatch ---
|
|
|
|
class TestCanPublishTreeDispatch:
|
|
"""Test can_publish_tree dispatches correctly by tree_type."""
|
|
|
|
def test_troubleshooting_uses_tree_validation(self):
|
|
"""Default tree_type uses troubleshooting validation."""
|
|
structure = {
|
|
"id": "root",
|
|
"type": "decision",
|
|
"question": "Test?",
|
|
"children": [
|
|
{"id": "y", "type": "solution", "title": "Yes"},
|
|
{"id": "n", "type": "solution", "title": "No"},
|
|
]
|
|
}
|
|
can, errors = can_publish_tree(structure, "My Tree", tree_type="troubleshooting")
|
|
assert can
|
|
|
|
def test_procedural_uses_procedural_validation(self):
|
|
can, errors = can_publish_tree(
|
|
make_valid_procedural_steps(),
|
|
"DC Build Procedure",
|
|
tree_type="procedural",
|
|
)
|
|
assert can
|
|
|
|
def test_procedural_rejects_troubleshooting_structure(self):
|
|
"""A troubleshooting structure should fail procedural validation."""
|
|
ts_structure = {
|
|
"id": "root",
|
|
"type": "decision",
|
|
"question": "Test?",
|
|
}
|
|
can, errors = can_publish_tree(ts_structure, "My Tree", tree_type="procedural")
|
|
assert not can
|
|
|
|
def test_procedural_validates_intake_form(self):
|
|
can, errors = can_publish_tree(
|
|
make_valid_procedural_steps(),
|
|
"DC Build",
|
|
tree_type="procedural",
|
|
intake_form=make_valid_intake_form(),
|
|
)
|
|
assert can
|
|
|
|
def test_procedural_rejects_duplicate_variable_names(self):
|
|
intake = [
|
|
{"variable_name": "name", "label": "Name", "field_type": "text", "required": True, "display_order": 1},
|
|
{"variable_name": "name", "label": "Name 2", "field_type": "text", "required": False, "display_order": 2},
|
|
]
|
|
can, errors = can_publish_tree(
|
|
make_valid_procedural_steps(),
|
|
"DC Build",
|
|
tree_type="procedural",
|
|
intake_form=intake,
|
|
)
|
|
assert not can
|
|
assert any("Duplicate" in e["message"] for e in errors)
|
|
|
|
def test_procedural_rejects_select_without_options(self):
|
|
intake = [
|
|
{"variable_name": "role", "label": "Server Role", "field_type": "select", "required": True, "display_order": 1},
|
|
]
|
|
can, errors = can_publish_tree(
|
|
make_valid_procedural_steps(),
|
|
"DC Build",
|
|
tree_type="procedural",
|
|
intake_form=intake,
|
|
)
|
|
assert not can
|
|
assert any("option" in e["message"].lower() for e in errors)
|
|
|
|
def test_empty_name_blocks_publish(self):
|
|
can, errors = can_publish_tree(
|
|
make_valid_procedural_steps(),
|
|
"",
|
|
tree_type="procedural",
|
|
)
|
|
assert not can
|
|
assert any("name" in e["field"] for e in errors)
|
|
|
|
|
|
# --- IntakeFormField Pydantic Schema Tests ---
|
|
|
|
class TestIntakeFormFieldSchema:
|
|
"""Test IntakeFormField Pydantic validation."""
|
|
|
|
def test_valid_text_field(self):
|
|
field = IntakeFormField(
|
|
variable_name="server_name",
|
|
label="Server Name",
|
|
field_type="text",
|
|
required=True,
|
|
display_order=1,
|
|
)
|
|
assert field.variable_name == "server_name"
|
|
|
|
def test_invalid_variable_name_uppercase(self):
|
|
with pytest.raises(Exception):
|
|
IntakeFormField(
|
|
variable_name="ServerName",
|
|
label="Server Name",
|
|
field_type="text",
|
|
required=True,
|
|
display_order=1,
|
|
)
|
|
|
|
def test_invalid_variable_name_starts_with_number(self):
|
|
with pytest.raises(Exception):
|
|
IntakeFormField(
|
|
variable_name="1server",
|
|
label="Server Name",
|
|
field_type="text",
|
|
required=True,
|
|
display_order=1,
|
|
)
|
|
|
|
def test_valid_variable_name_with_underscores(self):
|
|
field = IntakeFormField(
|
|
variable_name="ip_address_v4",
|
|
label="IPv4 Address",
|
|
field_type="ip_address",
|
|
required=True,
|
|
display_order=1,
|
|
)
|
|
assert field.variable_name == "ip_address_v4"
|
|
|
|
def test_select_requires_options(self):
|
|
with pytest.raises(Exception):
|
|
IntakeFormField(
|
|
variable_name="role",
|
|
label="Role",
|
|
field_type="select",
|
|
required=True,
|
|
display_order=1,
|
|
)
|
|
|
|
def test_select_with_options_valid(self):
|
|
field = IntakeFormField(
|
|
variable_name="role",
|
|
label="Role",
|
|
field_type="select",
|
|
required=True,
|
|
options=["AD DS", "DNS", "DHCP"],
|
|
display_order=1,
|
|
)
|
|
assert field.options == ["AD DS", "DNS", "DHCP"]
|
|
|
|
def test_multi_select_requires_options(self):
|
|
with pytest.raises(Exception):
|
|
IntakeFormField(
|
|
variable_name="roles",
|
|
label="Roles",
|
|
field_type="multi_select",
|
|
required=True,
|
|
display_order=1,
|
|
)
|
|
|
|
def test_checkbox_field(self):
|
|
field = IntakeFormField(
|
|
variable_name="confirm_backup",
|
|
label="Backup confirmed?",
|
|
field_type="checkbox",
|
|
required=False,
|
|
display_order=1,
|
|
)
|
|
assert field.field_type == "checkbox"
|
|
|
|
|
|
# --- TreeCreate Schema with Procedural Fields ---
|
|
|
|
class TestTreeCreateProceduralSchema:
|
|
"""Test TreeCreate schema with tree_type and intake_form."""
|
|
|
|
def test_defaults_to_troubleshooting(self):
|
|
tree = TreeCreate(
|
|
name="Test",
|
|
tree_structure={"id": "root", "type": "decision", "question": "Test?"},
|
|
)
|
|
assert tree.tree_type == "troubleshooting"
|
|
assert tree.intake_form is None
|
|
|
|
def test_procedural_with_intake_form(self):
|
|
tree = TreeCreate(
|
|
name="DC Build",
|
|
tree_type="procedural",
|
|
tree_structure=make_valid_procedural_steps(),
|
|
intake_form=[
|
|
IntakeFormField(
|
|
variable_name="server_name",
|
|
label="Server Name",
|
|
field_type="text",
|
|
required=True,
|
|
display_order=1,
|
|
),
|
|
],
|
|
)
|
|
assert tree.tree_type == "procedural"
|
|
assert len(tree.intake_form) == 1
|
|
|
|
def test_duplicate_variable_names_rejected(self):
|
|
with pytest.raises(Exception):
|
|
TreeCreate(
|
|
name="DC Build",
|
|
tree_type="procedural",
|
|
tree_structure=make_valid_procedural_steps(),
|
|
intake_form=[
|
|
IntakeFormField(
|
|
variable_name="name",
|
|
label="Name",
|
|
field_type="text",
|
|
required=True,
|
|
display_order=1,
|
|
),
|
|
IntakeFormField(
|
|
variable_name="name",
|
|
label="Name Again",
|
|
field_type="text",
|
|
required=False,
|
|
display_order=2,
|
|
),
|
|
],
|
|
)
|
|
|
|
|
|
# --- API Integration Tests ---
|
|
|
|
@pytest.mark.asyncio
|
|
class TestProceduralFlowsAPI:
|
|
"""Integration tests for procedural flow CRUD via API."""
|
|
|
|
async def test_create_procedural_draft(self, client, auth_headers):
|
|
"""Create a procedural flow as draft."""
|
|
tree_data = {
|
|
"name": "DC Build Procedure",
|
|
"description": "Domain Controller setup procedure",
|
|
"tree_type": "procedural",
|
|
"status": "draft",
|
|
"tree_structure": make_valid_procedural_steps(),
|
|
"intake_form": make_valid_intake_form(),
|
|
}
|
|
response = await client.post(
|
|
"/api/v1/trees",
|
|
json=tree_data,
|
|
headers=auth_headers,
|
|
)
|
|
assert response.status_code == 200 or response.status_code == 201
|
|
data = response.json()
|
|
assert data["tree_type"] == "procedural"
|
|
assert data["intake_form"] is not None
|
|
assert len(data["intake_form"]) == 3
|
|
|
|
async def test_create_procedural_published(self, client, auth_headers):
|
|
"""Create a procedural flow and publish it (validation runs)."""
|
|
tree_data = {
|
|
"name": "M365 User Onboarding",
|
|
"tree_type": "procedural",
|
|
"status": "published",
|
|
"tree_structure": make_valid_procedural_steps(),
|
|
}
|
|
response = await client.post(
|
|
"/api/v1/trees",
|
|
json=tree_data,
|
|
headers=auth_headers,
|
|
)
|
|
assert response.status_code == 200 or response.status_code == 201
|
|
|
|
async def test_create_procedural_published_invalid_fails(self, client, auth_headers):
|
|
"""Publish should fail if procedural structure is invalid."""
|
|
tree_data = {
|
|
"name": "Bad Procedure",
|
|
"tree_type": "procedural",
|
|
"status": "published",
|
|
"tree_structure": {
|
|
"steps": [
|
|
{"id": "s1", "type": "procedure_step", "title": "Step 1"},
|
|
# No end step
|
|
]
|
|
},
|
|
}
|
|
response = await client.post(
|
|
"/api/v1/trees",
|
|
json=tree_data,
|
|
headers=auth_headers,
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
async def test_list_trees_filter_by_type(self, client, auth_headers):
|
|
"""Create both types and filter by tree_type."""
|
|
# Create troubleshooting
|
|
await client.post(
|
|
"/api/v1/trees",
|
|
json={
|
|
"name": "Troubleshooting Tree",
|
|
"status": "draft",
|
|
"tree_structure": {"id": "root", "type": "decision", "question": "Test?"},
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
# Create procedural
|
|
await client.post(
|
|
"/api/v1/trees",
|
|
json={
|
|
"name": "Procedure Flow",
|
|
"tree_type": "procedural",
|
|
"status": "draft",
|
|
"tree_structure": make_valid_procedural_steps(),
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
|
|
# Filter by procedural
|
|
response = await client.get(
|
|
"/api/v1/trees?tree_type=procedural",
|
|
headers=auth_headers,
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert all(t["tree_type"] == "procedural" for t in data)
|
|
|
|
# Filter by troubleshooting
|
|
response = await client.get(
|
|
"/api/v1/trees?tree_type=troubleshooting",
|
|
headers=auth_headers,
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert all(t["tree_type"] == "troubleshooting" for t in data)
|
|
|
|
async def test_update_procedural_tree(self, client, auth_headers):
|
|
"""Update a procedural tree's intake form."""
|
|
# Create
|
|
create_resp = await client.post(
|
|
"/api/v1/trees",
|
|
json={
|
|
"name": "Procedure",
|
|
"tree_type": "procedural",
|
|
"status": "draft",
|
|
"tree_structure": make_valid_procedural_steps(),
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
tree_id = create_resp.json()["id"]
|
|
|
|
# Update with intake form
|
|
update_resp = await client.put(
|
|
f"/api/v1/trees/{tree_id}",
|
|
json={
|
|
"intake_form": make_valid_intake_form(),
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
assert update_resp.status_code == 200
|
|
data = update_resp.json()
|
|
assert data["intake_form"] is not None
|
|
|
|
async def test_start_session_procedural_with_variables(self, client, auth_headers):
|
|
"""Start a procedural session with intake form variables."""
|
|
# Create published procedural tree with intake form
|
|
create_resp = await client.post(
|
|
"/api/v1/trees",
|
|
json={
|
|
"name": "DC Build",
|
|
"tree_type": "procedural",
|
|
"status": "published",
|
|
"tree_structure": make_valid_procedural_steps(),
|
|
"intake_form": make_valid_intake_form(),
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
assert create_resp.status_code in (200, 201)
|
|
tree_id = create_resp.json()["id"]
|
|
|
|
# Start session with variables
|
|
session_resp = await client.post(
|
|
"/api/v1/sessions",
|
|
json={
|
|
"tree_id": tree_id,
|
|
"session_variables": {
|
|
"server_name": "DC01",
|
|
"ip_address": "192.168.1.10",
|
|
},
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
assert session_resp.status_code in (200, 201)
|
|
session_data = session_resp.json()
|
|
assert session_data["session_variables"]["server_name"] == "DC01"
|
|
assert session_data["tree_snapshot"]["tree_type"] == "procedural"
|
|
|
|
async def test_start_session_procedural_missing_required_field(self, client, auth_headers):
|
|
"""Starting a procedural session without required intake fields should fail."""
|
|
# Create published procedural tree with required intake form
|
|
create_resp = await client.post(
|
|
"/api/v1/trees",
|
|
json={
|
|
"name": "DC Build",
|
|
"tree_type": "procedural",
|
|
"status": "published",
|
|
"tree_structure": make_valid_procedural_steps(),
|
|
"intake_form": make_valid_intake_form(),
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
tree_id = create_resp.json()["id"]
|
|
|
|
# Try to start without required fields
|
|
session_resp = await client.post(
|
|
"/api/v1/sessions",
|
|
json={
|
|
"tree_id": tree_id,
|
|
# Missing server_name and ip_address (both required)
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
assert session_resp.status_code == 422
|
|
assert "Missing required" in session_resp.json()["detail"]
|
|
|
|
async def test_start_session_procedural_optional_fields_ok(self, client, auth_headers):
|
|
"""Starting a session with only required fields (optional missing) should work."""
|
|
create_resp = await client.post(
|
|
"/api/v1/trees",
|
|
json={
|
|
"name": "DC Build",
|
|
"tree_type": "procedural",
|
|
"status": "published",
|
|
"tree_structure": make_valid_procedural_steps(),
|
|
"intake_form": make_valid_intake_form(),
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
tree_id = create_resp.json()["id"]
|
|
|
|
# Start with only required fields (notes is optional)
|
|
session_resp = await client.post(
|
|
"/api/v1/sessions",
|
|
json={
|
|
"tree_id": tree_id,
|
|
"session_variables": {
|
|
"server_name": "DC01",
|
|
"ip_address": "192.168.1.10",
|
|
# notes is optional, not provided
|
|
},
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
assert session_resp.status_code in (200, 201)
|
|
|
|
async def test_fork_preserves_tree_type_and_intake_form(self, client, auth_headers):
|
|
"""Forking a procedural tree should preserve tree_type and intake_form."""
|
|
# Create procedural tree
|
|
create_resp = await client.post(
|
|
"/api/v1/trees",
|
|
json={
|
|
"name": "DC Build Original",
|
|
"tree_type": "procedural",
|
|
"status": "published",
|
|
"tree_structure": make_valid_procedural_steps(),
|
|
"intake_form": make_valid_intake_form(),
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
tree_id = create_resp.json()["id"]
|
|
|
|
# Fork it
|
|
fork_resp = await client.post(
|
|
f"/api/v1/trees/{tree_id}/fork",
|
|
json={"fork_reason": "Customized for Client X"},
|
|
headers=auth_headers,
|
|
)
|
|
assert fork_resp.status_code in (200, 201)
|
|
fork_data = fork_resp.json()
|
|
assert fork_data["tree_type"] == "procedural"
|
|
assert fork_data["intake_form"] is not None
|
|
assert len(fork_data["intake_form"]) == 3
|
|
|
|
async def test_existing_trees_default_troubleshooting(self, client, auth_headers):
|
|
"""Trees created without tree_type should default to troubleshooting."""
|
|
response = await client.post(
|
|
"/api/v1/trees",
|
|
json={
|
|
"name": "Legacy Tree",
|
|
"status": "draft",
|
|
"tree_structure": {"id": "root", "type": "decision", "question": "Test?"},
|
|
},
|
|
headers=auth_headers,
|
|
)
|
|
assert response.status_code in (200, 201)
|
|
data = response.json()
|
|
assert data["tree_type"] == "troubleshooting"
|
|
assert data.get("intake_form") is None
|