Files
amirhosein mazaheri baf58da562 feat(ingestion): add Phase 1 database and application foundation
Add the scoped PostgreSQL/pgvector schema, independent ingestion settings and ports, embedding profile checks, and trusted administrator bootstrap.

Include repository contracts and guidance, organized provider-free and PostgreSQL tests, deployment setup, and byte-preserving migration attributes.

Validation: 163 tests passed; all 48 PostgreSQL cases passed with pgvector Python 0.5.0 and 0.3.6. No production schema or historical metadata was changed.
2026-10-05 16:38:28 +03:30

75 lines
3.4 KiB
Python

"""Independent PostgreSQL connections enforce replay and bootstrap invariants."""
from uuid import uuid4
import pytest
from sqlalchemy import text
from tests.support.database_rows import seed
from concurrent.futures import ThreadPoolExecutor
from threading import Barrier
from argon2 import PasswordHasher
from sqlalchemy.exc import IntegrityError
from app.bootstrap import bootstrap
@pytest.mark.parametrize("kind", ["chunk", "job", "upload", "audit"])
def test_concurrent_uniqueness(migrated_db, kind):
engine, _ = migrated_db
with engine.begin() as connection:
org, actor, doc = seed(connection)
barrier = Barrier(2)
request = uuid4()
def insert_once():
params = {"id": uuid4(), "org": org, "actor": actor, "doc": doc,
"request": request, "key": str(uuid4()), "hash": "a" * 64}
sql = {
"chunk": """INSERT INTO document_chunks
(id,organization_id,document_id,ordinal,text,embedding)
VALUES (:id,:org,:doc,0,'test','[1,2,3]')""",
"job": """INSERT INTO ingestion_jobs(id,organization_id,document_id)
VALUES (:id,:org,:doc)""",
"upload": """INSERT INTO documents(id,organization_id,uploaded_by,title,
original_filename,upload_request_id,upload_payload_hash,storage_key,
file_sha256,mime_type,size_bytes)
VALUES (:id,:org,:actor,'test','test.md',:request,:hash,:key,:hash,'text/markdown',1)""",
"audit": """INSERT INTO audit_events(id,organization_id,actor_user_id,
action,target_id,idempotency_key)
VALUES (:id,:org,:actor,'ingestion_retry',:doc,:request)""",
}[kind]
try:
with engine.begin() as connection:
connection.execute(text("SET LOCAL statement_timeout='10s'"))
barrier.wait(timeout=10)
connection.execute(text(sql), params)
return "committed"
except IntegrityError:
return "rejected"
with ThreadPoolExecutor(max_workers=2) as pool:
results = list(pool.map(lambda _: insert_once(), range(2)))
assert sorted(results) == ["committed", "rejected"]
def test_concurrent_bootstrap_and_hashing(migrated_db):
engine, _ = migrated_db
barrier = Barrier(2)
def run(index):
try:
with engine.begin() as connection:
connection.execute(text("SET LOCAL statement_timeout='10s'"))
barrier.wait(timeout=10)
return bootstrap(
connection, organization_name="test", username=f"ADMIN{index}",
password="test-only-secret", assistant_name="Assistant", instruction="",
)
except ValueError:
return None
with ThreadPoolExecutor(max_workers=2) as pool:
results = list(pool.map(run, range(2)))
assert sum(item is not None for item in results) == 1
with engine.connect() as connection:
user = connection.execute(text("SELECT * FROM users")).mappings().one()
assert user["username"] == user["username"].lower()
assert user["role"] == "admin" and user["is_enabled"]
assert PasswordHasher().verify(user["password_hash"], "test-only-secret")
settings = connection.execute(text("SELECT * FROM organization_settings")).mappings().one()
assert (settings["revision"], settings["knowledge_revision"], settings["access_epoch"]) == (1, 1, 1)