#!/usr/bin/env python3 """Test the outreach agent's context assembly: it pulls the investor's CRM notes + recent matched email into the de-identifiable context block. Synthetic data only (guardrail #9). The scrub/Claude/rehydrate round-trip is exercised live in the preview. Run: cd backend && python3 mcp/test_outreach.py """ import os import sqlite3 import sys import tempfile sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import outreach_agent as oa # noqa: E402 FAILS = [] def check(cond, msg): print((" PASS " if cond else " FAIL ") + msg) if not cond: FAILS.append(msg) def main(): db = os.path.join(tempfile.mkdtemp(), "t.db") c = sqlite3.connect(db) c.row_factory = sqlite3.Row c.executescript(""" CREATE TABLE fundraising_investors (id TEXT PRIMARY KEY, investor_name TEXT, notes TEXT); CREATE TABLE emails (id TEXT PRIMARY KEY, subject TEXT, body_text TEXT, snippet TEXT, sent_at TEXT, is_matched INT); CREATE TABLE email_investor_links (id TEXT, email_id TEXT, fundraising_investor_id TEXT); """) c.execute("INSERT INTO fundraising_investors VALUES ('inv1','Harbor & Vine','Met at the conference; interested in Fund III.')") c.executemany("INSERT INTO emails (id,subject,body_text,sent_at,is_matched) VALUES (?,?,?,?,1)", [ ("e1", "Re: Fund III", "Thanks for the call. We are still weighing the lock-up terms.", "2026-06-02T10:00:00"), ("e2", "Intro", "Good to meet you at the dinner.", "2026-05-01T10:00:00"), ("e3", "Spam", "ignore me", "2026-04-01T10:00:00"), # not linked -> excluded ]) c.executemany("INSERT INTO email_investor_links (id,email_id,fundraising_investor_id) VALUES (?,?, 'inv1')", [("l1", "e1"), ("l2", "e2")]) c.commit() name, ctx = oa._context(c, "inv1") check(name == "Harbor & Vine", f"resolves investor name (got {name!r})") check("Met at the conference" in ctx, "includes CRM notes") check("lock-up terms" in ctx, "includes matched email body") check("Good to meet you" in ctx, "includes a second matched email") check("ignore me" not in ctx, "excludes email not linked to this investor") check(ctx.index("lock-up terms") < ctx.index("Good to meet you"), "newest email first") n2, c2 = oa._context(c, "missing") check(n2 is None and c2 is None, "unknown investor -> (None, None)") # type catalogue is intact check(set(["intro", "follow_up", "fund_update", "meeting_follow_up", "nurture"]) <= set(oa.OUTREACH_TYPES), "outreach types catalogue present") if FAILS: print(f"\nFAILED ({len(FAILS)})") for f in FAILS: print(" - " + f) sys.exit(1) print("\nALL PASS (outreach context assembly)") if __name__ == "__main__": main()