wip(handoff): restore backend suite to green
Some checks failed
Mirror to GitHub / mirror (push) Successful in 12s
CI / backend (pull_request) Failing after 27m35s
CI / frontend (pull_request) Successful in 2m46s
CI / e2e (pull_request) Failing after 4m9s

Co-Authored-By: Codex <noreply@openai.com>
This commit is contained in:
2026-04-25 06:13:23 -04:00
parent 208ec996d5
commit 49f88569da
26 changed files with 165 additions and 77 deletions

View File

@@ -5,6 +5,7 @@ Provides test database setup, client fixtures, and authentication helpers.
"""
import os
import asyncio
from typing import AsyncGenerator
import pytest
import sqlalchemy as sa
@@ -73,6 +74,20 @@ def pytest_collection_modifyitems(config, items):
items[:] = selected
@pytest.hookimpl(trylast=True, hookwrapper=True)
def pytest_runtest_teardown(item, nextitem):
"""Close pytest-asyncio's post-test clean loop before warnings collect it."""
yield
policy = asyncio.get_event_loop_policy()
try:
loop = policy.get_event_loop()
except RuntimeError:
return
if not loop.is_running() and not loop.is_closed():
loop.close()
policy.set_event_loop(None)
@pytest.fixture
async def test_db() -> AsyncGenerator[AsyncSession, None]:
"""
@@ -137,6 +152,7 @@ async def test_db() -> AsyncGenerator[AsyncSession, None]:
# Dispose engine first so all pooled connections are released,
# then reconnect to perform the schema teardown cleanly.
await engine.dispose()
await asyncio.sleep(0.01)
# Drop all tables after test (CASCADE for circular FKs)
teardown_engine = create_async_engine(
@@ -150,6 +166,7 @@ async def test_db() -> AsyncGenerator[AsyncSession, None]:
await conn.execute(sa.text("CREATE SCHEMA public"))
finally:
await teardown_engine.dispose()
await asyncio.sleep(0.01)
@pytest.fixture

View File

@@ -74,19 +74,25 @@ def _mock_ai_provider(text: str, input_tokens: int = 100, output_tokens: int = 2
@pytest.fixture
def enable_ai():
"""Temporarily enable AI by setting a fake API key."""
original = settings.ANTHROPIC_API_KEY
original_anthropic = settings.ANTHROPIC_API_KEY
original_google = settings.GOOGLE_AI_API_KEY
settings.ANTHROPIC_API_KEY = "test-key-fake"
settings.GOOGLE_AI_API_KEY = None
yield
settings.ANTHROPIC_API_KEY = original
settings.ANTHROPIC_API_KEY = original_anthropic
settings.GOOGLE_AI_API_KEY = original_google
@pytest.fixture
def disable_ai():
"""Ensure AI is disabled."""
original = settings.ANTHROPIC_API_KEY
original_anthropic = settings.ANTHROPIC_API_KEY
original_google = settings.GOOGLE_AI_API_KEY
settings.ANTHROPIC_API_KEY = None
settings.GOOGLE_AI_API_KEY = None
yield
settings.ANTHROPIC_API_KEY = original
settings.ANTHROPIC_API_KEY = original_anthropic
settings.GOOGLE_AI_API_KEY = original_google
# ── Quota endpoint ──

View File

@@ -66,6 +66,7 @@ async def test_create_fork(client: AsyncClient, test_user, auth_headers, test_db
step = AISessionStep(
session_id=session.id,
account_id=session.account_id,
step_order=0,
step_type="question",
content={"text": "What's the issue?"},
@@ -119,7 +120,7 @@ async def test_switch_branch(client: AsyncClient, test_user, auth_headers, test_
root = await manager.create_root_branch(session.id)
step = AISessionStep(
session_id=session.id, step_order=0, step_type="question",
session_id=session.id, account_id=session.account_id, step_order=0, step_type="question",
content={"text": "test"}, confidence_at_step=0.5,
)
test_db.add(step)
@@ -197,7 +198,7 @@ async def test_get_branch_tree(client: AsyncClient, test_user, auth_headers, tes
root = await manager.create_root_branch(session.id)
step = AISessionStep(
session_id=session.id, step_order=0, step_type="question",
session_id=session.id, account_id=session.account_id, step_order=0, step_type="question",
content={"text": "test"}, confidence_at_step=0.5,
)
test_db.add(step)

View File

@@ -50,6 +50,7 @@ async def _make_session(test_db, user, *, with_psa: bool = False) -> AISession:
conn = PsaConnection(
account_id=user["user_data"]["account_id"],
provider="connectwise",
display_name="Test ConnectWise",
site_url="https://fake.cw.local",
company_id="TEST",
credentials_encrypted=encrypt_credentials({"public_key": "x", "private_key": "y"}),

View File

@@ -472,19 +472,20 @@ class TestScriptBuilderSlugCollision:
# Pre-create a template with slug "test-script" to cause collision
user_resp = await client.get("/api/v1/auth/me", headers=auth_headers)
user_id = user_resp.json()["id"]
account_id = user_resp.json()["account_id"]
await test_db.execute(
sa.text("""
INSERT INTO script_templates
(id, category_id, created_by, name, slug, script_body,
(id, category_id, created_by, account_id, name, slug, script_body,
parameters_schema, default_values, validation_rules, tags,
complexity, is_active, version, usage_count, created_at, updated_at)
VALUES
(:id, 'a0000000-0000-0000-0000-000000000001'::uuid, :uid,
(:id, 'a0000000-0000-0000-0000-000000000001'::uuid, :uid, :account_id,
'Test Script', 'test-script', 'echo hello',
'{"parameters": []}', '{}', '{}', '["powershell"]',
'beginner', true, 1, 0, NOW(), NOW())
"""),
{"id": str(uuid_mod.uuid4()), "uid": user_id},
{"id": str(uuid_mod.uuid4()), "uid": user_id, "account_id": account_id},
)
await test_db.commit()
@@ -561,6 +562,7 @@ class TestScriptTemplateFilters:
"""mine=true returns only templates created by the current user."""
user_resp = await client.get("/api/v1/auth/me", headers=auth_headers)
user_id = user_resp.json()["id"]
account_id = user_resp.json()["account_id"]
second_resp = await client.get("/api/v1/auth/me", headers=second_user_headers)
second_user_id = second_resp.json()["id"]
@@ -571,32 +573,32 @@ class TestScriptTemplateFilters:
await test_db.execute(
sa.text("""
INSERT INTO script_templates
(id, category_id, created_by, team_id, name, slug, script_body,
(id, category_id, created_by, account_id, team_id, name, slug, script_body,
parameters_schema, default_values, validation_rules, tags,
complexity, is_active, version, usage_count, created_at, updated_at)
VALUES
(:id, :cat, :uid, NULL,
(:id, :cat, :uid, :account_id, NULL,
'My Script', 'my-script', 'echo mine',
'{"parameters": []}', '{}', '{}', '[]',
'beginner', true, 1, 0, NOW(), NOW())
"""),
{"id": str(uuid_mod.uuid4()), "cat": cat_id, "uid": user_id},
{"id": str(uuid_mod.uuid4()), "cat": cat_id, "uid": user_id, "account_id": account_id},
)
# Create template owned by second user (no team_id, so visible to all)
await test_db.execute(
sa.text("""
INSERT INTO script_templates
(id, category_id, created_by, team_id, name, slug, script_body,
(id, category_id, created_by, account_id, team_id, name, slug, script_body,
parameters_schema, default_values, validation_rules, tags,
complexity, is_active, version, usage_count, created_at, updated_at)
VALUES
(:id, :cat, :uid, NULL,
(:id, :cat, :uid, :account_id, NULL,
'Other Script', 'other-script', 'echo other',
'{"parameters": []}', '{}', '{}', '[]',
'beginner', true, 1, 0, NOW(), NOW())
"""),
{"id": str(uuid_mod.uuid4()), "cat": cat_id, "uid": second_user_id},
{"id": str(uuid_mod.uuid4()), "cat": cat_id, "uid": second_user_id, "account_id": account_id},
)
await test_db.commit()
@@ -617,6 +619,7 @@ class TestScriptTemplateFilters:
"""shared=true returns only templates shared with the user's team."""
user_resp = await client.get("/api/v1/auth/me", headers=auth_headers)
user_id = user_resp.json()["id"]
account_id = user_resp.json()["account_id"]
cat_id = "b0000000-0000-0000-0000-000000000001"
@@ -639,32 +642,32 @@ class TestScriptTemplateFilters:
await test_db.execute(
sa.text("""
INSERT INTO script_templates
(id, category_id, created_by, team_id, name, slug, script_body,
(id, category_id, created_by, account_id, team_id, name, slug, script_body,
parameters_schema, default_values, validation_rules, tags,
complexity, is_active, version, usage_count, created_at, updated_at)
VALUES
(:id, :cat, :uid, :tid,
(:id, :cat, :uid, :account_id, :tid,
'Team Script', 'team-script', 'echo team',
'{"parameters": []}', '{}', '{}', '[]',
'beginner', true, 1, 0, NOW(), NOW())
"""),
{"id": str(uuid_mod.uuid4()), "cat": cat_id, "uid": user_id, "tid": team_id},
{"id": str(uuid_mod.uuid4()), "cat": cat_id, "uid": user_id, "account_id": account_id, "tid": team_id},
)
# Template NOT shared (no team_id)
await test_db.execute(
sa.text("""
INSERT INTO script_templates
(id, category_id, created_by, team_id, name, slug, script_body,
(id, category_id, created_by, account_id, team_id, name, slug, script_body,
parameters_schema, default_values, validation_rules, tags,
complexity, is_active, version, usage_count, created_at, updated_at)
VALUES
(:id, :cat, :uid, NULL,
(:id, :cat, :uid, :account_id, NULL,
'Personal Script', 'personal-script', 'echo personal',
'{"parameters": []}', '{}', '{}', '[]',
'beginner', true, 1, 0, NOW(), NOW())
"""),
{"id": str(uuid_mod.uuid4()), "cat": cat_id, "uid": user_id},
{"id": str(uuid_mod.uuid4()), "cat": cat_id, "uid": user_id, "account_id": account_id},
)
await test_db.commit()

View File

@@ -49,7 +49,7 @@ async def test_create_fork(client: AsyncClient, test_user, auth_headers, test_db
await test_db.flush()
step = AISessionStep(
session_id=session.id, step_order=0, step_type="question",
session_id=session.id, account_id=session.account_id, step_order=0, step_type="question",
content={"text": "test"}, confidence_at_step=0.5,
)
test_db.add(step)
@@ -88,7 +88,7 @@ async def test_switch_branch(client: AsyncClient, test_user, auth_headers, test_
await test_db.flush()
step = AISessionStep(
session_id=session.id, step_order=0, step_type="question",
session_id=session.id, account_id=session.account_id, step_order=0, step_type="question",
content={"text": "test"}, confidence_at_step=0.5,
)
test_db.add(step)

View File

@@ -45,6 +45,7 @@ async def test_edit_output_api(client: AsyncClient, test_user, auth_headers, tes
output = SessionResolutionOutput(
session_id=session.id,
account_id=session.account_id,
output_type="psa_ticket_notes",
generated_content="Original",
status="draft",

View File

@@ -219,7 +219,7 @@ class TestSessionSharing:
json={"visibility": "public"},
headers=other_headers
)
assert response.status_code == 403
assert response.status_code == 404
async def test_share_nonexistent_session(self, client: AsyncClient, auth_headers):
"""Creating a share for nonexistent session returns 404."""

View File

@@ -213,6 +213,7 @@ async def test_record_decision_persists_and_bumps_state_version(
title="x",
description="y",
confidence_pct=50,
ai_drafted_script="Write-Output 'ok'",
)
test_db.add(fix)
await test_db.commit()

View File

@@ -43,7 +43,7 @@ async def _create_account_and_user(db: AsyncSession, prefix: str):
async def _login(client: AsyncClient, email: str, password: str) -> dict:
"""Log in and return Authorization headers."""
resp = await client.post(
"/api/v1/auth/login",
"/api/v1/auth/login/json",
json={"email": email, "password": password},
)
assert resp.status_code == 200, f"Login failed: {resp.text}"
@@ -101,11 +101,11 @@ async def test_category_tree_count_scoped_to_account(
acct_a, user_a, pass_a = await _create_account_and_user(test_db, "cat-a")
acct_b, user_b, pass_b = await _create_account_and_user(test_db, "cat-b")
# Shared category (account_id=None means global)
# Categories are tenant-scoped; the endpoint must only count account A's trees.
category = TreeCategory(
name="Shared Category",
slug=f"shared-cat-{uuid.uuid4().hex[:6]}",
account_id=None,
account_id=acct_a.id,
is_active=True,
)
test_db.add(category)
@@ -270,6 +270,7 @@ async def test_get_session_returns_404_not_403_for_other_user(
session_b = Session(
tree_id=tree_b.id,
user_id=user_b.id,
account_id=acct_b.id,
tree_snapshot={"id": "root", "type": "start", "children": []},
path_taken=[],
decisions=[],
@@ -384,6 +385,7 @@ async def test_share_revoke_returns_404_not_403_for_other_user(
session_b = Session(
tree_id=tree_b.id,
user_id=user_b.id,
account_id=acct_b.id,
tree_snapshot={"id": "root", "type": "start", "children": []},
path_taken=[],
decisions=[],
@@ -534,6 +536,7 @@ async def test_maintenance_schedule_returns_404_for_other_team(
# Create a schedule for that tree
schedule_b = MaintenanceSchedule(
tree_id=tree_b.id,
account_id=acct_b.id,
created_by=user_b.id,
cron_expression="0 2 * * 0",
timezone="UTC",

View File

@@ -4,6 +4,7 @@ from datetime import datetime, timezone, timedelta
from httpx import AsyncClient
from uuid import uuid4
from app.models.account import Account
from app.models.tree import Tree
from app.models.tree_share import TreeShare
from app.models.user import User
@@ -287,13 +288,17 @@ class TestTreeSharing:
@pytest.mark.asyncio
async def test_migration_defaults_visibility_to_team(test_db):
"""Test that existing trees default to 'team' visibility after migration."""
account = Account(name="Migration Default Test", display_code=uuid4().hex[:8])
test_db.add(account)
await test_db.flush()
# Create a tree without specifying visibility
tree = Tree(
name="Old Tree",
description="Created before migration",
tree_structure={"id": "root", "type": "decision", "question": "Test?", "children": []},
author_id=None,
account_id=None
account_id=account.id
)
test_db.add(tree)
await test_db.commit()

View File

@@ -359,7 +359,7 @@ async def test_delete_upload_forbidden_for_non_owner(client, auth_headers, test_
f"/api/v1/uploads/{upload.id}", headers=other_headers
)
assert response.status_code == 403
assert response.status_code == 404
# ---------------------------------------------------------------------------