My unit test deleted my real sales database
The short version
A test that empties its own table ran on my real sales ledger instead of a temp copy. One test file imported the database module before another test file had pointed it at a temp folder, and the module read its path only once, at import.
Within about a minute, the live loop treated 8 people I had already messaged as brand new. It was stopped before anyone got a second message. The fix is small: set the temp paths in the test package's __init__.py, and make every test that deletes check where it is first.
What I saw
# tests/test_parts.py, SendQueue.setUp
self.con = db.connect()
self.con.execute("DELETE FROM leads")
self.con.commit()That DELETE is fine on a throwaway database. On Oct 27 at 14:49 (Korea time) it ran on the real one: sales.db, the ledger of my AI sales loop, with every person it had found, messaged or closed.

At 14:49:38 the loop's next tick found a fake person the test had left behind, yt-gone, and tried to message them. That failed harmlessly. The tick after that was worse: 8 people I had already contacted looked new, so it started downloading their videos and designing fresh samples for them. It was stopped at 14:50:31. Nothing was sent twice.
Why it happened
The database module decided where the ledger lives once, when Python first imported it:
STATE = pathlib.Path(os.environ.get("CUEROOM_SALES_STATE", pathlib.Path.home() / ".local/state/cueroom-sales"))test_parts.py set CUEROOM_SALES_STATE to a temp folder at the top of the file. That worked while it was the only test file that touched the database. Then I added test_expire.py. Test discovery loads files in name order, so test_expire.py came first and imported the module while the variable was still unset. The path locked onto the real ledger, and every test after that, including the one that deletes, used it.

Getting the data back
Only one table was emptied. The others were untouched: the event log, every message sent and received, the judge's verdicts, and a folder on disk for each person. I rebuilt 53 people from those. The inbox display names for 15 DM conversations were only in the emptied table, so I read them again from the inbox list.
The fix
# tests/__init__.py runs before any test module
import os, sys, tempfile
if "sales.db" in sys.modules:
raise RuntimeError("sales.db was imported before the tests could point it at a temp folder")
_TMP = tempfile.mkdtemp(prefix="sales-test-")
os.environ["CUEROOM_SALES_STATE"] = _TMP + "/state"
os.environ["CUEROOM_SALES_LEADS"] = _TMP + "/leads"
os.environ["CUEROOM_SALES_CACHE"] = _TMP + "/cache"def setUp(self):
self.assertIn("sales-test-", str(db.STATE)) # refuse to run anywhere else
self.con = db.connect()
self.con.execute("DELETE FROM leads")What I'd do differently
- Don't give a module a default path that points at real data. Make the service pass the real path in, so a test that forgets gets an error instead of production.
- Any test setup that deletes should first check that it's standing in a temp folder.
- Keep append-only logs next to the tables you change. They're the only reason I could rebuild this one.