---
type: "Source File"
title: "!/usr/bin/env python3"
description: "#!/usr/bin/env python3 \"\"\"E2E: drive the REAL db.set_verdict path and confirm the cap holds. Runs against a throwaway clone of production. \"\"\" import os import shutil import sqlite3 import sys import tempfile HERE =."
resource: "https://www.copecheck.com"
tags: ["copecheck", "main", "source-file", "py"]
timestamp: "2026-09-25T16:32:22Z"
site: "main"
source_path: "copecheck/test_e2e_setverdict.py"
---

# !/usr/bin/env python3

Source path: `copecheck/test_e2e_setverdict.py`  
Index snapshot generated: `2026-11-24T13:42:54Z`  
File modified: `2026-09-25T16:32:22Z`

```py
#!/usr/bin/env python3
"""E2E: drive the REAL db.set_verdict path and confirm the cap holds.

Runs against a throwaway clone of production.
"""
import os
import shutil
import sqlite3
import sys
import tempfile

HERE = os.path.dirname(os.path.abspath(__file__))
SRC = os.path.join(HERE, "data", "copecheck.db")
TMP = tempfile.mkdtemp(prefix="e2e-")
COPY = os.path.join(TMP, "copecheck.db")
s = sqlite3.connect(f"file:{SRC}?mode=ro", uri=True)
d = sqlite3.connect(COPY)
s.backup(d)
d.close()
s.close()
os.environ["COPECHECK_DB"] = COPY
sys.path.insert(0, HERE)

import db  # noqa: E402
import publish_gate as gate  # noqa: E402

db.DB_PATH = COPY
PASS, FAIL = [], []


def check(n, c, d=""):
    (PASS if c else FAIL).append(n)
    print(f"  [{'PASS' if c else 'FAIL'}] {n} {d}")


# Create 10 pending articles with relevance high enough to be indexed (>=75).
slugs = []
with db.conn() as c:
    gate.ensure_schema(c)
    c.execute("BEGIN IMMEDIATE")
    for i in range(10):
        sl = f"__e2e_{i}"
        c.execute(
            """INSERT OR REPLACE INTO articles
                 (slug,url,url_hash,canonical_url,canonical_url_hash,title,
                  title_hash,source,status,body)
               VALUES (?,?,?,?,?,?,?,?, 'pending', ?)""",
            (sl, f"https://example.invalid/e2e{i}", f"eh{i}",
             f"https://example.invalid/e2e{i}", f"ech{i}",
             f"AI will replace every job number {i}", f"eth{i}",
             "Reuters", "x" * 3000))
        slugs.append(sl)

print("=== E2E: 10 high-relevance articles through db.set_verdict ===")
for i, sl in enumerate(slugs):
    # ascending relevance so the LAST ones are the most valuable
    db.set_verdict(sl, "## Verdict\n\n" + "body " * 200, f"one liner {i}",
                   "test-model", 0.0, relevance=80 + i)

with db.conn() as c:
    rows = c.execute(
        "SELECT slug,index_status,index_reason,relevance FROM articles "
        "WHERE slug LIKE '__e2e_%' ORDER BY relevance").fetchall()
    indexed = [r for r in rows if r["index_status"] in ("index", "trial_index")]
    noidx = [r for r in rows if r["index_status"] == "noindex"]
    check("exactly 5 of 10 became indexable", len(indexed) == 5, len(indexed))
    check("other 5 held at noindex", len(noidx) == 5, len(noidx))
    check("held ones say 'awaiting daily publish slot'",
          all("awaiting daily publish slot" in (r["index_reason"] or "")
              for r in noidx))
    check("all 10 still fully analysed (nothing lost)",
          all(c.execute("SELECT status FROM articles WHERE slug=?",
                        (r["slug"],)).fetchone()[0] == "analysed"
              for r in rows))
    q = c.execute("SELECT slug,priority FROM publish_queue "
                  "WHERE slug LIKE '__e2e_%'").fetchall()
    check("the 5 held ones are QUEUED for a later day", len(q) == 5, len(q))
    check("ledger shows exactly 5 cap-consuming publishes today",
          gate.published_today(c) == 5, gate.published_today(c))
    print("   queued priorities:", sorted(r["priority"] for r in q))

print("\n=== E2E: drainer promotes the queue on a later day ===")
with db.conn() as c:
    c.execute("BEGIN IMMEDIATE")
    # Simulate the next day by moving today's ledger rows back one day.
    c.execute("UPDATE publish_ledger SET publish_date='2000-01-01' "
              "WHERE publish_date=? AND exempt=0", (gate.today_utc(),))
    check("new day => 5 slots free", gate.remaining_today(c) == 5)

os.system(f"cd {HERE} && COPECHECK_DB={COPY} "
          f"{sys.executable} drain_publish_queue.py 2>&1 | sed 's/^/   /'")

with db.conn() as c:
    left = c.execute("SELECT COUNT(*) FROM publish_queue "
                     "WHERE slug LIKE '__e2e_%'").fetchone()[0]
    now_idx = c.execute(
        "SELECT COUNT(*) FROM articles WHERE slug LIKE '__e2e_%' "
        "AND index_status IN ('index','trial_index')").fetchone()[0]
    check("queue drained", left == 0, f"{left} left")
    check("all 10 eventually published across 2 days", now_idx == 10, now_idx)
    check("day 2 consumed exactly 5 slots", gate.published_today(c) == 5)

print("\n=== production untouched ===")
p = sqlite3.connect(f"file:{SRC}?mode=ro", uri=True)
leak = p.execute(
    "SELECT COUNT(*) FROM articles WHERE slug LIKE '__e2e_%'").fetchone()[0]
check("no e2e rows in production", leak == 0, leak)
p.close()

shutil.rmtree(TMP, ignore_errors=True)
print(f"\n{'='*50}\nPASSED {len(PASS)}  FAILED {len(FAIL)}")
if FAIL:
    print("FAILURES:", FAIL)
sys.exit(1 if FAIL else 0)
```
