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.7 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import Pipeline from './Pipeline';

const listPipelineSeriesSummaries = vi.fn();
const listUniverseNames = vi.fn();
const listLooms = vi.fn();

vi.mock('../services/api', () => ({
  listPipelineSeriesSummaries: (...a) => listPipelineSeriesSummaries(...a),
  createPipelineSeries: vi.fn(),
  deletePipelineSeries: vi.fn(),
  generateSeriesTitleLogo: vi.fn(),
  generateSeriesConcepts: vi.fn(),
  listUniverseNames: (...a) => listUniverseNames(...a),
  listLooms: (...a) => listLooms(...a),
  WORLD_LOGLINE_MAX: 400,
  WORLD_PREMISE_MAX: 2000,
  WORLD_STYLE_NOTES_MAX: 2000,
}));

vi.mock('../hooks/useSyncIntegrity', () => ({
  useSyncIntegrity: () => ({ integrity: null }),
  syncBadgeStatus: () => 'not-syncing',
}));

vi.mock('../components/sharing/ShareToButton', () => ({ default: () => <button type="button">share</button> }));
vi.mock('../components/sharing/SyncToPeerButton', () => ({ default: () => <button type="button">sync-to-peer</button> }));
vi.mock('../components/moodBoard/MoodBoardReferenceStrip', () => ({ default: () => <div>mood-board</div> }));
vi.mock('../components/ui/Toast', () => ({ default: { success: vi.fn(), error: vi.fn() } }));

const SERIES = [
  { id: 'series-1', name: 'Example Series' },
  { id: 'series-2', name: 'Second Series' },
];

const renderPage = async () => {
  const result = render(
    <MemoryRouter initialEntries={['/pipeline']}><Pipeline /></MemoryRouter>,
  );
  await act(async () => {});
  return result;
};

describe('Pipeline series list — branching-narrative badge', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    listPipelineSeriesSummaries.mockResolvedValue(SERIES);
    listUniverseNames.mockResolvedValue([]);
    listLooms.mockResolvedValue([
      { id: 'loom-1', seriesId: 'series-1' },
      { id: 'loom-2', seriesId: 'series-1' },
      { id: 'loom-3', seriesId: null },
      { id: 'loom-4', seriesId: 'series-gone' },
    ]);
  });

  it('counts only the looms linked to each row and leaves unlinked rows bare', async () => {
    await renderPage();
    await screen.findByText('Example Series');
    await waitFor(() => expect(screen.getByText('2 branching')).toBeInTheDocument());

    const badged = screen.getByText('2 branching').closest('li');
    expect(badged).toContainElement(screen.getByText('Example Series'));
    // series-2 has no looms, and the standalone / dangling ones match nothing.
    expect(screen.queryByText('1 branching')).toBeNull();
  });

  it('renders the list without badges when the loom fetch fails', async () => {
    listLooms.mockRejectedValueOnce(new Error('offline')).mockResolvedValue([]);
    await renderPage();
    await screen.findByText('Example Series');
    await waitFor(() => expect(screen.getByText('Second Series')).toBeInTheDocument());
    expect(screen.getByRole('alert')).toHaveTextContent('Could not load branching narrative badges');
    fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
    await waitFor(() => expect(screen.queryByRole('alert')).toBeNull());
    expect(screen.queryByText(/branching/)).toBeNull();
  });

  it('keeps a failed series read distinct from the empty list and retries it', async () => {
    listPipelineSeriesSummaries.mockRejectedValueOnce(new Error('offline')).mockResolvedValue([]);
    await renderPage();
    expect(await screen.findByRole('alert')).toHaveTextContent('Could not load series');
    expect(screen.queryByText(/No series yet/)).toBeNull();
    fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
    expect(await screen.findByText(/No series yet/)).toBeInTheDocument();
  });
});