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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import SyncTab from './SyncTab';
import { awaitEnabled } from '../../test/enabledBarrier.js';
const api = vi.hoisted(() => ({
getCalendarTokenStatus: vi.fn(),
clearCalendarToken: vi.fn(),
syncCalendarAccount: vi.fn(),
apiSyncGoogleCalendar: vi.fn(),
mcpSyncGoogleCalendar: vi.fn(),
}));
const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn(), warning: vi.fn() }));
const listeners = vi.hoisted(() => new Map());
vi.mock('../../services/api', () => api);
vi.mock('../ui/Toast', () => ({ default: toast }));
vi.mock('../../services/socket', () => ({ default: {
on: (event, handler) => listeners.set(event, handler),
off: (event, handler) => { if (listeners.get(event) === handler) listeners.delete(event); },
} }));
const account = { id: 'calendar-1', name: 'Calendar', enabled: true, type: 'outlook-calendar' };
const emit = (event, payload = {}) => act(() => listeners.get(`calendar:sync:${event}`)?.({ accountId: account.id, ...payload }));
const syncButton = () => screen.getByRole('button', { name: 'Sync' });
beforeEach(() => {
vi.clearAllMocks();
listeners.clear();
api.getCalendarTokenStatus.mockResolvedValue({ providers: [] });
});
describe('SyncTab empty state', () => {
it('shows a connect-calendar empty state with a link to Config when there are 0 accounts', async () => {
render(<MemoryRouter><SyncTab accounts={[]} onRefresh={vi.fn()} /></MemoryRouter>);
await waitFor(() => expect(api.getCalendarTokenStatus).toHaveBeenCalled());
expect(screen.getByText('No calendar connected')).toBeTruthy();
const link = screen.getByRole('link', { name: 'Add a calendar account' });
expect(link.getAttribute('href')).toBe('/calendar/config');
expect(screen.queryByText(/Add one in the Config tab/)).toBeNull();
});
});
describe('Calendar sync lifecycle', () => {
it('releases failed and partial syncs without claiming success, then accepts a new legacy success', async () => {
const onRefresh = vi.fn();
render(<SyncTab accounts={[account]} onRefresh={onRefresh} />);
emit('started');
expect(screen.queryByRole('button', { name: 'Sync' })).toBeNull();
emit('completed', { status: 'api-error' });
expect(syncButton()).toBeEnabled();
expect(toast.error).toHaveBeenCalledWith(expect.stringContaining('provider API'));
expect(toast.success).not.toHaveBeenCalled();
emit('started');
emit('completed', { status: 'partial', reason: 'One calendar was unavailable', newEvents: 2 });
expect(syncButton()).toBeEnabled();
expect(toast.warning).toHaveBeenCalledWith('Calendar sync incomplete: One calendar was unavailable');
expect(toast.success).not.toHaveBeenCalled();
expect(onRefresh).toHaveBeenCalledTimes(1);
emit('started');
emit('completed', { newEvents: 3 });
expect(toast.success).toHaveBeenCalledWith('Calendar sync complete: 3 events');
expect(onRefresh).toHaveBeenCalledTimes(2);
await waitFor(() => expect(api.getCalendarTokenStatus).toHaveBeenCalled());
});
it('explains an HTTP-only skipped sync and deduplicates HTTP-first completion', async () => {
api.syncCalendarAccount.mockResolvedValue({ status: 'skipped' });
render(<SyncTab accounts={[account]} onRefresh={vi.fn()} />);
fireEvent.click(syncButton());
await awaitEnabled(syncButton);
emit('started'); // The socket can deliver its start after HTTP settled.
expect(syncButton()).toBeEnabled();
expect(toast.warning).toHaveBeenCalledWith(expect.stringContaining('no usable token'));
expect(api.syncCalendarAccount).toHaveBeenCalledWith(account.id, { silent: true });
api.syncCalendarAccount.mockResolvedValue({ status: 'success', newEvents: 4 });
fireEvent.click(syncButton());
await waitFor(() => expect(toast.success).toHaveBeenCalledTimes(1));
emit('started');
expect(syncButton()).toBeEnabled();
emit('completed', { status: 'success', newEvents: 4 });
expect(toast.success).toHaveBeenCalledTimes(1);
});
it('reports repeated standalone Google pushes without settling an active manual sync', async () => {
const onRefresh = vi.fn();
render(<SyncTab accounts={[account]} onRefresh={onRefresh} />);
emit('completed', { calendarId: 'work', status: 'success', newEvents: 1 });
emit('completed', { calendarId: 'work', status: 'success', newEvents: 2 });
expect(toast.success).toHaveBeenCalledTimes(2);
expect(onRefresh).toHaveBeenCalledTimes(2);
expect(syncButton()).toBeEnabled();
emit('started');
emit('completed', { calendarId: 'work', status: 'success', newEvents: 3 });
expect(screen.queryByRole('button', { name: 'Sync' })).toBeNull();
emit('completed', { status: 'success', newEvents: 4 });
expect(syncButton()).toBeEnabled();
expect(toast.success).toHaveBeenLastCalledWith('Calendar sync complete: 4 events');
await waitFor(() => expect(api.getCalendarTokenStatus).toHaveBeenCalled());
});
it('ignores an older HTTP result after a newer socket cycle starts', async () => {
let resolve;
api.syncCalendarAccount.mockImplementation(() => new Promise(done => { resolve = done; }));
render(<SyncTab accounts={[account]} onRefresh={vi.fn()} />);
fireEvent.click(syncButton());
emit('started');
emit('completed', { status: 'success', newEvents: 1 });
emit('started');
await act(async () => resolve({ status: 'success', newEvents: 1 }));
expect(screen.queryByRole('button', { name: 'Sync' })).toBeNull();
expect(toast.success).toHaveBeenCalledTimes(1);
emit('completed', { status: 'push-only' });
expect(syncButton()).toBeEnabled();
expect(toast.warning).toHaveBeenCalledWith(expect.stringContaining('pushed updates'));
});
it('clears Google progress on HTTP failure and removes listeners before a late response', async () => {
const onRefresh = vi.fn();
api.apiSyncGoogleCalendar.mockRejectedValue(new Error('Google OAuth not configured'));
let resolve;
api.mcpSyncGoogleCalendar.mockImplementation(() => new Promise(done => { resolve = done; }));
const { rerender, unmount } = render(<SyncTab accounts={[{ ...account, type: 'google-calendar', syncMethod: 'google-api' }]} onRefresh={onRefresh} />);
fireEvent.click(screen.getByRole('button', { name: 'Sync (API)' }));
await waitFor(() => expect(toast.error).toHaveBeenCalledWith(expect.stringContaining('Google OAuth not configured')));
expect(screen.getByRole('button', { name: 'Sync (API)' })).toBeEnabled();
expect(screen.queryByText('Starting Google Calendar sync...')).toBeNull();
rerender(<SyncTab accounts={[{ ...account, type: 'google-calendar' }]} onRefresh={onRefresh} />);
fireEvent.click(screen.getByRole('button', { name: 'Sync (Claude)' }));
emit('started');
emit('progress', { message: 'Fetching calendars' });
expect(screen.getByText('Fetching calendars')).toBeTruthy();
unmount();
expect(listeners.size).toBe(0);
await act(async () => resolve({ status: 'success', newEvents: 9 }));
expect(toast.success).not.toHaveBeenCalled();
expect(onRefresh).not.toHaveBeenCalled();
});
});
describe('Calendar token clearing', () => {
const present = { providers: [{ provider: 'outlook', hasToken: true }] };
it('blocks duplicate clears and refreshes only after a bodyless success', async () => {
let resolve;
api.getCalendarTokenStatus.mockResolvedValue(present);
api.clearCalendarToken.mockImplementation(() => new Promise(done => { resolve = done; }));
render(<SyncTab accounts={[account]} onRefresh={vi.fn()} />);
const button = await screen.findByRole('button', { name: 'Clear' });
api.getCalendarTokenStatus.mockResolvedValue({ providers: [{ provider: 'outlook', hasToken: false }] });
fireEvent.click(button);
expect(button).toBeDisabled();
fireEvent.click(button);
expect(api.clearCalendarToken).toHaveBeenCalledExactlyOnceWith('outlook');
expect(toast.success).not.toHaveBeenCalled();
expect(api.getCalendarTokenStatus).toHaveBeenCalledTimes(1);
await act(async () => resolve(undefined));
await screen.findByText('No token');
expect(toast.success).toHaveBeenCalledExactlyOnceWith('outlook token cleared');
expect(api.getCalendarTokenStatus).toHaveBeenCalledTimes(2);
});
it('preserves the token on rejection without a component error toast and permits retry', async () => {
api.getCalendarTokenStatus.mockResolvedValue(present);
api.clearCalendarToken.mockRejectedValueOnce(new Error('Synthetic clear failure')).mockResolvedValueOnce(undefined);
render(<SyncTab accounts={[account]} onRefresh={vi.fn()} />);
const button = await screen.findByRole('button', { name: 'Clear' });
fireEvent.click(button);
await waitFor(() => expect(button).toBeEnabled());
expect(screen.getByText('Token present')).toBeTruthy();
expect(api.getCalendarTokenStatus).toHaveBeenCalledTimes(1);
expect(toast.success).not.toHaveBeenCalled();
expect(toast.error).not.toHaveBeenCalled();
fireEvent.click(button);
await waitFor(() => expect(toast.success).toHaveBeenCalledExactlyOnceWith('outlook token cleared'));
expect(api.clearCalendarToken).toHaveBeenCalledTimes(2);
expect(api.getCalendarTokenStatus).toHaveBeenCalledTimes(2);
});
});