Fetches upstream URL via safe_fetch, sha256-diffs against the prior body_sha stored in metadata, updates body_text + last_synced only when content changed. Unchanged syncs just touch last_synced. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
import subprocess
|
|
from unittest.mock import patch
|
|
from void_workers.handlers.sourcedoc import handle as handle_sd
|
|
|
|
|
|
def _reset_void_schema(conn):
|
|
conn.execute("DROP SCHEMA IF EXISTS public CASCADE")
|
|
conn.execute("CREATE SCHEMA public")
|
|
conn.execute("CREATE EXTENSION IF NOT EXISTS pgcrypto")
|
|
conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
|
|
|
|
|
|
def _run_node_migrations():
|
|
subprocess.run(
|
|
["node", "lib/db/migrate.js", "up"],
|
|
cwd="/project/src/void-v2",
|
|
check=True
|
|
)
|
|
|
|
|
|
def _seed(conn, slug='sd'):
|
|
sp = conn.execute(
|
|
f"INSERT INTO spaces(slug, name) VALUES('{slug}', 'SD') RETURNING id"
|
|
).fetchone()[0]
|
|
res = conn.execute(
|
|
"INSERT INTO resources(space_id, slug, name, runtime_type) "
|
|
"VALUES(%s, 'r', 'R', 'lxc') RETURNING id", (sp,)
|
|
).fetchone()[0]
|
|
sd = conn.execute(
|
|
"INSERT INTO source_docs(resource_id, name, upstream_url, sync_source) "
|
|
"VALUES(%s, 'doc', 'https://example.com/r', 'url') RETURNING id",
|
|
(res,)
|
|
).fetchone()[0]
|
|
return sp, res, sd
|
|
|
|
|
|
def test_sourcedoc_updates_body_text(conn):
|
|
_reset_void_schema(conn)
|
|
_run_node_migrations()
|
|
sp, res, sd = _seed(conn)
|
|
with patch("void_workers.handlers.sourcedoc.safe_fetch",
|
|
return_value=b"hello world doc body"):
|
|
out = handle_sd({"source_doc_id": str(sd)})
|
|
assert out.get("updated") is True
|
|
row = conn.execute("SELECT body_text FROM source_docs WHERE id=%s", (sd,)).fetchone()
|
|
assert "hello world" in (row[0] or "")
|
|
|
|
|
|
def test_sourcedoc_unchanged_when_sha_matches(conn):
|
|
_reset_void_schema(conn)
|
|
_run_node_migrations()
|
|
sp, res, sd = _seed(conn, slug='sd2')
|
|
|
|
# First sync writes body + sha into metadata
|
|
with patch("void_workers.handlers.sourcedoc.safe_fetch",
|
|
return_value=b"unchanged content"):
|
|
handle_sd({"source_doc_id": str(sd)})
|
|
|
|
# Second sync with same body
|
|
with patch("void_workers.handlers.sourcedoc.safe_fetch",
|
|
return_value=b"unchanged content"):
|
|
out = handle_sd({"source_doc_id": str(sd)})
|
|
assert out.get("unchanged") is True
|
|
|
|
|
|
def test_sourcedoc_skipped_when_gone(conn):
|
|
_reset_void_schema(conn)
|
|
_run_node_migrations()
|
|
out = handle_sd({"source_doc_id": "00000000-0000-0000-0000-000000000000"})
|
|
assert out.get("skipped") == "gone"
|