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

62.1 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { createHash } from 'crypto';

// Mock fs/promises before importing the module
vi.mock('fs/promises', () => ({
  writeFile: vi.fn(),
  readdir: vi.fn(),
  unlink: vi.fn()
}));

vi.mock('../lib/fileUtils.js', () => {
  const tryReadFile = vi.fn();
  return {
  tryReadFile,
  // Strict JSON reads (selectors) are driven from the same bytes mock.
  readJSONFileStrict: async (path, fallback) => {
    const raw = await tryReadFile(path);
    if (raw == null) return { ok: true, value: fallback };
    try { return { ok: true, value: JSON.parse(raw) }; } catch { return { ok: false, value: fallback }; }
  },
  unreadableStoreError: (path) => new Error(`Unreadable JSON file: ${path}`),
  atomicWrite: vi.fn().mockResolvedValue(undefined),
  ensureDir: vi.fn(),
  PATHS: { messages: '/mock/data/messages' },
  safeJSONParse: vi.fn((content, fallback) => {
    if (!content) return fallback;
    const parsed = JSON.parse(content);
    return parsed;
  }),
  UUID_RE: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
  safeDate: (d) => { const t = new Date(d).getTime(); return Number.isNaN(t) ? 0 : t; },
  filterBySearch: (items, search, fields) => {
    if (!search) return items;
    const q = search.toLowerCase();
    return items.filter(item =>
      fields.some(field => {
        const val = field.includes('.') ? field.split('.').reduce((o, k) => o?.[k], item) : item[field];
        return val?.toLowerCase?.().includes(q);
      })
    );
  }
  };
});

vi.mock('./messageAccounts.js', () => ({
  getAccount: vi.fn(),
  updateSyncStatus: vi.fn(),
  markSentIngested: vi.fn(() => Promise.resolve()),
  updateSendAsAliases: vi.fn(() => Promise.resolve())
}));

const gmailApi = vi.hoisted(() => ({ list: vi.fn(), get: vi.fn() }));
vi.mock('./gmailApi.js', () => ({
  createGmailApi: () => ({
    listMessages: gmailApi.list,
    getMessage: gmailApi.get,
    listSendAs: async () => ({ sendAs: [] }),
  }),
}));
vi.mock('./googleAuth.js', () => ({ getAuthenticatedClient: async () => ({}), GOOGLE_API_TIMEOUT_MS: 30_000 }));

vi.mock('./messageGmailSync.js', () => ({
  syncGmail: vi.fn()
}));

const browser = vi.hoisted(() => ({ findOrOpenPage: vi.fn(), evaluateOnPage: vi.fn(), isAuthPage: vi.fn(() => false), listCdpPages: vi.fn() }));
vi.mock('./browserService.js', () => browser);

vi.mock('./messagePlaywrightSync.js', () => ({
  syncPlaywright: vi.fn(),
  refreshMessageDetail: vi.fn()
}));

// Keep provider fallback and secondary activity writes inside the fixture;
// these sync tests must never reach browser credentials or a persistent store.
vi.mock('./messageApiSync.js', () => ({
  syncOutlookApi: vi.fn(async () => null),
}));
vi.mock('./humanActivity.js', async (importOriginal) => ({
  ...await importOriginal(),
  recordEvents: vi.fn(async () => ({ recorded: 0, skipped: 0 })),
  stripParticipantsForAccount: vi.fn(async () => 0),
}));

// tribe.js is loaded dynamically by logMessageTouchpoints; mock it so the
// producer test asserts the candidates without a live Postgres.
vi.mock('./tribe.js', () => ({
  autoLogTouchpoints: vi.fn().mockResolvedValue({ created: 0, matched: 0 }),
}));

// The dedupe day is derived in the user's LOCAL timezone. Mock the tz module
// self-contained (getUserTimezone → mockTimezone, default UTC so the existing
// UTC-day assertions hold; getLocalParts is a faithful Intl-based re-impl of the
// real one) — importActual would load the real module against the mocked
// fileUtils and fail on its settings read.
let mockTimezone = 'UTC';
vi.mock('../lib/timezone.js', () => ({
  getLocalParts: (utcDate, timezone) => {
    const parts = {};
    const fmt = new Intl.DateTimeFormat('en-US', {
      timeZone: timezone, year: 'numeric', month: '2-digit', day: '2-digit',
      hour: '2-digit', minute: '2-digit', hour12: false,
    });
    for (const { type, value } of fmt.formatToParts(utcDate)) parts[type] = value;
    return {
      year: parseInt(parts.year), month: parseInt(parts.month), day: parseInt(parts.day),
      hour: parts.hour === '24' ? 0 : parseInt(parts.hour), minute: parseInt(parts.minute),
    };
  },
}));
vi.mock('./userTimezone.js', () => ({
  getUserTimezone: vi.fn(async () => mockTimezone),
}));

import { readdir, unlink } from 'fs/promises';
import { tryReadFile as readFile, atomicWrite } from '../lib/fileUtils.js';
import { getMessages, getMessage, removeMessageFromCache, syncAccount, deleteCache, getSyncStatus, refreshMessage, refreshMessages, updateMessageEvaluations, logMessageTouchpoints, aggregatePagedMessages } from './messageSync.js';
import { autoLogTouchpoints } from './tribe.js';
import { getAccount, updateSyncStatus, markSentIngested } from './messageAccounts.js';
import { syncGmail } from './messageGmailSync.js';
import { syncPlaywright, refreshMessageDetail } from './messagePlaywrightSync.js';

const VALID_UUID = '11111111-1111-1111-1111-111111111111';
const VALID_UUID_2 = '22222222-2222-2222-2222-222222222222';

beforeEach(() => {
  vi.clearAllMocks();
  // Default: cache file not found — tryReadFile returns null on read error
  readFile.mockResolvedValue(null);
});

// ─── Cache I/O: getMessages ───

describe('getMessages', () => {
  it.each([true, false])('selects pending records before paging and advances the backlog (account=%s)', async (singleAccount) => {
    const caches = new Map([VALID_UUID, VALID_UUID_2].map((id, accountIndex) => [id, {
      messages: Array.from({ length: 101 }, (_, i) => ({
        id: `${accountIndex}-message-${i}`,
        date: new Date(Date.UTC(2026, 0, 1) - i * 1000).toISOString(),
        ...(i < 50 ? { evaluation: { score: 1 } } : {})
      }))
    }]));
    readdir.mockResolvedValue([...caches.keys()].map(id => `${id}.json`));
    readFile.mockImplementation(async path => JSON.stringify(caches.get([...caches.keys()].find(id => path.endsWith(`${id}.json`)))));
    atomicWrite.mockImplementation(async (path, cache) => { caches.set([...caches.keys()].find(id => path.endsWith(`${id}.json`)), cache); });
    const options = singleAccount ? { accountId: VALID_UUID } : {};
    const seen = new Set();
    for (let remaining = singleAccount ? 51 : 102; remaining > 0; remaining -= 20) {
      const page = await getMessages({ ...options, unevaluatedOnly: true, limit: 20 });
      expect(page.total).toBe(remaining);
      expect(page.messages).toHaveLength(Math.min(20, remaining));
      for (const message of page.messages) {
        expect(seen.has(message.id)).toBe(false);
        seen.add(message.id);
      }
      await updateMessageEvaluations(Object.fromEntries(page.messages.map(m => [m.id, { score: 2 }])));
    }
    expect((await getMessages({ ...options, unevaluatedOnly: true, limit: 20 })).messages).toEqual([]);
    const explicit = await getMessages({ ...options, messageIds: ['0-message-100', 'absent'], limit: 100 });
    expect(explicit.messages.map(m => m.id)).toEqual(['0-message-100']);
  });

  it('should return empty messages when cache file does not exist', async () => {
    const result = await getMessages({ accountId: VALID_UUID });
    expect(result.messages).toEqual([]);
    expect(result.total).toBe(0);
  });

  it('should load and return messages from cache for a specific account', async () => {
    const cache = {
      syncCursor: 'cur-1',
      messages: [
        { id: 'msg-1', subject: 'Hello', date: '2026-01-02T00:00:00Z', externalId: 'ext-1' },
        { id: 'msg-2', subject: 'World', date: '2026-01-01T00:00:00Z', externalId: 'ext-2' }
      ]
    };
    readFile.mockResolvedValue(JSON.stringify(cache));

    const result = await getMessages({ accountId: VALID_UUID });

    expect(result.messages).toHaveLength(2);
    expect(result.total).toBe(2);
    // Should be sorted newest first
    expect(result.messages[0].subject).toBe('Hello');
    expect(result.messages[1].subject).toBe('World');
  });

  it('should stamp accountId onto messages', async () => {
    const cache = { messages: [{ id: 'msg-1', subject: 'Test' }] };
    readFile.mockResolvedValue(JSON.stringify(cache));

    const result = await getMessages({ accountId: VALID_UUID });
    expect(result.messages[0].accountId).toBe(VALID_UUID);
  });

  it('should apply search filter', async () => {
    const cache = {
      messages: [
        { id: 'msg-1', subject: 'Meeting Notes', date: '2026-01-01' },
        { id: 'msg-2', subject: 'Invoice', date: '2026-01-01' }
      ]
    };
    readFile.mockResolvedValue(JSON.stringify(cache));

    const result = await getMessages({ accountId: VALID_UUID, search: 'meeting' });
    expect(result.messages).toHaveLength(1);
    expect(result.total).toBe(1);
  });

  it('should filter by from.name', async () => {
    const cache = {
      messages: [
        { id: 'msg-1', from: { name: 'Alice' }, date: '2026-01-01' },
        { id: 'msg-2', from: { name: 'Bob' }, date: '2026-01-01' }
      ]
    };
    readFile.mockResolvedValue(JSON.stringify(cache));

    const result = await getMessages({ accountId: VALID_UUID, search: 'alice' });
    expect(result.messages).toHaveLength(1);
    expect(result.messages[0].id).toBe('msg-1');
  });

  it('should filter by from.email', async () => {
    const cache = {
      messages: [
        { id: 'msg-1', from: { email: '[email protected]' }, date: '2026-01-01' },
        { id: 'msg-2', from: { email: '[email protected]' }, date: '2026-01-01' }
      ]
    };
    readFile.mockResolvedValue(JSON.stringify(cache));

    const result = await getMessages({ accountId: VALID_UUID, search: 'alice@' });
    expect(result.messages).toHaveLength(1);
    expect(result.messages[0].id).toBe('msg-1');
  });

  it('should filter by bodyText', async () => {
    const cache = {
      messages: [
        { id: 'msg-1', bodyText: 'Hello world', date: '2026-01-01' },
        { id: 'msg-2', bodyText: 'Goodbye', date: '2026-01-01' }
      ]
    };
    readFile.mockResolvedValue(JSON.stringify(cache));

    const result = await getMessages({ accountId: VALID_UUID, search: 'hello' });
    expect(result.messages).toHaveLength(1);
    expect(result.messages[0].id).toBe('msg-1');
  });

  it('should handle search with missing fields gracefully', async () => {
    const cache = {
      messages: [
        { id: 'msg-1', date: '2026-01-01' },
        { id: 'msg-2', subject: 'Test', date: '2026-01-01' }
      ]
    };
    readFile.mockResolvedValue(JSON.stringify(cache));

    const result = await getMessages({ accountId: VALID_UUID, search: 'test' });
    expect(result.messages).toHaveLength(1);
    expect(result.messages[0].id).toBe('msg-2');
  });

  it('should sort messages with invalid dates to the end', async () => {
    const cache = {
      messages: [
        { id: 'msg-bad', subject: 'Bad Date', date: 'not-a-date' },
        { id: 'msg-good', subject: 'Good Date', date: '2026-01-15T10:00:00Z' },
        { id: 'msg-null', subject: 'Null Date' }
      ]
    };
    readFile.mockResolvedValue(JSON.stringify(cache));

    const result = await getMessages({ accountId: VALID_UUID });
    expect(result.messages).toHaveLength(3);
    // Valid date should come first (newest first), invalid dates sort to end (timestamp 0)
    expect(result.messages[0].id).toBe('msg-good');
  });

  it('should apply offset and limit', async () => {
    const msgs = Array.from({ length: 10 }, (_, i) => ({
      id: `msg-${i}`, subject: `Msg ${i}`, date: `2026-01-${String(i + 1).padStart(2, '0')}`
    }));
    readFile.mockResolvedValue(JSON.stringify({ messages: msgs }));

    const result = await getMessages({ accountId: VALID_UUID, limit: 3, offset: 2 });
    expect(result.messages).toHaveLength(3);
    expect(result.total).toBe(10);
  });

  it('should aggregate across all account caches when no accountId', async () => {
    readdir.mockResolvedValue([`${VALID_UUID}.json`, `${VALID_UUID_2}.json`, 'not-uuid.json']);
    readFile.mockImplementation((filePath) => {
      if (filePath.includes(VALID_UUID_2)) {
        return Promise.resolve(JSON.stringify({
          messages: [{ id: 'msg-b', subject: 'From B', date: '2026-01-02' }]
        }));
      }
      return Promise.resolve(JSON.stringify({
        messages: [{ id: 'msg-a', subject: 'From A', date: '2026-01-01' }]
      }));
    });

    const result = await getMessages({});
    expect(result.total).toBe(2);
    // Newest first
    expect(result.messages[0].id).toBe('msg-b');
  });

  it('should return empty when readdir fails (no cache dir)', async () => {
    readdir.mockRejectedValue(new Error('ENOENT'));

    const result = await getMessages({});
    expect(result.messages).toEqual([]);
    expect(result.total).toBe(0);
  });
});

// ─── aggregatePagedMessages (bounded multi-account merge, #2540) ───

describe('aggregatePagedMessages', () => {
  // Build N accounts each with `perAccount` dated messages (newest = highest index).
  const buildCaches = (n, perAccount) =>
    Array.from({ length: n }, (_, a) => ({
      id: `acct-${a}`,
      cache: {
        messages: Array.from({ length: perAccount }, (_, i) => ({
          id: `a${a}-m${i}`,
          subject: `Acct ${a} Msg ${i}`,
          date: `2026-01-${String(i + 1).padStart(2, '0')}T00:00:00Z`,
        })),
      },
    }));

  it('returns the global date-sorted page and the exact total across accounts', () => {
    const caches = buildCaches(3, 5); // 15 messages total
    const { messages, total } = aggregatePagedMessages(caches, { limit: 4, offset: 0 });
    expect(total).toBe(15);
    expect(messages).toHaveLength(4);
    // Newest date is m4 (2026-01-05) present in all 3 accounts → top of the page.
    expect(messages[0].date).toBe('2026-01-05T00:00:00Z');
    // Sorted strictly newest-first.
    const dates = messages.map((m) => m.date);
    expect([...dates].sort().reverse()).toEqual(dates);
  });

  it('matches a full unbounded aggregate for an across-account page (offset+limit)', () => {
    const caches = buildCaches(4, 8);
    // Reference: spread everything, global sort, slice — the pre-#2540 behavior.
    const all = caches.flatMap(({ id, cache }) =>
      cache.messages.map((m) => ({ ...m, accountId: m.accountId || id })));
    all.sort((a, b) => new Date(b.date) - new Date(a.date));
    const ref = all.slice(3, 3 + 5).map((m) => m.id);
    const { messages, total } = aggregatePagedMessages(caches, { limit: 5, offset: 3 });
    expect(total).toBe(32);
    expect(messages.map((m) => m.id)).toEqual(ref);
  });

  it('stamps accountId from the owning cache when the message lacks one', () => {
    const caches = [{ id: 'acct-x', cache: { messages: [{ id: 'm1', date: '2026-01-01' }] } }];
    const { messages } = aggregatePagedMessages(caches, { limit: 10, offset: 0 });
    expect(messages[0].accountId).toBe('acct-x');
  });

  it('preserves a message-level accountId over the cache id', () => {
    const caches = [{ id: 'acct-x', cache: { messages: [{ id: 'm1', date: '2026-01-01', accountId: 'orig' }] } }];
    const { messages } = aggregatePagedMessages(caches, { limit: 10, offset: 0 });
    expect(messages[0].accountId).toBe('orig');
  });

  it('applies the search filter and reports the filtered total', () => {
    const caches = [
      { id: 'a', cache: { messages: [
        { id: 'm1', subject: 'Invoice', date: '2026-01-02' },
        { id: 'm2', subject: 'Lunch', date: '2026-01-01' },
      ] } },
      { id: 'b', cache: { messages: [{ id: 'm3', subject: 'Invoice due', date: '2026-01-03' }] } },
    ];
    const { messages, total } = aggregatePagedMessages(caches, { search: 'invoice', limit: 10, offset: 0 });
    expect(total).toBe(2);
    expect(messages.map((m) => m.id)).toEqual(['m3', 'm1']);
  });

  it('returns total but no messages when limit is 0', () => {
    const caches = buildCaches(2, 3);
    const { messages, total } = aggregatePagedMessages(caches, { limit: 0, offset: 0 });
    expect(total).toBe(6);
    expect(messages).toEqual([]);
  });
});

// ─── getMessage ───

describe('getMessage', () => {
  it('should return a specific message by id', async () => {
    const cache = {
      messages: [
        { id: 'msg-1', subject: 'Hello' },
        { id: 'msg-2', subject: 'World' }
      ]
    };
    readFile.mockResolvedValue(JSON.stringify(cache));

    const result = await getMessage(VALID_UUID, 'msg-2');
    expect(result.subject).toBe('World');
    expect(result.accountId).toBe(VALID_UUID);
  });

  it('should return null when message not found', async () => {
    readFile.mockResolvedValue(JSON.stringify({ messages: [] }));
    const result = await getMessage(VALID_UUID, 'nonexistent');
    expect(result).toBeNull();
  });
});

// ─── deleteCache ───

describe('deleteCache', () => {
  it('should call unlink for valid accountId', async () => {
    unlink.mockResolvedValue();
    await deleteCache(VALID_UUID);
    expect(unlink).toHaveBeenCalledWith(expect.stringContaining(`${VALID_UUID}.json`));
  });

  it('waits for an in-flight cache writer before erasing its result', async () => {
    const entered = Promise.withResolvers();
    const release = Promise.withResolvers();
    let stored = { messages: [{ id: 'msg-1' }] };
    readFile.mockImplementation(async () => stored ? JSON.stringify(stored) : null);
    readdir.mockResolvedValue([`${VALID_UUID}.json`]);
    atomicWrite.mockImplementationOnce(async (_path, data) => {
      entered.resolve();
      await release.promise;
      stored = data;
    });
    unlink.mockImplementationOnce(async () => { stored = null; });

    const writer = updateMessageEvaluations({ 'msg-1': { score: 9 } });
    await entered.promise;
    const cleanup = deleteCache(VALID_UUID);
    expect(stored).not.toBeNull();
    release.resolve();
    await Promise.all([writer, cleanup]);
    expect(stored).toBeNull();
    expect(await getMessages({ accountId: VALID_UUID })).toEqual({ messages: [], total: 0 });
  });

  it('rejects a sync with a stale enabled-account read after cleanup', async () => {
    const entered = Promise.withResolvers();
    const release = Promise.withResolvers();
    getAccount.mockImplementationOnce(async () => {
      entered.resolve();
      await release.promise;
      return { id: VALID_UUID, name: 'Example', type: 'gmail', enabled: true };
    }).mockResolvedValue(null);
    unlink.mockResolvedValue();
    const syncing = syncAccount(VALID_UUID);
    await entered.promise;
    await deleteCache(VALID_UUID);
    release.resolve();

    expect(await syncing).toEqual({ error: 'Account not found' });
    expect(syncGmail).not.toHaveBeenCalled();
    expect(atomicWrite).not.toHaveBeenCalled();
  });

  it('should silently skip invalid accountId', async () => {
    await deleteCache('not-a-uuid');
    expect(unlink).not.toHaveBeenCalled();
  });
});

// ─── syncAccount ───

describe('syncAccount', () => {
  const mockIo = { emit: vi.fn() };

  beforeEach(() => {
    mockIo.emit.mockClear();
  });

  it('should return 400 when account is disabled', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Test', type: 'gmail', enabled: false });

    const result = await syncAccount(VALID_UUID, mockIo);

    expect(result).toEqual({ error: 'Account is disabled', status: 400 });
    expect(mockIo.emit).not.toHaveBeenCalled();
  });

  it('should return error when account not found', async () => {
    getAccount.mockResolvedValue(null);

    const result = await syncAccount(VALID_UUID, mockIo);

    expect(result).toEqual({ error: 'Account not found' });
  });

  it('should call syncGmail for gmail accounts and save cache', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ syncCursor: null, messages: [] }));
    syncGmail.mockResolvedValue([{ id: 'msg-1', externalId: 'ext-1', date: '2026-01-01' }]);
    updateSyncStatus.mockResolvedValue();

    const result = await syncAccount(VALID_UUID, mockIo);

    expect(syncGmail).toHaveBeenCalled();
    expect(atomicWrite).toHaveBeenCalled();
    expect(result.newMessages).toBe(1);
    expect(result.total).toBe(1);
    expect(result.status).toBe('success');
    expect(updateSyncStatus).toHaveBeenCalledWith(VALID_UUID, 'success');
    expect(mockIo.emit).toHaveBeenCalledWith('messages:sync:started', { accountId: VALID_UUID, mode: 'unread' });
    expect(mockIo.emit).toHaveBeenCalledWith('messages:sync:completed', expect.objectContaining({ accountId: VALID_UUID, newMessages: 1 }));
    expect(mockIo.emit).toHaveBeenCalledWith('messages:changed', {});
  });

  it('releases the account lock after a Gmail request timeout', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ syncCursor: null, messages: [] }));
    syncGmail.mockRejectedValueOnce(new Error('Google API request timed out'))
      .mockResolvedValueOnce({ messages: [], status: 'success' });

    await expect(syncAccount(VALID_UUID, mockIo)).resolves.toMatchObject({
      error: 'Google API request timed out', status: 502,
    });
    await expect(syncAccount(VALID_UUID, mockIo)).resolves.toMatchObject({ status: 'success' });
    expect(syncGmail).toHaveBeenCalledTimes(2);
  });

  it('should call syncPlaywright for outlook accounts with mode', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Outlook', type: 'outlook', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ syncCursor: null, messages: [] }));
    syncPlaywright.mockResolvedValue([]);
    updateSyncStatus.mockResolvedValue();

    const result = await syncAccount(VALID_UUID, mockIo, { mode: 'full' });

    expect(syncPlaywright).toHaveBeenCalledWith(
      expect.objectContaining({ type: 'outlook' }),
      expect.any(Object),
      mockIo,
      { mode: 'full' }
    );
    expect(result.newMessages).toBe(0);
  });

  it('retains actionable browser selector failures in the HTTP result and socket completion', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Outlook', type: 'outlook', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [{ id: 'existing', externalId: 'existing-row' }] }));
    const reason = 'Invalid Outlook messageRow selector. Edit it in Messages > Sync and test again.';
    syncPlaywright.mockResolvedValue({ messages: [], status: 'extraction-failed', error: reason });
    updateSyncStatus.mockResolvedValue();

    expect(await syncAccount(VALID_UUID, mockIo, { mode: 'full' })).toMatchObject({
      status: 'extraction-failed', reason, total: 1, pruned: 0
    });
    expect(mockIo.emit).toHaveBeenCalledWith('messages:sync:completed', expect.objectContaining({ status: 'extraction-failed', reason }));
    expect(mockIo.emit).not.toHaveBeenCalledWith('messages:changed', {});
  });

  it('selects the supported full mode for a Teams sync that omits the mode', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Teams', type: 'teams', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ syncCursor: null, messages: [] }));
    syncPlaywright.mockResolvedValue([]);
    updateSyncStatus.mockResolvedValue();

    await syncAccount(VALID_UUID, mockIo);

    expect(syncPlaywright).toHaveBeenCalledWith(expect.objectContaining({ type: 'teams' }), expect.any(Object), mockIo, { mode: 'full' });
    expect(mockIo.emit).toHaveBeenCalledWith('messages:sync:started', { accountId: VALID_UUID, mode: 'full' });
  });

  it('rejects an explicit Teams unread sync before any browser dispatch or cache write', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Teams', type: 'teams', enabled: true });
    syncPlaywright.mockClear();
    atomicWrite.mockClear();
    readFile.mockClear();

    const result = await syncAccount(VALID_UUID, mockIo, { mode: 'unread' });

    expect(result).toMatchObject({ status: 400, error: expect.stringContaining('unread') });
    expect(syncPlaywright).not.toHaveBeenCalled();
    expect(readFile).not.toHaveBeenCalled();
    expect(atomicWrite).not.toHaveBeenCalled();
    expect(mockIo.emit).not.toHaveBeenCalled();
    // The lock was never taken, so a following supported sync is not blocked.
    syncPlaywright.mockResolvedValue([]);
    readFile.mockResolvedValue(JSON.stringify({ syncCursor: null, messages: [] }));
    updateSyncStatus.mockResolvedValue();
    expect(await syncAccount(VALID_UUID, mockIo, { mode: 'full' })).toMatchObject({ status: 'success' });
  });

  it('overwrites a legacy fabricated-read Teams record with unknown read state', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Teams', type: 'teams', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ syncCursor: null, messages: [
      { id: 'legacy', externalId: 'teams-ext', date: '2026-01-01', isRead: true, isUnread: false }
    ] }));
    syncPlaywright.mockResolvedValue([{ id: 'new', externalId: 'teams-ext', date: '2026-01-01', isRead: null, isUnread: null }]);
    updateSyncStatus.mockResolvedValue();
    atomicWrite.mockClear();

    await syncAccount(VALID_UUID, mockIo);

    expect(atomicWrite.mock.calls[0][1].messages).toEqual([
      expect.objectContaining({ id: 'legacy', isRead: null, isUnread: null })
    ]);
  });

  it('removes only the confirmed action target from the current cache', async () => {
    readFile.mockResolvedValue(JSON.stringify({ syncCursor: 'keep-cursor', messages: [{ id: 'target' }, { id: 'other' }] }));
    await removeMessageFromCache(VALID_UUID, 'target');
    expect(atomicWrite.mock.calls[0][1]).toEqual({ syncCursor: 'keep-cursor', messages: [{ id: 'other' }] });
  });

  it('should deduplicate by externalId during sync', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    const existingCache = {
      syncCursor: 'cur-1',
      messages: [{ id: 'msg-1', externalId: 'ext-1', date: '2026-01-01' }]
    };
    readFile.mockResolvedValue(JSON.stringify(existingCache));
    // Provider returns one duplicate and one new
    syncGmail.mockResolvedValue([
      { id: 'msg-1-dup', externalId: 'ext-1', date: '2026-01-01' },
      { id: 'msg-2', externalId: 'ext-2', date: '2026-01-02' }
    ]);
    updateSyncStatus.mockResolvedValue();

    const result = await syncAccount(VALID_UUID, mockIo);

    expect(result.newMessages).toBe(1);
    expect(result.total).toBe(2); // 1 existing + 1 new
    // Verify saved cache has 2 messages
    const savedData = atomicWrite.mock.calls[0][1];
    expect(savedData.messages).toHaveLength(2);
  });

  it('keeps provider row identities separate when summary hashes collide', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [{ id: 'local-first', externalId: 'same-summary', providerRowId: 'first' }] }));
    syncPlaywright.mockResolvedValue({ status: 'success', messages: [
      { id: 'incoming-first', externalId: 'same-summary', providerRowId: 'first' },
      { id: 'incoming-second', externalId: 'same-summary', providerRowId: 'second' }
    ] });
    await syncAccount(VALID_UUID, mockIo);
    expect(atomicWrite.mock.calls[0][1].messages).toEqual([
      expect.objectContaining({ id: 'local-first', providerRowId: 'first' }),
      expect.objectContaining({ id: 'incoming-second', providerRowId: 'second' })
    ]);
  });

  it('replays a full cached sync with linear identity reads and preserves local IDs and evaluations', async () => {
    const count = 500;
    const messages = Array.from({ length: count }, (_, i) => ({
      id: `local-${i}`, externalId: `summary-${i}`, providerRowId: `row-${i}`,
      browserMessageKind: 'member', date: '2026-01-01T12:00:00Z',
      from: { email: '[email protected]' }, evaluation: { action: 'review' }, activityIdentity: `activity-${i}`,
    }));
    let dateReads = 0;
    const incoming = messages.map(({ evaluation: _evaluation, ...message }) => ({
      ...message, id: `incoming-${message.id}`, isRead: true,
      get date() { dateReads++; return message.date; },
    }));
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages }));
    syncPlaywright.mockResolvedValue({ status: 'success', messages: incoming });

    const result = await syncAccount(VALID_UUID, mockIo);

    expect(result).toMatchObject({ newMessages: 0, total: count });
    expect(atomicWrite.mock.calls[0][1].messages).toEqual(messages.map(message => ({ ...message, isRead: true })));
    // Count actual identity input reads rather than a machine-dependent deadline.
    // Re-scanning the incoming batch for every message takes >250,000 reads.
    expect(dateReads).toBeLessThanOrEqual(count * 8);
  });

  it('reindexes a legacy row when a full body makes it a member during the same sync', async () => {
    const identity = { externalId: 'summary', providerRowId: 'row', date: '2026-01-01T12:00:00Z', from: { email: '[email protected]' } };
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [{ ...identity, id: 'local', threadId: 'thread', bodyFull: false }] }));
    syncPlaywright.mockResolvedValue({ status: 'success', messages: [
      { ...identity, id: 'row-update', bodyFull: true, bodyText: 'First body' },
      { ...identity, id: 'member-update', externalId: 'member-summary', browserMessageKind: 'member', bodyFull: true, bodyText: 'Member body' },
    ] });

    expect(await syncAccount(VALID_UUID, mockIo)).toMatchObject({ newMessages: 0, total: 1 });
    expect(atomicWrite.mock.calls[0][1].messages).toEqual([expect.objectContaining({
      id: 'local', externalId: 'member-summary', browserMessageKind: 'member', bodyText: 'Member body',
    })]);
  });

  it('retains a known row ID when a later unambiguous extraction omits it', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [{ id: 'local', externalId: 'summary', providerRowId: 'stable-row' }] }));
    syncPlaywright.mockResolvedValue({ status: 'success', messages: [{ id: 'new-local', externalId: 'summary', providerRowId: null, isRead: true }] });
    await syncAccount(VALID_UUID, mockIo);
    expect(atomicWrite.mock.calls[0][1].messages).toEqual([expect.objectContaining({ id: 'local', providerRowId: 'stable-row', isRead: true })]);
  });

  it('upgrades a unique legacy cache entry with its ingested provider row ID', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [{ id: 'legacy-local', externalId: 'summary' }] }));
    syncPlaywright.mockResolvedValue({ status: 'success', messages: [{ id: 'new-local', externalId: 'summary', providerRowId: 'stable-row' }] });
    await syncAccount(VALID_UUID, mockIo);
    expect(atomicWrite.mock.calls[0][1].messages).toEqual([expect.objectContaining({ id: 'legacy-local', providerRowId: 'stable-row' })]);
  });

  it('should keep messages without externalId (no dedup for those)', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [{ id: 'msg-1' }] }));
    syncGmail.mockResolvedValue([{ id: 'msg-2' }]); // no externalId
    updateSyncStatus.mockResolvedValue();

    const result = await syncAccount(VALID_UUID, mockIo);

    expect(result.newMessages).toBe(1);
    expect(result.total).toBe(2);
  });

  it('should trim messages when exceeding maxMessages', async () => {
    getAccount.mockResolvedValue({
      id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true,
      syncConfig: { maxMessages: 2 }
    });
    const existingCache = {
      messages: [
        { id: 'msg-old', externalId: 'ext-old', date: '2026-01-01' },
        { id: 'msg-mid', externalId: 'ext-mid', date: '2026-01-02' }
      ]
    };
    readFile.mockResolvedValue(JSON.stringify(existingCache));
    syncGmail.mockResolvedValue([
      { id: 'msg-new', externalId: 'ext-new', date: '2026-01-03' }
    ]);
    updateSyncStatus.mockResolvedValue();

    const result = await syncAccount(VALID_UUID, mockIo);

    expect(result.total).toBe(2); // trimmed from 3 to 2
    const savedData = atomicWrite.mock.calls[0][1];
    expect(savedData.messages).toHaveLength(2);
    // Oldest message should have been trimmed
    const ids = savedData.messages.map(m => m.id);
    expect(ids).toContain('msg-new');
    expect(ids).toContain('msg-mid');
    expect(ids).not.toContain('msg-old');
  });

  it('should handle structured provider result with status', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [] }));
    syncGmail.mockResolvedValue({ messages: [{ id: 'msg-1', externalId: 'ext-1' }], status: 'partial' });
    updateSyncStatus.mockResolvedValue();

    const result = await syncAccount(VALID_UUID, mockIo);

    expect(result.status).toBe('partial');
    expect(result.newMessages).toBe(1);
    expect(updateSyncStatus).toHaveBeenCalledWith(VALID_UUID, 'partial');
    // Should NOT emit messages:changed for non-success status
    expect(mockIo.emit).not.toHaveBeenCalledWith('messages:changed', {});
    expect(mockIo.emit).toHaveBeenCalledWith('messages:sync:completed', expect.objectContaining({ status: 'partial' }));
  });

  it('refuses a second sync while the first is still reading the account', async () => {
    let resolveAccount;
    getAccount.mockImplementationOnce(() => new Promise((resolve) => { resolveAccount = resolve; }));
    readFile.mockResolvedValue(JSON.stringify({ messages: [] }));
    syncGmail.mockResolvedValue([]);
    updateSyncStatus.mockResolvedValue();

    const firstSync = syncAccount(VALID_UUID, mockIo);
    await Promise.resolve();

    const secondResult = await syncAccount(VALID_UUID, mockIo);
    expect(secondResult).toEqual({ error: 'Sync already in progress', status: 409 });
    expect(getAccount).toHaveBeenCalledTimes(1);
    expect(mockIo.emit).not.toHaveBeenCalled();

    resolveAccount({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    await firstSync;

    const after = await syncAccount(VALID_UUID, mockIo);
    expect(after).not.toHaveProperty('status', 409);
  });

  it('releases the lock when the account is missing, disabled, or the mode is unsupported', async () => {
    getAccount.mockResolvedValueOnce(null);
    expect(await syncAccount(VALID_UUID, mockIo)).toEqual({ error: 'Account not found' });

    getAccount.mockResolvedValueOnce({ id: VALID_UUID, name: 'Test', type: 'gmail', enabled: false });
    expect(await syncAccount(VALID_UUID, mockIo)).toEqual({ error: 'Account is disabled', status: 400 });

    getAccount.mockResolvedValueOnce({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    expect(await syncAccount(VALID_UUID, mockIo, { mode: 'not-a-mode' })).toEqual({
      error: 'gmail accounts do not support not-a-mode sync', status: 400,
    });

    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [] }));
    syncGmail.mockResolvedValue([]);
    updateSyncStatus.mockResolvedValue();
    const after = await syncAccount(VALID_UUID, mockIo);
    expect(after).not.toHaveProperty('status', 409);
    expect(syncGmail).toHaveBeenCalledTimes(1);
  });

  it('should return 409 when sync is already in progress (lock)', async () => {
    // Use a deferred promise to keep loadCache hanging so the lock stays held
    let resolveReadFile;
    getAccount.mockResolvedValue({ id: VALID_UUID_2, name: 'Gmail', type: 'gmail', enabled: true });
    // First readFile call (loadCache inside providerSync) hangs; subsequent calls resolve
    let callCount = 0;
    readFile.mockImplementation(() => {
      callCount++;
      if (callCount === 1) {
        return new Promise(resolve => { resolveReadFile = resolve; });
      }
      return Promise.resolve(JSON.stringify({ messages: [] }));
    });
    updateSyncStatus.mockResolvedValue();

    // Start first sync (don't await — it will hang on loadCache)
    const firstSync = syncAccount(VALID_UUID_2, mockIo);
    // Yield to let the first sync reach the lock point
    await new Promise(r => setImmediate(r));

    // Second sync should be rejected with 409
    const secondResult = await syncAccount(VALID_UUID_2, mockIo);
    expect(secondResult).toEqual({ error: 'Sync already in progress', status: 409 });

    // Clean up: resolve the hanging readFile so firstSync completes
    resolveReadFile(JSON.stringify({ messages: [] }));
    await firstSync;
  });

  it('should release lock after sync completes', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [] }));
    syncGmail.mockResolvedValue([]);
    updateSyncStatus.mockResolvedValue();

    await syncAccount(VALID_UUID, mockIo);
    // Second sync should work (lock released)
    const result = await syncAccount(VALID_UUID, mockIo);
    expect(result).not.toHaveProperty('status', 409);
  });

  it('should release lock and emit failed on provider error', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Err', type: 'badtype', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [] }));
    updateSyncStatus.mockResolvedValue();

    // 'badtype' triggers throw new Error('Unsupported account type: badtype')
    const result = await syncAccount(VALID_UUID, mockIo);

    expect(result).toEqual({ error: 'Unsupported account type: badtype', status: 502 });
    expect(updateSyncStatus).toHaveBeenCalledWith(VALID_UUID, 'error');
    expect(mockIo.emit).toHaveBeenCalledWith('messages:sync:failed', {
      accountId: VALID_UUID,
      error: 'Unsupported account type: badtype'
    });

    // Lock should be released — next sync should not get 409
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    const result2 = await syncAccount(VALID_UUID, mockIo);
    expect(result2).not.toHaveProperty('status', 409);
  });

  it('should work when io is null/undefined', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    readFile.mockResolvedValue(JSON.stringify({ messages: [] }));
    syncGmail.mockResolvedValue([]);
    updateSyncStatus.mockResolvedValue();

    const result = await syncAccount(VALID_UUID, null);
    expect(result.newMessages).toBe(0);
  });
});

// ─── getSyncStatus ───

describe('getSyncStatus', () => {
  it('should return sync status for existing account', async () => {
    getAccount.mockResolvedValue({
      id: VALID_UUID, lastSyncAt: '2026-01-01T00:00:00Z', lastSyncStatus: 'success'
    });

    const result = await getSyncStatus(VALID_UUID);

    expect(result).toEqual({
      accountId: VALID_UUID,
      lastSyncAt: '2026-01-01T00:00:00Z',
      lastSyncStatus: 'success'
    });
  });

  it('should return null for nonexistent account', async () => {
    getAccount.mockResolvedValue(null);
    expect(await getSyncStatus(VALID_UUID)).toBeNull();
  });
});

// ─── refreshMessage (absent-vs-cleared body merge, #1826) ───

describe('refreshMessage', () => {
  // Mirror makeExternalId() in messageSync.js so a crafted detail entry matches
  // an existing cached message by externalId (exercising the in-place merge path).
  const makeExternalId = (date, sender, subject) =>
    'pw-' + createHash('md5').update(`${date}|${sender}|${subject}`).digest('hex').slice(0, 12);

  it('overwrites cached body with an empty extracted body (clear), not the stale text', async () => {
    const extId = makeExternalId('2026-01-01', 'Alice', 'Hi');
    readFile.mockResolvedValue(JSON.stringify({
      syncCursor: null,
      messages: [{ id: 'msg-1', externalId: extId, bodyText: 'stale body', from: { name: 'Alice' }, subject: 'Hi', date: '2026-01-01' }]
    }));
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook' });
    refreshMessageDetail.mockResolvedValue([{ from: 'Alice', date: '2026-01-01', body: '' }]);

    const result = await refreshMessage(VALID_UUID, 'msg-1');

    const merged = result.find(m => m.externalId === extId);
    expect(merged.bodyText).toBe('');
    expect(merged.bodyFull).toBe(true);
  });

  it('keeps the cached body when the extracted body is absent (null/undefined)', async () => {
    const extId = makeExternalId('2026-01-01', 'Alice', 'Hi');
    readFile.mockResolvedValue(JSON.stringify({
      syncCursor: null,
      messages: [{ id: 'msg-1', externalId: extId, bodyText: 'keep me', from: { name: 'Alice' }, subject: 'Hi', date: '2026-01-01' }]
    }));
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook' });
    refreshMessageDetail.mockResolvedValue([{ from: 'Alice', date: '2026-01-01' }]); // no body key

    const result = await refreshMessage(VALID_UUID, 'msg-1');

    const merged = result.find(m => m.externalId === extId);
    expect(merged.bodyText).toBe('keep me');
  });

  it('preserves an unmatched original rather than borrowing another conversation member body', async () => {
    // Different member evidence must not clear an unrelated legacy record.
    readFile.mockResolvedValue(JSON.stringify({
      syncCursor: null,
      messages: [{ id: 'msg-1', bodyText: 'stale original', from: { name: 'Bob' }, subject: 'Re: x', date: '2026-02-01' }]
    }));
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook' });
    refreshMessageDetail.mockResolvedValue([{ from: 'Carol', date: '2026-03-01', body: '' }]);

    const result = await refreshMessage(VALID_UUID, 'msg-1');

    expect(result.every(m => m.id !== 'msg-1')).toBe(true);
    expect(atomicWrite.mock.calls[0][1].messages.find(m => m.id === 'msg-1').bodyText).toBe('stale original');
  });

  it('loads and saves the cache once when refreshing multiple messages', async () => {
    readFile.mockResolvedValue(JSON.stringify({
      syncCursor: null,
      messages: [
        { id: 'msg-1', bodyText: 'preview 1', from: { name: 'Alice' }, subject: 'One', date: '2026-01-01' },
        { id: 'msg-2', bodyText: 'preview 2', from: { name: 'Bob' }, subject: 'Two', date: '2026-01-02' },
      ]
    }));
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook' });
    refreshMessageDetail
      .mockResolvedValueOnce([{ from: 'Alice', date: '2026-01-01', body: 'full 1' }])
      .mockResolvedValueOnce([{ from: 'Bob', date: '2026-01-02', body: 'full 2' }]);

    const result = await refreshMessages(VALID_UUID, ['msg-1', 'msg-2']);

    expect(result.updated).toBe(2);
    expect(readFile).toHaveBeenCalledTimes(1);
    expect(getAccount).toHaveBeenCalledTimes(1);
    expect(refreshMessageDetail).toHaveBeenCalledTimes(2);
    expect(atomicWrite).toHaveBeenCalledTimes(1);
    const savedMessages = atomicWrite.mock.calls[0][1].messages;
    expect(savedMessages.find(message => message.id === 'msg-1').bodyText).toBe('full 1');
    expect(savedMessages.find(message => message.id === 'msg-2').bodyText).toBe('full 2');
  });
});

// ─── Per-account write-tail serialization (#2537) ───
// These prove the cache mutators (`updateMessageEvaluations`, `refreshMessage`)
// serialize against each other on the same account so two concurrent load→save
// regions can't clobber each other. The mock is stateful — reads see the last
// write — and each op takes a tick, so an UNSERIALIZED implementation would load
// stale state and lose the other op's update, failing these assertions.

describe('cache mutator serialization (#2537)', () => {
  const tick = () => new Promise((r) => setTimeout(r, 5));

  // Wire readFile/atomicWrite to a shared in-memory cache so interleaved
  // load→save regions race exactly as they would on disk.
  function statefulCache(initial) {
    let stored = initial;
    readFile.mockImplementation(async () => {
      await tick();
      return JSON.stringify(stored);
    });
    atomicWrite.mockImplementation(async (_path, data) => {
      await tick();
      stored = data;
    });
    return () => stored;
  }

  it('serializes syncAccount against refreshMessage (both writes survive)', async () => {
    const getStored = statefulCache({
      messages: [{ id: 'msg-1', externalId: 'ext-1', from: { name: 'Ada' }, subject: 'Hi', date: '2026-01-01', bodyText: 'old' }],
    });
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Gmail', type: 'gmail', enabled: true });
    updateSyncStatus.mockResolvedValue();
    // Sync appends a brand-new message (ext-2); refresh rewrites msg-1's body.
    syncGmail.mockResolvedValue([{ id: 'srv-2', externalId: 'ext-2', subject: 'New', date: '2026-01-02', from: { name: 'Bob' } }]);
    refreshMessageDetail.mockResolvedValue([{ from: 'Ada', date: '2026-01-01', body: 'refreshed body' }]);

    await Promise.all([
      syncAccount(VALID_UUID, null),
      refreshMessage(VALID_UUID, 'msg-1'),
    ]);

    const messages = getStored().messages;
    // Serialization preserves BOTH: the synced message AND the refreshed body.
    // An unserialized interleave drops whichever saved first.
    expect(messages.find((m) => m.externalId === 'ext-2')).toBeTruthy();
    expect(messages.find((m) => m.id === 'msg-1').bodyText).toBe('refreshed body');
  });

  it('serializes refreshMessage against updateMessageEvaluations (both writes survive)', async () => {
    const getStored = statefulCache({
      messages: [{ id: 'msg-1', from: { name: 'Ada' }, subject: 'Hi', date: '2026-01-01', bodyText: 'old' }],
    });
    readdir.mockImplementation(async () => [`${VALID_UUID}.json`]);
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'outlook' });
    refreshMessageDetail.mockResolvedValue([{ from: 'Ada', date: '2026-01-01', body: 'refreshed body' }]);

    // Body refresh and evaluation update both mutate msg-1 concurrently.
    await Promise.all([
      refreshMessage(VALID_UUID, 'msg-1'),
      updateMessageEvaluations({ 'msg-1': { score: 9 } }),
    ]);

    const msg1 = getStored().messages.find((m) => m.id === 'msg-1');
    // Serialization preserves BOTH writes; a clobber would drop one of them.
    expect(msg1.bodyText).toBe('refreshed body');
    expect(msg1.evaluation).toEqual({ score: 9 });
  });
});

describe('logMessageTouchpoints — candidate building (#2033)', () => {
  const account = { id: VALID_UUID, type: 'gmail', email: '[email protected]' };

  beforeEach(() => {
    autoLogTouchpoints.mockClear();
    autoLogTouchpoints.mockResolvedValue({ created: 1, matched: 1 });
    mockTimezone = 'UTC';
  });

  it('builds a message candidate keyed per thread + day, excluding the account owner', async () => {
    await logMessageTouchpoints(account, [{
      id: 'm1',
      threadId: 'thread-9',
      subject: 'Lunch?',
      date: '2026-06-01T12:00:00Z',
      from: { name: 'Ada', email: '[email protected]' },
      to: ['[email protected]', '[email protected]'],
      cc: [],
    }]);

    expect(autoLogTouchpoints).toHaveBeenCalledTimes(1);
    const [candidates] = autoLogTouchpoints.mock.calls[0];
    expect(candidates).toHaveLength(1);
    expect(candidates[0]).toMatchObject({
      source: 'message',
      dedupeKey: `msg:${VALID_UUID}:thread-9:2026-06-01`,
      summary: 'Lunch?',
    });
    // Own address excluded; sender + other recipient remain.
    const emails = candidates[0].identities.map((i) => i.email).sort();
    expect(emails).toEqual(['[email protected]', '[email protected]']);
  });

  it('falls back to the message id when there is no threadId', async () => {
    await logMessageTouchpoints(account, [{
      id: 'm2', subject: 'Hi', date: '2026-06-02T00:00:00Z',
      from: { name: 'Grace', email: '[email protected]' }, to: [], cc: [],
    }]);
    const [candidates] = autoLogTouchpoints.mock.calls[0];
    expect(candidates[0].dedupeKey).toBe(`msg:${VALID_UUID}:m2:2026-06-02`);
  });

  it('keeps a name-only participant so the matcher unique-name fallback applies', async () => {
    await logMessageTouchpoints(account, [{
      id: 'm4', subject: 'Hi', date: '2026-06-01T00:00:00Z',
      from: { name: 'Ada Lovelace', email: '' }, to: [], cc: [],
    }]);
    const [candidates] = autoLogTouchpoints.mock.calls[0];
    expect(candidates[0].identities).toEqual([{ email: '', name: 'Ada Lovelace' }]);
  });

  it('skips a message with no counterpart beyond the account owner', async () => {
    await logMessageTouchpoints(account, [{
      id: 'm3', date: '2026-06-01T00:00:00Z',
      from: { name: 'Me', email: '[email protected]' }, to: ['[email protected]'], cc: [],
    }]);
    expect(autoLogTouchpoints).not.toHaveBeenCalled();
  });

  it('derives the dedupe day in the user local timezone, not UTC', async () => {
    // 03:00Z on Jun 2 is still Jun 1 (20:00) in America/Los_Angeles. The dedupe
    // day must be the LOCAL day (2026-06-01) so a same-local-evening thread that
    // straddles UTC midnight collapses to one touchpoint, matching cadence math.
    mockTimezone = 'America/Los_Angeles';
    await logMessageTouchpoints(account, [{
      id: 'm5', threadId: 'thread-tz', subject: 'Evening',
      date: '2026-06-02T03:00:00Z',
      from: { name: 'Ada', email: '[email protected]' }, to: [], cc: [],
    }]);
    const [candidates] = autoLogTouchpoints.mock.calls[0];
    expect(candidates[0].dedupeKey).toBe(`msg:${VALID_UUID}:thread-tz:2026-06-01`);
  });
});

// Real Gmail adapter through persisted syncAccount: detail loss must not erase evaluations.
describe('full inbox coverage contract', () => {
  const externalId = id => 'api-gmail-' + createHash('md5').update(id).digest('hex').slice(0, 12);
  let saved;
  beforeEach(async () => {
    saved = { messages: [
      { id: 'local-a', externalId: externalId('a'), date: '2026-01-02' },
      { id: 'local-b', externalId: externalId('b'), date: '2026-01-01', evaluation: { priority: 'high' } },
      { id: 'local-only' },
    ] };
    readFile.mockImplementation(async () => JSON.stringify(saved));
    atomicWrite.mockImplementation(async (_path, data) => { saved = structuredClone(data); });
    getAccount.mockResolvedValue({ id: VALID_UUID, name: 'Example inbox', email: '[email protected]', type: 'gmail', enabled: true });
    syncGmail.mockImplementation((await vi.importActual('./messageGmailSync.js')).syncGmail);
    gmailApi.list.mockImplementation(async ({ q }) => ({
      messages: q === 'in:inbox' ? [{ id: 'a' }, { id: 'b' }] : [],
    }));
    gmailApi.get.mockImplementation(async (id) => ({
      id, labelIds: ['INBOX'], payload: { headers: [] },
    }));
  });

  it('keeps full Gmail bodies in the cache and broadcasts only progress until the saved completion', async () => {
    const count = 200; // Gmail's full-sync transport ceiling.
    const body = 'x'.repeat(8192);
    const io = { emit: vi.fn((event) => {
      if (event === 'messages:sync:completed') {
        expect(saved.messages.filter(message => message.bodyText === body)).toHaveLength(count);
      }
    }) };
    gmailApi.list.mockImplementation(async ({ q, pageToken, maxResults }) => {
      const start = Number(pageToken || 0);
      const end = Math.min(start + maxResults, count);
      return q === 'in:inbox' ? {
        messages: Array.from({ length: end - start }, (_, i) => ({ id: `synthetic-${start + i}` })),
        ...(end < count ? { nextPageToken: String(end) } : {}),
      } : {};
    });
    gmailApi.get.mockImplementation(async (id) => ({
      id, labelIds: ['INBOX'], payload: { headers: [], mimeType: 'text/plain', body: { data: Buffer.from(body).toString('base64url') } },
    }));

    expect(await syncAccount(VALID_UUID, io, { mode: 'full' })).toMatchObject({ newMessages: count });
    expect(io.emit).toHaveBeenCalledWith('messages:sync:completed', expect.objectContaining({ newMessages: count }));
    expect(io.emit.mock.calls.some(([, payload]) => payload.messages)).toBe(false);
    expect(Buffer.byteLength(JSON.stringify(io.emit.mock.calls))).toBeLessThan(100_000);
  });

  it('preserves identity and evaluation through failure and retry, then reconciles complete snapshots', async () => {
    gmailApi.get.mockImplementationOnce(async () => ({
      id: 'a', labelIds: ['INBOX', 'UNREAD'], payload: { headers: [] },
    })).mockRejectedValueOnce(new Error('temporary detail failure'));
    await syncAccount(VALID_UUID, null, { mode: 'full' });
    expect(saved.messages).toHaveLength(3);
    expect(saved.messages[0]).toMatchObject({ id: 'local-a', isUnread: true });
    expect(saved.messages[1]).toMatchObject({ id: 'local-b', evaluation: { priority: 'high' } });
    expect(markSentIngested).toHaveBeenLastCalledWith(VALID_UUID, expect.objectContaining({ partial: true }));
    await syncAccount(VALID_UUID, null, { mode: 'full' });
    expect(saved.messages[1]).toMatchObject({ id: 'local-b', evaluation: { priority: 'high' } });
    gmailApi.list.mockResolvedValue({ messages: [{ id: 'b' }] });
    await syncAccount(VALID_UUID, null, { mode: 'full' });
    expect(saved.messages.map(m => m.id)).toEqual(['local-b', 'local-only']);
    gmailApi.list.mockResolvedValue({});
    await syncAccount(VALID_UUID, null, { mode: 'full' });
    expect(saved.messages).toEqual([{ id: 'local-only' }]);
  });

  it('retains unseen mail when the Gmail inbox reaches its cap', async () => {
    gmailApi.list.mockImplementation(async ({ q, maxResults, pageToken }) => (
      q === 'in:inbox' ? {
        messages: Array.from({ length: maxResults }, (_, i) => ({ id: 'new-' + (Number(pageToken || 0) + i) })),
        nextPageToken: String(Number(pageToken || 0) + maxResults),
      } : {}
    ));
    await syncAccount(VALID_UUID, null, { mode: 'full' });
    expect(saved.messages.find(m => m.id === 'local-b')).toMatchObject({ evaluation: { priority: 'high' } });
    expect(saved.messages).toHaveLength(203);
  });

  it.each([
    ['legacy array', [{ id: 'new', externalId: 'new' }], 'full'],
    ['unproven browser success', { messages: [], status: 'success', inboxComplete: false }, 'full'],
    ['unread snapshot', { messages: [], status: 'success', inboxComplete: true }, 'unread'],
    ['partial status', { messages: [], status: 'partial', inboxComplete: true }, 'full'],
  ])('does not prune on %s', async (_name, result, mode) => {
    syncGmail.mockResolvedValue(result);
    await syncAccount(VALID_UUID, null, { mode });
    expect(saved.messages.find(m => m.id === 'local-b')).toMatchObject({ evaluation: { priority: 'high' } });
  });

  it('keeps configured retention independent of incomplete membership', async () => {
    getAccount.mockResolvedValue({ id: VALID_UUID, type: 'gmail', enabled: true, syncConfig: { maxMessages: 1 } });
    syncGmail.mockResolvedValue({ messages: [], status: 'success', inboxComplete: false });
    await syncAccount(VALID_UUID, null, { mode: 'full' });
    expect(saved.messages.map(m => m.id)).toEqual(['local-a']);
  });

  it('does not certify malformed details or listings', async () => {
    gmailApi.get.mockResolvedValue({});
    await syncAccount(VALID_UUID, null, { mode: 'full' });
    expect(saved.messages).toHaveLength(3);
    gmailApi.list.mockResolvedValue({ messages: 'invalid' });
    expect(await syncAccount(VALID_UUID, null, { mode: 'full' })).toMatchObject({ status: 502 });
    expect(saved.messages).toHaveLength(3);
  });
});


describe('browser ingestion identity across sync, cache, and activity (#11150)', () => {
  async function fixture(initial = []) {
    let stored = { messages: initial };
    const events = new Map();
    const { recordEvents, messageActivityCandidates } = await import('./humanActivity.js');
    const account = { id: VALID_UUID, type: 'outlook', enabled: true };
    for (const candidate of messageActivityCandidates(account, initial.map(m => ({ ...m, activityIdentity: m.externalId })))) events.set(candidate.dedupeKey, candidate);
    recordEvents.mockImplementation(async candidates => {
      for (const candidate of candidates) if (!events.has(candidate.dedupeKey)) events.set(candidate.dedupeKey, candidate);
    });
    readFile.mockImplementation(async path => path.endsWith('selectors.json') ? '{}' : JSON.stringify(stored));
    atomicWrite.mockImplementation(async (_path, data) => { stored = structuredClone(data); });
    getAccount.mockResolvedValue(account);
    browser.findOrOpenPage.mockResolvedValue({ url: 'https://outlook.office.com/mail/' });
    const actual = await vi.importActual('./messagePlaywrightSync.js');
    syncPlaywright.mockImplementation(actual.syncPlaywright);
    return { events, stored: () => stored };
  }

  const summary = { from: 'Example Sender', fromEmail: '[email protected]', subject: 'Same subject', date: '2026-01-01T12:00:00Z', isUnread: true };
  function scrape(rows, details) {
    browser.evaluateOnPage.mockImplementation(async (_page, script) => {
      if (script.includes('const seen = new Map()')) return rows;
      if (script.includes('const targetId =')) return details ? true : { found: false };
      return details;
    });
  }

  it('keeps colliding rows distinct and replays mutable summaries without losing local evaluations or activity identity', async () => {
    const f = await fixture();
    scrape([{ ...summary, providerRowId: 'row-a' }, { ...summary, providerRowId: 'row-b' }]);
    await syncAccount(VALID_UUID);
    expect(f.stored().messages).toHaveLength(2);
    expect(f.events.size).toBe(2);
    const original = f.stored().messages.find(m => m.providerRowId === 'row-a');
    original.evaluation = { score: 9 };
    scrape([{ ...summary, providerRowId: 'row-a', from: 'Renamed Sender', date: '2026-01-01T07:00:00-05:00' }, { ...summary, providerRowId: 'row-b' }]);
    await syncAccount(VALID_UUID);
    await syncAccount(VALID_UUID);
    expect(f.stored().messages).toHaveLength(2);
    expect(f.stored().messages.find(m => m.providerRowId === 'row-a')).toMatchObject({ id: original.id, evaluation: { score: 9 }, from: { name: 'Renamed Sender' } });
    expect(f.events.size).toBe(2);
  });

  it('retains the event key and local evaluation of an unambiguous legacy row', async () => {
    const f = await fixture([{ id: 'legacy-local', externalId: 'pw-legacy', providerRowId: 'row-a', from: { name: summary.from }, date: summary.date, subject: summary.subject, evaluation: { score: 8 } }]);
    scrape([{ ...summary, providerRowId: 'row-a', from: 'New display name' }]);
    await syncAccount(VALID_UUID);
    await syncAccount(VALID_UUID);
    expect(f.stored().messages).toHaveLength(1);
    expect(f.stored().messages[0]).toMatchObject({ id: 'legacy-local', evaluation: { score: 8 }, activityIdentity: 'pw-legacy' });
    expect([...f.events.keys()]).toEqual([`msg:${VALID_UUID}:pw-legacy`]);
  });

  it('adopts a newly available child ID by unique canonical evidence and retains it if the provider later omits it', async () => {
    const legacy = { id: 'legacy-member', externalId: 'pw-old-display', providerRowId: 'row-a',
      threadId: 'legacy-thread', bodyFull: true, bodyText: 'old body', date: summary.date,
      from: { name: summary.from, email: summary.fromEmail }, subject: summary.subject, evaluation: { score: 8 } };
    const f = await fixture([legacy]);
    const details = [{ providerMessageId: 'child-a', from: 'Renamed', fromEmail: summary.fromEmail,
      date: '2026-01-01T07:00:00-05:00', body: 'new body' }];
    scrape([{ ...summary, providerRowId: 'row-a' }], details);
    await syncAccount(VALID_UUID);
    expect(f.stored().messages).toHaveLength(1);
    expect(f.stored().messages[0]).toMatchObject({ id: legacy.id, providerMessageId: 'child-a', evaluation: { score: 8 }, bodyText: 'new body' });
    expect(f.events.size).toBe(1);
    refreshMessageDetail.mockResolvedValue(details.map(({ providerMessageId, ...detail }) => detail));
    await refreshMessage(VALID_UUID, legacy.id);
    expect(f.stored().messages).toHaveLength(1);
    expect(f.stored().messages[0].providerMessageId).toBe('child-a');
  });

  it('upgrades a summary to its unambiguous child without adding a second activity event', async () => {
    const f = await fixture();
    scrape([{ ...summary, providerRowId: 'row-a' }]);
    await syncAccount(VALID_UUID);
    const original = f.stored().messages[0];
    original.evaluation = { score: 7 };
    const details = [
      { from: 'Renamed Sender', fromEmail: summary.fromEmail, date: '2026-01-01T07:00:00-05:00', body: 'Current message' },
      { from: summary.from, fromEmail: summary.fromEmail, date: '2025-12-31T12:00:00Z', body: 'Earlier message' }
    ];
    scrape([{ ...summary, providerRowId: 'row-a' }], details);
    await syncAccount(VALID_UUID);
    await syncAccount(VALID_UUID);
    expect(f.stored().messages).toHaveLength(2);
    expect(f.stored().messages.find(m => m.id === original.id)).toMatchObject({ bodyText: 'Current message', evaluation: { score: 7 }, browserMessageKind: 'member' });
    expect(f.events.size).toBe(2);
  });

  it('retains every expanded member on cached replay and isolates refresh bodies by provider row', async () => {
    const f = await fixture();
    const details = [
      { from: summary.from, fromEmail: summary.fromEmail, date: summary.date, body: 'First body', providerMessageId: 'child-a' },
      { from: summary.from, fromEmail: summary.fromEmail, date: summary.date, body: 'Second body', providerMessageId: 'child-b' }
    ];
    scrape([{ ...summary, providerRowId: 'row-a' }, { ...summary, providerRowId: 'row-b' }], details);
    await syncAccount(VALID_UUID);
    expect(f.stored().messages).toHaveLength(4);
    expect(f.events.size).toBe(4);
    await syncAccount(VALID_UUID);
    expect(f.stored().messages).toHaveLength(4);
    expect(f.events.size).toBe(4);
    const first = f.stored().messages.find(m => m.providerRowId === 'row-a');
    refreshMessageDetail.mockResolvedValue(details.map(d => ({ ...d, body: 'Refreshed ' + d.body })));
    await refreshMessage(VALID_UUID, first.id);
    expect(f.stored().messages.filter(m => m.providerRowId === 'row-a').map(m => m.bodyText)).toEqual(['Refreshed First body', 'Refreshed Second body']);
    expect(f.stored().messages.filter(m => m.providerRowId === 'row-b').map(m => m.bodyText)).toEqual(['First body', 'Second body']);
  });

  it.each([null, 'repeated-provider-id'])('preserves ambiguous members rather than overwriting one with another (child ID: %s)', async (providerMessageId) => {
    const f = await fixture();
    const details = ['Body one', 'Body two'].map(body => ({ providerMessageId, from: summary.from, fromEmail: summary.fromEmail, date: summary.date, body }));
    scrape([{ ...summary, providerRowId: 'row-a' }], details);
    await syncAccount(VALID_UUID);
    await syncAccount(VALID_UUID);
    expect(f.stored().messages.map(m => m.bodyText)).toEqual(['Body one', 'Body two']);
    expect(f.events.size).toBe(2);
    const first = f.stored().messages[0];
    refreshMessageDetail.mockResolvedValue([details[0], { ...details[1], body: 'Changed ambiguous body' }]);
    await refreshMessage(VALID_UUID, first.id);
    expect(f.stored().messages.map(m => m.bodyText)).toEqual(['Body one', 'Body two', 'Changed ambiguous body']);
  });
});