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.

5.0 KB
Raw Blame
import type { WidgetManifest } from '@workbench/widgetContracts';

import { describe, expect, it, vi } from 'vitest';

import {
  getWidgetById,
  getWidgetHosts,
  registeredWidgets,
  registerFirstPartyWidgets,
  registerWidgets,
} from './widgetRegistry';
import { RETIRED_REGION_WIDGET_TYPE_IDS, RETIRED_WIDGET_TYPE_IDS } from './workbenchState';

const TestIcon = () => null;
const TestView = () => null;
const load = () => Promise.resolve({ view: TestView });

const createManifest = (overrides: Partial<WidgetManifest> = {}): WidgetManifest => ({
  allowMultiple: false,
  allowedRegions: ['right'],
  failurePolicy: { isolateRenderFailure: true, onRegistrationFailure: 'disable' },
  icon: TestIcon,
  id: 'vendor.widget',
  label: 'Test Widget',
  load,
  version: 1,
  ...overrides,
});

describe('widget registry', () => {
  it('registers first-party widget manifests without icon validation failures', () => {
    const widgets = registerFirstPartyWidgets();

    expect(widgets).toHaveLength(16);
    expect(widgets.map((widget) => widget.manifest.id)).toEqual(
      expect.arrayContaining(['layers', 'image-map', 'shortcuts'])
    );
    expect(widgets.flatMap((widget) => widget.failure ?? [])).toEqual([]);
    expect(widgets.every((widget) => widget.status === 'enabled')).toBe(true);
  });

  it('no longer offers a region a type its stored placements are stripped from', () => {
    const retired = Object.entries(RETIRED_REGION_WIDGET_TYPE_IDS).flatMap(([region, typeIds]) =>
      [...(typeIds ?? [])].map((typeId) => ({ region, typeId }))
    );

    expect(retired).not.toEqual([]);
    for (const { region, typeId } of retired) {
      expect(getWidgetById(typeId)?.manifest.allowedRegions, typeId).not.toContain(region);
    }
  });

  it('registers none of the retired widget types', () => {
    expect([...RETIRED_WIDGET_TYPE_IDS].filter((typeId) => getWidgetById(typeId))).toEqual([]);
  });

  it('normalizes default state and api version', () => {
    const loader = vi.fn(load);
    const [widget] = registerWidgets([createManifest({ load: loader, state: undefined })]);

    expect(widget.status).toBe('enabled');
    expect(widget.manifest.apiVersion).toBe(1);
    expect(widget.manifest.state).toMatchObject({ persistence: 'project', version: 1 });
    expect(widget.manifest.state.createInitial()).toEqual({});
    expect(loader).not.toHaveBeenCalled();
  });

  it('disables invalid icon manifests', () => {
    const [widget] = registerWidgets([createManifest({ icon: 'invalid' as unknown as WidgetManifest['icon'] })]);

    expect(widget.status).toBe('disabled');
    expect(widget.failure?.message).toContain('must provide an icon component');
  });

  it('fails closed for empty regions', () => {
    const [widget] = registerWidgets([createManifest({ allowedRegions: [] })]);

    expect(widget.status).toBe('disabled');
    expect(widget.failure?.message).toContain('must declare at least one allowed region');
  });

  it('fails closed without a deferred implementation loader', () => {
    const [widget] = registerWidgets([createManifest({ load: undefined as unknown as WidgetManifest['load'] })]);

    expect(widget.status).toBe('disabled');
    expect(widget.failure?.message).toContain('must provide a deferred implementation loader');
  });

  it('honors hidden failure policy for invalid manifests', () => {
    const [widget] = registerWidgets([
      createManifest({
        failurePolicy: { isolateRenderFailure: true, onRegistrationFailure: 'hide' },
        load: undefined as unknown as WidgetManifest['load'],
      }),
    ]);

    expect(widget.status).toBe('hidden');
    expect(widget.failure).toBeDefined();
  });

  it('rejects unstable ids and unsupported api versions', () => {
    const [badId, badApiVersion] = registerWidgets([
      createManifest({ id: 'bad id' }),
      createManifest({ apiVersion: 2 as unknown as WidgetManifest['apiVersion'], id: 'vendor.bad-api' }),
    ]);

    expect(badId.status).toBe('disabled');
    expect(badId.failure?.message).toContain('stable non-empty string id');
    expect(badApiVersion.status).toBe('disabled');
    expect(badApiVersion.failure?.message).toContain('unsupported apiVersion');
  });
});

describe('widget hosts', () => {
  it('offers a host resource only for manifests that declare loadHost', () => {
    const hosts = getWidgetHosts();

    expect(hosts.length).toBeGreaterThan(0);

    for (const widget of hosts) {
      expect(widget.manifest.loadHost).toBeTypeOf('function');
      expect(widget.host).toBeDefined();
    }

    for (const widget of registeredWidgets) {
      if (!widget.manifest.loadHost) {
        expect(widget.host).toBeUndefined();
      }
    }
  });

  it('loads a host without touching the widget implementation resource', async () => {
    const workflow = getWidgetById('workflow');

    expect(workflow?.host).toBeDefined();

    const statusBeforeHostLoad = workflow?.implementation.getStatus();
    await workflow?.host?.load();

    // The whole point: mounting the singleton host must not drag the view chunk in.
    expect(workflow?.implementation.getStatus()).toBe(statusBeforeHostLoad);
  });
});