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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
/* oxlint-disable react-perf/jsx-no-new-object-as-prop */
import { Box, ChakraProvider, HStack, Menu, Portal, Text } from '@chakra-ui/react';
import { system } from '@theme/system';
import { createWidgetRecovery } from '@workbench/widget-frame/widgetRecoveryStore';
import { createInstance } from 'i18next';
import { act, type ComponentType, type ReactNode } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import { afterEach, describe, expect, it, vi } from 'vitest';
interface CenterAreaTestProject {
floatingWidgets?: Record<string, { returnIndex: number; returnRegion: string }>;
id: string;
invocation: { sourceId: string };
queue: { items: never[] };
widgetInstances: Record<string, { id: string; title?: string; typeId: string }>;
widgetRegions: {
center: { activeInstanceId: string; instanceIds: string[]; isCollapsed: boolean; sizePx: number };
};
}
const centerAreaMocks = vi.hoisted(() => {
const icon = () => null;
const loadedImplementation = () => {
const promise: Promise<object> & { status?: string; value?: object } = Promise.resolve({});
promise.status = 'fulfilled';
promise.value = {};
return { getStatus: () => 'loaded', load: () => promise, preload: () => {}, retry: () => promise };
};
const activeItem = {
icon,
id: 'preview-instance',
instance: { id: 'preview-instance' },
label: 'Preview',
status: 'enabled',
typeId: 'preview',
widget: {
implementation: loadedImplementation(),
manifest: {
centerPlacement: 'view',
chrome: { header: 'visible' },
icon,
},
status: 'enabled',
},
};
const project: CenterAreaTestProject = {
id: 'test-project',
invocation: { sourceId: 'generate' },
queue: { items: [] },
widgetInstances: {},
widgetRegions: {
center: { activeInstanceId: activeItem.id, instanceIds: [activeItem.id], isCollapsed: false, sizePx: 0 },
},
};
return {
activeItem,
chromeSlots: null as null | {
actions: ComponentType<{ region: 'center' }>;
label: ComponentType<{ region: 'center' }>;
},
dockFloating: vi.fn(),
focusFloating: vi.fn(),
focusRegion: vi.fn(),
project,
revealFloating: vi.fn(),
trailingActionsWidth: '189px',
};
});
const identity = vi.hoisted(() => ({
session: { multiuserEnabled: true, phase: 'ready', user: { is_admin: true, user_id: 'admin-user' } },
}));
vi.mock('@features/identity', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useAuthSession: () => identity.session,
}));
vi.mock('@features/models', async (importOriginal) => ({
...(await importOriginal<object>()),
useModelLoads: () => [],
}));
vi.mock('@features/queue/contracts', async (importOriginal) => ({
...(await importOriginal<object>()),
getProjectQueueIndicatorState: () => ({ hasOpenQueueWork: false, progressState: null, runningQueueItemId: null }),
}));
vi.mock('@features/queue/react', async (importOriginal) => ({
...(await importOriginal<object>()),
useQueueItemProgress: () => null,
}));
vi.mock('@workbench/focusRegions', () => ({
useFocusRegionProps: () => ({}),
useHighlightedRegion: () => null,
useWorkbenchFocus: () => ({
focusFloating: centerAreaMocks.focusFloating,
focusRegion: centerAreaMocks.focusRegion,
getTarget: () => null,
}),
}));
vi.mock('@workbench/widgetRegionViewModel', () => ({
createWidgetRegionViewModelFromState: ({ regionState }: { regionState: { instanceIds: string[] } }) => ({
placedItems: regionState.instanceIds.length > 0 ? [centerAreaMocks.activeItem] : [],
}),
getWidgetRegionItems: () => [],
isRequiredCenterView: () => true,
}));
vi.mock('@workbench/widgetRegistry', () => ({
getWidgetById: (typeId: string) =>
typeId === 'preview' ? { manifest: { id: 'preview', label: 'Preview' }, status: 'enabled' } : undefined,
getWidgetsForRegion: () => [],
}));
const recoveryOwner = vi.hoisted(() => ({
widgetRecovery: new Map(),
getSnapshot: () => ({ activeProject: { id: 'test-project', widgetInstances: { 'test-instance': {} } } }),
}));
vi.mock('@workbench/WorkbenchContext', () => ({
useWorkbenchInternalStore: () => recoveryOwner,
useWorkbenchReload: () => async () => {},
useActiveProjectId: () => centerAreaMocks.project.id,
useActiveProjectSelector: (selector: (project: typeof centerAreaMocks.project) => unknown) =>
selector(centerAreaMocks.project),
useWorkbenchCommands: () => ({
widgets: { dockFloating: centerAreaMocks.dockFloating, revealFloating: centerAreaMocks.revealFloating },
}),
useWorkbenchSelector: (selector: (snapshot: { backendConnection: { status: string } }) => unknown) =>
selector({ backendConnection: { status: 'connected' } }),
}));
vi.mock('@workbench/widget-frame', () => ({
WidgetIdentityIcon: () => <Box boxSize="3.5" />,
WidgetChromeSlotById: ({ slot }: { slot: 'actions' | 'label' | 'icon' }) => {
if (slot === 'icon') {
return <Box boxSize="3.5" />;
}
const Slot = centerAreaMocks.chromeSlots?.[slot];
if (Slot) {
return slot === 'actions' ? (
<>
<Slot region="center" />
<Box aria-label="Widget settings" as="button" boxSize="6" flexShrink={0} />
<Box aria-label="Widget menu" as="button" boxSize="6" flexShrink={0} />
</>
) : (
<Slot region="center" />
);
}
if (slot === 'actions') {
return <Box data-testid="trailing-actions" flexShrink={0} w={centerAreaMocks.trailingActionsWidth} />;
}
return (
<HStack flex="1" minW="0" w="300px">
<Text flexShrink={0}>Panther and the Flask</Text>
<Text truncate>a423880b-184a-4910-977e-fe7d7fa9f732.png</Text>
</HStack>
);
},
WidgetRendererById: () => <Box h="full" w="full" />,
WidgetSourceLockBadge: () => null,
useWidgetIntentPreloadProps: () => ({}),
}));
import { CenterArea } from './CenterArea';
const i18n = createInstance();
void i18n.use(initReactI18next).init({
fallbackLng: 'en',
initAsync: false,
lng: 'en',
resources: {
en: {
translation: {
common: { scrollToEnd: 'Scroll to end', scrollToStart: 'Scroll to start' },
widgets: {
centerRegion: 'Center view',
centerViewEmpty: 'No view',
centerViewLabel: 'Center view: {{label}}',
floating: {
centerFloating: '{{label}} is in a floating window',
destinations: { right: 'right panel' },
dockTo: 'Dock to {{destination}}',
showWindow: 'Show window',
},
},
},
},
},
});
let host: HTMLDivElement | null = null;
let root: Root | null = null;
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const render = async (children: ReactNode, width = 571) => {
recoveryOwner.widgetRecovery.set('test-instance', createWidgetRecovery());
host = document.createElement('div');
host.style.cssText = `display:flex;height:320px;width:${width}px;`;
document.body.append(host);
root = createRoot(host);
await act(async () => {
root?.render(
<I18nextProvider i18n={i18n}>
<ChakraProvider value={system}>{children}</ChakraProvider>
</I18nextProvider>
);
await Promise.resolve();
});
};
afterEach(async () => {
recoveryOwner.widgetRecovery.clear();
await act(async () => {
root?.unmount();
await Promise.resolve();
});
host?.remove();
host = null;
root = null;
});
describe('CenterArea chrome layout', () => {
it('keeps non-shrinking trailing actions inside the center region', async () => {
await render(<CenterArea />);
const centerRegion = host?.querySelector<HTMLElement>('section');
const trailingActions = host?.querySelector<HTMLElement>('[data-testid="trailing-actions"]');
expect(centerRegion).not.toBeNull();
expect(trailingActions).not.toBeNull();
if (!centerRegion || !trailingActions) {
throw new Error('Expected center chrome geometry.');
}
expect(trailingActions.getBoundingClientRect().right).toBeLessThanOrEqual(
centerRegion.getBoundingClientRect().right
);
});
it('keeps the islands apart and inside the minimum center width, scrolling what does not fit', async () => {
await render(<CenterArea />, 320);
// Resize observations, and the cues they set, land before the second frame.
await act(
() =>
new Promise((resolve) => {
requestAnimationFrame(() => requestAnimationFrame(resolve));
})
);
const centerRegion = host!.querySelector<HTMLElement>('section')!.getBoundingClientRect();
const [startIsland, endIsland] = [...host!.querySelectorAll<HTMLElement>('[data-center-chrome-island]')];
const start = startIsland!.getBoundingClientRect();
const end = endIsland!.getBoundingClientRect();
const viewMenu = startIsland!.querySelector('button[aria-label^="Center view"]')!.getBoundingClientRect();
expect(start.left).toBeGreaterThanOrEqual(centerRegion.left);
expect(start.right).toBeLessThanOrEqual(end.left);
expect(end.right).toBeLessThanOrEqual(centerRegion.right);
// The actions give way at the width that leaves the start island its floor: border, row padding, the view menu's
// 5rem, and the 2.75rem end cue.
expect(start.width).toBeGreaterThanOrEqual(2 + 4 + 80 + 44);
expect(viewMenu.width).toBeGreaterThanOrEqual(30);
expect(viewMenu.right).toBeLessThanOrEqual(
startIsland!.querySelector('[data-overflow-cue="end"]')!.getBoundingClientRect().left
);
for (const island of [startIsland!, endIsland!]) {
const row = island.firstElementChild!;
expect(row.scrollWidth).toBeGreaterThan(row.clientWidth);
expect(island.querySelector('[data-overflow-cue="end"]')).not.toBeNull();
}
});
});
describe('CenterArea view menu trigger', () => {
const trigger = () => host!.querySelector<HTMLButtonElement>('button[aria-label="Center view: Preview"]')!;
const viewName = () => [...trigger().querySelectorAll('p')].find((element) => element.textContent === 'Preview')!;
const tooltip = () =>
[...document.querySelectorAll<HTMLElement>('[data-scope="tooltip"][data-part="content"]')].find(
(candidate) => candidate.textContent === 'Center view: Preview'
);
const focusByKeyboard = async () => {
await act(() => {
document.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true, key: 'Tab' }));
trigger().focus();
return Promise.resolve();
});
};
it('names the view at a wide center, without a tooltip repeating it', async () => {
await render(<CenterArea />, 560);
expect(viewName().getClientRects().length).toBeGreaterThan(0);
expect(viewName().getBoundingClientRect().width).toBeGreaterThan(0);
expect(getComputedStyle(trigger()).paddingInline).toBe('8px');
expect(trigger().getBoundingClientRect().width).toBeGreaterThanOrEqual(80);
await focusByKeyboard();
expect(tooltip()?.dataset.state).not.toBe('open');
});
it('minimises to its icon and chevron below 560px, still named and still opening the view menu', async () => {
await render(<CenterArea />, 560);
await act(async () => {
host!.style.width = '559px';
await new Promise((resolve) => {
requestAnimationFrame(resolve);
});
});
expect(viewName().getClientRects().length).toBe(0);
expect(getComputedStyle(trigger()).paddingInline).toBe('0px');
// The 14px icon, a small gap, and the 12px chevron, at the 2rem icon-button floor; no name, however short.
expect(trigger().getBoundingClientRect().width).toBeLessThanOrEqual(36);
expect(trigger().getAttribute('aria-label')).toBe('Center view: Preview');
await focusByKeyboard();
expect(tooltip()?.dataset.state).toBe('open');
await act(async () => {
trigger().click();
await Promise.resolve();
});
expect(trigger().getAttribute('aria-expanded')).toBe('true');
expect(document.querySelector('[role="menuitemradio"][aria-checked="true"]')?.textContent).toContain('Preview');
});
});
describe('CenterArea view menu beside wide actions', () => {
afterEach(() => {
centerAreaMocks.trailingActionsWidth = '189px';
});
it('minimises when the room left to the start island is below 18rem, even at a wide center', async () => {
centerAreaMocks.trailingActionsWidth = '320px';
await render(<CenterArea />, 600);
const trigger = host!.querySelector<HTMLButtonElement>('button[aria-label="Center view: Preview"]')!;
const startIsland = trigger.closest<HTMLElement>('[data-center-chrome-island]')!;
const name = [...trigger.querySelectorAll('p')].find((element) => element.textContent === 'Preview')!;
expect(startIsland.parentElement!.getBoundingClientRect().width).toBeLessThan(288);
expect(name.getClientRects().length).toBe(0);
expect(trigger.getBoundingClientRect().width).toBeLessThanOrEqual(36);
expect(startIsland.textContent).toContain('Panther and the Flask');
});
});
describe('CenterArea with its last view floating', () => {
const dockedCenter = centerAreaMocks.project.widgetRegions.center;
afterEach(() => {
centerAreaMocks.project.floatingWidgets = undefined;
centerAreaMocks.project.widgetInstances = {};
centerAreaMocks.project.widgetRegions.center = dockedCenter;
centerAreaMocks.dockFloating.mockClear();
centerAreaMocks.focusFloating.mockClear();
centerAreaMocks.focusRegion.mockClear();
centerAreaMocks.revealFloating.mockClear();
});
const floatLastView = () => {
centerAreaMocks.project.floatingWidgets = { 'preview-instance': { returnIndex: 2, returnRegion: 'right' } };
centerAreaMocks.project.widgetInstances = {
'preview-instance': { id: 'preview-instance', typeId: 'preview' },
};
centerAreaMocks.project.widgetRegions.center = { ...dockedCenter, instanceIds: [] };
};
const button = (name: string) =>
[...host!.querySelectorAll<HTMLButtonElement>('button')].find((candidate) => candidate.textContent === name);
it('says where the view went and offers both ways back, instead of calling it unavailable', async () => {
floatLastView();
await render(<CenterArea />);
expect(host?.textContent).toContain('Preview is in a floating window');
expect(button('Show window')).not.toBeUndefined();
expect(host?.textContent).not.toContain('unavailable');
await act(async () => {
button('Show window')?.click();
await Promise.resolve();
});
expect(centerAreaMocks.revealFloating).toHaveBeenCalledWith('preview-instance');
expect(centerAreaMocks.focusFloating).toHaveBeenCalledWith('preview-instance');
expect(centerAreaMocks.dockFloating).not.toHaveBeenCalled();
await act(async () => {
button('Dock to right panel')?.click();
await Promise.resolve();
});
expect(centerAreaMocks.dockFloating).toHaveBeenCalledWith('preview-instance');
expect(centerAreaMocks.focusRegion).toHaveBeenCalledWith('center', 'preview');
});
it('still reports an empty center whose view is not floating as unavailable', async () => {
centerAreaMocks.project.widgetRegions.center = { ...dockedCenter, instanceIds: [] };
await render(<CenterArea />);
expect(host?.textContent).toContain('common.widgetViewUnavailable');
expect(button('Show window')).toBeUndefined();
});
});
describe('CenterArea with the workflow editor chrome at a narrow width', () => {
afterEach(() => {
centerAreaMocks.chromeSlots = null;
centerAreaMocks.activeItem.label = 'Preview';
});
const renderWorkflowChrome = async (source?: { libraryWorkflowId: string; revision: number }) => {
const [{ loadWorkflowWidgetImplementation }, { WorkflowUiProvider }, { QueryClient, QueryClientProvider }] =
await Promise.all([
import('@features/workflow/widget'),
import('@features/workflow/react'),
import('@tanstack/react-query'),
]);
const implementation = await loadWorkflowWidgetImplementation();
const document = { id: 'workflow-1', name: 'A workflow whose name is far too long for a narrow center view' };
const linkedSource = source ? { ...source, ownerUserId: 'another-user' } : undefined;
const snapshot = {
activeWorkflow: { document, source: linkedSource },
activeWorkflowId: document.id,
canRedo: true,
canUndo: true,
id: 'project-1',
projectGraph: document,
workflows: [{ document, source: linkedSource }],
};
const port = <T,>(value: T) => ({ getSnapshot: () => value, subscribe: () => () => {} });
const adapter = {
CommandShortcut: () => null,
commands: { redo: () => undefined, undo: () => undefined },
getProjectGraph: () => document,
persistence: port({ error: null, hasLocalRecovery: true, lastSavedAt: null, status: 'saved' }),
project: port(snapshot),
widgets: { open: () => undefined, patchValues: () => undefined },
} as unknown as Parameters<typeof WorkflowUiProvider>[0]['adapter'];
const Label = implementation.headerLabel as ComponentType<{ region: 'center' }>;
const runtime = { instanceId: 'workflow:center', region: 'center', typeId: 'workflow' };
const HeaderActions = implementation.headerActions as ComponentType<{ region: 'center'; runtime: typeof runtime }>;
const MenuItems = implementation.headerMenu as ComponentType<{ region: 'center'; runtime: typeof runtime }>;
const Actions = ({ region }: { region: 'center' }) => <HeaderActions region={region} runtime={runtime} />;
centerAreaMocks.chromeSlots = { actions: Actions, label: Label };
centerAreaMocks.activeItem.label = 'Workflow';
await render(
<QueryClientProvider client={new QueryClient()}>
<WorkflowUiProvider adapter={adapter}>
<CenterArea />
{/* The widget's ⋯ menu entries, held open beside the chrome the frame stand-in replaces. */}
<Menu.Root open>
<Portal>
<Menu.Positioner>
<Menu.Content>
<MenuItems region="center" runtime={runtime} />
</Menu.Content>
</Menu.Positioner>
</Portal>
</Menu.Root>
</WorkflowUiProvider>
</QueryClientProvider>,
320
);
};
const isHitTestable = (element: Element) => {
const rect = element.getBoundingClientRect();
const hit = document.elementFromPoint(rect.x + rect.width / 2, rect.y + rect.height / 2);
return rect.width > 0 && hit !== null && element.contains(hit);
};
it.each([
['unlinked', undefined],
['linked, with its Update button', { libraryWorkflowId: 'library-1', revision: 2 }],
])('keeps the workflow name and every header action clickable when %s', async (_label, source) => {
await renderWorkflowChrome(source);
const workflowsButton = host!.querySelector('[data-template-link]')?.closest('button');
const actions = [
...host!.querySelectorAll('[data-hotkey-widget-region="center"] > :last-child :is(button, [role="status"])'),
];
expect(workflowsButton).not.toBeNull();
expect(
actions.filter((action) => action.checkVisibility()).map((action) => action.getAttribute('aria-label'))
).toEqual([
'widgets.workflow.addNode',
'widgets.workflow.saveToLibraryWithEllipsis',
...(source ? ['widgets.workflow.updateTemplateWithEllipsis'] : []),
'widgets.workflow.persistenceSaved',
'Widget settings',
'Widget menu',
]);
await vi.waitFor(() =>
expect([...document.querySelectorAll('[role="menuitem"]')].slice(0, 2).map((item) => item.textContent)).toEqual([
'widgets.workflow.toolbar.undo',
'widgets.workflow.toolbar.redo',
])
);
expect(isHitTestable(workflowsButton!)).toBe(true);
expect(
actions
.filter((action) => action.checkVisibility() && !isHitTestable(action))
.map((action) => action.getAttribute('aria-label'))
).toEqual([]);
});
});