Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

11.1 KB
Raw Blame
import type { GenerateWidgetValues, GenerationIssue, MainModelConfig } from '@features/generation/contracts';
import type * as IdentityModule from '@features/identity';
import type * as ModelsModule from '@features/models';
import type { WidgetRegion } from '@workbench/layoutContracts';
import type * as ProjectsApiModule from '@workbench/projects/api';
import type { RegisteredWidget } from '@workbench/widgetContracts';
import type * as WorkbenchContextModule from '@workbench/WorkbenchContext';

import { getDefaultGenerateSettings } from '@features/generation/settings';
import { registerPositivePromptElement } from '@features/generation/ui/promptFields/promptFocus';
import {
  resetArchitectureCapabilities,
  setArchitectureCapabilities,
} from '@features/models/core/architectureCapabilities';
import { architectureCapabilitiesFixture } from '@features/models/core/architectureCapabilities.testing';
import { accountLifecycle } from '@platform/state/accountLifecycle';
import { DEFAULT_PREFERENCES, getWorkbenchPreferences, patchWorkbenchPreferences } from '@workbench/settings/store';
import { getWidgetById, getWidgetsForRegion } from '@workbench/widgetRegistry';
import { getProjectWidgetValues } from '@workbench/widgetState';
import { createWorkbenchStore } from '@workbench/workbenchStore';
import { WorkbenchWidgetRegistryProvider } from '@workbench/WorkbenchWidgetRegistryContext';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import type { InvokeIssueAction } from './invokeIssueActions';
import type { InvocationIssue } from './invokeIssueTargets';

const workbench = vi.hoisted(() => ({
  canManageModels: true,
  managerRequests: [] as unknown[],
  store: null as ReturnType<typeof createWorkbenchStore> | null,
}));

vi.mock('@workbench/projects/api', async (importOriginal) => ({
  ...(await importOriginal<typeof ProjectsApiModule>()),
  setClientStateValue: async () => {},
}));
vi.mock('@workbench/WorkbenchContext', async (importOriginal) => ({
  ...(await importOriginal<typeof WorkbenchContextModule>()),
  useWorkbenchCommands: () => workbench.store!.commands,
  useWorkbenchQueries: () => workbench.store!.queries,
}));
vi.mock('@features/models', async (importOriginal) => ({
  ...(await importOriginal<typeof ModelsModule>()),
  openModelManager: (request: unknown) => workbench.managerRequests.push(request),
}));
vi.mock('@features/identity', async (importOriginal) => ({
  ...(await importOriginal<typeof IdentityModule>()),
  useCapabilities: () => ({ canManageModels: workbench.canManageModels }),
}));

const { useInvokeIssueActions } = await import('./invokeIssueActions');

(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

const sdxlModel: MainModelConfig = { base: 'sdxl', key: 'sdxl-model', name: 'SDXL', type: 'main' };

let host: HTMLDivElement;
let root: Root;
let getActions: ((issue: InvocationIssue) => InvokeIssueAction[]) | null = null;

const withoutPreload = (widget: RegisteredWidget): RegisteredWidget => ({
  ...widget,
  implementation: { ...widget.implementation, preload: () => {} },
});
const getQuietWidgetsForRegion = (region: WidgetRegion) => getWidgetsForRegion(region).map(withoutPreload);
const getQuietWidgetById = (typeId: string) => {
  const widget = getWidgetById(typeId);

  return widget ? withoutPreload(widget) : undefined;
};

const Probe = ({ onActions }: { onActions: (actions: (issue: InvocationIssue) => InvokeIssueAction[]) => void }) => {
  onActions(useInvokeIssueActions());

  return null;
};
const publish = (actions: (issue: InvocationIssue) => InvokeIssueAction[]) => {
  getActions = actions;
};
const renderProbe = () =>
  act(() =>
    root.render(
      <WorkbenchWidgetRegistryProvider
        getWidgetById={getQuietWidgetById}
        getWidgetsForRegion={getQuietWidgetsForRegion}
      >
        <Probe onActions={publish} />
      </WorkbenchWidgetRegistryProvider>
    )
  );

const store = () => workbench.store!;
const project = () => store().getSnapshot().activeProject;
const isPlaced = (typeId: string) =>
  Object.values(project().widgetRegions).some((region) =>
    region.instanceIds.some((instanceId) => project().widgetInstances[instanceId]?.typeId === typeId)
  );
const actionsFor = (issue: InvocationIssue) => getActions!(issue);
const run = async (issue: InvocationIssue, id: string) => {
  const action = actionsFor(issue).find((candidate) => candidate.id === id);

  expect(action).toBeDefined();
  await act(() => action!.run());
};

const generationIssue = (overrides: Partial<GenerationIssue> = {}): GenerationIssue => ({
  id: 'guidance:cfgScale:range',
  message: 'Guidance is out of range.',
  target: { field: 'cfgScale', section: 'guidance' },
  ...overrides,
});

beforeEach(async () => {
  accountLifecycle.activate('invoke-issue-actions-test');
  await patchWorkbenchPreferences(DEFAULT_PREFERENCES);
  setArchitectureCapabilities(architectureCapabilitiesFixture);
  const values: GenerateWidgetValues = {
    ...getDefaultGenerateSettings(sdxlModel),
    cfgScale: 30,
    model: sdxlModel,
    modelKey: sdxlModel.key,
  };
  resetArchitectureCapabilities();
  workbench.store = createWorkbenchStore();
  store().commands.layout.applyPreset('compose');
  store().commands.generation.setSettings(values);
  host = document.createElement('div');
  document.body.append(host);
  root = createRoot(host);
  await renderProbe();
});

afterEach(async () => {
  await act(() => root.unmount());
  host.remove();
  getActions = null;
  workbench.store = null;
  accountLifecycle.invalidate();
});

describe('useInvokeIssueActions', () => {
  it('offers nothing for a lost connection: it clears on reconnect', () => {
    expect(actionsFor({ message: 'The server is disconnected.', target: { kind: 'connection' } })).toEqual([]);
  });

  it('applies a Generate fix in place and keeps the list open', async () => {
    const issue: InvocationIssue = {
      message: 'Guidance is out of range.',
      target: { issue: generationIssue({ fix: { kind: 'clampGuidance', value: 7 } }), kind: 'generation' },
    };

    expect(actionsFor(issue).find(({ id }) => id === 'fix')?.stays).toBe(true);
    await run(issue, 'fix');

    expect(getProjectWidgetValues(project(), 'generate').cfgScale).toBe(7);
  });

  it('shows a Generate issue by opening its section and the Generate panel', async () => {
    await patchWorkbenchPreferences({ generateSectionsOpen: { guidance: false } });
    await run(
      { message: 'Guidance is out of range.', target: { issue: generationIssue(), kind: 'generation' } },
      'show'
    );

    expect(getWorkbenchPreferences().generateSectionsOpen.guidance).toBe(true);
    expect(isPlaced('generate')).toBe(true);
  });

  it('sends a missing model to the Model Manager, filtered to main models, for those who can install', async () => {
    workbench.managerRequests = [];
    const issue: InvocationIssue = {
      message: 'SDXL is not installed.',
      target: {
        issue: generationIssue({ id: 'model:notInstalled:sdxl-model', target: { section: 'model' } }),
        kind: 'generation',
      },
    };

    workbench.canManageModels = false;
    await renderProbe();
    expect(actionsFor(issue).map(({ id }) => id)).toEqual(['show']);
    workbench.canManageModels = true;
    await renderProbe();
    await run(issue, 'models');

    expect(workbench.managerRequests).toEqual([{ modelType: 'main', projectId: project().id }]);
  });

  it('opens the workflow editor in the center to show a node', async () => {
    await run({ message: 'Missing input.', target: { kind: 'workflowNode', nodeId: 'node-1' } }, 'show');

    const center = project().widgetRegions.center;
    expect(project().widgetInstances[center.activeInstanceId]?.typeId).toBe('workflow');
  });

  it('adds a source widget the layout lacks', async () => {
    expect(isPlaced('video')).toBe(false);

    await run({ message: 'Not in this layout.', target: { kind: 'mountSource', sourceId: 'video' } }, 'add');

    expect(isPlaced('video')).toBe(true);
  });

  it('switches a video route to the Gallery in place', async () => {
    store().commands.generation.setDestination('canvas');
    const issue: InvocationIssue = {
      message: 'Video goes to the Gallery.',
      target: { destination: 'gallery', kind: 'destination' },
    };

    expect(actionsFor(issue)[0]?.stays).toBe(true);
    await run(issue, 'destination');

    expect(project().invocation.destination).toBe('gallery');
  });

  it('opens the canvas and its Layers for a layer issue, and the source widget for the rest', async () => {
    await run({ message: 'Choose a model.', target: { kind: 'layer', layerId: 'control-1' } }, 'show');
    expect(isPlaced('layers')).toBe(true);

    await run({ message: 'Settings are incomplete.', target: { kind: 'widget', widgetId: 'video' } }, 'show');
    expect(isPlaced('video')).toBe(true);

    await run(
      { message: 'The prompt could not be expanded.', target: { kind: 'prompt', widgetId: 'generate' } },
      'show'
    );
    expect(isPlaced('generate')).toBe(true);
  });

  it("opens Video and focuses its own prompt, not Generate's, for a Video prompt that cannot expand", async () => {
    const generatePrompt = document.createElement('textarea');
    const videoPrompt = document.createElement('textarea');
    document.body.append(generatePrompt, videoPrompt);
    registerPositivePromptElement('generate', generatePrompt, null);
    registerPositivePromptElement('video', videoPrompt, null);

    try {
      await run(
        { message: 'The prompt could not be expanded.', target: { kind: 'prompt', widgetId: 'video' } },
        'show'
      );
      await act(
        () =>
          new Promise<void>((resolve) => {
            requestAnimationFrame(() => resolve());
          })
      );

      expect(isPlaced('video')).toBe(true);
      expect(document.activeElement).toBe(videoPrompt);
    } finally {
      registerPositivePromptElement('generate', null, generatePrompt);
      registerPositivePromptElement('video', null, videoPrompt);
      generatePrompt.remove();
      videoPrompt.remove();
    }
  });

  it("opens Upscale and its Detail guidance, and focuses Upscale's prompt, not Generate's", async () => {
    await patchWorkbenchPreferences({ generateSectionsOpen: { upscaleDetailGuidance: false } });
    const generatePrompt = document.createElement('textarea');
    const upscalePrompt = document.createElement('textarea');
    document.body.append(generatePrompt, upscalePrompt);
    registerPositivePromptElement('generate', generatePrompt, null);
    registerPositivePromptElement('upscale', upscalePrompt, null);

    try {
      await run(
        { message: 'The prompt could not be expanded.', target: { kind: 'prompt', widgetId: 'upscale' } },
        'show'
      );
      await expect.poll(() => document.activeElement).toBe(upscalePrompt);

      expect(getWorkbenchPreferences().generateSectionsOpen.upscaleDetailGuidance).toBe(true);
      expect(isPlaced('upscale')).toBe(true);
    } finally {
      registerPositivePromptElement('generate', null, generatePrompt);
      registerPositivePromptElement('upscale', null, upscalePrompt);
      generatePrompt.remove();
      upscalePrompt.remove();
    }
  });
});