148 lines
5.1 KiB
Python
148 lines
5.1 KiB
Python
"""Tests for the context builder."""
|
|
|
|
from atocore.context.builder import build_context, get_last_context_pack
|
|
from atocore.context.project_state import init_project_state_schema, set_state
|
|
from atocore.ingestion.pipeline import ingest_file
|
|
from atocore.models.database import init_db
|
|
|
|
|
|
def test_build_context_returns_pack(tmp_data_dir, sample_markdown):
|
|
"""Test that context builder returns a valid pack."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
pack = build_context("What is AtoCore?")
|
|
assert pack.total_chars > 0
|
|
assert len(pack.chunks_used) > 0
|
|
assert pack.budget_remaining >= 0
|
|
assert "--- End Context ---" in pack.formatted_context
|
|
|
|
|
|
def test_context_respects_budget(tmp_data_dir, sample_markdown):
|
|
"""Test that context builder respects character budget."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
pack = build_context("What is AtoCore?", budget=500)
|
|
assert pack.total_chars <= 500
|
|
assert len(pack.formatted_context) <= 500
|
|
|
|
|
|
def test_context_with_project_hint(tmp_data_dir, sample_markdown):
|
|
"""Test that project hint boosts relevant chunks."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
pack = build_context("What is the architecture?", project_hint="atocore")
|
|
assert len(pack.chunks_used) > 0
|
|
assert pack.total_chars > 0
|
|
|
|
|
|
def test_last_context_pack_stored(tmp_data_dir, sample_markdown):
|
|
"""Test that last context pack is stored for debug."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
build_context("test prompt")
|
|
last = get_last_context_pack()
|
|
assert last is not None
|
|
assert last.query == "test prompt"
|
|
|
|
|
|
def test_full_prompt_structure(tmp_data_dir, sample_markdown):
|
|
"""Test that the full prompt has correct structure."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
pack = build_context("What are memory types?")
|
|
assert "knowledge base" in pack.full_prompt.lower()
|
|
assert "What are memory types?" in pack.full_prompt
|
|
|
|
|
|
def test_project_state_included_in_context(tmp_data_dir, sample_markdown):
|
|
"""Test that trusted project state is injected into context."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
# Set some project state
|
|
set_state("atocore", "status", "phase", "Phase 0.5 complete")
|
|
set_state("atocore", "decision", "database", "SQLite for structured data")
|
|
|
|
pack = build_context("What is AtoCore?", project_hint="atocore")
|
|
|
|
# Project state should appear in context
|
|
assert "--- Trusted Project State ---" in pack.formatted_context
|
|
assert "Phase 0.5 complete" in pack.formatted_context
|
|
assert "SQLite for structured data" in pack.formatted_context
|
|
assert pack.project_state_chars > 0
|
|
|
|
|
|
def test_trusted_state_precedence_is_restated_in_retrieved_context(tmp_data_dir, sample_markdown):
|
|
"""When trusted state and retrieval coexist, the context should restate precedence explicitly."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
set_state("atocore", "status", "phase", "Phase 2")
|
|
pack = build_context("What is AtoCore?", project_hint="atocore")
|
|
|
|
assert "If retrieved context conflicts with Trusted Project State above" in pack.formatted_context
|
|
|
|
|
|
def test_project_state_takes_priority_budget(tmp_data_dir, sample_markdown):
|
|
"""Test that project state is included even with tight budget."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
set_state("atocore", "status", "phase", "Phase 1 in progress")
|
|
|
|
# Small budget — project state should still be included
|
|
pack = build_context("status?", project_hint="atocore", budget=500)
|
|
assert "Phase 1 in progress" in pack.formatted_context
|
|
|
|
|
|
def test_project_state_respects_total_budget(tmp_data_dir, sample_markdown):
|
|
"""Trusted state should still fit within the total context budget."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
set_state("atocore", "status", "notes", "x" * 400)
|
|
set_state("atocore", "decision", "details", "y" * 400)
|
|
|
|
pack = build_context("status?", project_hint="atocore", budget=120)
|
|
assert pack.total_chars <= 120
|
|
assert pack.budget_remaining >= 0
|
|
assert len(pack.formatted_context) <= 120
|
|
|
|
|
|
def test_project_hint_matches_state_case_insensitively(tmp_data_dir, sample_markdown):
|
|
"""Project state lookup should not depend on exact casing."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
set_state("AtoCore", "status", "phase", "Phase 2")
|
|
pack = build_context("status?", project_hint="atocore")
|
|
assert "Phase 2" in pack.formatted_context
|
|
|
|
|
|
def test_no_project_state_without_hint(tmp_data_dir, sample_markdown):
|
|
"""Test that project state is not included without project hint."""
|
|
init_db()
|
|
init_project_state_schema()
|
|
ingest_file(sample_markdown)
|
|
|
|
set_state("atocore", "status", "phase", "Phase 1")
|
|
|
|
pack = build_context("What is AtoCore?")
|
|
assert pack.project_state_chars == 0
|
|
assert "--- Trusted Project State ---" not in pack.formatted_context
|