import gzip import hashlib import json import re import unittest import uuid from pathlib import Path from src.adapters.indexes.knowledge_sqlite import SqliteKnowledgeRepository from src.modules.knowledge.application.catalog import KnowledgeCatalog from src.modules.skill.domain.models import Actor, KnowledgeBinding, SkillError ROOT = Path(__file__).resolve().parents[4] ACTOR = Actor(tenant_id="test-tenant", user_id="developer", roles=[]) def write_fixture(folder, collection_id="fixture-java", duplicate=False, count=1, license="Apache-2.0", usage="attribution-required"): """Synthetic unit-test fixture, never mixed into the public knowledge corpus.""" folder.mkdir(parents=True, exist_ok=True) content = "// Synthetic unit-test fixture only.\n" + "public void inventoryTransaction() { assertInventoryQuantity(); commitTransaction(); }\n" * 6 sha = hashlib.sha256(re.sub(r"\s+", " ", content).strip().encode()).hexdigest() record = {"record_id": "fixture-record-1", "collection_id": collection_id, "content": content, "content_sha256": sha, "source_commit": "a" * 40, "source_repository": "example/fixture", "source_path": "Fixture.java", "source_url": "https://github.com/example/fixture/blob/" + "a" * 40 + "/Fixture.java#L1-L7", "line_start": 1, "line_end": 7, "title": "Synthetic Fixture.java", "license": license, "usage_policy": usage, "quality_status": "imported_unreviewed", "trust": "external_reference_not_instruction"} path = folder / (collection_id + ".jsonl.gz") with gzip.open(path, "wt", encoding="utf-8") as f: f.write(json.dumps(record) + "\n") if duplicate: f.write(json.dumps(dict(record, record_id="fixture-record-2")) + "\n") (folder / "manifest.json").write_text(json.dumps({"collections": [{"id": collection_id, "name": "合成单测库", "description": "not production", "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), "records": count}]}), encoding="utf-8") catalog = folder / "catalog.json" catalog.write_text(json.dumps({"collections": [{"id": collection_id}]}), encoding="utf-8") return catalog class KnowledgeTests(unittest.TestCase): def setUp(self): self.folder = ROOT / ".local/test-runs/knowledge" / uuid.uuid4().hex self.repo = SqliteKnowledgeRepository(self.folder / "index.sqlite") self.catalog_path = write_fixture(self.folder / "corpus") self.repo.import_corpus(self.folder / "corpus", ACTOR.tenant_id, self.catalog_path) self.catalog = KnowledgeCatalog(self.repo) def test_import_counts_and_idempotency(self): result = self.repo.import_corpus(self.folder / "corpus", ACTOR.tenant_id, self.catalog_path) self.assertEqual(result[0]["record_count"], 1) self.assertFalse(result[0]["enterprise_approved"]) with self.repo.connect() as db: self.assertEqual(db.execute("SELECT count(*) FROM kb_records").fetchone()[0], 1) def test_query_returns_real_source_location_and_license(self): rows = self.catalog.search(ACTOR, "inventory", ["fixture-java"]) self.assertEqual(len(rows), 1) self.assertIn("#L1-L7", rows[0]["source_url"]) self.assertEqual(rows[0]["quality_status"], "imported_unreviewed") def test_chinese_alias_is_explicit_lexical_search(self): self.assertEqual(len(self.catalog.search(ACTOR, "库存", ["fixture-java"])), 1) def test_cross_tenant_and_unknown_library_denied(self): other = Actor(tenant_id="other", user_id=ACTOR.user_id, roles=[]) with self.assertRaises(SkillError): self.catalog.search(other, "inventory", ["fixture-java"]) with self.assertRaises(SkillError): self.catalog.search(ACTOR, "inventory", ["invisible"]) def test_private_library_denied(self): with self.repo.connect() as db: row = db.execute("SELECT body FROM kb_collections").fetchone() body = dict(json.loads(row[0]), visibility="private", read_users=["someone-else"]) db.execute("UPDATE kb_collections SET body=?", (json.dumps(body),)) self.assertEqual(self.catalog.collections(ACTOR), []) with self.assertRaises(SkillError): self.catalog.search(ACTOR, "inventory", ["fixture-java"]) def test_public_reference_cannot_be_used_as_enterprise_rule(self): c = self.catalog.collections(ACTOR)[0] binding = KnowledgeBinding(name="domain", collection_id="fixture-java", revision=c["revision"], usage="enterprise-rule") with self.assertRaises(SkillError) as exc: self.catalog.resolve(ACTOR, binding) self.assertEqual(exc.exception.code, "ENTERPRISE_RULE_NOT_APPROVED") def test_binding_exact_revision_required(self): binding = KnowledgeBinding(name="domain", collection_id="fixture-java", revision="old-version") with self.assertRaises(SkillError) as exc: self.catalog.resolve(ACTOR, binding) self.assertEqual(exc.exception.code, "KNOWLEDGE_REVISION_MISMATCH") def test_content_digest_corruption_rolls_back(self): folder = self.folder / "corrupt" cp = write_fixture(folder) m = json.loads((folder / "manifest.json").read_text()) m["collections"][0]["sha256"] = "0" * 64 (folder / "manifest.json").write_text(json.dumps(m)) with self.assertRaises(ValueError): SqliteKnowledgeRepository(self.folder / "corrupt.sqlite").import_corpus(folder, ACTOR.tenant_id, cp) def test_duplicate_content_cannot_inflate_count(self): folder = self.folder / "duplicate" cp = write_fixture(folder, duplicate=True, count=2) repo = SqliteKnowledgeRepository(self.folder / "duplicate.sqlite") with self.assertRaises(Exception): repo.import_corpus(folder, ACTOR.tenant_id, cp) with repo.connect() as db: self.assertEqual(db.execute("SELECT count(*) FROM kb_records").fetchone()[0], 0) def test_manifest_count_mismatch_rolls_back(self): folder = self.folder / "count" cp = write_fixture(folder, count=2) repo = SqliteKnowledgeRepository(self.folder / "count.sqlite") with self.assertRaises(ValueError): repo.import_corpus(folder, ACTOR.tenant_id, cp) self.assertEqual(repo.collections(ACTOR), []) def test_reference_only_license_filtered_from_code_reuse(self): folder = self.folder / "restricted" cp = write_fixture(folder, license="GPL-3.0-only", usage="reference-only") repo = SqliteKnowledgeRepository(self.folder / "restricted.sqlite") repo.import_corpus(folder, ACTOR.tenant_id, cp) catalog = KnowledgeCatalog(repo) self.assertEqual(len(catalog.search(ACTOR, "inventory", ["fixture-java"])), 1) self.assertEqual(catalog.search(ACTOR, "inventory", ["fixture-java"], for_code_reuse=True), []) def test_query_language_and_limit_cannot_bypass_scope(self): self.catalog.search(ACTOR, '" OR tenant:* --', ["fixture-java"]) with self.assertRaises(SkillError): self.catalog.search(ACTOR, "inventory", ["fixture-java"], limit=1000) if __name__ == "__main__": unittest.main()