Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

7.0 KB
Raw Blame
/* @vitest-environment jsdom */

import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom';
import type { Navigator } from 'react-router-dom';

import { RouteSurfaceActivityBoundary } from '../RouteSurfaceActivityBoundary';
import { ShowPageRoute } from './ShowPageRoute';

const api = vi.hoisted(() => ({
  getSession: vi.fn(),
  getShowPages: vi.fn(),
  connectWorkbenchEvents: vi.fn(),
}));
const shareControl = vi.hoisted(() => ({ canManageInstance: undefined as boolean | undefined }));
const viewport = vi.hoisted(() => ({ isDesktop: false }));
const wm = vi.hoisted(() => ({
  windows: [] as unknown[],
  openApp: vi.fn(),
  focus: vi.fn(),
  restore: vi.fn(),
}));

vi.mock('../../context/ApiContext', () => ({ useApi: () => api }));
vi.mock('../../context/DockContext', () => ({ useDock: () => ({ unpin: vi.fn() }) }));
vi.mock('../../context/InstanceAuthorizationContext', () => ({
  useInstanceAuthorization: () => ({ capabilities: { can_manage_instance: true } }),
}));
vi.mock('../../context/WindowManagerContext', () => ({ useWindowManager: () => wm }));
vi.mock('../../context/showPageDrag', () => ({
  useShowPageDrag: () => ({ active: false, begin: vi.fn(), end: vi.fn(), dropToDock: vi.fn() }),
}));
vi.mock('../../lib/useIsDesktop', () => ({ useIsDesktop: () => viewport.isDesktop }));
vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
vi.mock('../workbench/ShowPageShareControl', () => ({
  ShowPageShareControl: ({ canManageInstance }: { canManageInstance?: boolean }) => {
    shareControl.canManageInstance = canManageInstance;
    return <button type="button" aria-label="chat.showPage.share" />;
  },
}));

const LocationProbe = () => {
  const location = useLocation();
  return <output data-testid="location">{location.pathname}{location.search}</output>;
};

beforeEach(() => {
  viewport.isDesktop = false;
  wm.windows = [];
});

afterEach(() => {
  cleanup();
  vi.clearAllMocks();
  shareControl.canManageInstance = undefined;
});

describe('mobile Show Page app route', () => {
  it('uses one full-width header with back, annotation, chat, and share controls', async () => {
    api.getSession.mockResolvedValue({ id: 'session-1', status: 'active', title: 'Release dashboard' });
    api.getShowPages.mockResolvedValue({ pages: [] });
    api.connectWorkbenchEvents.mockReturnValue(vi.fn());

    render(
      <MemoryRouter initialEntries={['/apps/show/session-1']}>
        <Routes>
          <Route path="/apps/show/:sessionId" element={<ShowPageRoute />} />
          <Route path="/chat/:sessionId" element={null} />
        </Routes>
        <LocationProbe />
      </MemoryRouter>,
    );

    expect(await screen.findByText('Release dashboard')).toBeTruthy();
    const header = screen.getByRole('banner');
    expect(header.querySelectorAll('button')).toHaveLength(4);
    expect(header.querySelector('a')).toBeNull();
    expect(screen.getByRole('button', { name: 'common.back' })).toBeTruthy();
    expect(screen.getByRole('button', { name: 'chat.showPage.annotate.unavailable' })).toBeTruthy();
    expect(screen.getByRole('button', { name: 'chat.showPage.backToChat' })).toBeTruthy();
    expect(screen.getByRole('button', { name: 'chat.showPage.share' })).toBeTruthy();
    expect(shareControl.canManageInstance).toBe(true);

    const surface = header.parentElement;
    expect(surface?.className).toContain('h-full');
    expect(surface?.className).toContain('w-full');
    expect(surface?.className).toContain('pb-[env(safe-area-inset-bottom)]');
    expect(surface?.className).not.toContain('rounded');
    expect(surface?.className).not.toContain('border border-border');
    expect(screen.getByTitle('chat.showPage.title').getAttribute('src')).toBe('/show/session-1/?vibe-embed=1');

    fireEvent.click(screen.getByRole('button', { name: 'chat.showPage.backToChat' }));
    expect(screen.getByTestId('location').textContent).toBe('/chat/session-1?view=chat');
  });

  it('keeps action controls out of the missing-session placeholder', async () => {
    api.getSession.mockResolvedValue(null);

    render(
      <MemoryRouter initialEntries={['/apps/show/session-1']}>
        <Routes>
          <Route path="/apps/show/:sessionId" element={<ShowPageRoute />} />
        </Routes>
      </MemoryRouter>,
    );

    const header = await screen.findByRole('banner');
    expect(await screen.findByText('apps.showPage.missingTitle')).toBeTruthy();
    expect(header.querySelectorAll('button')).toHaveLength(1);
    expect(screen.getByRole('button', { name: 'common.back' })).toBeTruthy();
    expect(screen.queryByRole('button', { name: 'chat.showPage.annotate.unavailable' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'chat.showPage.backToChat' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'chat.showPage.share' })).toBeNull();
  });
});

// Desktop turns this route into a handoff: open the window, then get out of the
// way. It is a command, not a place — and because it is lazily loaded, Settings
// can take the foreground before its chunk arrives. It runs anyway, both halves,
// because a command held back is a command that fires later, once the user has
// asked for something else. Neither half reaches over the top of Settings: the
// window manager withholds the foreground announcement from a retired surface,
// and the surface takes the redirect through its own replacement.
describe('desktop Show Page window handoff', () => {
  const renderRoute = (active: boolean, inactiveReplace?: Navigator['replace']) => render(
    <MemoryRouter initialEntries={['/apps/show/session-1']}>
      <RouteSurfaceActivityBoundary active={active} inactiveReplace={inactiveReplace}>
        <Routes>
          <Route path="/apps/show/:sessionId" element={<ShowPageRoute />} />
          <Route path="/" element={<div data-testid="canvas" />} />
        </Routes>
      </RouteSurfaceActivityBoundary>
      <LocationProbe />
    </MemoryRouter>,
  );

  it('runs whole while the surface is retired, handing the redirect to it', () => {
    viewport.isDesktop = true;
    const inactiveReplace = vi.fn();
    renderRoute(false, inactiveReplace);

    expect(wm.openApp).toHaveBeenCalledWith('showpage', { params: { sessionId: 'session-1' } });
    // The retired surface decides where its own origin goes — here it just
    // records the ask — so the foreground url is untouched while Settings owns it.
    expect(inactiveReplace).toHaveBeenCalledTimes(1);
    expect(inactiveReplace.mock.calls[0][0]).toMatchObject({ pathname: '/' });
    expect(screen.getByTestId('location').textContent).toBe('/apps/show/session-1');
  });

  it('opens the window and hands back to the canvas on the live surface', () => {
    viewport.isDesktop = true;
    renderRoute(true);

    expect(wm.openApp).toHaveBeenCalledWith('showpage', { params: { sessionId: 'session-1' } });
    expect(screen.getByTestId('location').textContent).toBe('/');
  });
});