Aug 7 - Update: knowledge base MT19

This commit is contained in:
2026-08-07 17:32:43 -04:00
parent 38ab66d021
commit 3c35835505
6 changed files with 276 additions and 2 deletions
+161
View File
@@ -0,0 +1,161 @@
"""
tests/test_knowledge_sort_order.py
----------------------------------
Behaviour tests for MT-19 — admin-controlled ordering of support knowledge.
Knowledge entries are injected into the support chat's Groq system prompt and
the combined text is capped, so ORDER decides which guidance the assistant
reaches for first and which gets truncated away. These tests pin that the
column actually drives the prompt, not just the admin table.
"""
import pytest
@pytest.fixture
def client(app):
with app.app_context():
from app import db
from app.models import inspector_assignment # noqa: F401
db.drop_all()
db.create_all()
yield app.test_client()
db.session.remove()
def _user(username, role):
from app import db
from app.models.user import User
u = User(username=username, full_name=username.title(), role=role,
email=f'{username}@example.com', active=True)
u.set_password('pw-correct1')
db.session.add(u)
db.session.commit()
return u
def _login(client, user):
resp = client.post('/auth/login',
data={'username': user.username, 'password': 'pw-correct1'},
follow_redirects=True)
assert 'Login - ' not in resp.get_data(as_text=True), 'login failed'
return resp
def _kb(title, body, sort_order=0, active=True):
from app import db
from app.models.support import SupportKnowledge
from app.utils.time_utils import now_eastern
e = SupportKnowledge(title=title, body=body, active=active,
sort_order=sort_order,
created_at=now_eastern(), updated_at=now_eastern())
db.session.add(e)
db.session.commit()
return e
def test_default_sort_order_is_zero(client):
from app import db
from app.models.support import SupportKnowledge
from app.utils.time_utils import now_eastern
e = SupportKnowledge(title='T', body='B', active=True,
created_at=now_eastern(), updated_at=now_eastern())
db.session.add(e)
db.session.commit()
assert e.sort_order == 0
def test_sort_order_drives_the_chat_prompt(client):
"""The behaviour this phase exists for: a low sort_order promotes an entry
in the text handed to the assistant, regardless of creation order."""
from app.routes.support import _system_prompt_with_kb
_kb('Created First', 'AAA-FIRST', sort_order=10)
_kb('Created Second', 'ZZZ-SECOND', sort_order=1)
prompt = _system_prompt_with_kb()
assert prompt.index('ZZZ-SECOND') < prompt.index('AAA-FIRST'), (
'sort_order did not reorder the prompt')
def test_ties_break_on_id_so_order_is_stable(client):
from app.routes.support import _system_prompt_with_kb
_kb('Alpha', 'AAA-ALPHA', sort_order=5)
_kb('Beta', 'BBB-BETA', sort_order=5)
prompt = _system_prompt_with_kb()
assert prompt.index('AAA-ALPHA') < prompt.index('BBB-BETA')
def test_inactive_entries_stay_out_of_the_prompt(client):
"""Ordering must not have widened what reaches the assistant."""
from app.routes.support import _system_prompt_with_kb
_kb('Live', 'AAA-LIVE', sort_order=9)
_kb('Disabled', 'ZZZ-DISABLED', sort_order=0, active=False)
prompt = _system_prompt_with_kb()
assert 'AAA-LIVE' in prompt
assert 'ZZZ-DISABLED' not in prompt
def test_admin_can_set_sort_order_on_add(client):
from app.models.support import SupportKnowledge
admin = _user('ada', 'admin')
_login(client, admin)
client.post('/support/admin/knowledge/add',
data={'title': 'Priority', 'body': 'Body text', 'sort_order': '3'},
follow_redirects=True)
entry = SupportKnowledge.query.filter_by(title='Priority').first()
assert entry is not None
assert entry.sort_order == 3
def test_blank_sort_order_on_edit_keeps_the_existing_value(client):
"""The column is NOT NULL and blank must mean 'leave it alone', not
'silently promote this entry to the top'."""
from app.models.support import SupportKnowledge
admin = _user('ada', 'admin')
entry = _kb('Keep', 'Body', sort_order=7)
_login(client, admin)
client.post(f'/support/admin/knowledge/{entry.id}/edit',
data={'title': 'Keep', 'body': 'Body', 'sort_order': ''},
follow_redirects=True)
from app import db
assert db.session.get(SupportKnowledge, entry.id).sort_order == 7
@pytest.mark.parametrize('raw,expected', [
('abc', 0), # non-numeric
('-5', 0), # clamped low
('99999', 9999), # clamped high
])
def test_bad_sort_order_never_reaches_a_not_null_column(client, raw, expected):
from app.models.support import SupportKnowledge
admin = _user('ada', 'admin')
_login(client, admin)
client.post('/support/admin/knowledge/add',
data={'title': f'E{raw}', 'body': 'B', 'sort_order': raw},
follow_redirects=True)
entry = SupportKnowledge.query.filter_by(title=f'E{raw}').first()
assert entry is not None, 'entry was rejected instead of coerced'
assert entry.sort_order == expected
def test_admin_list_orders_the_same_way_as_the_prompt(client):
"""The admin must see the real priority, not a different order."""
admin = _user('ada', 'admin')
_kb('Zebra Entry', 'B', sort_order=1)
_kb('Apple Entry', 'B', sort_order=9)
_login(client, admin)
body = client.get('/support/admin/knowledge').get_data(as_text=True)
assert body.index('Zebra Entry') < body.index('Apple Entry')