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

10.9 KB
Raw Blame
/**
 * Pure-logic tests for the broker case state machine + recheck backoff +
 * refresh parsers (issue #2144), plus the subject-scoped case-list query shape
 * (#3658). The DB is mocked (the pure tests don't touch it); the real DB
 * round-trip lives in privacyBrokers.db.test.js.
 */

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

const { queryMock, withTransactionMock } = vi.hoisted(() => ({ queryMock: vi.fn(), withTransactionMock: vi.fn() }));
vi.mock('../lib/db.js', () => ({ query: queryMock, withTransaction: withTransactionMock }));

const SELF = '00000000-0000-4000-8000-000000000001';
vi.mock('./privacySubjects.js', () => ({ resolveSubjectId: (id) => id || SELF }));

const {
  CASE_STATES,
  SCAN_VERDICTS,
  assertTransition,
  allowedTransitionsFor,
  computeNextRecheckAt,
  parseCaRegistryCsv,
  parseBadboolList,
  listBrokerCases,
  recordScanVerdict,
  ensureSeeded,
  resetEnsureSeededForTests,
} = await import('./privacyBrokers.js');

beforeEach(() => queryMock.mockReset());

describe('ensureSeeded — syncing curated brokers on boot/upgrade (#4019)', () => {
  it('calls seedCuratedBrokers on initial ensureSeeded invocation', async () => {
    resetEnsureSeededForTests();
    await expect(ensureSeeded()).resolves.not.toThrow();
  });
});

describe('listBrokerCases — subject scoping (#3658)', () => {
  it('always scopes to a subject and binds the state filter as $2 (not a literal)', async () => {
    queryMock.mockResolvedValue({ rows: [] });
    await listBrokerCases({ state: 'found' });
    const [sql, params] = queryMock.mock.calls[0];
    expect(sql).toMatch(/WHERE c\.subject_id = \$1 AND c\.state = \$2/);
    expect(params).toEqual([SELF, 'found']);
  });

  it('scopes to an explicit subject with no state filter', async () => {
    queryMock.mockResolvedValue({ rows: [] });
    await listBrokerCases({ subjectId: 'subject-2' });
    const [sql, params] = queryMock.mock.calls[0];
    expect(sql).toMatch(/WHERE c\.subject_id = \$1\s/);
    expect(params).toEqual(['subject-2']);
  });
});

describe('assertTransition — valid paths', () => {
  it('allows every scan verdict from unscanned', () => {
    for (const to of SCAN_VERDICTS) {
      expect(() => assertTransition('unscanned', to)).not.toThrow();
    }
  });

  it('walks the opt-out lifecycle forward', () => {
    const chain = [
      ['found', 'optout_in_progress'],
      ['optout_in_progress', 'submitted'],
      ['submitted', 'verification_pending'],
      ['verification_pending', 'awaiting_processing'],
    ];
    for (const [from, to] of chain) expect(() => assertTransition(from, to)).not.toThrow();
  });

  it('allows any state → human_task_queued', () => {
    for (const from of CASE_STATES) {
      if (from === 'human_task_queued') continue;
      expect(() => assertTransition(from, 'human_task_queued')).not.toThrow();
    }
  });

  it('allows idempotent same-state re-stamp', () => {
    expect(() => assertTransition('submitted', 'submitted')).not.toThrow();
  });
});

describe('assertTransition — confirmed_removed is rescan-only', () => {
  it('rejects confirmed_removed without viaRescan even from awaiting_processing', () => {
    expect(() => assertTransition('awaiting_processing', 'confirmed_removed'))
      .toThrowError(/only reachable from a verifying re-scan/i);
  });

  it('accepts confirmed_removed from awaiting_processing WITH viaRescan', () => {
    expect(() => assertTransition('awaiting_processing', 'confirmed_removed', { viaRescan: true })).not.toThrow();
  });

  it('rejects confirmed_removed from a submission state even with viaRescan', () => {
    // A submission page can never confirm removal — only verification_pending /
    // awaiting_processing / human_task_queued may.
    expect(() => assertTransition('submitted', 'confirmed_removed', { viaRescan: true }))
      .toThrowError(/invalid transition/i);
  });

  it('rejects a bare submitted → confirmed_removed (the classic hole)', () => {
    let code;
    try { assertTransition('submitted', 'confirmed_removed'); } catch (e) { code = e.code; }
    expect(code).toBe('CONFIRMED_REQUIRES_RESCAN');
  });
});

describe('assertTransition — reappeared is confirmed_removed + rescan only', () => {
  it('accepts confirmed_removed → reappeared via rescan', () => {
    expect(() => assertTransition('confirmed_removed', 'reappeared', { viaRescan: true })).not.toThrow();
  });
  it('rejects reappeared from any other state', () => {
    expect(() => assertTransition('found', 'reappeared', { viaRescan: true })).toThrow();
  });
  it('rejects reappeared without rescan', () => {
    expect(() => assertTransition('confirmed_removed', 'reappeared')).toThrow();
  });
});

describe('assertTransition — invalid transitions rejected', () => {
  it('rejects skipping the lifecycle (found → submitted)', () => {
    expect(() => assertTransition('found', 'submitted')).toThrowError(/invalid transition/i);
  });
  it('rejects an unknown target state', () => {
    let code;
    try { assertTransition('unscanned', 'banana'); } catch (e) { code = e.code; }
    expect(code).toBe('INVALID_CASE_STATE');
  });
  it('rejects an unknown source state', () => {
    expect(() => assertTransition('bogus', 'found')).toThrowError(/unknown case state/i);
  });
});

describe('allowedTransitionsFor — client action gating (issue #2417)', () => {
  it('never returns a target assertTransition would reject as a manual move', () => {
    // The list is the authority the UI filters against, so every entry MUST be a
    // legal non-rescan transition from that state.
    for (const from of CASE_STATES) {
      for (const to of allowedTransitionsFor(from)) {
        expect(() => assertTransition(from, to)).not.toThrow();
      }
    }
  });

  it('excludes the rescan-only targets (confirmed_removed, reappeared)', () => {
    for (const from of CASE_STATES) {
      const allowed = allowedTransitionsFor(from);
      expect(allowed).not.toContain('confirmed_removed');
      expect(allowed).not.toContain('reappeared');
    }
  });

  it('offers human_task_queued from every OTHER state, never itself', () => {
    for (const from of CASE_STATES) {
      const allowed = allowedTransitionsFor(from);
      expect(allowed).not.toContain(from); // no idempotent self
      if (from !== 'human_task_queued') expect(allowed).toContain('human_task_queued');
    }
  });

  it('gives blocked its legal positive move (found) but NOT submitted (the classic hole)', () => {
    const allowed = allowedTransitionsFor('blocked');
    expect(allowed).toContain('found');
    expect(allowed).toContain('not_found');
    expect(allowed).not.toContain('submitted');
  });

  it('lets a queued human task be marked submitted or dismissed to not_found', () => {
    const allowed = allowedTransitionsFor('human_task_queued');
    expect(allowed).toContain('submitted');
    expect(allowed).toContain('not_found');
  });

  it('lets awaiting_processing transition to found and human_task_queued (#4020)', () => {
    const allowed = allowedTransitionsFor('awaiting_processing');
    expect(allowed).toContain('found');
    expect(allowed).toContain('human_task_queued');
  });

  it('returns only the any-state target for an unknown state', () => {
    expect(allowedTransitionsFor('bogus')).toEqual(['human_task_queued']);
  });
});

describe('computeNextRecheckAt — state-dependent backoff', () => {
  const now = new Date('2026-07-08T00:00:00.000Z');
  const days = (state) => (new Date(computeNextRecheckAt(state, now)) - now) / (24 * 60 * 60 * 1000);

  it('uses the documented backoffs', () => {
    expect(days('submitted')).toBe(3);
    expect(days('awaiting_processing')).toBe(7);
    expect(days('confirmed_removed')).toBe(30);
    expect(days('not_found')).toBe(60);
  });
  it('rechecks unscanned immediately (0 days)', () => {
    expect(days('unscanned')).toBe(0);
  });
  it('falls back to 14 days for an unmapped state', () => {
    expect(days('weird')).toBe(14);
  });
});

describe('parseCaRegistryCsv', () => {
  it('extracts name + url rows', () => {
    const csv = 'Business Name,Website URL\nAcme Data,https://acme.example\n"Beta, Inc.",https://beta.example';
    const out = parseCaRegistryCsv(csv);
    expect(out.length).toBe(2);
    expect(out[0]).toMatchObject({ name: 'Acme Data', source: 'ca_registry', confidence: 'auto' });
    expect(out[0].id).toBe('ca-acme-data');
    expect(out[0].urls).toEqual({ home: 'https://acme.example' });
  });
  it('returns [] on empty / headerless input', () => {
    expect(parseCaRegistryCsv('')).toEqual([]);
    expect(parseCaRegistryCsv('just one line')).toEqual([]);
  });
});

describe('parseBadboolList', () => {
  it('normalizes a JSON array of broker entries', () => {
    const out = parseBadboolList([{ name: 'Foo Search', url: 'https://foo.example' }, { id: 'bar', name: 'Bar' }]);
    expect(out.length).toBe(2);
    expect(out[0]).toMatchObject({ id: 'foo-search', name: 'Foo Search', source: 'badbool', confidence: 'auto' });
    expect(out[1].id).toBe('bar');
  });
  it('accepts a { brokers: [...] } wrapper and drops nameless rows', () => {
    const out = parseBadboolList({ brokers: [{ url: 'https://x.example' }, { name: 'Yes' }] });
    expect(out.map((b) => b.name)).toEqual(['Yes']);
  });
});

describe('recordScanVerdict — 23505 unique constraint fallback (#4021)', () => {
  it('handles unique constraint violation 23505 during concurrent insert and falls back to update branch', async () => {
    const clientMock = {
      query: vi.fn(),
    };
    withTransactionMock.mockImplementation(async (fn) => fn(clientMock));

    // 1. SELECT broker -> found
    clientMock.query.mockResolvedValueOnce({ rows: [{ id: 'spokeo' }] });
    // 2. SELECT existing case -> none found initially
    clientMock.query.mockResolvedValueOnce({ rows: [] });
    // 3. SAVEPOINT -> ok
    clientMock.query.mockResolvedValueOnce({});
    // 4. INSERT -> throws Postgres error 23505
    const err23505 = new Error('duplicate key value violates unique constraint "idx_privacy_broker_cases_broker_subject"');
    err23505.code = '23505';
    clientMock.query.mockRejectedValueOnce(err23505);
    // 5. ROLLBACK TO SAVEPOINT -> ok
    clientMock.query.mockResolvedValueOnce({});
    // 6. Re-SELECT existing case FOR UPDATE -> now found (inserted by concurrent call)
    clientMock.query.mockResolvedValueOnce({ rows: [{ id: 'case-123', state: 'unscanned' }] });
    // 7. UPDATE case -> success
    const mockCaseRow = {
      id: 'case-123',
      subject_id: SELF,
      broker_id: 'spokeo',
      state: 'found',
      found: true,
      evidence: '{}',
      disclosed_fields: [],
      channel: null,
      reason: null,
      next_recheck_at: new Date().toISOString(),
      created_at: new Date().toISOString(),
      updated_at: new Date().toISOString(),
    };
    clientMock.query.mockResolvedValueOnce({ rows: [mockCaseRow] });

    const result = await recordScanVerdict('spokeo', 'found');
    expect(result).toBeDefined();
    expect(result.id).toBe('case-123');
    expect(result.state).toBe('found');
  });
});