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

14.7 KB
Raw Blame
/**
 * Shared mock scaffold for the VideoGen page suites.
 *
 * Six suites (`pages/VideoGen.terms`, `.federatedTarget`, `.composeWhileBusy`,
 * `.textEncoderAutoDownload`, `.modelLoading`, `.remix`) each used to carry a
 * near-verbatim ~140-line copy of the same 25 `vi.mock` registrations, the same
 * `state` object, the same model fixture and the same `renderPage()` helper.
 * Every endpoint or hook the page started calling had to be added in five
 * places, or four suites broke at once.
 *
 * **Importing this module registers the mocks** — that is the whole point, and it
 * is what the vitest hoisting rules allow. `vi.mock` is hoisted to the top of the
 * file it is written in, so these registrations run when this module is evaluated,
 * which is while the importing test file's static imports are being resolved and
 * therefore before its `await loadVideoGenPage()`. The relative specifiers resolve
 * identically from here and from `pages/` (`src/test/../services/api` and
 * `src/pages/../services/api` are the same module), so the mocked paths are the
 * ones the page itself imports.
 *
 * Every mock reads through the exported `state`, so a suite varies behavior by
 * assigning to it in `beforeEach` rather than by re-registering a mock. Call
 * `resetVideoGenMockState()` first to get the documented defaults back.
 */

import { act, render } from '@testing-library/react';
import { MemoryRouter, useNavigate } from 'react-router';
import { vi } from 'vitest';
import { MockEventSource } from './mockEventSource';

/** Universe style the stub picker hands to the page when its button is clicked. */
const DEFAULT_UNIVERSE_STYLE = {
  id: 'u-1',
  name: 'Example Universe',
  influences: { embrace: ['inky linework'], avoid: ['glossy'] },
};

/**
 * Every knob the suites vary. Mutated in `beforeEach`; read lazily by the
 * mock factories below, so a reassignment takes effect on the next render.
 */
export const state = {
  /** `GET /api/instances` peers — a media-provider peer makes the target picker appear. */
  peers: [],
  /** `getVideoGenStatus`; a spy so a suite can defer it, count calls or vary the payload. */
  getVideoGenStatus: vi.fn(),
  /**
   * `getVideoGenModelContext`; the probe-free half the Model picker reads (#5835).
   * A spy for the same reasons — a suite defers it to assert the page mid-flight.
   */
  getVideoGenModelContext: vi.fn(),
  generateVideo: vi.fn(),
  /** The form's prompt preview (#11317); resolves null (no panel) unless a test sets one. */
  previewVideoPrompt: vi.fn(),
  activeJob: null,
  /** Cache-status entries keyed by download id, read by the default `getModelStatus`. */
  modelStatuses: {},
  /** `useModelDownloadStatus().extra` — carries the SHARED text encoder's cache status. */
  modelDownloadExtra: {},
  modelDownloading: false,
  /** Override to answer ids the map cannot (e.g. the `__text_encoder_option__:` prefix). */
  getModelStatus: (id) => state.modelStatuses[id] ?? null,
  start: vi.fn(),
  startWhenIdle: vi.fn(),
  queuedModelId: null,
  repair: vi.fn(),
  cancel: vi.fn(),
  refresh: vi.fn(),
  /** `RuntimeInstallModal`'s `onComplete`, captured so a suite can fire it. */
  runtimeInstallComplete: null,
  universeStyle: DEFAULT_UNIVERSE_STYLE,
  /** `GET /api/video-gen/history`; a spy so a suite can defer it, reject it, or seed records. */
  listVideoHistory: vi.fn(),
  /** `GET /api/video-gen/history/:id`, the fallback for a record older than the page load. */
  getVideoHistoryItem: vi.fn(),
  /**
   * Last props handed to the panels below, captured by mocks that still render
   * NOTHING — the five older suites assert over the DOM, so a probe element
   * here would change what they see. The Remix suite reads the restored
   * sampler/LoRA values and drives the in-page Remix through
   * `galleryProps.onRemix`, which is the page's real gallery handler.
   */
  advancedParams: null,
  loraPicker: null,
  galleryProps: null,
  /** `useMediaCompletionRefresh`'s options, so a suite can fire a completion refresh. */
  completionRefresh: null,
  /** The library `listLorasFull` resolves to by default; the LoRA picker reads names from it. */
  availableLoras: [],
  /** `listLorasFull`; a spy so a suite can defer it and land the library AFTER history. */
  listLorasFull: vi.fn(),
  /**
   * The mounted router's `navigate`, for a suite that needs a SECOND in-place
   * navigation — a URL handoff arriving while the page is already mounted,
   * which is a different code path from a fresh mount on that URL.
   */
  navigate: null,
  /** `getVideoGenRuntimeStatus` answer for a BYOV runtime probe. */
  runtimeStatus: { installed: true, ready: true, current: true },
};

const SPIES = ['getVideoGenStatus', 'getVideoGenModelContext', 'generateVideo', 'previewVideoPrompt', 'start', 'startWhenIdle', 'repair', 'cancel', 'refresh', 'listVideoHistory', 'getVideoHistoryItem', 'listLorasFull'];

/** Restore every documented default, including fresh spies. Call it first in `beforeEach`. */
export function resetVideoGenMockState() {
  state.peers = [];
  state.settings = null;
  state.activeJob = null;
  state.modelStatuses = {};
  state.modelDownloadExtra = {};
  state.modelDownloading = false;
  state.getModelStatus = (id) => state.modelStatuses[id] ?? null;
  state.queuedModelId = null;
  state.runtimeInstallComplete = null;
  state.universeStyle = DEFAULT_UNIVERSE_STYLE;
  // The real media-run hook opens this for a resumed/started render; a test
  // drives its frames through `lastEventSource()`.
  MockEventSource.reset();
  globalThis.EventSource = MockEventSource;
  state.advancedParams = null;
  state.loraPicker = null;
  state.galleryProps = null;
  state.completionRefresh = null;
  state.availableLoras = [];
  state.navigate = null;
  state.runtimeStatus = { installed: true, ready: true, current: true };
  for (const key of SPIES) state[key].mockReset();
  state.listVideoHistory.mockResolvedValue([]);
  state.getVideoHistoryItem.mockRejectedValue(Object.assign(new Error('Video history entry not found'), { status: 404 }));
  state.previewVideoPrompt.mockResolvedValue(null);
  state.listLorasFull.mockImplementation(async () => state.availableLoras);
}

/** A video model as `GET /api/video-gen/status` reports it. */
export const videoGenModel = (id, overrides = {}) => ({
  id,
  name: `MiniMax ${id}`,
  repo: `example-org/${id}`,
  revision: '1111111111111111111111111111111111111111',
  runtime: 'minimax_h3',
  supportedModes: ['text'],
  defaultFrames: 124,
  frameOptions: [124, 141],
  fpsOptions: [24],
  steps: 8,
  guidance: 0,
  samplerLocked: true,
  supportsNegativePrompt: false,
  supportsTiling: false,
  supportsDisableAudio: false,
  ...overrides,
});

/** The eligibility gate a territory-restricted model carries. */
export const videoGenTermsGate = (termsId) => ({
  id: termsId,
  title: `Terms for ${termsId}`,
  summary: 'This model is available only in its applicable territory.',
  acknowledgement: `I am eligible and accept ${termsId}.`,
  licenseUrl: 'https://example.com/license',
});

/**
 * A `/model-context` payload over `models` — the model list plus the three
 * shaping numbers the picker's auto-select reads, with no python probe behind
 * them. The first model is the default unless overridden.
 */
export const videoGenModelContext = (models, overrides = {}) => ({
  models,
  defaultModel: models[0]?.id ?? null,
  systemMemoryGb: 128,
  fflfLtx2PixelBudget: 8_000_000,
  ...overrides,
});

/** A `/status` payload over `models`; the first model is the default unless overridden. */
export const videoGenStatus = (models, overrides = {}) => ({
  connected: true,
  pythonPath: '/opt/example/python3',
  defaultModel: models[0]?.id ?? null,
  models,
  byovRuntimes: [],
  systemMemoryGb: 128,
  backendDisclosures: [],
  ...overrides,
});

vi.mock('../services/api', () => ({
  // The page offers a federated render target (#4348); with no peer opted in as
  // a media provider the picker renders nothing and every local path is unchanged.
  getInstances: vi.fn(async () => ({ peers: state.peers })),
  getVideoGenStatus: (...args) => state.getVideoGenStatus(...args),
  getVideoGenModelContext: (...args) => state.getVideoGenModelContext(...args),
  generateVideo: (...args) => state.generateVideo(...args),
  previewVideoPrompt: (...args) => state.previewVideoPrompt(...args),
  cancelVideoGen: vi.fn(async () => ({})),
  listVideoHistory: (...args) => state.listVideoHistory(...args),
  getVideoHistoryItem: (...args) => state.getVideoHistoryItem(...args),
  deleteVideoHistoryItem: vi.fn(async () => ({})),
  setVideoHidden: vi.fn(async () => ({})),
  extractLastFrame: vi.fn(async () => ({})),
  upscaleVideo: vi.fn(async () => ({})),
  listImageGallery: vi.fn(async () => []),
  patchSettingsSlice: vi.fn(async () => ({})),
  getActiveVideoJob: vi.fn(async () => ({ activeJob: state.activeJob })),
  getSettings: vi.fn(async () => state.settings ?? ({ imageGen: { grok: { enabled: false } } })),
  getVideoGenRuntimeStatus: vi.fn(async () => state.runtimeStatus),
  listLorasFull: (...args) => state.listLorasFull(...args),
  // The prompt-enhancement controls mount useProviderModels, which fetches the
  // provider list from a mount effect. Unmocked it throws out of a passive
  // effect — the tests still pass, but the unhandled rejection fails the run.
  getProviders: vi.fn(async () => ({ providers: [] })),
  getVisionModels: vi.fn(async () => ({ models: [] })),
}));

vi.mock('../hooks/useModelDownloadStatus', async (importOriginal) => ({
  ...await importOriginal(),
  TEXT_ENCODER_DOWNLOAD_ID: '__text_encoder__',
  textEncoderDownloadId: (id) => `__text_encoder_option__:${id}`,
  useModelDownloadStatus: () => ({
    extra: state.modelDownloadExtra,
    loading: false,
    statusError: null,
    activeModelId: null,
    progress: null,
    lastError: null,
    downloading: state.modelDownloading,
    repairing: false,
    getStatus: (id) => state.getModelStatus(id),
    start: state.start,
    startWhenIdle: state.startWhenIdle,
    queuedModelId: state.queuedModelId,
    cancel: state.cancel,
    repair: state.repair,
    refresh: state.refresh,
  }),
}));

vi.mock('../hooks/useMediaCompletionRefresh', () => ({
  useMediaCompletionRefresh: (options) => { state.completionRefresh = options; },
}));
vi.mock('../hooks/useMediaAnnotations', () => ({
  useMediaAnnotations: () => ({ annotations: {}, updateAnnotation: vi.fn(), getCardProps: vi.fn(() => ({})) }),
}));
vi.mock('../hooks/usePreviewRoute', () => ({ default: () => [null, vi.fn()] }));
vi.mock('../components/ui/Toast', () => ({
  default: Object.assign(vi.fn(), { error: vi.fn(), success: vi.fn(), loading: vi.fn() }),
}));

// The prompt helpers stay observable rather than blanked: whether they remain
// usable while a render is already in flight is itself one of the assertions.
vi.mock('../components/media/PromptEnhancer', () => ({
  default: ({ disabled, maxPromptLength }) => (
    <div
      data-testid="prompt-enhancer"
      data-disabled={disabled ? '1' : '0'}
      // The backend's prompt cap, net of the style prefix — the enhancer has to
      // write inside it or the render is rejected for a prompt the user never typed.
      data-max-prompt-length={maxPromptLength ?? ''}
    >
      Enhance with AI
    </div>
  ),
}));
vi.mock('../components/media/PromptFromMedia', () => ({
  default: ({ disabled }) => (
    <div data-testid="prompt-from-media" data-disabled={disabled ? '1' : '0'}>Prompt from media</div>
  ),
}));
// Interactive so a suite can drive the style into the page; inert everywhere else.
vi.mock('../components/media/UniverseStylePicker', () => ({
  default: ({ onChange }) => (
    <button type="button" onClick={() => onChange(state.universeStyle)}>Use universe style</button>
  ),
}));
// Captures `onComplete` (the runtime-setup refresh) and exposes `streamMethod`.
vi.mock('../components/install/RuntimeInstallModal', () => ({
  default: ({ onComplete, streamMethod }) => {
    state.runtimeInstallComplete = onComplete;
    return <div data-testid="runtime-install-modal" data-stream-method={streamMethod} />;
  },
}));

// Keep the policy-bearing controls real; replace unrelated, heavyweight page
// surfaces so these stay focused orchestration tests rather than a gallery/SSE
// integration suite.
vi.mock('../components/Drawer', () => ({ default: () => null }));
vi.mock('../components/settings/ImageGenTab', () => ({ ImageGenTab: () => null }));
vi.mock('../components/settings/LocalSetupPanel', () => ({ default: () => null }));
vi.mock('../components/videoGen/FramePanel', () => ({ default: () => null }));
vi.mock('../components/videoGen/KeyframePanel', () => ({ default: () => null }));
vi.mock('../components/videoGen/AudioPanel', () => ({ default: () => null }));
vi.mock('../components/videoGen/ExtendPanel', () => ({ default: () => null }));
vi.mock('../components/videoGen/IcLoraPanel', () => ({ default: () => null }));
vi.mock('../components/videoGen/AdvancedParamsPanel', () => ({
  default: (props) => { state.advancedParams = props; return null; },
}));
vi.mock('../components/videoGen/RuntimeFingerprint', () => ({ default: () => null }));
vi.mock('../components/videoGen/VideoGenGallery', () => ({
  default: (props) => { state.galleryProps = props; return null; },
}));
vi.mock('../components/media/MediaPreview', () => ({ default: () => null }));
// The music-video picker lists projects over the network; its own suite covers it.
vi.mock('../components/media/MusicVideoFilingSelect', () => ({ default: () => null }));
vi.mock('../components/media/StylePresetPicker', () => ({ default: () => null }));
vi.mock('../components/media/MediaJobsQueue', () => ({ default: () => null }));
vi.mock('../components/imageGen/LoraPicker', () => ({
  default: (props) => { state.loraPicker = props; return null; },
}));
vi.mock('../components/media/ResolutionField', () => ({ default: () => null }));

let VideoGen = null;

/** Publishes the router's `navigate` on `state`. Renders nothing. */
function NavigateProbe() {
  state.navigate = useNavigate();
  return null;
}

/**
 * Import the page under the mocks above. Every suite loads it dynamically at
 * module scope so the registrations are in place first; the component is kept
 * here so `renderVideoGenPage()` needs no argument.
 */
export async function loadVideoGenPage() {
  ({ default: VideoGen } = await import('../pages/VideoGen.jsx'));
  return VideoGen;
}

/**
 * Mount the page on its own route, flushing the mount effects. `entry` overrides
 * the location so a suite can exercise a URL handoff (`/media/video?remix=<id>`).
 */
export async function renderVideoGenPage(entry = '/media/video') {
  if (!VideoGen) throw new Error('await loadVideoGenPage() at module scope before rendering');
  let view;
  await act(async () => {
    view = render(
      <MemoryRouter initialEntries={[entry]}>
        <NavigateProbe />
        <VideoGen />
      </MemoryRouter>,
    );
  });
  return view;
}