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

9.2 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { runInNewContext } from 'node:vm';
const mocks = vi.hoisted(() => ({ account: vi.fn(), message: vi.fn(), evaluate: vi.fn(), write: vi.fn(), trash: vi.fn(), modify: vi.fn(), correction: vi.fn() }));
vi.mock('./messageAccounts.js', () => ({ getAccount: mocks.account }));
vi.mock('./messageSync.js', () => ({ getMessage: mocks.message, removeMessageFromCache: mocks.write }));
vi.mock('./messagePlaywrightSync.js', () => ({ findOrOpenPage: async () => ({}), getPages: async () => [], isAuthPage: () => false, evaluateOnPage: mocks.evaluate }));
vi.mock('./messageTriageRules.js', () => ({ recordCorrection: mocks.correction }));
vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => ({}), GOOGLE_API_TIMEOUT_MS: 30_000 }));
vi.mock('./gmailApi.js', () => ({ createGmailApi: () => ({ trashMessage: mocks.trash, modifyMessage: mocks.modify }) }));
import { executeAction } from './messageActions.js';
const accountId = '11111111-1111-1111-1111-111111111111';
const message = { id: 'requested', subject: 'Status update', from: { name: 'Example Sender', email: '[email protected]' }, date: '2026-09-26 10:30' };

// Execute the actual CDP script against a synthetic provider DOM, including the
// production field reader, click, and confirmation loop. No mailbox/network I/O.
function mailbox(records, { remove = true, scroll = false, missingControl = false, pageScroll = false } = {}) {
  const attr = values => ({ getAttribute: name => values[name] ?? null });
  const span = (text, title) => ({ ...attr({ title }), textContent: text });
  const list = { ...attr({}), isConnected: true, parentElement: pageScroll ? { scrollHeight: 100, clientHeight: 10 } : null, scrollHeight: scroll ? 100 : 0, clientHeight: 0 };
  const clicked = [];
  let visible;
  visible = records.map((record, index) => {
    const row = {
      ...attr({ 'data-itemid': record.providerRowId, 'data-legacy-message-id': record.providerRowId }),
      isConnected: true, matches: () => !!record.providerRowId,
      querySelector(selector) {
        if (selector === 'div[aria-label="Select a conversation"] > span[aria-label]') return attr({ 'aria-label': record.from?.name });
        if (selector === 'div[aria-label="Select a conversation"]') return { parentElement: { nextElementSibling: { children: [
          { querySelector: () => attr({ title: record.from?.email }) },
          { querySelectorAll: () => [span(record.subject), span(record.date, record.date)] }, span('Preview')
        ] } } };
        if (selector === '[email]') return { ...attr({ email: record.from?.email, name: record.from?.name }), textContent: record.from?.name };
        if (selector === 'td.xW [title]') return attr({ title: record.date });
        if (selector === '.bog') return span(record.subject);
        return null;
      },
      querySelectorAll: () => missingControl ? [] : ['Archive', 'Delete'].map(label => ({ ...attr({ 'aria-label': label }), click() { clicked.push({ index, label }); if (remove) visible = visible.filter(r => r !== row); } }))
    };
    return row;
  });
  list.querySelectorAll = () => visible;
  mocks.evaluate.mockImplementation((_page, script) => runInNewContext(script, {
    document: { querySelector: () => list }, location: { href: 'https://example.com/inbox' }, setTimeout: callback => callback()
  }));
  return clicked;
}
beforeEach(() => { vi.clearAllMocks(); mocks.message.mockResolvedValue({ ...message }); mocks.correction.mockResolvedValue(undefined); });
for (const provider of ['gmail', 'outlook']) describe(`${provider} browser identity`, () => {
  beforeEach(() => mocks.account.mockResolvedValue({ type: provider }));
  it('selects the stable ID despite duplicate blank subjects and removes only its cache record', async () => {
    mocks.message.mockResolvedValue({ ...message, subject: '', providerRowId: 'second' });
    const clicked = mailbox([{ ...message, subject: '', providerRowId: 'first' }, { ...message, subject: '', providerRowId: 'second' }]);
    expect(await executeAction(accountId, message.id, 'delete')).toMatchObject({ success: true, messageId: message.id });
    expect(clicked).toEqual([{ index: 1, label: 'Delete' }]);
    expect(mocks.write).toHaveBeenCalledWith(accountId, 'requested');
  });
  it('requires exact legacy identity instead of a subject substring or first row', async () => {
    const clicked = mailbox([{ ...message, subject: 'Re: Status update' }, { ...message, from: { ...message.from, email: '[email protected]' } }, message]);
    await executeAction(accountId, message.id, 'archive');
    expect(clicked).toEqual([{ index: 2, label: 'Archive' }]);
  });
  it('allows a complete non-scrolling inbox inside a scrollable page', async () => {
    const clicked = mailbox([message], { pageScroll: true });
    await executeAction(accountId, message.id, 'archive');
    expect(clicked).toEqual([{ index: 0, label: 'Archive' }]);
  });
  it('matches a cached ISO timestamp to an equivalent complete provider timestamp', async () => {
    mocks.message.mockResolvedValue({ ...message, date: '2026-09-26T10:30:00.000Z' });
    const clicked = mailbox([{ ...message, date: 'Sat, 26 Sep 2026 10:30:00 GMT' }]);
    await executeAction(accountId, message.id, 'archive');
    expect(clicked).toEqual([{ index: 0, label: 'Archive' }]);
  });
  it.each(['duplicate', 'blank', 'missing-date', 'missing-sender', 'missing-id', 'virtualized'])('rejects %s identity without action or cache writes', async scenario => {
    const target = { ...message };
    if (scenario === 'blank') target.subject = '';
    if (scenario === 'missing-date') target.date = '';
    if (scenario === 'missing-sender') target.from = {};
    if (scenario === 'missing-id') target.providerRowId = 'absent';
    mocks.message.mockResolvedValue(target);
    const clicked = mailbox(scenario === 'duplicate' ? [target, target] : [message], { scroll: scenario === 'virtualized' });
    await expect(executeAction(accountId, message.id, 'delete')).rejects.toMatchObject({ status: 409, code: 'MESSAGE_IDENTITY_CONFLICT' });
    expect(clicked).toEqual([]); expect(mocks.write).not.toHaveBeenCalled(); expect(mocks.correction).not.toHaveBeenCalled();
  });
  it('keeps cache when the row remains or CDP returns no confirmation', async () => {
    mailbox([message], { remove: false });
    await expect(executeAction(accountId, message.id, 'archive')).rejects.toMatchObject({ code: 'MESSAGE_ACTION_UNCONFIRMED' });
    mocks.evaluate.mockResolvedValue(null);
    await expect(executeAction(accountId, message.id, 'archive')).rejects.toMatchObject({ code: 'MESSAGE_ACTION_UNCONFIRMED' });
    expect(mocks.write).not.toHaveBeenCalled();
  });
  it('never uses a global action when the target lacks its own control', async () => {
    const clicked = mailbox([message], { missingControl: true });
    await expect(executeAction(accountId, message.id, 'delete')).rejects.toMatchObject({ code: 'MESSAGE_IDENTITY_CONFLICT' });
    expect(clicked).toEqual([]);
  });
});
it('preserves Gmail API-by-ID archive and delete without browser calls', async () => {
  mocks.account.mockResolvedValue({ type: 'gmail' }); mocks.message.mockResolvedValue({ ...message, apiId: 'api-target' });
  await executeAction(accountId, message.id, 'delete'); await executeAction(accountId, message.id, 'archive');
  expect(mocks.trash).toHaveBeenCalledWith('api-target');
  expect(mocks.modify).toHaveBeenCalledWith('api-target', { removeLabelIds: ['INBOX'] });
  expect(mocks.evaluate).not.toHaveBeenCalled();
});

describe('executeAction caller-error statuses', () => {
  beforeEach(() => { mocks.account.mockReset(); mocks.message.mockReset(); });
  it('answers a stale message or account with 404 NOT_FOUND', async () => {
    mocks.account.mockResolvedValueOnce({ id: accountId, type: 'gmail' });
    mocks.message.mockResolvedValueOnce(null);
    await expect(executeAction(accountId, 'gone', 'archive')).rejects.toMatchObject({ status: 404, code: 'NOT_FOUND', message: 'Message not found' });
    mocks.account.mockResolvedValueOnce(null);
    await expect(executeAction(accountId, 'gone', 'archive')).rejects.toMatchObject({ status: 404, code: 'NOT_FOUND', message: 'Account not found' });
  });
  it('answers a bad action with 400', async () => {
    await expect(executeAction(accountId, 'm', 'explode')).rejects.toMatchObject({ status: 400 });
  });
});

// Regression: a correction write failure cannot turn a completed provider action
// into a failed request that invites a duplicate archive/delete.
it('keeps a confirmed provider action successful when triage persistence fails', async () => {
  const log = vi.spyOn(console, 'error').mockImplementation(() => {});
  mocks.account.mockResolvedValue({ type: 'gmail' });
  mocks.message.mockResolvedValue({ ...message, apiId: 'api-target', evaluation: { action: 'keep' } });
  mocks.correction.mockRejectedValue(new Error('Synthetic sensitive diagnostic'));
  expect(await executeAction(accountId, message.id, 'archive')).toMatchObject({ success: true });
  expect(mocks.modify).toHaveBeenCalledTimes(1);
  expect(mocks.correction).toHaveBeenCalledTimes(1);
  expect(mocks.write).toHaveBeenCalledWith(accountId, message.id);
  expect(log).toHaveBeenCalledWith('❌ Message triage correction could not be persisted');
  log.mockRestore();
});