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.7 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen, within, act } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
import NeedsAttentionBanner from './NeedsAttentionBanner.jsx';
import MusicVideoLayout from './MusicVideoLayout.jsx';
import { MUSIC_VIDEO_STAGES } from '../../lib/musicVideoStages.js';
import { deriveAttentionItems, revealAttention } from '../../lib/musicVideoAttention.js';

const renderWithRouter = (ui) => render(<MemoryRouter>{ui}</MemoryRouter>);

const actions = () => ({
  onResumeRevision: vi.fn(), onCancelRevision: vi.fn(), onResumeCastAndSets: vi.fn(),
  onContinueAutoReview: vi.fn(), onCancelAutoReview: vi.fn(), onReattachRender: vi.fn(),
});

const stranded = {
  id: 'mv-example',
  status: 'rendering',
  scenes: [{ sceneId: 'scene-a' }],
  castAndSets: { status: 'imaging', interrupted: true },
  revisions: [{ id: 'mvrev-open', status: 'open', sections: [{ sceneId: 'scene-a', kind: 'video', verdict: 'rejected' }] }],
  autoReviews: [],
};

describe('NeedsAttentionBanner (#9940)', () => {
  it('renders nothing when there is nothing to attend to', () => {
    const { container } = renderWithRouter(<NeedsAttentionBanner items={[]} />);
    expect(container).toBeEmptyDOMElement();
  });

  it('settles each stale approval in place: Keep approved, or Revert a kept input', () => {
    const handlers = { onKeepApproval: vi.fn(), onRevertApproval: vi.fn() };
    const readiness = {
      castAndSets: { approved: true, stale: { changedFields: ['style'], revertible: ['style'] } },
      art: { approved: false, problems: [], stale: { changedFields: ['cast'] } },
      storyboard: { approved: false, problems: ['Cover the master with timed shots.'], stale: { changedFields: ['concept'], revertible: ['concept'] } },
    };
    renderWithRouter(<NeedsAttentionBanner items={deriveAttentionItems({ id: 'mv-example', scenes: [] }, { readiness })} actions={handlers} />);
    const banner = screen.getByRole('region', { name: 'Needs attention' });
    fireEvent.click(within(banner).getByRole('button', { name: 'Keep Cast & Sets check-in approved' }));
    expect(handlers.onKeepApproval).toHaveBeenCalledWith('castAndSets');
    fireEvent.click(within(banner).getByRole('button', { name: 'Keep Art direction approved' }));
    expect(handlers.onKeepApproval).toHaveBeenCalledWith('art');
    // A storyboard with other open problems can't simply be kept; its changed input can still be put back.
    expect(within(banner).queryByRole('button', { name: 'Keep Timed storyboard approved' })).toBeNull();
    fireEvent.click(within(banner).getByRole('button', { name: 'Revert concept on Timed storyboard' }));
    expect(handlers.onRevertApproval).toHaveBeenCalledWith('storyboard', 'concept');
  });

  it('folds many reverts behind one toggle so Keep approved stays the obvious choice', () => {
    const revertible = Array.from({ length: 26 }, (_, i) => `scene ${i + 1} prompt`);
    const readiness = { storyboard: { approved: false, problems: [], stale: { changedFields: ['storyboard shots', ...revertible], revertible } } };
    renderWithRouter(<NeedsAttentionBanner items={deriveAttentionItems({ id: 'mv-example', scenes: [] }, { readiness })} actions={{ onKeepApproval: vi.fn(), onRevertApproval: vi.fn() }} />);
    const banner = screen.getByRole('region', { name: 'Needs attention' });
    expect(banner).toHaveTextContent('changed since: storyboard shots, 26 scene prompts.');
    expect(within(banner).getByText('See 26 changes to Timed storyboard you can undo').closest('details').open).toBe(false);
    expect(within(banner).getByRole('button', { name: 'Keep Timed storyboard approved' })).toBeVisible();
  });

  it('offers Resume and Cancel for an open revision, wired to the id the project holds', () => {
    const handlers = actions();
    renderWithRouter(<NeedsAttentionBanner items={deriveAttentionItems({ ...stranded, status: 'complete', castAndSets: null })} actions={handlers} />);
    const banner = screen.getByRole('region', { name: 'Needs attention' });
    expect(banner).toHaveTextContent('A section revision is open');
    expect(within(banner).getByRole('link', { name: /scene 1/i })).toHaveAttribute('href', '/music-video/mv-example/board/scene/scene-a');
    fireEvent.click(within(banner).getByRole('button', { name: 'Resume the open revision' }));
    expect(handlers.onResumeRevision).toHaveBeenCalledWith('mvrev-open');
    fireEvent.click(within(banner).getByRole('button', { name: 'Cancel the open revision' }));
    expect(handlers.onCancelRevision).toHaveBeenCalledWith('mvrev-open');
  });

  it('covers an interrupted check-in, a waiting auto-review and an unwatched final render, each with its own exit', () => {
    const handlers = actions();
    const project = {
      ...stranded,
      revisions: [{ id: 'mvrev-owned', status: 'open', sections: [{ sceneId: 'scene-a', kind: 'image', verdict: 'rejected' }] }],
      autoReviews: [{ id: 'mvar-example', status: 'running', attempts: [{ n: 1, revisionId: 'mvrev-owned' }] }],
    };
    renderWithRouter(<NeedsAttentionBanner items={deriveAttentionItems(project)} actions={handlers} />);
    fireEvent.click(screen.getByRole('button', { name: 'Resume the Cast & Sets check-in' }));
    expect(handlers.onResumeCastAndSets).toHaveBeenCalledTimes(1);
    fireEvent.click(screen.getByRole('button', { name: 'Continue the auto-review run' }));
    expect(handlers.onContinueAutoReview).toHaveBeenCalledWith('mvar-example');
    fireEvent.click(screen.getByRole('button', { name: 'Cancel the auto-review run' }));
    expect(handlers.onCancelAutoReview).toHaveBeenCalledWith('mvar-example');
    fireEvent.click(screen.getByRole('button', { name: 'Reattach to the final render' }));
    expect(handlers.onReattachRender).toHaveBeenCalledTimes(1);
    // The run owns its revision: no second, competing Resume for it.
    expect(screen.queryByRole('button', { name: 'Resume the open revision' })).not.toBeInTheDocument();
  });

  it('shows only Cancel while a revision re-renders, and disables every exit while a request is busy', () => {
    const rendering = { ...stranded, status: 'complete', castAndSets: null, revisions: [{ ...stranded.revisions[0], status: 'rendering' }] };
    renderWithRouter(<NeedsAttentionBanner items={deriveAttentionItems(rendering)} busy actions={actions()} />);
    expect(screen.queryByRole('button', { name: 'Resume the open revision' })).not.toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Cancel the open revision' })).toBeDisabled();
  });

  it('sits in the Music Video header under the status line, on every stage', () => {
    renderWithRouter(<MusicVideoLayout project={{ id: 'example-project', name: 'Example Project' }} stage="board"
      onStageChange={() => {}} progress={{ current: 'board', stages: MUSIC_VIDEO_STAGES.map((stage) => ({ ...stage, state: 'todo' })) }}
      spend={{ spentUsd: 0 }}
      status={{ headline: 'Stage 3 of 7: Board', tone: 'muted', facts: [] }}
      attention={<NeedsAttentionBanner items={deriveAttentionItems(stranded)} actions={actions()} />} />);
    const header = screen.getByRole('banner');
    expect(within(header).getByRole('region', { name: 'Needs attention' })).toBeInTheDocument();
    // Both live in the sticky header, so a director never has to hunt for the way out.
    expect(within(header).getByRole('status', { name: 'Project status' })).toBeInTheDocument();
  });

  it('opens the folded chip when a refusal toast reveals it', () => {
    render(<NeedsAttentionBanner items={deriveAttentionItems({ ...stranded, status: 'complete', revisions: [], autoReviews: [] })} actions={actions()} />);
    const chip = screen.getByRole('button', { name: '1 needs attention' });
    let revealed;
    act(() => { revealed = revealAttention(); });
    expect(revealed).toBe(true);
    expect(chip).toHaveAttribute('aria-expanded', 'true');
    expect(chip).toHaveFocus();
    // A second reveal leaves the open banner open.
    act(() => { revealAttention(); });
    expect(chip).toHaveAttribute('aria-expanded', 'true');
  });

  it('folds to a count chip at every width and expands on tap (#10170)', () => {
    render(<NeedsAttentionBanner items={deriveAttentionItems({ ...stranded, status: 'complete', revisions: [], autoReviews: [] })} actions={actions()} />);
    const chip = screen.getByRole('button', { name: '1 needs attention' });
    expect(chip).not.toHaveClass('sm:hidden');
    expect(chip).toHaveAttribute('aria-expanded', 'false');
    expect(chip.parentElement.querySelector(':scope > .hidden')).toBeTruthy();
    fireEvent.click(chip);
    expect(chip).toHaveAttribute('aria-expanded', 'true');
    expect(chip.parentElement.querySelector(':scope > .hidden')).toBeNull();
    expect(screen.getByRole('region', { name: 'Needs attention' })).toHaveTextContent('Cast & Sets');
  });
});