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

6.2 KB
Raw Blame
import { describe, it, expect, vi, afterEach } from 'vitest';
import { act, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import RunTaskButton from './RunTaskButton';

const APPS = [
  { id: 'app-1', name: 'Example App', repoPath: '~/code/example-app' },
  { id: 'app-2', name: 'Second App', archived: true },
];

// Place the trigger near the right edge of a phone-width viewport — the case
// that used to push the left-anchored panel off-screen.
function stubViewport({ width = 390, height = 780, rect } = {}) {
  Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: width });
  Object.defineProperty(window, 'innerHeight', { writable: true, configurable: true, value: height });
  vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
    top: 600, bottom: 640, left: 300, right: 360, width: 60, height: 200, x: 300, y: 600,
    ...rect,
  });
}

afterEach(() => {
  vi.restoreAllMocks();
});

describe('RunTaskButton', () => {
  it('fires a plain global run when no active apps exist', async () => {
    const user = userEvent.setup();
    const onTrigger = vi.fn();
    render(<RunTaskButton taskType="review" apps={[]} onTrigger={onTrigger} />);
    await user.click(screen.getByRole('button', { name: /Run Now/i }));
    expect(onTrigger).toHaveBeenCalledWith('review');
  });

  it('runs an install-wide task with NO app even when apps exist', async () => {
    // The regression this guards: an install-wide sweep dispatches once for the
    // WHOLE install, but the picker only ever calls onTrigger WITH an appId. Left
    // to the default branch, every click on a machine that has apps would quietly
    // reduce the sweep to a single repo, and the install-wide lane would be
    // unreachable from the UI entirely.
    const user = userEvent.setup();
    const onTrigger = vi.fn();
    render(<RunTaskButton taskType="repo-sync" apps={APPS} onTrigger={onTrigger} installWide />);
    await user.click(screen.getByRole('button', { name: /Run on All Apps/i }));
    expect(onTrigger).toHaveBeenCalledWith('repo-sync');
    expect(onTrigger.mock.calls[0]).toHaveLength(1);
    expect(screen.queryByText('Example App')).toBeNull();
  });

  it('runs a programmatic task with NO app, and without the all-apps label', async () => {
    // A programmatic handler acts on PortOS's own records; the server rejects
    // a request that names a managed app, so the picker would produce a dead
    // button on any install that has apps. It is not an install-wide SWEEP
    // either, so it must not claim to run on all of them.
    const user = userEvent.setup();
    const onTrigger = vi.fn();
    render(<RunTaskButton taskType="universe-bible-images" apps={APPS} onTrigger={onTrigger} programmatic />);
    expect(screen.queryByRole('button', { name: /Run on All Apps/i })).toBeNull();
    await user.click(screen.getByRole('button', { name: /Run Now/i }));
    expect(onTrigger).toHaveBeenCalledWith('universe-bible-images');
    expect(onTrigger.mock.calls[0]).toHaveLength(1);
  });

  it('lists only active apps and runs the task on the picked one', async () => {
    const user = userEvent.setup();
    const onTrigger = vi.fn();
    render(<RunTaskButton taskType="review" apps={APPS} onTrigger={onTrigger} />);
    await user.click(screen.getByRole('button', { name: /Run on App/i }));
    expect(screen.queryByText('Second App')).toBeNull();
    await user.click(screen.getByText('Example App'));
    expect(onTrigger).toHaveBeenCalledWith('review', 'app-1');
    // Picking an app dismisses the panel.
    expect(screen.queryByText('Example App')).toBeNull();
  });

  it('shows sending progress and keeps a receipt at the app-card action', async () => {
    const user = userEvent.setup();
    let resolveTrigger;
    const onTrigger = vi.fn(() => new Promise(resolve => { resolveTrigger = resolve; }));
    render(<RunTaskButton taskType="review" apps={APPS} onTrigger={onTrigger} />);

    await user.click(screen.getByRole('button', { name: /Run on App/i }));
    await user.click(screen.getByText('Example App'));

    expect(screen.getByRole('button', { name: 'Sending…' })).toBeDisabled();
    expect(screen.getByRole('status')).toHaveTextContent('Sending request');

    await act(async () => { resolveTrigger({ id: 'request-1' }); });

    expect(screen.getByRole('status')).toHaveTextContent('Request queued for Example App');
    expect(screen.getByText('Request queued for Example App')).toBeVisible();
    expect(screen.getByRole('status')).toHaveTextContent('Checks and available capacity determine when an agent appears');
    expect(screen.getByRole('button', { name: /Run on App/i })).toBeEnabled();
  });

  it('keeps the app panel inside the viewport on a phone-width screen', async () => {
    stubViewport();
    const user = userEvent.setup();
    render(<RunTaskButton taskType="review" apps={APPS} onTrigger={vi.fn()} />);
    await user.click(screen.getByRole('button', { name: /Run on App/i }));

    const panel = screen.getByText('Example App').closest('div.fixed');
    expect(panel).toBeTruthy();
    const left = Number.parseFloat(panel.style.left);
    const width = Number.parseFloat(panel.style.width);
    expect(left).toBeGreaterThanOrEqual(8);
    // The pre-migration panel was pinned to the trigger's left edge and only
    // narrowed by max-w, so it ran past the right edge instead of moving.
    expect(left + width).toBeLessThanOrEqual(window.innerWidth - 8);
  });

  it("moves focus into the app panel and Escape closes it, returning focus to the trigger", async () => {
    const user = userEvent.setup();
    render(<RunTaskButton taskType="review" apps={APPS} onTrigger={vi.fn()} />);
    const trigger = screen.getByRole("button", { name: /Run on App/i });
    expect(trigger.getAttribute("aria-haspopup")).toBe("true");
    await user.click(trigger);
    expect(trigger.getAttribute("aria-expanded")).toBe("true");
    expect(document.activeElement).toBe(screen.getByText("Example App").closest("button"));

    await user.keyboard("{Escape}");
    expect(screen.queryByText("Example App")).toBeNull();
    expect(trigger.getAttribute("aria-expanded")).toBe("false");
    expect(document.activeElement).toBe(trigger);
  });
});