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

5.7 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { MemoryRouter, Route, Routes } from 'react-router';

vi.mock('../services/apiWritersRoom', async (importOriginal) => ({
  ...await importOriginal(),
  listWritersRoomFolders: vi.fn(async () => []),
  listWritersRoomWorks: vi.fn(async () => ({ items: [], total: 0, nextCursor: null })),
  getWritersRoomWork: vi.fn(),
  createWritersRoomWork: vi.fn(async () => ({ id: 'work-new' })),
}));

import WritersRoom from './WritersRoom';
import { listWritersRoomFolders, listWritersRoomWorks, getWritersRoomWork } from '../services/apiWritersRoom';

describe('WritersRoom empty state', () => {
  it('shows a retriable error when the folder list fails to load', async () => {
    listWritersRoomFolders.mockRejectedValueOnce(new Error('offline')).mockResolvedValueOnce([]);
    render(<MemoryRouter><WritersRoom /></MemoryRouter>);
    expect(await screen.findByRole('alert')).toHaveTextContent('Could not load folders');
    fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
    await waitFor(() => expect(screen.queryByRole('alert')).toBeNull());
  });

  it('opens the existing New work form from the unselected work pane', async () => {
    render(
      <MemoryRouter initialEntries={['/writers-room']}>
        <Routes>
          <Route path="/writers-room" element={<WritersRoom />} />
          <Route path="/writers-room/works/:workId" element={<div>New work opened</div>} />
        </Routes>
      </MemoryRouter>,
    );

    const main = screen.getByRole('region', { name: 'Work editor' });
    expect(within(main).getByText('Create a work or pick one from the library to start writing.')).toBeInTheDocument();
    expect(within(main).queryByText(/Write for 10/)).toBeNull();

    fireEvent.click(screen.getByRole('button', { name: 'New folder' }));
    expect(screen.getByRole('textbox', { name: 'Folder name' })).toBeInTheDocument();
    fireEvent.click(within(main).getByRole('button', { name: 'New work' }));

    expect(await screen.findByRole('textbox', { name: 'Work title' })).toBeInTheDocument();
    expect(screen.getByRole('textbox', { name: 'Work title' })).toHaveFocus();
    expect(screen.queryByRole('textbox', { name: 'Folder name' })).toBeNull();
    expect(screen.getByRole('combobox', { name: 'Work kind' })).toBeInTheDocument();
    fireEvent.change(screen.getByRole('textbox', { name: 'Work title' }), { target: { value: 'A new draft' } });
    fireEvent.click(screen.getByRole('button', { name: 'Create' }));
    expect(await screen.findByText('New work opened')).toBeInTheDocument();
  });
});


it('loads 50 works, appends on demand, and restarts an expired snapshot', async () => {
  listWritersRoomWorks.mockResolvedValueOnce({ items: [{ id: 'a', title: 'First work' }], total: 2, nextCursor: 'page-two' })
    .mockResolvedValueOnce({ items: [{ id: 'b', title: 'Second work' }], total: 3, nextCursor: 'page-three' })
    .mockRejectedValueOnce(Object.assign(new Error('Library page expired. Restart the library to continue.'), { code: 'CURSOR_EXPIRED' }))
    .mockResolvedValueOnce({ items: [{ id: 'c', title: 'Fresh work' }], total: 1, nextCursor: null });
  render(<MemoryRouter><WritersRoom /></MemoryRouter>);
  expect(await screen.findByText('First work')).toBeInTheDocument();
  expect(screen.queryByText('Second work')).toBeNull();
  expect(listWritersRoomWorks).toHaveBeenLastCalledWith({ limit: 50, cursor: null }, expect.objectContaining({ silent: true }));
  fireEvent.click(screen.getByRole('button', { name: 'Load more works' }));
  expect(await screen.findByText('Second work')).toBeInTheDocument();
  expect(screen.getByText('First work')).toBeInTheDocument();
  expect(listWritersRoomWorks).toHaveBeenLastCalledWith({ limit: 50, cursor: 'page-two' }, expect.anything());
  fireEvent.click(screen.getByRole('button', { name: 'Load more works' }));
  fireEvent.click(await screen.findByRole('button', { name: 'Retry loading' }));
  expect(await screen.findByText('Fresh work')).toBeInTheDocument();
  expect(screen.queryByText('First work')).toBeNull();
  expect(listWritersRoomWorks).toHaveBeenLastCalledWith({ limit: 50, cursor: null }, expect.anything());
});

describe('WritersRoom open work failures', () => {
  const renderAt = (path) => render(
    <MemoryRouter initialEntries={[path]}>
      <Routes>
        <Route path="/writers-room" element={<WritersRoom />} />
        <Route path="/writers-room/works/:workId" element={<WritersRoom />} />
      </Routes>
    </MemoryRouter>,
  );

  it('shows a not-found panel for a missing work and returns to the library', async () => {
    getWritersRoomWork.mockRejectedValueOnce(Object.assign(new Error('Invalid work id'), { status: 400 }));
    renderAt('/writers-room/works/gone');
    expect(await screen.findByText('This work no longer exists')).toBeInTheDocument();
    expect(screen.queryByText('No work selected')).toBeNull();
    fireEvent.click(screen.getByRole('button', { name: 'Back to library' }));
    expect(await screen.findByText('No work selected')).toBeInTheDocument();
  });

  it('offers Retry after a failed read and opens the work once it recovers', async () => {
    getWritersRoomWork
      .mockRejectedValueOnce(Object.assign(new Error('offline'), { status: 503 }))
      .mockResolvedValueOnce({ id: 'w1', title: 'Recovered', drafts: [], activeDraftVersionId: null });
    renderAt('/writers-room/works/w1');
    expect(await screen.findByText("Couldn't open this work")).toBeInTheDocument();
    fireEvent.click(screen.getByRole('button', { name: 'Retry' }));
    await waitFor(() => expect(screen.queryByText("Couldn't open this work")).toBeNull());
    expect(getWritersRoomWork).toHaveBeenCalledTimes(2);
  });
});