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-array-as-prop, react-perf/jsx-no-new-function-as-prop */
import type { ExpandPromptSuggestion } from '@features/generation/core/types';
import type { SavedPromptModels } from '@features/generation/ui/promptFields/PositivePromptActions';
import type { ChangeEvent } from 'react';
import { ChakraProvider } from '@chakra-ui/react';
import { DndContext } from '@dnd-kit/core';
import { expandPrompt, imageToPrompt } from '@features/generation/data/promptUtilities';
import { PositivePromptField } from '@features/generation/ui/promptFields/PositivePromptField';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { system } from '@theme/system';
import i18next from 'i18next';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import { userEvent } from 'vitest/browser';
declare module 'vitest/browser' {
interface BrowserCommands {
imeCompose: (steps: readonly { kind: 'commit' | 'compose'; text: string }[]) => Promise<void>;
}
}
const i18n = i18next.createInstance();
await i18n.use(initReactI18next).init({
fallbackLng: 'en',
lng: 'en',
resources: {
en: {
translation: {
widgets: {
generate: {
expandSuggestedModelAvailable: 'Recommended: {{model}}',
expandSuggestedModelMissing: '{{model}} is not installed',
expandUseSuggestedModelLabel: 'Use {{model}}',
},
},
},
},
},
});
const TEXT_ONLY = { base: 'any', key: 'qwen', name: 'Qwen', source: 'Qwen/Qwen2.5-1.5B-Instruct', type: 'text_llm' };
const ENHANCER = {
base: 'any',
key: 'e2b',
name: 'Gemma-4 E2B',
source: 'google/gemma-4-E2B-it',
supports_images: true,
type: 'text_llm',
};
/** A record whose primary type is another one but whose files also serve as a Text LLM. */
const ROLE_ENHANCER = {
...ENHANCER,
key: 'role-e4b',
name: 'Gemma 4 E4B (encoder)',
roles: [{ format: null, type: 'text_llm', variant: null }],
source: 'google/gemma-4-E4B-it',
type: 'qwen3_5_encoder',
};
const MOSS_PROMPT_ID = '0f8f5b2e-1c9e-4f2a-9a4e-1f1f1f1f0011';
const MOSS_SUGGESTION: ExpandPromptSuggestion = {
image: null,
imageSystemPromptId: null,
modelName: 'Gemma 4 E4B (Prompt Enhancer)',
modelSource: 'google/gemma-4-E4B-it',
systemPromptId: MOSS_PROMPT_ID,
};
const FLUX3_PROMPT_ID = '0f8f5b2e-1c9e-4f2a-9a4e-1f1f1f1f0012';
const prompt = (id: string, content: string) => ({
content,
id,
isPublic: true,
maxTokens: null,
name: id,
userId: 'system',
});
const VISION_A = { base: 'any', key: 'llava-a', name: 'LLaVA A', type: 'llava_onevision' };
const PROMPTS = [
prompt('default', 'DEFAULT'),
prompt('t2v', 'T2V'),
prompt('i2v', 'I2V'),
prompt(MOSS_PROMPT_ID, 'MOSS'),
prompt(FLUX3_PROMPT_ID, 'FLUX3'),
];
const FRAME = { height: 512, image_name: 'frame.png', width: 768 };
const LTX_SUGGESTION: ExpandPromptSuggestion = {
image: FRAME,
imageSystemPromptId: 'i2v',
modelName: 'LTX-2.5 Prompt Enhancer (Gemma-4 E2B)',
modelSource: 'google/gemma-4-E2B-it',
systemPromptId: 't2v',
};
const notices = vi.hoisted(() => ({ info: vi.fn(), offer: vi.fn(), undoable: vi.fn() }));
// Read at render time by the mocks below, so each test can set them before rendering.
let catalog: (
| typeof TEXT_ONLY
| typeof ENHANCER
| typeof VISION_A
| (Omit<typeof ROLE_ENHANCER, 'roles'> & { roles?: unknown[] })
)[] = [];
let arePromptsLoading = false;
const StubSelect = ({
label,
onChange,
options,
value,
}: {
label: string;
onChange: (value: string) => void;
options: { key: string; name: string }[];
value: string | null;
}) => (
<select
aria-label={label}
value={value ?? ''}
onChange={(event: ChangeEvent<HTMLSelectElement>) => onChange(event.currentTarget.value)}
>
<option value="">none</option>
{options.map((option) => (
<option key={option.key} value={option.key}>
{option.name}
</option>
))}
</select>
);
vi.mock('@features/generation/ui/GenerationUiContext', async (importOriginal) => ({
...(await importOriginal<object>()),
GenerationModelSelect: ({
onChange,
value,
}: {
onChange: (model: (typeof catalog)[number] | null) => void;
value: string | null;
}) => (
<StubSelect
label="text llm"
options={catalog}
value={value}
onChange={(key) => onChange(catalog.find((model) => model.key === key) ?? null)}
/>
),
useGenerationUi: () => ({
account: { isAdmin: false, userId: 'system' },
capabilities: { canManagePromptTemplates: false, canManageSharedSystemPrompts: false },
gallery: { selectedImage: { imageName: 'selected.png' } },
models: { catalog, ensureLoaded: vi.fn(), openManager: vi.fn() },
notifications: { info: notices.info, offer: notices.offer, reportError: vi.fn(), undoable: notices.undoable },
project: { activeProjectId: 'project-1' },
promptHistory: { clear: vi.fn(), items: [] },
}),
}));
vi.mock('@features/generation/ui/promptFields/SystemPromptsField', () => ({
SystemPromptsField: ({
onSelect,
selectedId,
}: {
onSelect: (id: string | null) => void;
selectedId: string | null;
}) => (
<StubSelect
label="system prompt"
options={PROMPTS.map(({ id, name }) => ({ key: id, name }))}
value={selectedId}
onChange={(id) => onSelect(id || null)}
/>
),
}));
vi.mock('@features/generation/ui/promptFields/useSystemPrompts', () => ({
useSystemPrompts: () => ({
isLoaded: !arePromptsLoading,
isLoading: arePromptsLoading,
prompts: arePromptsLoading ? [] : PROMPTS,
}),
}));
vi.mock('@features/generation/data/promptUtilities', async (importOriginal) => ({
...(await importOriginal<object>()),
expandPrompt: vi.fn(() => Promise.resolve({ expanded_prompt: 'rewritten', seed: 1 })),
imageToPrompt: vi.fn(() => Promise.resolve({ prompt: 'described' })),
}));
vi.mock('@features/generation/data/wildcards', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
createWildcard: vi.fn(),
deleteWildcard: vi.fn(),
invalidateWildcardDependents: vi.fn(),
updateWildcard: vi.fn(),
wildcardsQueryOptions: () => ({ queryFn: () => Promise.resolve([]), queryKey: ['generation', 'wildcards'] }),
}));
let host: HTMLDivElement | null = null;
let root: Root | null = null;
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
beforeEach(() => {
notices.info.mockClear();
notices.offer.mockClear();
notices.undoable.mockClear();
vi.mocked(expandPrompt).mockClear();
vi.mocked(imageToPrompt).mockClear();
catalog = [TEXT_ONLY, ENHANCER];
arePromptsLoading = false;
});
afterEach(async () => {
await act(() => root?.unmount());
host?.remove();
host = null;
root = null;
});
let rendered: {
onChange: (value: string) => void;
projectId: string;
savedPromptModels: SavedPromptModels | undefined;
suggestion: ExpandPromptSuggestion | null;
} | null = null;
const renderField = (
suggestion: ExpandPromptSuggestion | null,
savedPromptModels?: SavedPromptModels,
onChange: (value: string) => void = vi.fn(),
projectId = 'project-1'
) => {
rendered = { onChange, projectId, savedPromptModels, suggestion };
root?.render(
<I18nextProvider i18n={i18n}>
<QueryClientProvider client={new QueryClient()}>
<ChakraProvider value={system}>
<DndContext>
<PositivePromptField
expandPromptSuggestion={suggestion}
heightPx={96}
loras={[]}
projectId={projectId}
savedPromptModels={savedPromptModels}
selectedModel={undefined}
showSyntaxHighlighting={false}
value="she waves"
onChange={onChange}
onResizeEnd={vi.fn()}
onUsePrompt={vi.fn()}
/>
</DndContext>
</ChakraProvider>
</QueryClientProvider>
</I18nextProvider>
);
};
const renderAndOpen = async (
suggestion: ExpandPromptSuggestion | null,
savedPromptModels?: SavedPromptModels,
trigger = 'widgets.generate.expandPrompt',
onChange?: (value: string) => void
) => {
host = document.createElement('div');
document.body.append(host);
root = createRoot(host);
await act(() => renderField(suggestion, savedPromptModels, onChange));
await act(() => host?.querySelector<HTMLButtonElement>(`[aria-label="${trigger}"]`)?.click());
};
const savedModels = (overrides: Partial<SavedPromptModels> = {}): SavedPromptModels => {
const onChange = vi.fn((patch: Partial<SavedPromptModels>) => {
if (rendered?.savedPromptModels?.onChange === onChange) {
renderField(
rendered.suggestion,
{ ...rendered.savedPromptModels, ...patch },
rendered.onChange,
rendered.projectId
);
}
});
return { expandPromptModelKey: null, imageToPromptModelKey: null, ...overrides, onChange };
};
const select = (label: string) => document.querySelector<HTMLSelectElement>(`select[aria-label="${label}"]`);
const choose = (label: string, value: string) =>
act(() => {
const element = select(label);
if (element) {
element.value = value;
element.dispatchEvent(new Event('change', { bubbles: true }));
}
});
const expandButton = () =>
[...document.querySelectorAll<HTMLButtonElement>('button')].find(
(button) => button.textContent === 'widgets.generate.expand'
);
const firstFrameCheckbox = () =>
[...document.querySelectorAll('label')].find((label) =>
label.textContent?.includes('widgets.generate.expandFromFirstFrame')
);
const runExpand = async () => {
await act(() => expandButton()?.click());
expect(expandPrompt).toHaveBeenCalledOnce();
return vi.mocked(expandPrompt).mock.calls[0][0];
};
it('starts from the suggested enhancer and describes from the first frame with the image prompt', async () => {
await renderAndOpen(LTX_SUGGESTION);
expect(select('text llm')?.value).toBe('e2b');
expect(await runExpand()).toMatchObject({ image_name: 'frame.png', model_key: 'e2b', system_prompt: 'I2V' });
});
it('uses the text prompt and says why for a model that cannot read images', async () => {
await renderAndOpen(LTX_SUGGESTION);
await choose('text llm', 'qwen');
expect(firstFrameCheckbox()).toBeUndefined();
expect(document.body.textContent).toContain('widgets.generate.expandFirstFrameUnreadable');
const request = await runExpand();
expect(request).toMatchObject({ model_key: 'qwen', system_prompt: 'T2V' });
expect(request.image_name).toBeUndefined();
});
it("keeps the surface's saved pick over an installed suggestion, also after a remount, and saves a different one", async () => {
const saved = savedModels({ expandPromptModelKey: 'qwen' });
await renderAndOpen(LTX_SUGGESTION, saved);
expect(select('text llm')?.value).toBe('qwen');
expect(document.body.textContent).toContain('Recommended: Gemma-4 E2B');
await act(() => root?.unmount());
host?.remove();
await renderAndOpen(LTX_SUGGESTION, saved);
expect(select('text llm')?.value).toBe('qwen');
await choose('text llm', 'e2b');
expect(saved.onChange).toHaveBeenCalledExactlyOnceWith({ expandPromptModelKey: 'e2b' });
});
it('offers a record that serves as a Text LLM through a role, and suggests it by source', async () => {
catalog = [TEXT_ONLY, ROLE_ENHANCER];
await renderAndOpen(MOSS_SUGGESTION, savedModels());
expect(select('text llm')?.value).toBe('role-e4b');
});
it('does not take a record of another type without the Text LLM role as a candidate', async () => {
catalog = [TEXT_ONLY, { ...ROLE_ENHANCER, roles: [] }];
await renderAndOpen(MOSS_SUGGESTION, savedModels());
expect(select('text llm')?.value).toBe('qwen');
expect(document.body.textContent).toContain('is not installed');
});
it("starts another project from its own saved Expand Prompt model, not the previous project's pick", async () => {
await renderAndOpen(null, savedModels());
await choose('text llm', 'qwen');
expect(select('text llm')?.value).toBe('qwen');
await act(() => renderField(null, savedModels({ expandPromptModelKey: 'e2b' }), undefined, 'project-2'));
expect(select('text llm')?.value).toBe('e2b');
});
const isPopoverOpen = (title: string) =>
[...document.querySelectorAll<HTMLElement>('[data-scope="popover"][data-part="content"]')].some(
(content) => content.dataset.state === 'open' && content.textContent?.includes(title)
);
// A real click on the page outside the popover; zag settles the dismissal outside act.
const clickOutside = async () => {
const outside = document.createElement('div');
outside.style.height = '200px';
document.body.append(outside);
await userEvent.click(outside);
outside.remove();
await settle();
};
const settle = () =>
act(async () => {
await new Promise<void>((resolve) => {
globalThis.setTimeout(resolve, 100);
});
});
const pendingRun = () => {
let finish: (() => void) | undefined;
const running = new Promise<void>((resolve) => {
finish = resolve;
});
return {
finish: () => {
finish?.();
return settle();
},
running,
};
};
const trigger = (label: string) => host?.querySelector<HTMLButtonElement>(`[aria-label="${label}"]`);
const isSpinning = (button: HTMLButtonElement | null | undefined) =>
button?.querySelector('.chakra-spinner') !== null && button?.getAttribute('aria-busy') === 'true';
const promptBox = () =>
host?.querySelector<HTMLTextAreaElement>('textarea[aria-label="widgets.generate.positivePrompt"]');
const buttonWithText = (text: string) =>
[...document.querySelectorAll<HTMLButtonElement>('button')].find((button) => button.textContent === text);
const undoLastResult = () => {
const [, notice] = notices.undoable.mock.calls.at(-1) as [string, { onUndo: () => void }];
return act(() => notice.onUndo());
};
it('writes a result that lands while the popover is open as an edit, closes it, and offers an Undo', async () => {
const run = pendingRun();
const onChange = vi.fn();
vi.mocked(expandPrompt).mockImplementationOnce(() => run.running.then(() => ({ expanded_prompt: 'x', seed: 1 })));
await renderAndOpen(null, undefined, 'widgets.generate.expandPrompt', onChange);
await act(() => expandButton()?.click());
expect(isSpinning(trigger('widgets.generate.expandPrompt'))).toBe(true);
await run.finish();
expect(isPopoverOpen('widgets.generate.expand')).toBe(false);
expect(isSpinning(trigger('widgets.generate.expandPrompt'))).toBe(false);
expect(promptBox()?.value).toBe('x');
expect(onChange).toHaveBeenLastCalledWith('x');
expect(document.activeElement).toBe(promptBox());
expect(notices.undoable).toHaveBeenCalledExactlyOnceWith('widgets.generate.promptExpanded', {
onUndo: expect.any(Function),
projectId: 'project-1',
supersedes: 'generate-prompt:project-1',
});
// What Ctrl+Z runs: the browser's own undo, which a controlled value write would have wiped.
await act(() => {
promptBox()?.focus();
document.execCommand('undo');
});
expect(promptBox()?.value).toBe('she waves');
});
it('keeps running once the popover closes, and lands without taking focus from the field the user typed in', async () => {
const run = pendingRun();
vi.mocked(expandPrompt).mockImplementationOnce(() => run.running.then(() => ({ expanded_prompt: 'x', seed: 1 })));
await renderAndOpen(null);
const elsewhere = document.createElement('textarea');
document.body.append(elsewhere);
await act(() => expandButton()?.click());
await clickOutside();
expect(isPopoverOpen('widgets.generate.expand')).toBe(false);
expect(isSpinning(trigger('widgets.generate.expandPrompt'))).toBe(true);
await userEvent.type(elsewhere, 'notes');
await run.finish();
expect(promptBox()?.value).toBe('x');
expect(document.activeElement).toBe(elsewhere);
expect(elsewhere.value).toBe('notes');
await undoLastResult();
expect(promptBox()?.value).toBe('she waves');
elsewhere.remove();
});
it('drops the result of a request the user stopped waiting for', async () => {
const run = pendingRun();
const onChange = vi.fn();
vi.mocked(expandPrompt).mockImplementationOnce(() => run.running.then(() => ({ expanded_prompt: 'x', seed: 1 })));
await renderAndOpen(null, undefined, 'widgets.generate.expandPrompt', onChange);
await act(() => expandButton()?.click());
await act(() => buttonWithText('widgets.generate.stopWaiting')?.click());
expect(isSpinning(trigger('widgets.generate.expandPrompt'))).toBe(false);
await run.finish();
expect(promptBox()?.value).toBe('she waves');
expect(onChange).not.toHaveBeenCalled();
expect(notices.undoable).not.toHaveBeenCalled();
});
it('offers the result in a toast instead of overwriting a prompt edited while it ran', async () => {
const run = pendingRun();
vi.mocked(expandPrompt).mockImplementationOnce(() =>
run.running.then(() => ({ expanded_prompt: 'rewritten', seed: 1 }))
);
await renderAndOpen(null);
await act(() => expandButton()?.click());
await act(() => promptBox()?.setSelectionRange(9, 9));
await userEvent.type(promptBox()!, ' hello');
await run.finish();
expect(promptBox()?.value).toBe('she waves hello');
expect(notices.undoable).not.toHaveBeenCalled();
const [title, offer] = notices.offer.mock.calls[0] as [
string,
{ actions: { label: string; onClick: () => void }[]; message: string },
];
expect(title).toBe('widgets.generate.promptChangedWhileRunning');
expect(offer.message).toBe('rewritten');
expect(offer.actions.map((action) => action.label)).toEqual(['widgets.generate.replacePrompt', 'common.discard']);
await act(() => offer.actions[0]!.onClick());
expect(promptBox()?.value).toBe('rewritten');
await undoLastResult();
expect(promptBox()?.value).toBe('she waves hello');
});
it('drops a result for a project this field no longer shows', async () => {
const run = pendingRun();
const onChange = vi.fn();
vi.mocked(expandPrompt).mockImplementationOnce(() => run.running.then(() => ({ expanded_prompt: 'x', seed: 1 })));
await renderAndOpen(null, undefined, 'widgets.generate.expandPrompt', onChange);
await act(() => expandButton()?.click());
await act(() => renderField(null, undefined, onChange, 'project-2'));
await run.finish();
expect(promptBox()?.value).toBe('she waves');
expect(onChange).not.toHaveBeenCalled();
expect(notices.offer).not.toHaveBeenCalled();
expect(notices.undoable).not.toHaveBeenCalled();
});
it('runs Image to Prompt and writes its prompt', async () => {
catalog = [VISION_A];
const run = pendingRun();
vi.mocked(imageToPrompt).mockImplementationOnce(() => run.running.then(() => ({ prompt: 'described' })));
await renderAndOpen(null, undefined, 'widgets.generate.imageToPrompt');
const generateButton = () => buttonWithText('widgets.generate.generatePrompt');
await act(() => generateButton()?.click());
expect(isSpinning(trigger('widgets.generate.imageToPrompt'))).toBe(true);
await run.finish();
expect(isSpinning(trigger('widgets.generate.imageToPrompt'))).toBe(false);
expect(isPopoverOpen('widgets.generate.generatePrompt')).toBe(false);
expect(promptBox()?.value).toBe('described');
expect(notices.undoable).toHaveBeenCalledWith('widgets.generate.promptFromImage', expect.anything());
});