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

3.0 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

vi.mock('../../../services/api', () => ({
  getDigitalTwinDocuments: vi.fn(),
  getDigitalTwinDocument: vi.fn(),
  updateSoulDocument: vi.fn(),
  createSoulDocument: vi.fn(),
  deleteSoulDocument: vi.fn()
}));

vi.mock('../../ui/Toast', () => ({
  default: { success: vi.fn(), error: vi.fn() }
}));

import DocumentsTab from './DocumentsTab';
import * as api from '../../../services/api';
import toast from '../../ui/Toast';

const DOC = { id: 'doc-1', title: 'Core Values', category: 'core', enabled: true, weight: 5, content: 'original', lastModified: '2026-01-01T00:00:00.000Z' };

describe('DocumentsTab failed save', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    api.getDigitalTwinDocuments.mockResolvedValue([DOC]);
    api.getDigitalTwinDocument.mockResolvedValue(DOC);
  });

  it('re-enables Save after a rejected update so the unsaved edit can be retried', async () => {
    api.updateSoulDocument.mockRejectedValueOnce(new Error('Document is locked'));
    const user = userEvent.setup();
    render(<DocumentsTab onRefresh={vi.fn()} />);

    await user.click(await screen.findByRole('button', { name: /Core Values/ }));
    await user.click(await screen.findByRole('button', { name: 'Edit' }));
    await user.click(screen.getByRole('button', { name: 'Save' }));

    await waitFor(() => expect(api.updateSoulDocument).toHaveBeenCalledTimes(1));
    await waitFor(() => expect(screen.getByRole('button', { name: 'Save' })).not.toBeDisabled());
    expect(toast.success).not.toHaveBeenCalled();

    // The retry goes through: the editor was not stranded behind a stuck `saving` flag.
    api.updateSoulDocument.mockResolvedValueOnce({});
    await user.click(screen.getByRole('button', { name: 'Save' }));
    await waitFor(() => expect(toast.success).toHaveBeenCalledWith('Document saved'));
  });
});

describe('DocumentsTab failed load', () => {
  beforeEach(() => vi.clearAllMocks());

  it('shows a retryable load error instead of "No documents yet", then the list once Retry succeeds', async () => {
    api.getDigitalTwinDocuments.mockRejectedValueOnce(new Error('boom')).mockResolvedValueOnce([DOC]);
    const user = userEvent.setup();
    render(<DocumentsTab onRefresh={vi.fn()} />);

    expect(await screen.findByRole('alert')).toHaveTextContent('Could not load documents');
    expect(screen.queryByText('No documents yet')).toBeNull();

    await user.click(screen.getByRole('button', { name: 'Retry' }));
    expect(await screen.findByRole('button', { name: /Core Values/ })).toBeInTheDocument();
    expect(screen.queryByRole('alert')).toBeNull();
  });

  it('keeps "No documents yet" for a genuinely empty library', async () => {
    api.getDigitalTwinDocuments.mockResolvedValue([]);
    render(<DocumentsTab onRefresh={vi.fn()} />);
    expect(await screen.findByText('No documents yet')).toBeInTheDocument();
    expect(screen.queryByRole('alert')).toBeNull();
  });
});