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.

20.1 KB
Raw Blame
/* oxlint-disable react-perf/jsx-no-new-object-as-prop */
import type { ExtensionRegistry } from '@workbench/extensions/extensionRegistry';
import type { Project } from '@workbench/projectContracts';
import type { WidgetContributionSource } from '@workbench/widgetContracts';
import type { WorkbenchInternalStore } from '@workbench/workbenchStore';

import { ChakraProvider, createListCollection, Menu, Portal, Select } from '@chakra-ui/react';
import { PromptTextarea } from '@features/generation/components';
import { adjustFocusedPromptAttention } from '@features/generation/react';
import { MenuContent } from '@platform/ui/Menu';
import { SegmentedControl } from '@platform/ui/SegmentedControl';
import { AppToaster, createActionToast, toaster } from '@platform/ui/toaster';
import { system } from '@theme/system';
import { createExtensionRegistry } from '@workbench/extensions/extensionRegistry';
import { useFloatingWindowFocus, useFocusRegionProps } from '@workbench/focusRegions';
import { WorkbenchFocusProvider } from '@workbench/WorkbenchRuntime';
import { createWorkbenchStore } from '@workbench/workbenchStore';
import { act, useCallback, useState, type ChangeEvent } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { userEvent } from 'vitest/browser';

const runtimeMocks = vi.hoisted(() => ({
  extensions: null as unknown as ExtensionRegistry,
  store: null as unknown as WorkbenchInternalStore,
}));

vi.mock('@workbench/WorkbenchContext', () => ({
  useActiveProjectSelector: <Selected,>(selector: (project: Project) => Selected) =>
    selector(runtimeMocks.store.getSnapshot().activeProject),
  useOptionalWorkbenchExtensions: () => runtimeMocks.extensions,
  useWorkbenchExtensions: () => runtimeMocks.extensions,
  useWorkbenchInternalStore: () => runtimeMocks.store,
  useWorkbenchLiveCanvasEngines: () => ({ commitFrame: () => false }),
  useWorkbenchQueries: () => runtimeMocks.store.queries,
  useWorkbenchSubscription: () => runtimeMocks.store.subscribe,
}));
vi.mock('./firstPartyCommands', () => ({ useRegisterFirstPartyCommands: () => {} }));

import { IS_MAC_OS } from './keys';
import { WorkbenchHotkeyRuntime } from './WorkbenchHotkeyRuntime';

const Harness = () => {
  const { activate } = useFloatingWindowFocus('image-map', runtimeMocks.store.getSnapshot().activeProject.id);
  const handlePointerDown = useCallback(() => activate({ byPointer: true }), [activate]);

  return (
    <>
      <div data-testid="right" {...useFocusRegionProps('right')}>
        Right panel
      </div>
      <div
        data-floating-window="image-map"
        data-hotkey-widget-instance-id="image-map"
        data-hotkey-widget-region="floating"
        data-hotkey-widget-type-id="image-map"
        data-testid="window"
        onPointerDownCapture={handlePointerDown}
      >
        <button type="button">Window control</button>
      </div>
    </>
  );
};

let host: HTMLDivElement | null = null;
let root: Root | null = null;
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

let ran: string[] = [];

const projectId = () => runtimeMocks.store.getSnapshot().activeProject.id;
const contribute = (
  id: string,
  key: string,
  scope: 'focused-region' | 'instance' | 'widget',
  source: WidgetContributionSource
) => {
  runtimeMocks.extensions.commands.register({ handler: () => ran.push(id), id, source, title: id });
  runtimeMocks.extensions.hotkeys.register({ commandId: id, defaultKeys: [key], id, scope, source, title: id });
};
const pressKey = (key: string, target: EventTarget = document.body) =>
  act(async () => {
    target.dispatchEvent(
      new KeyboardEvent('keydown', { bubbles: true, cancelable: true, code: `Key${key.toUpperCase()}`, key })
    );
    await Promise.resolve();
  });
const pressOpenSettings = (target: EventTarget) =>
  act(async () => {
    target.dispatchEvent(
      new KeyboardEvent('keydown', {
        bubbles: true,
        cancelable: true,
        code: 'Comma',
        key: ',',
        ...(IS_MAC_OS ? { metaKey: true } : { ctrlKey: true }),
      })
    );
    await Promise.resolve();
  });
const pressOn = (testId: string) =>
  act(() =>
    host!.querySelector(`[data-testid="${testId}"]`)!.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }))
  );

beforeEach(async () => {
  ran = [];
  runtimeMocks.store = createWorkbenchStore();
  runtimeMocks.store.commands.widgets.float('image-map');
  runtimeMocks.extensions = createExtensionRegistry();

  const floating: WidgetContributionSource = {
    instanceId: 'image-map',
    projectId: projectId(),
    region: 'floating',
    typeId: 'image-map',
  };
  const docked: WidgetContributionSource = {
    instanceId: 'gallery',
    projectId: projectId(),
    region: 'right',
    typeId: 'gallery',
  };

  contribute('map.instance', 'j', 'instance', floating);
  contribute('map.widget', 'k', 'widget', floating);
  contribute('map.window', 'y', 'focused-region', floating);
  contribute('gallery.rightRegion', 'y', 'focused-region', docked);

  host = document.createElement('div');
  document.body.append(host);
  root = createRoot(host);
  await act(() => {
    root?.render(
      <ChakraProvider value={system}>
        <WorkbenchFocusProvider>
          <WorkbenchHotkeyRuntime />
          <Harness />
          <AppToaster />
        </WorkbenchFocusProvider>
      </ChakraProvider>
    );
  });
});

afterEach(async () => {
  await act(() => toaster.remove());
  await act(() => root?.unmount());
  host?.remove();
  host = null;
  root = null;
});

describe('WorkbenchHotkeyRuntime under a floating window', () => {
  it('runs nothing widget-scoped while nothing holds focus', async () => {
    await pressKey('j');
    await pressKey('k');
    await pressKey('y');

    expect(ran).toEqual([]);
  });

  it('aims key presses outside any widget at the active floating window', async () => {
    await pressOn('window');

    await pressKey('j');
    await pressKey('k');
    await pressKey('y');

    expect(ran).toEqual(['map.instance', 'map.widget', 'map.window']);
  });

  it('aims key presses inside the window at it, whatever focus last recorded', async () => {
    await pressOn('right');
    const control = host!.querySelector('button')!;

    await pressKey('j', control);
    await pressKey('y', control);

    expect(ran).toEqual(['map.instance', 'map.window']);
  });

  it('gives the shared key back to the docked region once that region holds focus', async () => {
    await pressOn('window');
    await pressOn('right');

    await pressKey('y');
    await pressKey('j');

    expect(ran).toEqual(['gallery.rightRegion']);
  });

  it('stops aiming at the window once it is closed', async () => {
    await pressOn('window');
    await act(() => runtimeMocks.store.commands.widgets.closeFloating('image-map'));

    await pressKey('j');
    await pressKey('y');

    expect(ran).toEqual([]);
  });
});

describe('WorkbenchHotkeyRuntime and toast buttons', () => {
  it('leaves Enter and Space on a focused toast button to the button, while a widget binds Enter', async () => {
    const floating: WidgetContributionSource = {
      instanceId: 'image-map',
      projectId: projectId(),
      region: 'floating',
      typeId: 'image-map',
    };
    contribute('map.apply', 'enter', 'widget', floating);
    const retried = vi.fn();
    await pressOn('window');

    await act(() => {
      createActionToast({ actions: [{ label: 'Retry', onClick: retried }], title: 'Saved', type: 'warning' });
    });
    await act(() => vi.waitFor(() => expect(document.querySelector('[data-scope="toast"] button')).not.toBeNull()));
    const retry = [...document.querySelectorAll<HTMLButtonElement>('[data-scope="toast"] button')].find(
      (button) => button.textContent === 'Retry'
    )!;

    retry.focus();
    await act(() => userEvent.keyboard('{Enter}'));

    expect(retried).toHaveBeenCalledOnce();
    expect(ran).toEqual([]);

    await pressKey('Enter', host!.querySelector('[data-testid="window"]')!);
    expect(ran).toEqual(['map.apply']);
  });
});

describe('WorkbenchHotkeyRuntime and selected-image shortcuts', () => {
  const mountWidget = (typeId: string, region: string) => {
    const element = document.createElement('div');
    element.dataset.hotkeyWidgetInstanceId = typeId;
    element.dataset.hotkeyWidgetRegion = region;
    element.dataset.hotkeyWidgetTypeId = typeId;
    host!.append(element);
    return element;
  };

  beforeEach(() => {
    for (const id of [
      'viewer.recallAll',
      'viewer.recallSeed',
      'viewer.recallPrompts',
      'viewer.useSize',
      'viewer.loadWorkflow',
      'viewer.remix',
      'gallery.remix',
    ]) {
      runtimeMocks.extensions.commands.register({ handler: () => ran.push(id), id, title: id });
    }
  });

  it('recalls from the gallery selection with focus in Gallery or Preview', async () => {
    const gallery = mountWidget('gallery', 'right');
    const preview = mountWidget('preview', 'center');

    await pressKey('a', gallery);
    await pressKey('s', gallery);
    await pressKey('p', gallery);
    await pressKey('d', gallery);
    await pressKey('w', gallery);
    await pressKey('s', preview);

    expect(ran).toEqual([
      'viewer.recallAll',
      'viewer.recallSeed',
      'viewer.recallPrompts',
      'viewer.useSize',
      'viewer.loadWorkflow',
      'viewer.recallSeed',
    ]);
  });

  it('keeps remix on each widget’s own binding and recall out of other widgets', async () => {
    const gallery = mountWidget('gallery', 'right');
    const preview = mountWidget('preview', 'center');

    await pressKey('r', gallery);
    await pressKey('r', preview);
    await pressKey('s', host!.querySelector('[data-testid="window"]')!);

    expect(ran).toEqual(['gallery.remix', 'viewer.remix']);
  });
});

describe('WorkbenchHotkeyRuntime and catalog rows a widget also contributes', () => {
  it('lets the widget registration of a catalog id answer, with its own flags', async () => {
    const canvas = document.createElement('div');
    canvas.dataset.hotkeyWidgetInstanceId = 'canvas';
    canvas.dataset.hotkeyWidgetRegion = 'center';
    canvas.dataset.hotkeyWidgetTypeId = 'canvas';
    host!.append(canvas);
    const source: WidgetContributionSource = {
      instanceId: 'canvas',
      projectId: projectId(),
      region: 'center',
      typeId: 'canvas',
    };

    await act(() => {
      runtimeMocks.extensions.commands.register({
        handler: () => ran.push('canvas.tool.brush'),
        id: 'canvas.tool.brush',
        source,
        title: 'Brush',
      });
      runtimeMocks.extensions.hotkeys.register({
        commandId: 'canvas.tool.brush',
        defaultKeys: ['b'],
        id: 'canvas.tool.brush',
        preventDefault: false,
        source,
        title: 'Brush',
      });
    });
    const event = new KeyboardEvent('keydown', { bubbles: true, cancelable: true, code: 'KeyB', key: 'b' });

    await act(async () => {
      canvas.dispatchEvent(event);
      await Promise.resolve();
    });

    expect(ran).toEqual(['canvas.tool.brush']);
    expect(event.defaultPrevented).toBe(false);
  });
});

describe('WorkbenchHotkeyRuntime and open menus', () => {
  let menuRoot: Root | null = null;

  beforeEach(() => {
    for (const id of ['viewer.recallSeed', 'app.openSettings']) {
      runtimeMocks.extensions.commands.register({ handler: () => ran.push(id), id, title: id });
    }
  });

  afterEach(async () => {
    await act(() => menuRoot?.unmount());
    menuRoot = null;
  });

  const ImageMenu = () => (
    <Menu.Root>
      <Menu.Trigger>Image actions</Menu.Trigger>
      <Portal>
        <Menu.Positioner>
          <MenuContent>
            <Menu.Item value="select">Select</Menu.Item>
            <Menu.Item value="star">Star</Menu.Item>
          </MenuContent>
        </Menu.Positioner>
      </Portal>
    </Menu.Root>
  );

  const sizes = createListCollection({ items: ['Small', 'Seed', 'Square'] });
  const SizeSelect = () => (
    <Select.Root collection={sizes}>
      <Select.Control>
        <Select.Trigger>
          <Select.ValueText placeholder="Size" />
        </Select.Trigger>
      </Select.Control>
      <Portal>
        <Select.Positioner>
          <Select.Content>
            {sizes.items.map((item) => (
              <Select.Item key={item} item={item}>
                {item}
              </Select.Item>
            ))}
          </Select.Content>
        </Select.Positioner>
      </Portal>
    </Select.Root>
  );

  const openPopup = async (popup: React.ReactNode, role: 'listbox' | 'menu') => {
    await pressOn('right');
    const menuHost = document.createElement('div');
    host!.append(menuHost);
    menuRoot = createRoot(menuHost);
    await act(() => {
      menuRoot?.render(<ChakraProvider value={system}>{popup}</ChakraProvider>);
    });
    await act(() => userEvent.click(menuHost.querySelector('button')!));
    const content = await vi.waitFor(() => {
      const element = document.querySelector<HTMLElement>(`[role="${role}"][data-state="open"]`);
      expect(element).not.toBeNull();
      return element!;
    });
    await vi.waitFor(() => expect(document.activeElement).toBe(content));
    return content;
  };

  it('still runs a global chord pressed inside a portaled menu', async () => {
    const menu = await openPopup(<ImageMenu />, 'menu');

    await pressKey('s', menu);
    await pressOpenSettings(menu);

    expect(ran).toEqual(['app.openSettings']);
  });

  it('leaves a letter typed into a portaled Select listbox to the Select while Gallery holds focus', async () => {
    const listbox = await openPopup(<SizeSelect />, 'listbox');

    await pressKey('s', listbox);
    expect(ran).toEqual([]);
    await pressOpenSettings(listbox);
    expect(ran).toEqual(['app.openSettings']);
  });

  it('leaves a letter typed into a portaled menu to the menu while Gallery holds focus', async () => {
    await pressOn('right');
    await pressKey('s');
    expect(ran).toEqual(['viewer.recallSeed']);

    const menuHost = document.createElement('div');
    host!.append(menuHost);
    menuRoot = createRoot(menuHost);
    await act(() => {
      menuRoot?.render(
        <ChakraProvider value={system}>
          <ImageMenu />
        </ChakraProvider>
      );
    });
    await act(() => userEvent.click(menuHost.querySelector('button')!));
    const menu = await vi.waitFor(() => {
      const content = document.querySelector<HTMLElement>('[role="menu"][data-state="open"]');
      expect(content).not.toBeNull();
      return content!;
    });
    await vi.waitFor(() => expect(document.activeElement).toBe(menu));

    await pressKey('s', menu);
    await pressKey('s', menu.querySelector('[role="menuitem"]')!);

    expect(ran).toEqual(['viewer.recallSeed']);
    await pressKey('s');
    expect(ran).toEqual(['viewer.recallSeed', 'viewer.recallSeed']);
  });
});

describe('WorkbenchHotkeyRuntime and prompt weighting', () => {
  let promptRoot: Root | null = null;

  beforeEach(() => {
    runtimeMocks.extensions.commands.register({
      handler: () => adjustFocusedPromptAttention('increment', false),
      id: 'app.promptWeightUp',
      title: 'app.promptWeightUp',
    });
  });

  afterEach(async () => {
    await act(() => promptRoot?.unmount());
    promptRoot = null;
  });

  const PromptHarness = ({ attentionSyntax }: { attentionSyntax: 'edit' | 'none' }) => {
    const [value, setValue] = useState('a cat');
    const handleChange = useCallback(
      (event: ChangeEvent<HTMLTextAreaElement>) => setValue(event.currentTarget.value),
      []
    );

    return (
      <PromptTextarea
        aria-label="Prompt"
        attentionSyntax={attentionSyntax}
        defaultHeightPx={96}
        minHeightPx={56}
        resizeHandleAriaLabel="Resize prompt"
        showSyntaxHighlighting
        value={value}
        onChange={handleChange}
      />
    );
  };

  const pressWeightUp = async (attentionSyntax: 'edit' | 'none') => {
    const promptHost = document.createElement('div');
    host!.append(promptHost);
    promptRoot = createRoot(promptHost);
    await act(() => {
      promptRoot?.render(
        <ChakraProvider value={system}>
          <PromptHarness attentionSyntax={attentionSyntax} />
        </ChakraProvider>
      );
    });
    const textarea = promptHost.querySelector('textarea')!;
    textarea.focus();
    textarea.setSelectionRange(2, 5);

    let claimed = false;
    await act(async () => {
      claimed = !textarea.dispatchEvent(
        new KeyboardEvent('keydown', {
          bubbles: true,
          cancelable: true,
          code: 'ArrowUp',
          ctrlKey: true,
          key: 'ArrowUp',
        })
      );
      await Promise.resolve();
    });

    return { claimed, textarea };
  };

  it('weights the selection in a prompt whose model reads attention syntax', async () => {
    const { claimed, textarea } = await pressWeightUp('edit');

    expect(claimed).toBe(true);
    await expect.poll(() => textarea.value).toBe('a cat+');
  });

  it('leaves Ctrl+Up to the text field when the model reads the prompt as plain text', async () => {
    const { claimed, textarea } = await pressWeightUp('none');

    expect(claimed).toBe(false);
    expect(textarea.value).toBe('a cat');
  });
});

const VIEW_OPTIONS = [
  { label: 'Original', value: 'original' },
  { label: 'Map', value: 'map' },
  { label: 'Overlay', value: 'overlay' },
];

describe('WorkbenchHotkeyRuntime and a focused segmented control', () => {
  let controlRoot: Root | null = null;

  afterEach(async () => {
    await act(() => controlRoot?.unmount());
    controlRoot = null;
  });

  const Segments = () => {
    const [value, setValue] = useState('map');
    return (
      <SegmentedControl ariaLabel="View" isFullWidth={false} options={VIEW_OPTIONS} value={value} onChange={setValue} />
    );
  };

  it('leaves arrow keys to the radio group while Gallery, which binds them, holds focus', async () => {
    contribute('gallery.galleryNavRight', 'arrowright', 'focused-region', {
      instanceId: 'gallery',
      projectId: projectId(),
      region: 'right',
      typeId: 'gallery',
    });
    await pressOn('right');
    const controlHost = document.createElement('div');
    host!.append(controlHost);
    controlRoot = createRoot(controlHost);
    await act(() => {
      controlRoot?.render(
        <ChakraProvider value={system}>
          <Segments />
        </ChakraProvider>
      );
    });
    const radio = (value: string) => controlHost.querySelector<HTMLInputElement>(`input[value="${value}"]`)!;
    const prevented: boolean[] = [];
    // Registered after the runtime's listener, so it sees the press once the runtime has handled it.
    const record = (event: KeyboardEvent) => prevented.push(event.defaultPrevented);
    window.addEventListener('keydown', record);

    try {
      await act(async () => {
        radio('map').focus();
        await userEvent.keyboard('{ArrowRight}');
      });
    } finally {
      window.removeEventListener('keydown', record);
    }

    expect(prevented).toEqual([false]);
    expect(ran).toEqual([]);
    expect(radio('overlay').checked).toBe(true);
    expect(document.activeElement).toBe(radio('overlay'));

    await pressKey('ArrowRight', host!.querySelector('[data-testid="right"]')!);
    expect(ran).toEqual(['gallery.galleryNavRight']);
  });
});

describe('WorkbenchHotkeyRuntime and a held key', () => {
  const pressCancel = (repeat: boolean) =>
    act(async () => {
      document.body.dispatchEvent(
        new KeyboardEvent('keydown', {
          bubbles: true,
          cancelable: true,
          code: 'KeyX',
          key: 'X',
          repeat,
          shiftKey: true,
        })
      );
      await Promise.resolve();
    });

  it('runs a once-per-press command once while its key is held, and again on the next press', async () => {
    await act(() => {
      runtimeMocks.extensions.commands.register({
        handler: () => ran.push('app.cancelQueueItem'),
        id: 'app.cancelQueueItem',
        title: 'Cancel current or next queue item',
      });
    });

    await pressCancel(false);
    await pressCancel(true);
    await pressCancel(true);
    expect(ran).toEqual(['app.cancelQueueItem']);

    await pressCancel(false);
    expect(ran).toEqual(['app.cancelQueueItem', 'app.cancelQueueItem']);
  });
});