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

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

vi.mock('../../../services/apiApps', () => ({ getAppQualityHistory: vi.fn().mockResolvedValue({ points: [] }) }));

vi.mock('../../../services/api', () => ({
  PORTOS_APP_ID: 'portos-default',
  getAppSpriteBindings: vi.fn(() => Promise.resolve({ bindings: [] })),
  deleteApp: vi.fn(() => Promise.resolve(null)),
  archiveApp: vi.fn(() => Promise.resolve({})),
  unarchiveApp: vi.fn(() => Promise.resolve({})),
  openAppInEditor: vi.fn(() => Promise.resolve({})),
  openAppFolder: vi.fn(() => Promise.resolve({})),
  openAppInXcode: vi.fn(() => Promise.resolve({ success: true })),
  refreshAppConfig: vi.fn(() => Promise.resolve({})),
  detectAppIcon: vi.fn(() => Promise.resolve({ detected: false })),
  installXcodeScripts: vi.fn(() => Promise.resolve({})),
}));

vi.mock('../../../hooks/useAppOperation', () => ({
  useAppOperation: vi.fn(() => ({
    steps: [],
    isOperating: false,
    operationType: null,
    error: null,
    completed: false,
    startUpdate: vi.fn(),
    startStandardize: vi.fn(),
  })),
}));

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

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

const APP = {
  id: 'app-1',
  name: 'Example App',
  type: 'static',
  repoPath: '/mock/example-app',
};

function renderOverview(app = APP) {
  return render(
    <MemoryRouter>
      <OverviewTab app={app} onRefresh={vi.fn()} />
    </MemoryRouter>,
  );
}

describe('OverviewTab PortOS registration removal', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    api.deleteApp.mockResolvedValue(null);
    api.getAppSpriteBindings.mockResolvedValue({ bindings: [] });
  });

  it('explains that removal keeps the repository and requires confirmation', async () => {
    const user = userEvent.setup();
    renderOverview();

    expect(screen.getByRole('button', { name: 'Remove from PortOS' })).toBeTruthy();
    expect(screen.getByText(/without deleting its repository from disk/)).toBeTruthy();

    await user.click(screen.getByRole('button', { name: 'Remove from PortOS' }));

    expect(screen.getByText('Remove Example App from PortOS? Its repository will stay on disk.')).toBeTruthy();
    expect(api.deleteApp).not.toHaveBeenCalled();
  });

  it('removes the association and returns to the app registry after confirmation', async () => {
    const user = userEvent.setup();
    renderOverview();

    await user.click(screen.getByRole('button', { name: 'Remove from PortOS' }));
    await user.click(screen.getByRole('button', { name: 'Remove' }));

    await waitFor(() => expect(api.deleteApp).toHaveBeenCalledWith('app-1'));
    expect(toast.success).toHaveBeenCalledWith('Example App removed from PortOS — files kept on disk');
  });

  it('does not offer removal for the PortOS baseline app', async () => {
    renderOverview({ ...APP, id: 'portos-default', name: 'PortOS' });

    expect(screen.queryByRole('button', { name: 'Remove from PortOS' })).toBeNull();
    await waitFor(() => expect(api.getAppSpriteBindings).toHaveBeenCalledWith('portos-default'));
  });
});

describe('OverviewTab sprite bindings', () => {
  beforeEach(() => vi.clearAllMocks());

  it('shows a load error with Retry when the bindings read fails, then the assets after retry', async () => {
    api.getAppSpriteBindings.mockRejectedValueOnce(new Error('bindings unavailable'));
    api.getAppSpriteBindings.mockResolvedValueOnce({ bindings: [{ recordId: 'sprite-1', name: 'Example Sprite', kind: 'character' }] });
    const user = userEvent.setup();
    renderOverview();

    expect(await screen.findByRole('alert')).toHaveTextContent(/could not load published sprite assets.*bindings unavailable/i);
    expect(screen.queryByText('Published Sprite Assets')).toBeNull();

    await user.click(screen.getByRole('button', { name: 'Retry' }));
    expect(await screen.findByRole('link', { name: 'Example Sprite' })).toBeTruthy();
    expect(screen.queryByRole('alert')).toBeNull();
    expect(api.getAppSpriteBindings).toHaveBeenCalledTimes(2);
  });
});

describe('OverviewTab native app actions', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    api.getAppSpriteBindings.mockResolvedValue({ bindings: [] });
    api.openAppInXcode.mockResolvedValue({ success: true });
  });

  afterEach(() => vi.unstubAllGlobals());

  it('keeps the Xcode action available from the native app detail workspace', async () => {
    const user = userEvent.setup();
    renderOverview({ ...APP, id: 'app-ios', name: 'Example iOS App', type: 'ios-native' });

    await user.click(screen.getByRole('button', { name: 'Open Example iOS App in Xcode' }));

    expect(api.openAppInXcode).toHaveBeenCalledWith('app-ios');
    await waitFor(() => expect(toast.success).toHaveBeenCalledWith('Opening Example iOS App in Xcode'));
  });

  it('surfaces a failed Xcode launch through the shared request error boundary', async () => {
    const { openAppInXcode } = await vi.importActual('../../../services/apiApps');
    api.openAppInXcode.mockImplementation(openAppInXcode);
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
      ok: false,
      status: 404,
      json: async () => ({ error: 'No Xcode project found', code: 'XCODE_PROJECT_NOT_FOUND' }),
    }));
    const user = userEvent.setup();
    renderOverview({ ...APP, id: 'app-ios', name: 'Example iOS App', type: 'ios-native' });

    await user.click(screen.getByRole('button', { name: 'Open Example iOS App in Xcode' }));

    await waitFor(() => expect(toast.error).toHaveBeenCalledWith('No Xcode project found'));
    expect(toast.error).toHaveBeenCalledTimes(1);
    expect(toast.success).not.toHaveBeenCalled();
    expect(screen.getByRole('button', { name: 'Open Example iOS App in Xcode' }).disabled).toBe(false);
  });
});

describe('OverviewTab launch video card', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    window.localStorage?.clear();
    api.getAppSpriteBindings.mockResolvedValue({ bindings: [] });
  });

  it('renders the launch video card for standard apps and permits dismissal', async () => {
    const user = userEvent.setup();
    renderOverview({ ...APP, id: 'standard-app', name: 'Standard App' });
    await waitFor(() => expect(api.getAppSpriteBindings).toHaveBeenCalledWith('standard-app'));

    expect(screen.getByRole('heading', { name: 'Launch videos' })).toBeTruthy();
    const dismissButton = screen.getByRole('button', { name: 'Dismiss launch video card' });
    expect(dismissButton).toBeTruthy();

    await user.click(dismissButton);

    expect(screen.queryByRole('heading', { name: 'Launch videos' })).toBeNull();
    expect(window.localStorage.getItem('portos:launch-video-card-dismissed:standard-app')).toBe('true');
  });

  it('does not render launch video card when previously dismissed', async () => {
    window.localStorage.setItem('portos:launch-video-card-dismissed:standard-app', 'true');
    renderOverview({ ...APP, id: 'standard-app', name: 'Standard App' });
    await waitFor(() => expect(api.getAppSpriteBindings).toHaveBeenCalledWith('standard-app'));

    expect(screen.queryByRole('heading', { name: 'Launch videos' })).toBeNull();
  });

  it('does not render launch video card for PortOS baseline app', async () => {
    renderOverview({ ...APP, id: 'portos-default', name: 'PortOS' });
    await waitFor(() => expect(api.getAppSpriteBindings).toHaveBeenCalledWith('portos-default'));

    expect(screen.queryByRole('heading', { name: 'Launch videos' })).toBeNull();
  });

  it('does not render launch video card for Eidoverse Worlds app', async () => {
    renderOverview({ ...APP, id: 'eidoverse-worlds', name: 'Eidoverse Worlds', pm2ProcessNames: ['eidoverse-worlds'] });
    await waitFor(() => expect(api.getAppSpriteBindings).toHaveBeenCalledWith('eidoverse-worlds'));

    expect(screen.queryByRole('heading', { name: 'Launch videos' })).toBeNull();
  });
});