Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

8.1 KB
Raw Blame
"""Who an email address belongs to: aliases and the company's own domains.

A person has one primary address (their sign-in) and any number of aliases.
Mail matches its sender by either, exactly; on the company's own domains a
teammate also matches by local part (``+tag`` dropped, the company's domains
counted as one). The outbound allow-list (``RosterAllow``) follows the same
rule, so the Executive may email exactly the addresses mail would match.
"""
from __future__ import annotations

from collections.abc import Iterator
from pathlib import Path
from types import SimpleNamespace

import pytest

from openexecutive.memory import episodic
from openexecutive.memory import workspace_settings as ws
from openexecutive.people import identity
from openexecutive.people import registry as people_registry
from openexecutive.people import store as people_store


@pytest.fixture(autouse=True)
def db(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
    path = tmp_path / "identity.db"
    monkeypatch.setattr(people_store, "DB_PATH", path)
    monkeypatch.setattr(episodic, "DB_PATH", path)
    people_store.initialize_db()
    people_registry.invalidate()
    yield path
    people_registry.invalidate()


@pytest.fixture
def roster() -> SimpleNamespace:
    owner = people_store.upsert_person(
        full_name="Olivia Owner", is_principal=True, email="[email protected]"
    )
    anna = people_store.upsert_person(full_name="Anna Smith", email="[email protected]")
    client = people_store.upsert_person(
        full_name="Carl Client", email="[email protected]", kind="contact"
    )
    return SimpleNamespace(owner=owner, anna=anna, client=client)


def _name(addr: str, **kw: bool) -> str | None:
    person = identity.resolve_email_sender(addr, **kw)
    return person.full_name if person is not None else None


def test_company_domains_come_from_the_principals_address(roster: SimpleNamespace) -> None:
    assert identity.company_domains() == frozenset({"acme.com"})


def test_a_free_mail_principal_has_no_company_domain() -> None:
    people_store.upsert_person(full_name="Solo", is_principal=True, email="[email protected]")
    assert identity.company_domains() == frozenset()
    people_store.upsert_person(full_name="Sam", email="[email protected]")
    # Nobody on gmail.com matches by local part: anyone can register sam+x.
    assert _name("[email protected]") is None


def test_the_setting_overrides_the_derived_domains(roster: SimpleNamespace) -> None:
    ws.set_company_domains(["acme.com", "acme.io"])
    assert identity.company_domains() == frozenset({"acme.com", "acme.io"})
    # [email protected] is the Anna at [email protected]: the company's domains are one.
    assert _name("[email protected]") == "Anna Smith"


def test_plus_tags_are_dropped_only_on_a_company_domain(roster: SimpleNamespace) -> None:
    assert _name("[email protected]") == "Anna Smith"
    people_store.set_person_emails(roster.anna, ["[email protected]"])
    assert _name("[email protected]") == "Anna Smith"
    assert _name("[email protected]") is None


def test_an_unknown_local_part_on_the_company_domain_matches_nobody(roster: SimpleNamespace) -> None:
    # The domain alone never says who someone is.
    assert _name("[email protected]") is None


def test_the_domain_rule_never_matches_a_contact(roster: SimpleNamespace) -> None:
    assert _name("[email protected]", include_contacts=True) == "Carl Client"  # exact still does
    assert _name("[email protected]", include_contacts=True) is None


def test_two_teammates_with_one_local_part_match_nobody(roster: SimpleNamespace) -> None:
    ws.set_company_domains(["acme.com", "acme.io"])
    people_store.upsert_person(full_name="Anna Other", email="[email protected]")
    # Exact addresses still resolve; the ambiguous local-part match fails closed.
    assert _name("[email protected]") == "Anna Other"
    assert _name("[email protected]") is None


def test_an_alias_matches_mail_but_never_signs_in(roster: SimpleNamespace) -> None:
    people_store.set_person_emails(roster.anna, ["[email protected]"])
    assert people_store.find_person_by_address("[email protected]").full_name == "Anna Smith"
    # find_person_by_email is the sign-in lookup: primary only.
    assert people_store.find_person_by_email("[email protected]") is None
    assert people_store.get_person(roster.anna).email_aliases == ["[email protected]"]


def test_an_address_belongs_to_one_person(roster: SimpleNamespace) -> None:
    with pytest.raises(people_store.AddressInUseError):
        people_store.set_person_emails(roster.anna, ["[email protected]"])
    people_store.set_person_emails(roster.anna, ["[email protected]"])
    with pytest.raises(people_store.AddressInUseError):
        people_store.add_person_email(roster.client, "[email protected]")
    assert people_store.add_person_email(roster.anna, "[email protected]") is False


def test_bad_aliases_are_refused_and_the_primary_dropped(roster: SimpleNamespace) -> None:
    assert people_store.clean_aliases(
        [" [email protected]", "[email protected]", "", "[email protected]"], primary="[email protected]"
    ) == ["[email protected]"]
    for bad in ["no-at-sign", "a@b", "a [email protected]", "<[email protected]>", "[email protected], [email protected]"]:
        with pytest.raises(ValueError):
            people_store.clean_aliases([bad])
    with pytest.raises(ValueError):
        people_store.clean_aliases([f"a{i}@b.com" for i in range(11)])


def test_archiving_frees_the_aliases(roster: SimpleNamespace) -> None:
    people_store.set_person_emails(roster.anna, ["[email protected]"])
    people_store.archive_person(roster.anna)
    assert people_store.find_person_by_address("[email protected]") is None
    people_store.set_person_emails(roster.client, ["[email protected]"])


def test_the_allow_list_follows_the_same_rule(roster: SimpleNamespace) -> None:
    people_store.set_person_emails(roster.anna, ["[email protected]"])
    allow = identity.RosterAllow(people_store.list_people(), extra=["[email protected]"])
    assert "[email protected]" in allow
    assert "[email protected]" in allow
    assert "[email protected]" in allow
    assert "[email protected]" not in allow
    assert "[email protected]" not in allow
    # Built from the team only (the gateway adds contacts on the principal's
    # own turn): a contact's address is refused like a stranger's.
    assert "[email protected]" not in allow
    with_contacts = identity.RosterAllow(people_store.list_people(include_contacts=True))
    assert "[email protected]" in with_contacts
    assert "[email protected]" not in with_contacts


def test_the_migration_adds_aliases_to_an_existing_roster(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    import sqlite3

    path = tmp_path / "old.db"
    with sqlite3.connect(path) as conn:
        conn.execute(
            "CREATE TABLE people (id INTEGER PRIMARY KEY AUTOINCREMENT, full_name TEXT NOT NULL,"
            " role TEXT NOT NULL DEFAULT '', is_principal INTEGER NOT NULL DEFAULT 0,"
            " department_slugs_json TEXT NOT NULL DEFAULT '[]', email TEXT, slack_user_id TEXT,"
            " telegram_chat_id TEXT, preferred_channel TEXT NOT NULL DEFAULT 'any',"
            " response_sla_hours INTEGER NOT NULL DEFAULT 24, on_leave_until TEXT,"
            " reports_to_person_id INTEGER, archived INTEGER NOT NULL DEFAULT 0,"
            " created_at TEXT NOT NULL, updated_at TEXT NOT NULL)"
        )
        conn.execute(
            "INSERT INTO people (full_name, email, created_at, updated_at)"
            " VALUES ('Old Timer', '[email protected]', 'x', 'x')"
        )
    monkeypatch.setattr(people_store, "DB_PATH", path)
    people_store.initialize_db()
    people_store.initialize_db()  # idempotent
    person = people_store.find_person_by_address("[email protected]")
    assert person is not None and person.email_aliases == []


def test_an_alias_never_makes_its_domain_a_company_domain(roster: SimpleNamespace) -> None:
    # A personal ISP address on the principal must not let anyone who
    # registers [email protected] pass as Anna.
    people_store.set_person_emails(roster.owner, ["[email protected]"])
    assert identity.company_domains() == frozenset({"acme.com"})
    assert _name("[email protected]") is None
    allow = identity.RosterAllow(people_store.list_people())
    assert "[email protected]" not in allow