modified: .gitlab-ci.yml modified: bench/qa_questions.txt modified: bench/qa_sweep.py modified: bench/run.sh modified: docs/TICKETS.md modified: docs/_source/README.md modified: docs/_source/_ext/makefile_targets.py modified: docs/_source/api/cli.rst modified: docs/_source/api/distill.rst modified: docs/_source/api/mesh.rst modified: docs/_source/api/qa.rst modified: docs/_source/api/retrieval.rst modified: docs/_source/api/storage.rst modified: docs/_source/api/substrate.rst modified: docs/_source/concepts.rst modified: docs/_source/conf.py modified: docs/_source/cookbook.rst modified: docs/_source/index.rst modified: docs/_source/license.rst modified: docs/_source/quickstart.rst modified: docs/bench-maxing.md modified: docs/benchmarks.md modified: docs/cti-architecture.md modified: docs/diagrams/aborist-modules.dot modified: docs/diagrams/aborist-modules.svg modified: docs/diagrams/mesh-data-flow.dot modified: docs/diagrams/mesh-epoch-lifecycle.dot modified: docs/diagrams/mesh-epoch-lifecycle.svg modified: docs/diagrams/mesh-group-decisions.dot modified: docs/diagrams/mesh-group-decisions.svg modified: docs/diagrams/mesh-identity-stack.dot modified: docs/diagrams/mesh-secret-envelope.dot modified: docs/mesh.md modified: docs/qa-modes-bench.md modified: docs/seven-point-program.md modified: docs/tickets/ticket-000001-retrieval-keywords-audit-gap.md modified: docs/tickets/ticket-000002-reference-frame-polarity-contract.md modified: docs/tickets/ticket-000003-anchor-class-warrant.md modified: docs/tickets/ticket-000005-label-ladder-migration.md modified: docs/tickets/ticket-000006-bench-emergent-findings.md modified: docs/tickets/ticket-000007-query-layer-hyphen-fold.md modified: docs/tickets/ticket-000008-broad-quantifier-preflight-guard.md modified: docs/tickets/ticket-000009-quantifier-preflight-dag-binding.md modified: docs/tickets/ticket-000010-metacognition-preflight-guard.md modified: docs/tickets/ticket-000011-soft-preflight-hint-sidecar.md modified: scripts/backfill_concepts.py modified: scripts/bench_emergent.py modified: tests/crawler/test_async_web_fetcher.py modified: tests/crawler/test_bridge.py modified: tests/crawler/test_web_fetch.py modified: tests/test_bench_qa_sweep.py modified: tests/test_burn.py modified: tests/test_burn_doc.py modified: tests/test_claim_lattice.py modified: tests/test_cli_render.py modified: tests/test_compress.py modified: tests/test_concepts.py modified: tests/test_dag.py modified: tests/test_directives.py modified: tests/test_distill.py modified: tests/test_distill_recursive.py modified: tests/test_evict.py modified: tests/test_frame.py modified: tests/test_grok_source.py modified: tests/test_html_source.py modified: tests/test_ingest.py modified: tests/test_inspect.py modified: tests/test_journal.py modified: tests/test_keys.py modified: tests/test_llm_context_base.py modified: tests/test_merkle.py modified: tests/test_mesh.py modified: tests/test_mesh_aead.py modified: tests/test_mesh_chain.py modified: tests/test_mesh_cli.py modified: tests/test_mesh_cli_pull.py modified: tests/test_mesh_wire.py modified: tests/test_mesh_wire_e2e.py modified: tests/test_metacognition.py modified: tests/test_migration_audit_mode.py modified: tests/test_providence_source.py modified: tests/test_qa.py modified: tests/test_qa_quality_live.py modified: tests/test_quantifier_caps.py modified: tests/test_quantifier_classifier.py modified: tests/test_quantifier_phase4.py modified: tests/test_quantifier_reminder.py modified: tests/test_query.py modified: tests/test_reclassify.py modified: tests/test_repair.py modified: tests/test_resume.py modified: tests/test_snapshot.py modified: tests/test_soft_preflight.py modified: tests/test_tfidf.py modified: tests/test_vcs_source.py modified: tests/test_verify.py modified: tests/test_verify_json.py modified: tests/test_versioned_ingest.py modified: tests/test_warrant.py modified: tests/test_wikipedia_old.py modified: tests/test_wikipedia_xml.py modified: tests/test_wikitext.py
252 lines
9.1 KiB
Python
252 lines
9.1 KiB
Python
"""Tests for the Grok account-export source.
|
|
|
|
Synthetic fixture mirrors the real prod-grok-backend.json shape: a
|
|
`conversations` array of `{conversation, responses}` entries, plus a
|
|
`media_posts` array of generation prompts. Real personal data is NOT
|
|
imported into the test suite.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from arborist.ingest import ingest_source
|
|
from arborist.sources.grok import (
|
|
GrokExportSource,
|
|
GrokMediaPostsSource,
|
|
_extract_urls,
|
|
_normalize_ts,
|
|
_scrub,
|
|
)
|
|
from arborist.store import connect
|
|
|
|
|
|
def _make_export(tmp_path: Path) -> Path:
|
|
"""Build a minimal but representative export tree, return its root."""
|
|
user_dir = tmp_path / "ttl" / "30d" / "export_data" / "user-uuid"
|
|
user_dir.mkdir(parents=True)
|
|
payload = {
|
|
"conversations": [
|
|
{
|
|
"conversation": {
|
|
"id": "conv-aaaa-1111",
|
|
"title": "Quantum Slop",
|
|
"create_time": "2026-04-25T12:00:00Z",
|
|
"user_id": "user-uuid",
|
|
},
|
|
"responses": [
|
|
{
|
|
"response": {
|
|
"_id": "msg-001",
|
|
"conversation_id": "conv-aaaa-1111",
|
|
"sender": "human",
|
|
"model": "grok-4-auto",
|
|
"create_time": {"$date": "2026-04-25T12:00:00Z"},
|
|
"message": (
|
|
"what is up with quantum slop? see "
|
|
"https://example.com/qs and "
|
|
"https://example.org/qpu, also (https://example.net/x)."
|
|
),
|
|
"metadata": {},
|
|
},
|
|
"share_link": None,
|
|
},
|
|
{
|
|
"response": {
|
|
"_id": "msg-002",
|
|
"conversation_id": "conv-aaaa-1111",
|
|
"sender": "ASSISTANT",
|
|
"model": "grok-3",
|
|
# Mongo extended JSON unix millis (real export shape).
|
|
"create_time": {"$numberLong": "1777306441511"},
|
|
"message": (
|
|
"Quantum slop is a misnomer."
|
|
"<grok:render card_id=\"abc\"/>"
|
|
" Real QPUs differ.<grok:render card_id=\"d\">"
|
|
"ignored body</grok:render>"
|
|
),
|
|
"metadata": {},
|
|
},
|
|
"share_link": None,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
"conversation": {
|
|
"id": "conv-bbbb-2222",
|
|
"title": "Empty thread",
|
|
"create_time": "2026-04-26T00:00:00Z",
|
|
"user_id": "user-uuid",
|
|
},
|
|
# Both messages empty after scrubbing -> doc should be skipped.
|
|
"responses": [
|
|
{
|
|
"response": {
|
|
"_id": "msg-101",
|
|
"sender": "human",
|
|
"message": "",
|
|
"model": "",
|
|
},
|
|
"share_link": None,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
"projects": [],
|
|
"tasks": [],
|
|
"media_posts": [
|
|
{
|
|
"id": "media-7777",
|
|
"user_id": "user-uuid",
|
|
"original_prompt": "a fox standing on a permacomputer",
|
|
"media_type": "image",
|
|
"create_time": "2026-03-12T08:00:00Z",
|
|
"link": "https://assets.example/img/abc.webp",
|
|
},
|
|
{
|
|
"id": "media-8888",
|
|
"user_id": "user-uuid",
|
|
"original_prompt": "",
|
|
"media_type": "image",
|
|
"create_time": "2026-03-12T08:00:01Z",
|
|
"link": "https://assets.example/img/empty.webp",
|
|
},
|
|
],
|
|
}
|
|
(user_dir / "prod-grok-backend.json").write_text(json.dumps(payload))
|
|
return tmp_path
|
|
|
|
|
|
def test_normalize_ts_handles_iso_string():
|
|
assert _normalize_ts("2026-04-25T12:00:00Z") == "2026-04-25T12:00:00Z"
|
|
|
|
|
|
def test_normalize_ts_handles_mongo_date_wrapper():
|
|
assert _normalize_ts({"$date": "2026-04-25T12:00:00Z"}) == "2026-04-25T12:00:00Z"
|
|
|
|
|
|
def test_normalize_ts_handles_mongo_numberlong_millis():
|
|
# 1_777_306_441_511 ms = 2026-04-27T16:14:01.511Z
|
|
iso = _normalize_ts({"$numberLong": "1777306441511"})
|
|
assert iso.startswith("2026-04-27T16:14:01")
|
|
assert iso.endswith("Z")
|
|
|
|
|
|
def test_normalize_ts_handles_nested_date_numberlong():
|
|
iso = _normalize_ts({"$date": {"$numberLong": "1777306441511"}})
|
|
assert iso.startswith("2026-04-27T16:14:01")
|
|
|
|
|
|
def test_normalize_ts_garbage_returns_empty():
|
|
assert _normalize_ts({"weird": "shape"}) == ""
|
|
assert _normalize_ts(None) == ""
|
|
|
|
|
|
def test_scrub_drops_grok_render_markup():
|
|
s = "before<grok:render card_id=\"x\"/>middle<grok:render card_id=\"y\">body</grok:render>after"
|
|
assert _scrub(s) == "beforemiddleafter"
|
|
|
|
|
|
def test_extract_urls_trims_trailing_punctuation_and_dedupes():
|
|
text = (
|
|
"see https://example.com/qs, and https://example.org/qpu. "
|
|
"also (https://example.net/x). https://example.com/qs again."
|
|
)
|
|
urls = _extract_urls(text)
|
|
assert urls == [
|
|
"https://example.com/qs",
|
|
"https://example.org/qpu",
|
|
"https://example.net/x",
|
|
]
|
|
|
|
|
|
def test_grok_export_source_yields_one_doc_per_nonempty_conversation(tmp_path):
|
|
root = _make_export(tmp_path)
|
|
src = GrokExportSource(path=root)
|
|
docs = list(src.iter_documents())
|
|
assert len(docs) == 1
|
|
doc = docs[0]
|
|
assert doc.uri == "grok://conversation/conv-aaaa-1111"
|
|
assert doc.source_type == "grok_export"
|
|
assert doc.title == "Quantum Slop"
|
|
# Both turns serialized with sender/model headers.
|
|
assert "human (grok-4-auto)" in doc.content
|
|
assert "assistant (grok-3)" in doc.content
|
|
# Mongo extended JSON shapes must be normalized — no raw wrappers leak.
|
|
assert "$numberLong" not in doc.content
|
|
assert "$date" not in doc.content
|
|
# Render markers stripped.
|
|
assert "grok:render" not in doc.content
|
|
# URLs surfaced as hyperlink edges.
|
|
edge_uris = {e.dst_uri for e in doc.edges}
|
|
assert "https://example.com/qs" in edge_uris
|
|
assert "https://example.org/qpu" in edge_uris
|
|
assert "https://example.net/x" in edge_uris
|
|
assert all(e.edge_type == "hyperlink" for e in doc.edges)
|
|
|
|
|
|
def test_grok_media_source_yields_one_doc_per_nonempty_post(tmp_path):
|
|
root = _make_export(tmp_path)
|
|
src = GrokMediaPostsSource(path=root)
|
|
docs = list(src.iter_documents())
|
|
assert len(docs) == 1
|
|
doc = docs[0]
|
|
assert doc.uri == "grok://media/media-7777"
|
|
assert doc.source_type == "grok_media"
|
|
assert "fox standing on a permacomputer" in doc.content
|
|
asset_edges = [e for e in doc.edges if e.edge_type == "asset"]
|
|
assert len(asset_edges) == 1
|
|
assert asset_edges[0].dst_uri == "https://assets.example/img/abc.webp"
|
|
|
|
|
|
def test_resolves_export_root_from_json_or_dir(tmp_path):
|
|
root = _make_export(tmp_path)
|
|
json_path = (
|
|
root / "ttl" / "30d" / "export_data" / "user-uuid" / "prod-grok-backend.json"
|
|
)
|
|
# Directly the JSON file.
|
|
a = list(GrokExportSource(path=json_path).iter_documents())
|
|
# The user-id directory.
|
|
b = list(GrokExportSource(path=json_path.parent).iter_documents())
|
|
# The export root (auto-walks down).
|
|
c = list(GrokExportSource(path=root).iter_documents())
|
|
assert [d.uri for d in a] == [d.uri for d in b] == [d.uri for d in c]
|
|
|
|
|
|
def test_resolve_missing_path_raises(tmp_path):
|
|
with pytest.raises(FileNotFoundError):
|
|
GrokExportSource(path=tmp_path / "nope")
|
|
|
|
|
|
def test_grok_ingest_round_trip(tmp_path):
|
|
"""End-to-end: ingest a tiny export into a fresh DB, verify counts."""
|
|
root = _make_export(tmp_path)
|
|
db_path = tmp_path / "arborist.db"
|
|
conn = connect(db_path)
|
|
try:
|
|
conv_stats = ingest_source(conn, GrokExportSource(path=root))
|
|
media_stats = ingest_source(conn, GrokMediaPostsSource(path=root))
|
|
finally:
|
|
conn.close()
|
|
assert conv_stats.inserted == 1
|
|
assert media_stats.inserted == 1
|
|
# Re-ingest is a no-op (idempotent at content-root level).
|
|
conn = connect(db_path)
|
|
try:
|
|
again = ingest_source(conn, GrokExportSource(path=root))
|
|
finally:
|
|
conn.close()
|
|
assert again.inserted == 0
|
|
assert again.skipped_duplicate == 1
|
|
|
|
|
|
def test_serialized_content_is_deterministic(tmp_path):
|
|
"""Same export -> same document_root every time. Required for idempotent
|
|
re-ingest and stable cache keys."""
|
|
root = _make_export(tmp_path)
|
|
a = list(GrokExportSource(path=root).iter_documents())
|
|
b = list(GrokExportSource(path=root).iter_documents())
|
|
assert a[0].content == b[0].content
|