Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

22.2 KB
Raw Blame
/**
 * Pure-logic + injectable-dep tests for the opt-out automation engine
 * (issue #2145): disclosure guardrail, lane selection, template rendering,
 * anti-phishing verification scoring, planner ordering, and the lane/run-loop
 * wiring. No network / DB — privacyBrokers transitions + the message/vault/scan
 * services are mocked so the orchestration is exercised without Postgres.
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';

const transitionCase = vi.fn(async (id, to, patch) => ({ id, state: to, ...patch }));
const listBrokers = vi.fn(async () => []);
const listBrokerCases = vi.fn(async () => []);
const getBroker = vi.fn(async () => null);

vi.mock('./privacyBrokers.js', () => ({
  transitionCase,
  listBrokers,
  listBrokerCases,
  getBroker,
  getCaseForBroker: vi.fn(async () => null),
}));
vi.mock('./privacyVault.js', () => ({ listScanEligibleValues: vi.fn(async () => []) }));
// #3658 consent gate — mocked so the pass runs without a DB, and so a test can
// flip it to the refusing behaviour to prove the guard is engine-enforced.
const SELF = '00000000-0000-4000-8000-000000000001';
const { assertSubjectConsentMock } = vi.hoisted(() => ({
  assertSubjectConsentMock: vi.fn(async (id) => ({ id: id || '00000000-0000-4000-8000-000000000001' })),
}));
vi.mock('./privacySubjects.js', () => ({
  resolveSubjectId: (id) => id || SELF,
  assertSubjectConsent: assertSubjectConsentMock,
}));
vi.mock('./privacyScan.js', async (importOriginal) => {
  const actual = await importOriginal();
  return { ...actual, probeBroker: vi.fn(async () => ({ skipped: true, reason: 'inconclusive' })) };
});
const createDraft = vi.fn(async (d) => ({ id: 'draft-1', status: 'draft', ...d }));
const approveDraft = vi.fn(async () => ({ id: 'draft-1', status: 'approved' }));
vi.mock('./messageDrafts.js', () => ({ createDraft, approveDraft }));
vi.mock('./messageAccounts.js', () => ({ listAccounts: vi.fn(async () => [{ id: 'acct-1', type: 'gmail', canSend: true }]) }));
vi.mock('./messageSync.js', () => ({ getMessages: vi.fn(async () => ({ messages: [] })) }));
vi.mock('./settings.js', () => ({ getSettings: vi.fn(async () => ({ privacy: { recheck: {} } })) }));

const {
  DISCLOSURE_ALLOWLIST, buildDisclosurePayload, computeDisclosedFields,
  chooseLane, chooseEmailTemplate, fillTemplate, renderOptOutEmail,
  brokerDomains, scoreVerificationEmail, planOptOutActions,
  emailLane, webFormLane, runOptOutPass, runVerificationPass, getOptOutDigest,
} = await import('./privacyOptOut.js');
const _scan = await import('./privacyScan.js');
const settings = await import('./settings.js');

beforeEach(() => {
  vi.clearAllMocks();
  assertSubjectConsentMock.mockImplementation(async (id) => ({ id: id || SELF }));
  transitionCase.mockImplementation(async (id, to, patch) => ({ id, state: to, ...patch }));
  listBrokers.mockResolvedValue([]);
  listBrokerCases.mockResolvedValue([]);
  getBroker.mockResolvedValue(null);
  createDraft.mockImplementation(async (d) => ({ id: 'draft-1', status: 'draft', ...d }));
  settings.getSettings.mockResolvedValue({ privacy: { recheck: {} } });
});

const SCAN_VALUES = [
  { type: 'legal_name', value: 'Jane Q Doe' },
  { type: 'email', value: '[email protected]' },
  { type: 'phone', value: '+1 503 555 0100' },
  { type: 'address', value: '1 Oak Ave, Portland, OR 97201', status: 'current' },
];

describe('buildDisclosurePayload', () => {
  it('populates only allowlisted keys from vault scan values', () => {
    const p = buildDisclosurePayload(SCAN_VALUES);
    expect(p).toEqual({ full_name: 'Jane Q Doe', email: '[email protected]', phone: '+1 503 555 0100', city: 'Portland', state: 'OR' });
    for (const k of Object.keys(p)) expect(DISCLOSURE_ALLOWLIST).toContain(k);
  });
  it('prefers a current address for city/state', () => {
    const p = buildDisclosurePayload([
      { type: 'legal_name', value: 'A B' },
      { type: 'address', value: 'Salem, OR', status: 'previous' },
      { type: 'address', value: 'Austin, TX', status: 'current' },
    ]);
    expect(p.city).toBe('Austin');
    expect(p.state).toBe('TX');
  });
  it('includes dob only when present as a scan value', () => {
    const p = buildDisclosurePayload([...SCAN_VALUES, { type: 'dob', value: '1990-01-01' }]);
    expect(p.dob).toBe('1990-01-01');
  });
});

describe('computeDisclosedFields — allowlist ∩ broker ∩ available', () => {
  const payload = { full_name: 'Jane Q Doe', email: '[email protected]', phone: '1', city: 'Portland', state: 'OR' };
  it('intersects the fixed allowlist with the broker declared fields we hold', () => {
    const broker = { disclosureFields: ['full_name', 'city', 'state', 'email', 'listing_url'] };
    expect(computeDisclosedFields(broker, payload)).toEqual(['full_name', 'email', 'city', 'state']);
  });
  it('adds listing_url only when the case has one', () => {
    const broker = { disclosureFields: ['full_name', 'listing_url'] };
    expect(computeDisclosedFields(broker, payload)).toEqual(['full_name']);
    expect(computeDisclosedFields(broker, payload, { listingUrls: ['https://x/1'] })).toEqual(['full_name', 'listing_url']);
  });
  it('never emits a field outside the allowlist even if the broker asks', () => {
    const broker = { disclosureFields: ['full_name', 'ssn', 'passport', 'mothers_maiden'] };
    const withEvil = { ...payload, ssn: '123', passport: 'X' };
    expect(computeDisclosedFields(broker, withEvil)).toEqual(['full_name']);
  });
});

describe('chooseLane', () => {
  it('email method with an address → email', () => {
    expect(chooseLane({ optout: { method: 'email', email: '[email protected]' } })).toBe('email');
  });
  it('web_form method with a url → web_form', () => {
    expect(chooseLane({ optout: { method: 'web_form', url: 'https://b.com/optout' } })).toBe('web_form');
  });
  it('anti-bot walled form with an email fallback → email', () => {
    expect(chooseLane({ antibot: true, optout: { method: 'web_form', url: 'https://b.com/o', email: '[email protected]' } })).toBe('email');
  });
  it('no channel → human', () => {
    expect(chooseLane({ optout: { method: 'fax' } })).toBe('human');
  });
});

describe('chooseEmailTemplate', () => {
  it('CA-address subject → ccpa', () => {
    expect(chooseEmailTemplate({ optout: { notes: '' } }, { state: 'CA' })).toBe('ccpa');
  });
  it('gdpr note wins', () => {
    expect(chooseEmailTemplate({ optout: { notes: 'Use the GDPR lane' } })).toBe('gdpr');
  });
  it('default generic', () => {
    expect(chooseEmailTemplate({ optout: { notes: '' } }, { state: 'OR' })).toBe('generic');
  });
});

describe('fillTemplate + renderOptOutEmail', () => {
  it('fills tokens and collapses unknowns', () => {
    expect(fillTemplate('Hi {{a}} / {{missing}}', { a: 'X' })).toBe('Hi X / ');
  });
  it('splits subject from body and injects disclosure', async () => {
    const templateLoader = async () => 'Subject: Opt-out for {{fullName}}\n\nBody\n{{disclosedSummary}}\n';
    const out = await renderOptOutEmail({
      broker: { name: 'Spokeo', disclosureFields: ['full_name', 'email'] },
      payload: { full_name: 'Jane Q Doe', email: '[email protected]' },
      disclosedFields: ['full_name', 'email'],
      templateLoader,
      now: new Date('2026-07-08T00:00:00Z'),
    });
    expect(out.subject).toBe('Opt-out for Jane Q Doe');
    expect(out.body).toContain('Full name: Jane Q Doe');
    expect(out.body).toContain('Email: [email protected]');
    expect(out.body).not.toMatch(/^Subject:/m);
  });
});

describe('brokerDomains + scoreVerificationEmail (anti-phishing)', () => {
  const broker = { urls: { home: 'https://www.spokeo.com' }, optout: { url: 'https://www.spokeo.com/optout', email: '[email protected]' } };
  it('collects the broker domains', () => {
    expect(brokerDomains(broker)).toContain('spokeo.com');
  });
  it('trusts a confirmation email with a matching link domain', () => {
    const msg = { subject: 'Confirm your opt-out', body: 'Please click https://www.spokeo.com/confirm?t=abc to complete your removal.' };
    const s = scoreVerificationEmail(msg, broker);
    expect(s.isConfirmation).toBe(true);
    expect(s.verificationUrl).toContain('spokeo.com/confirm');
  });
  it('rejects a look-alike phishing domain', () => {
    const msg = { subject: 'Confirm your opt-out', body: 'Click https://spokeo-confirm.evil.com/x to complete.' };
    const s = scoreVerificationEmail(msg, broker);
    expect(s.isConfirmation).toBe(false);
    expect(s.reason).toBe('no_domain_match');
  });
  it('rejects a matching-domain email that is not a confirmation', () => {
    const msg = { subject: 'Your weekly newsletter', body: 'News from https://www.spokeo.com/blog' };
    expect(scoreVerificationEmail(msg, broker).isConfirmation).toBe(false);
  });
});

describe('planOptOutActions ordering', () => {
  it('cluster parents first, then found before indirect, then by name', () => {
    const cwb = [
      { case: { state: 'indirect_exposure' }, broker: { name: 'Zeta', clusterParent: null } },
      { case: { state: 'found' }, broker: { name: 'Beta', clusterParent: 'parent' } },
      { case: { state: 'found' }, broker: { name: 'Alpha', clusterParent: null } },
    ];
    const { submit } = planOptOutActions(cwb);
    expect(submit.map((s) => s.broker.name)).toEqual(['Alpha', 'Zeta', 'Beta']);
  });
  it('splits verify-poll states out', () => {
    const cwb = [
      { case: { state: 'submitted' }, broker: { name: 'A' } },
      { case: { state: 'found' }, broker: { name: 'B' } },
    ];
    const { submit, verify } = planOptOutActions(cwb);
    expect(submit).toHaveLength(1);
    expect(verify).toHaveLength(1);
  });
});

describe('emailLane', () => {
  const broker = { id: 'mylife', name: 'MyLife', optout: { method: 'email', email: '[email protected]' }, disclosureFields: ['full_name', 'email'] };
  const payload = { full_name: 'Jane Q Doe', email: '[email protected]' };
  it('creates a draft locked to the broker address and transitions to submitted', async () => {
    const res = await emailLane(broker, { id: 'c1' }, { disclosedFields: ['full_name', 'email'], payload, autoApprove: false });
    expect(res.outcome).toBe('submitted');
    expect(createDraft).toHaveBeenCalledWith(expect.objectContaining({ to: ['[email protected]'], generatedBy: 'privacy-optout' }));
    expect(approveDraft).not.toHaveBeenCalled();
    const states = transitionCase.mock.calls.map((c) => c[1]);
    expect(states).toEqual(['optout_in_progress', 'submitted']);
  });
  it('auto-approve approves the draft but never sends it', async () => {
    approveDraft.mockClear();
    const res = await emailLane(broker, { id: 'c1' }, { disclosedFields: ['full_name', 'email'], payload, autoApprove: true });
    expect(approveDraft).toHaveBeenCalledWith('draft-1');
    expect(res).toMatchObject({ outcome: 'submitted', autoApproved: true });
    expect(res).not.toHaveProperty('sent');
    const evidence = transitionCase.mock.calls.at(-1)[2].evidence;
    expect(evidence).toMatchObject({ autoApproved: true });
    expect(evidence).not.toHaveProperty('sent');
  });
  it.each([
    ['an Outlook account drafts over the browser transport', [{ id: 'outlook-1', type: 'outlook', canSend: true }], 'outlook-1', 'playwright'],
    ['Gmail is preferred over a browser-delivered account', [{ id: 'outlook-1', type: 'outlook', canSend: true }, { id: 'gmail-1', type: 'gmail', canSend: true }], 'gmail-1', 'api']
  ])('%s', async (_label, accounts, accountId, sendVia) => {
    await emailLane(broker, { id: 'c1' }, { disclosedFields: ['full_name', 'email'], payload, accountsProvider: async () => accounts });
    expect(createDraft).toHaveBeenCalledWith(expect.objectContaining({ accountId, sendVia }));
  });
  it('never drafts a broker email from a Teams chat', async () => {
    createDraft.mockClear();
    const res = await emailLane(broker, { id: 'c1' }, { disclosedFields: ['full_name', 'email'], payload, accountsProvider: async () => [{ id: 'teams-1', type: 'teams', canSend: true }] });
    expect(res).toMatchObject({ outcome: 'account_required' });
    expect(createDraft).not.toHaveBeenCalled();
  });
  it('queues a human task when the broker has no email', async () => {
    const res = await emailLane({ id: 'x', optout: {} }, { id: 'c1' }, { disclosedFields: ['full_name'], payload });
    expect(res.outcome).toBe('human_task_queued');
  });
});

describe('webFormLane', () => {
  const broker = { id: 'spokeo', name: 'Spokeo', optout: { method: 'web_form', url: 'https://www.spokeo.com/optout', playbook: ['step 1'] }, disclosureFields: ['full_name', 'email', 'listing_url'] };
  it('always queues a human task with the playbook and never submits', async () => {
    const res = await webFormLane(broker, { id: 'c1' }, { disclosedFields: ['full_name'] });
    expect(res.outcome).toBe('human_task_queued');
    expect(transitionCase).toHaveBeenCalledWith('c1', 'human_task_queued', expect.objectContaining({
      channel: 'web_form', reason: 'manual_submit_required',
      evidence: expect.objectContaining({ playbook: ['step 1'], requires: 'manual_submit' }),
    }));
    expect(transitionCase.mock.calls.map((c) => c[1])).not.toContain('submitted');
  });
  it('records no_optout_url when the broker has no form URL', async () => {
    await webFormLane({ id: 'x', optout: {} }, { id: 'c1' }, { disclosedFields: ['full_name'] });
    expect(transitionCase).toHaveBeenCalledWith('c1', 'human_task_queued', expect.objectContaining({ reason: 'no_optout_url' }));
  });
});

describe('runOptOutPass', () => {
  it('aborts when there is no scan-eligible name', async () => {
    const vault = await import('./privacyVault.js');
    vault.listScanEligibleValues.mockResolvedValueOnce([]);
    const res = await runOptOutPass({ runVerification: false });
    expect(res.reason).toBe('no_disclosure_identity');
  });
  it('routes a found email-broker through the email lane', async () => {
    const vault = await import('./privacyVault.js');
    vault.listScanEligibleValues.mockResolvedValue(SCAN_VALUES);
    listBrokers.mockResolvedValue([{ id: 'mylife', name: 'MyLife', optout: { method: 'email', email: '[email protected]' }, disclosureFields: ['full_name', 'email'], clusterParent: null }]);
    listBrokerCases.mockResolvedValue([{ id: 'c1', brokerId: 'mylife', state: 'found', evidence: {} }]);
    const res = await runOptOutPass({ runVerification: false });
    expect(res.submitted[0].lane).toBe('email');
    expect(res.submitted[0].outcome).toBe('submitted');
  });
  it('reads the auto-approve toggle from settings', async () => {
    const vault = await import('./privacyVault.js');
    vault.listScanEligibleValues.mockResolvedValue(SCAN_VALUES);
    settings.getSettings.mockResolvedValue({ privacy: { recheck: { autoApproveOptOutEmails: true } } });
    listBrokers.mockResolvedValue([]);
    listBrokerCases.mockResolvedValue([]);
    const res = await runOptOutPass({ runVerification: false });
    expect(res.autonomy).toEqual({ autoApprove: true });
  });
});

describe('runVerificationPass', () => {
  it('advances submitted → verification_pending on a trusted confirmation', async () => {
    const broker = { id: 'spokeo', urls: { home: 'https://www.spokeo.com' }, optout: { url: 'https://www.spokeo.com/optout' } };
    getBroker.mockResolvedValue(broker);
    listBrokerCases
      .mockResolvedValueOnce([{ id: 'c1', brokerId: 'spokeo', state: 'submitted', evidence: {} }])
      .mockResolvedValueOnce([]); // no verification_pending/awaiting to re-scan
    const messagesProvider = vi.fn(async () => ({ messages: [{ id: 'm1', subject: 'Confirm your opt-out', body: 'Click https://www.spokeo.com/confirm to complete removal.' }] }));
    const res = await runVerificationPass({ messagesProvider });
    expect(res.advanced).toHaveLength(1);
    expect(transitionCase).toHaveBeenCalledWith('c1', 'verification_pending', expect.anything());
  });
  it('confirms removal only when a read-only re-scan returns not_found', async () => {
    const broker = { id: 'spokeo', urls: { search: 'https://www.spokeo.com/{firstName}-{lastName}/{state}' } };
    getBroker.mockResolvedValue(broker);
    const vault = await import('./privacyVault.js');
    vault.listScanEligibleValues.mockResolvedValue(SCAN_VALUES);
    listBrokerCases
      .mockResolvedValueOnce([]) // no submitted
      .mockResolvedValueOnce([{ id: 'c2', brokerId: 'spokeo', state: 'awaiting_processing', evidence: {} }]);
    const removalProbe = vi.fn(async () => ({ verdict: 'not_found' }));
    const res = await runVerificationPass({ removalProbe });
    expect(res.confirmed).toHaveLength(1);
    expect(transitionCase).toHaveBeenCalledWith('c2', 'confirmed_removed', expect.objectContaining({ viaRescan: true }));
  });
  it('increments recheck count and moves verification_pending to awaiting_processing when still found (#4020)', async () => {
    getBroker.mockResolvedValue({ id: 'spokeo', urls: { search: 'https://www.spokeo.com/{firstName}/{state}' } });
    const vault = await import('./privacyVault.js');
    vault.listScanEligibleValues.mockResolvedValue(SCAN_VALUES);
    listBrokerCases
      .mockResolvedValueOnce([])
      .mockResolvedValueOnce([{ id: 'c3', brokerId: 'spokeo', state: 'verification_pending', evidence: {} }]);
    const removalProbe = vi.fn(async () => ({ verdict: 'found', found: true }));
    const res = await runVerificationPass({ removalProbe });
    expect(res.confirmed).toHaveLength(0);
    expect(transitionCase).toHaveBeenCalledWith('c3', 'awaiting_processing', expect.objectContaining({
      evidence: expect.objectContaining({ verificationRechecks: 1 }),
    }));
  });
  it('transitions stranded awaiting_processing cases to human_task_queued with reason optout_unfulfilled after max rechecks (#4020)', async () => {
    getBroker.mockResolvedValue({ id: 'spokeo', urls: { search: 'https://www.spokeo.com/{firstName}/{state}' } });
    const vault = await import('./privacyVault.js');
    vault.listScanEligibleValues.mockResolvedValue(SCAN_VALUES);
    listBrokerCases
      .mockResolvedValueOnce([])
      .mockResolvedValueOnce([{ id: 'c4', brokerId: 'spokeo', state: 'awaiting_processing', evidence: { verificationRechecks: 2 } }]);
    const removalProbe = vi.fn(async () => ({ verdict: 'found', found: true }));
    const res = await runVerificationPass({ removalProbe, maxVerificationRechecks: 3 });
    expect(res.unfulfilled).toHaveLength(1);
    expect(transitionCase).toHaveBeenCalledWith('c4', 'human_task_queued', expect.objectContaining({
      reason: 'optout_unfulfilled',
      evidence: expect.objectContaining({ verificationRechecks: 3 }),
    }));
  });
});

describe('getOptOutDigest', () => {
  it('aggregates human-task + blocked cases with playbook + reason', async () => {
    listBrokerCases.mockImplementation(async ({ state } = {}) => {
      if (state === 'human_task_queued') return [{ id: 'h1', brokerId: 'wp', brokerName: 'Whitepages', state, allowedTransitions: ['submitted', 'not_found', 'human_task_queued'], reason: 'auto_submit_disabled', evidence: { optout_url: 'https://wp/o', playbook: ['call back'] } }];
      if (state === 'blocked') return [{ id: 'b1', brokerId: 'rad', brokerName: 'Radaris', state, allowedTransitions: ['found', 'not_found', 'human_task_queued'], reason: 'antibot_wall', evidence: { match_basis: 'antibot_wall' }, identity: { search_url: 'https://rad/p/Jane/Doe/' } }];
      return [];
    });
    const digest = await getOptOutDigest();
    expect(digest.total).toBe(2);
    expect(digest.humanTasks).toBe(1);
    expect(digest.blocked).toBe(1);
    expect(digest.items[0].playbook).toEqual(['call back']);
    // Server-derived legal moves pass through so the digest strip filters the
    // same as the drawer (issue #2417).
    expect(digest.items[0].allowedTransitions).toEqual(['submitted', 'not_found', 'human_task_queued']);
    expect(digest.items[1].allowedTransitions).toEqual(['found', 'not_found', 'human_task_queued']);
    // Blocked items surface the filled search URL for a manual browser check —
    // decrypted from the case's sealed identity evidence, never read from the
    // plain evidence projection (#8333).
    expect(digest.items[1].searchUrl).toBe('https://rad/p/Jane/Doe/');
    expect(digest.items[0].searchUrl).toBe(null);
    expect(listBrokerCases).toHaveBeenCalledWith(expect.objectContaining({ state: 'blocked', includeIdentity: true }));
  });
});

// ─── Consent gate (issue #3658) ─────────────────────────────────────────────
// NO CONSENT, NO ACTION — carried verbatim from unbroker. The refusal is at the
// SERVICE layer, so a cron / direct API / agent caller is refused exactly like a
// hidden UI button would be. Asserted here, NOT only in a UI test.
describe('consent gate — engine-enforced (#3658, #8332)', () => {
  // Scope-aware stand-in for the real gate: the subject holds the local-vault
  // AND read-only scan grants but NOT broker_optout — neither may unlock a
  // submission or its verification probes.
  const refuse = () => {
    assertSubjectConsentMock.mockImplementation(async (id, { scope }) => {
      if (scope === 'pii_vault' || scope === 'broker_scan') return { id };
      const err = new Error('no consent');
      err.status = 403;
      err.code = 'SUBJECT_CONSENT_REQUIRED';
      throw err;
    });
  };

  it('runOptOutPass refuses a subject without broker_optout consent, before reading the vault', async () => {
    refuse();
    const vault = await import('./privacyVault.js');
    await expect(runOptOutPass({ subjectId: 'subject-2' }))
      .rejects.toMatchObject({ code: 'SUBJECT_CONSENT_REQUIRED' });
    expect(vault.listScanEligibleValues).not.toHaveBeenCalled();
    expect(listBrokers).not.toHaveBeenCalled();
    expect(transitionCase).not.toHaveBeenCalled();
    expect(createDraft).not.toHaveBeenCalled();
  });

  it('runVerificationPass refuses a subject without broker_optout consent, before reading the ledger', async () => {
    refuse();
    await expect(runVerificationPass({ subjectId: 'subject-2' }))
      .rejects.toMatchObject({ code: 'SUBJECT_CONSENT_REQUIRED' });
    expect(listBrokerCases).not.toHaveBeenCalled();
    expect(transitionCase).not.toHaveBeenCalled();
  });

  it('passes the resolved subject through to the guard and the ledger reads', async () => {
    listBrokerCases.mockResolvedValue([]);
    await runVerificationPass({ subjectId: 'subject-2' });
    expect(assertSubjectConsentMock).toHaveBeenCalledWith('subject-2', { scope: 'broker_optout', action: 'opt-out verification pass' });
    expect(listBrokerCases).toHaveBeenCalledWith({ subjectId: 'subject-2' });
  });
});