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

18.0 KB
Raw Blame
import { describe, it, expect, vi } from 'vitest';
import { fireEvent, render, screen, within } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import RuntimeServersCard from './RuntimeServersCard.jsx';

// Every row renders the same status/action vocabulary, so a text query alone
// would match the wrong runtime. Scope to the row that names it.
const row = (label) => screen.getByRole('group', { name: label });

const renderCard = (props = {}) => {
  const handlers = {
    onRefresh: vi.fn(),
    onControlOllama: vi.fn(),
    onControlLmStudio: vi.fn(),
    onInstallBackend: vi.fn(),
    onInstallLlama: vi.fn(),
    onUpgradeLlama: vi.fn(),
    onStopLlama: vi.fn(),
    onConfigureLlama: vi.fn(),
    onConfigureMtplx: vi.fn(),
    onInstallMtplx: vi.fn(),
    onStartMtplx: vi.fn(),
    onSaveIdleWindow: vi.fn(),
    onStopMtplx: vi.fn(),
    onConfigureSlotstream: vi.fn(),
    onInstallSlotstream: vi.fn(),
    onStartSlotstream: vi.fn(),
    onStopSlotstream: vi.fn(),
    onSaveStartup: vi.fn(),
    onToggleKeepLoaded: vi.fn(),
  };
  render(
    <MemoryRouter>
      <RuntimeServersCard
        status={{ ollama: { canAutoInstall: true }, lmstudio: {} }}
        llamaStatus={null}
        mtplxStatus={null}
        loading={false}
        busy={false}
        actionInProgress={null}
        {...handlers}
        {...props}
      />
    </MemoryRouter>,
  );
  for (const summary of screen.queryAllByText('Startup & residency')) fireEvent.click(summary);
  return handlers;
};

describe('RuntimeServersCard', () => {
  it('lists every local runtime PortOS can run, not just the two catalog backends', () => {
    renderCard();
    for (const label of ['Ollama', 'LM Studio', 'llama.cpp', 'MTPLX', 'Slotstream']) {
      expect(screen.getByText(label)).toBeInTheDocument();
    }
  });

  it('keeps dense runtime rows in a bounded container-aware grid', () => {
    renderCard({
      llamaStatus: {
        installed: true,
        running: false,
        managed: true,
        runAtStartup: true,
        version: '0.4.0',
        latestVersion: '0.4.1',
        updateAvailable: true,
        canUpgrade: true,
      },
    });
    const llama = row('llama.cpp');

    expect(llama.className).toMatch(/\bflex-col\b/);
    expect(llama.className).toContain('@min-[48rem]:grid');
    expect(llama.className).toContain('@min-[48rem]:grid-cols-[10rem_minmax(0,1fr)_minmax(12rem,20rem)]');
    expect(llama.className).not.toContain('sm:w-44');
  });

  it('offers Start for an installed-but-stopped backend and Stop for a running one', () => {
    const handlers = renderCard({
      status: {
        ollama: { installed: true, available: false, modelCount: 2, baseUrl: 'http://localhost:11434' },
        lmstudio: { installed: true, available: true, modelCount: 1, baseUrl: 'http://localhost:1234' },
      },
    });

    fireEvent.click(within(row('Ollama')).getByRole('button', { name: /Start/ }));
    expect(handlers.onControlOllama).toHaveBeenCalledWith('start');

    fireEvent.click(within(row('LM Studio')).getByRole('button', { name: /Stop/ }));
    expect(handlers.onControlLmStudio).toHaveBeenCalledWith('stop');
  });

  it('will not offer to stop a PM2 runtime PortOS did not start', () => {
    renderCard({
      llamaStatus: { installed: true, running: true, managed: false, endpoint: 'http://127.0.0.1:5568/v1' },
    });
    const llama = row('llama.cpp');
    expect(within(llama).getByText('Running (external)')).toBeInTheDocument();
    expect(within(llama).queryByRole('button', { name: /Stop/ })).toBeNull();
  });

  it('sends llama.cpp to its launcher instead of offering a Start it cannot honour', () => {
    // llama-server takes a REQUIRED model path — a one-click Start here would
    // have to guess which multi-gigabyte GGUF the user meant.
    const handlers = renderCard({ llamaStatus: { installed: true, running: false } });
    const llama = row('llama.cpp');
    expect(within(llama).queryByRole('button', { name: /^Start/ })).toBeNull();
    expect(within(llama).getByText(/Choose Configure to pick a model/)).toBeInTheDocument();

    fireEvent.click(within(llama).getByRole('button', { name: /Configure/ }));
    expect(handlers.onConfigureLlama).toHaveBeenCalled();
  });

  it('shows a llama.cpp update and delegates the action to the runtime handler', () => {
    const handlers = renderCard({
      llamaStatus: {
        installed: true,
        running: false,
        version: '0.1.1-dev',
        latestVersion: '0.3.0',
        updateAvailable: true,
        canUpgrade: true,
      },
    });
    const llama = row('llama.cpp');

    expect(within(llama).getByText('v0.1.1-dev')).toBeInTheDocument();
    expect(within(llama).getByText('v0.3.0 available')).toBeInTheDocument();
    fireEvent.click(within(llama).getByRole('button', { name: 'Update to v0.3.0' }));

    expect(handlers.onUpgradeLlama).toHaveBeenCalledWith();
  });

  it('links to llama.cpp release notes when Homebrew cannot update the installation', () => {
    renderCard({
      llamaStatus: {
        installed: true,
        running: false,
        latestVersion: '0.3.0',
        updateAvailable: true,
        canUpgrade: false,
        downloadUrl: 'https://github.com/ggml-org/llama.cpp/releases',
      },
    });
    const llama = row('llama.cpp');

    expect(within(llama).queryByRole('button', { name: /Update to/ })).toBeNull();
    expect(within(llama).getByRole('link', { name: /Update available/ })).toHaveAttribute(
      'href',
      'https://github.com/ggml-org/llama.cpp/releases',
    );
  });

  // MTPLX can be started on demand or explicitly when a verified checkpoint is
  // already cached. Neither path downloads weights.
  it('offers MTPLX Start when a checkpoint is cached', () => {
    const handlers = renderCard({
      mtplxStatus: { installed: true, running: false, supported: true, cachedModels: ['Example/Qwen-MTP'], endpoint: 'http://127.0.0.1:8000/v1' },
    });
    fireEvent.click(within(row('MTPLX')).getByRole('button', { name: /^Start/ }));
    expect(handlers.onStartMtplx).toHaveBeenCalledWith();
  });

  it('keeps MTPLX Start unavailable when no checkpoint is cached', () => {
    renderCard({
      mtplxStatus: { installed: true, running: false, supported: true, cachedModels: [], endpoint: 'http://127.0.0.1:8000/v1' },
    });
    const mtplx = row('MTPLX');
    expect(within(mtplx).queryByRole('button', { name: /^Start/ })).toBeNull();
    expect(within(mtplx).getByText(/use Configure to download one/)).toBeInTheDocument();
  });

  // llama.cpp keeps its Stop: it is started explicitly from the launcher, and
  // its idle release keeps the process up rather than removing it.
  it('still offers Stop for a running MTPLX', () => {
    const handlers = renderCard({
      mtplxStatus: { installed: true, running: true, managed: true, supported: true, cachedModels: ['Example/Qwen-MTP'] },
    });
    fireEvent.click(within(row('MTPLX')).getByRole('button', { name: /^Stop/ }));
    expect(handlers.onStopMtplx).toHaveBeenCalled();
  });

  it('invokes a row action with NO arguments — never React\'s click event', () => {
    // A handler bound straight to `onClick` is handed React's SyntheticEvent as
    // its first argument, which throws on its circular DOM refs the moment a
    // handler tries to serialize it into a request body.
    const handlers = renderCard({
      mtplxStatus: { installed: true, running: true, managed: true, supported: true, cachedModels: ['Example/Qwen-MTP'] },
    });
    fireEvent.click(within(row('MTPLX')).getByRole('button', { name: /^Stop/ }));
    expect(handlers.onStopMtplx).toHaveBeenCalledWith();

    fireEvent.click(within(row('Ollama')).getByRole('button', { name: /Install/ }));
    expect(handlers.onInstallBackend).toHaveBeenCalledWith('ollama');
  });

  it('offers Slotstream Start when a checkpoint is cached and shows the memory plan', () => {
    const handlers = renderCard({
      slotstreamStatus: {
        installed: true,
        running: false,
        supported: true,
        cachedModels: ['qwen-moe'],
        memoryPlan: { targetGb: 22, expectedPeakGb: 22, expectedWarmDecodeToks: 8, auto: false },
      },
    });
    const slotstream = row('Slotstream');
    expect(within(slotstream).getByText(/target 22 GB/)).toBeInTheDocument();
    fireEvent.click(within(slotstream).getByRole('button', { name: /^Start/ }));
    expect(handlers.onStartSlotstream).toHaveBeenCalledWith();
  });

  it('keeps Slotstream Start unavailable when no checkpoint is cached', () => {
    renderCard({
      slotstreamStatus: { installed: true, running: false, supported: true, cachedModels: [], cacheError: null },
    });
    const slotstream = row('Slotstream');
    expect(within(slotstream).queryByRole('button', { name: /^Start/ })).toBeNull();
    expect(within(slotstream).getByText(/a start never fetches weights/i)).toBeInTheDocument();
  });

  it('says why Slotstream cannot start when its checkpoint cache is unreadable', () => {
    // An unreadable cache also reports zero checkpoints, so it withholds Start
    // too — without its own reason the row renders "stopped" with nothing to
    // click and no explanation of what to fix.
    renderCard({
      slotstreamStatus: {
        installed: true,
        running: false,
        supported: true,
        cachedModels: [],
        cacheError: 'could not read Slotstream cache (EACCES)',
      },
    });
    const slotstream = row('Slotstream');
    expect(within(slotstream).queryByRole('button', { name: /^Start/ })).toBeNull();
    expect(within(slotstream).getByText(/cache unreadable/i)).toHaveTextContent('EACCES');
  });

  it('points at the in-app download when the MTPLX cache is empty', () => {
    // Nothing downloads weights on its own, and `mtplx serve` exits before it
    // binds on an empty cache — so the row names the in-app fix (the Configure
    // card below) rather than a terminal command (PRD NR-9).
    renderCard({
      mtplxStatus: { installed: true, running: false, supported: true, cachedModels: [], cacheError: null },
    });
    const mtplx = row('MTPLX');
    expect(within(mtplx).queryByRole('button', { name: /^Start/ })).toBeNull();
    expect(within(mtplx).getByText(/use Configure to download one/)).toBeInTheDocument();
    expect(within(mtplx).queryByText(/terminal/i)).toBeNull();
  });

  it('reports a runtime this host cannot run without offering to install it', () => {
    renderCard({ mtplxStatus: { installed: false, running: false, supported: false, unsupportedReason: 'MTPLX runs only on macOS with Apple Silicon.' } });
    const mtplx = row('MTPLX');
    expect(within(mtplx).getByText('Unavailable on this platform')).toBeInTheDocument();
    expect(within(mtplx).queryByRole('button', { name: /Install/ })).toBeNull();
  });

  it('flags a PM2 runtime that is in the saved boot list', () => {
    renderCard({
      llamaStatus: { installed: true, running: true, managed: true, pid: 321, runAtStartup: true, endpoint: 'http://127.0.0.1:5568/v1' },
      mtplxStatus: { installed: true, running: true, managed: true, pid: 654, runAtStartup: false, supported: true, endpoint: 'http://127.0.0.1:8000/v1' },
    });
    expect(within(row('llama.cpp')).getByText('starts at boot')).toBeInTheDocument();
    expect(within(row('MTPLX')).queryByText('starts at boot')).toBeNull();
  });

  it('saves the PM2 process list so the running daemons survive a reboot', () => {
    const handlers = renderCard();
    fireEvent.click(screen.getByRole('button', { name: /Save PM2 list for reboot/ }));
    expect(handlers.onSaveStartup).toHaveBeenCalled();
  });

  it('links out to the vendor when PortOS cannot install a backend on this platform', () => {
    renderCard({
      status: {
        ollama: { installed: false, canAutoInstall: false, downloadUrl: 'https://ollama.com/download' },
        lmstudio: {},
      },
    });
    const ollama = row('Ollama');
    expect(within(ollama).queryByRole('button', { name: /Install/ })).toBeNull();
    expect(within(ollama).getByRole('link', { name: /Download/ })).toHaveAttribute('href', 'https://ollama.com/download');
  });

  // ===========================================================================
  // IDLE RELEASE
  // ===========================================================================
  describe('idle release window', () => {
    const idleField = (label) => within(row(label)).getByLabelText('Idle release');

    it('shows the saved window for each managed daemon', () => {
      renderCard({
        llamaStatus: { installed: true, running: true, idleMinutes: 30 },
        mtplxStatus: { installed: true, running: true, supported: true, idleMinutes: 15 },
      });
      expect(idleField('llama.cpp')).toHaveValue(30);
      expect(idleField('MTPLX')).toHaveValue(15);
    });

    it('saves the window on blur, per runtime', () => {
      const handlers = renderCard({ llamaStatus: { installed: true, running: true, idleMinutes: 0 } });
      const field = idleField('llama.cpp');
      fireEvent.change(field, { target: { value: '45' } });
      fireEvent.blur(field);
      expect(handlers.onSaveIdleWindow).toHaveBeenCalledWith('llama', 45);
    });

    // 0 is a real choice, not a cleared field — it reproduces the always-on
    // behaviour every install had before this setting existed.
    it('saves an explicit 0 and labels it "never"', () => {
      const handlers = renderCard({ llamaStatus: { installed: true, running: true, idleMinutes: 30 } });
      const field = idleField('llama.cpp');
      fireEvent.change(field, { target: { value: '0' } });
      fireEvent.blur(field);
      expect(handlers.onSaveIdleWindow).toHaveBeenCalledWith('llama', 0);
      expect(within(row('llama.cpp')).getByText(/never/)).toBeInTheDocument();
    });

    it('does not re-save a value that did not change', () => {
      const handlers = renderCard({ llamaStatus: { installed: true, running: true, idleMinutes: 30 } });
      fireEvent.blur(idleField('llama.cpp'));
      expect(handlers.onSaveIdleWindow).not.toHaveBeenCalled();
    });

    it('reverts a nonsensical value instead of saving it', () => {
      const handlers = renderCard({ llamaStatus: { installed: true, running: true, idleMinutes: 30 } });
      const field = idleField('llama.cpp');
      fireEvent.change(field, { target: { value: '-5' } });
      fireEvent.blur(field);
      expect(handlers.onSaveIdleWindow).not.toHaveBeenCalled();
      expect(field).toHaveValue(30);
    });

    // A window longer than a day is indistinguishable from "never" and is far
    // likelier a units mix-up (seconds typed into a minutes field).
    it('clamps a window longer than a day', () => {
      const handlers = renderCard({ llamaStatus: { installed: true, running: true, idleMinutes: 0 } });
      const field = idleField('llama.cpp');
      fireEvent.change(field, { target: { value: '99999' } });
      fireEvent.blur(field);
      expect(handlers.onSaveIdleWindow).toHaveBeenCalledWith('llama', 1440);
    });

    it('offers no window for a runtime that is not installed', () => {
      renderCard({ llamaStatus: { installed: false, running: false } });
      expect(within(row('llama.cpp')).queryByLabelText('Idle release')).toBeNull();
    });

    // Ollama and LM Studio own their own lifecycles — PortOS has no process to
    // stop and no flag to pass, so offering the control would be a lie.
    it('offers no window for the runtimes PortOS does not manage as PM2 processes', () => {
      renderCard({});
      expect(within(row('Ollama')).queryByLabelText('Idle release')).toBeNull();
      expect(within(row('LM Studio')).queryByLabelText('Idle release')).toBeNull();
    });

    it('warns that only PortOS traffic counts', () => {
      renderCard({});
      expect(screen.getByText(/Only PortOS traffic counts/)).toBeInTheDocument();
    });

    it('shows the Keep loaded toggle and fires onToggleKeepLoaded when clicked', () => {
      const handlers = renderCard({
        mtplxStatus: { installed: true, running: true, supported: true, keepLoaded: false },
      });
      const checkbox = within(row('MTPLX')).getByRole('checkbox', { name: 'Keep MTPLX loaded' });
      expect(checkbox).not.toBeChecked();

      fireEvent.click(checkbox);
      expect(handlers.onToggleKeepLoaded).toHaveBeenCalledWith('mtplx', true);
    });

    it('disables the idle release field when Keep loaded is checked', () => {
      renderCard({
        mtplxStatus: { installed: true, running: true, supported: true, keepLoaded: true },
      });
      const checkbox = within(row('MTPLX')).getByRole('checkbox', { name: 'Keep MTPLX loaded' });
      expect(checkbox).toBeChecked();
      expect(idleField('MTPLX')).toBeDisabled();
    });
  });

  // ===========================================================================
  // RELEASE REASON (MEMORY PRESSURE)
  // ===========================================================================
  describe('release reason display', () => {
    it('shows host memory pressure release reason when daemon is stopped', () => {
      renderCard({
        slotstreamStatus: {
          installed: true,
          running: false,
          supported: true,
          releaseReason: 'released at 09:14 — host memory pressure',
        },
      });
      const slotstream = row('Slotstream');
      expect(within(slotstream).getByText('released at 09:14 — host memory pressure')).toBeInTheDocument();
    });

    it('hides release reason when daemon is running', () => {
      renderCard({
        slotstreamStatus: {
          installed: true,
          running: true,
          supported: true,
          releaseReason: 'released at 09:14 — host memory pressure',
        },
      });
      const slotstream = row('Slotstream');
      expect(within(slotstream).queryByText('released at 09:14 — host memory pressure')).toBeNull();
    });
  });
});

it('does not claim an unread runtime is uninstalled or offer installation', () => {
  renderCard({ status: null, llamaStatus: null, mtplxStatus: null, slotstreamStatus: null });
  for (const name of ['Ollama', 'LM Studio', 'llama.cpp', 'MTPLX', 'Slotstream']) {
    const runtime = within(row(name));
    expect(runtime.getByText('Status unavailable')).toBeInTheDocument();
    expect(runtime.queryByRole('button', { name: 'Install' })).not.toBeInTheDocument();
  }
});