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 type { CanvasEditRefusal, CanvasLayerEditRefusal, CanvasNodeContract } from '@workbench/canvas-engine/api';
import { ChakraProvider } from '@chakra-ui/react';
import { AppToaster, toaster } from '@platform/ui/toaster';
import { system } from '@theme/system';
import { getDocumentIndex, getDocumentLeaves } from '@workbench/canvas-engine/api';
import { createStructuralEngineStub } from '@workbench/canvas-engine/controllers/structuralEngine.testStub';
import { groupContract, layerContract } from '@workbench/canvas-engine/document-model/documentFixtures.testStub';
import i18next from 'i18next';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { initReactI18next } from 'react-i18next';
import { afterEach, describe, expect, it } from 'vitest';
import { page, userEvent } from 'vitest/browser';
import { CanvasEditRefusalNotices } from './CanvasEditRefusalNotices';
await i18next.use(initReactI18next).init({
initAsync: false,
lng: 'en',
resources: { en: { translation: await fetch('/locales/en.json').then((response) => response.json()) } },
});
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
let host: HTMLDivElement | null = null;
let root: Root | null = null;
afterEach(async () => {
await act(() => {
toaster.remove();
root?.unmount();
});
host?.remove();
host = null;
root = null;
});
const openToasts = (): HTMLElement[] =>
[...document.querySelectorAll<HTMLElement>('[data-part="root"][data-scope="toast"]')].filter(
(toast) => toast.getAttribute('data-state') === 'open'
);
const toastTexts = () =>
openToasts().map((toast) => ({
actions: [...toast.querySelectorAll('button')]
.map((button) => button.textContent)
.filter((label) => label !== '' && label !== null),
message: toast.querySelector('[data-part="description"]')?.textContent,
title: toast.querySelector('[data-part="title"]')?.textContent,
}));
const mount = async (layers: readonly CanvasNodeContract[], selectedLayerId: string | null) => {
const stub = createStructuralEngineStub({ layers, selectedLayerId });
const listeners = new Set<(refusal: CanvasEditRefusal) => void>();
const engine = {
...stub.engine,
tools: {
onEditRefused: (listener: (refusal: CanvasEditRefusal) => void) => {
listeners.add(listener);
return () => listeners.delete(listener);
},
},
};
host = document.createElement('div');
document.body.append(host);
root = createRoot(host);
await act(() =>
root?.render(
<ChakraProvider value={system}>
<AppToaster />
<CanvasEditRefusalNotices engine={engine as never} />
</ChakraProvider>
)
);
const emit = (refusal: CanvasEditRefusal) => act(() => listeners.forEach((listener) => listener(refusal)));
return { emit, stub };
};
const refusal = (
status: CanvasLayerEditRefusal['status'],
layerId: string,
layerName: string,
canPaintNewLayer = false
): CanvasLayerEditRefusal => ({ canPaintNewLayer, layerId, layerName, status });
describe('CanvasEditRefusalNotices', () => {
it.each([
['layer-hidden', '“Sky” is hidden', 'Show the layer to edit it.'],
['layer-display-hidden', '“Sky” is hidden on the canvas', 'Show it on the canvas to edit it.'],
[
'erase-unsupported',
'“Sky” can’t be erased',
'The eraser works on paint layers, images, control layers and masks.',
],
[
'gradient-unsupported',
'Can’t add a gradient to “Sky”',
'A gradient is a raster layer of its own. Select a raster layer, or nothing, to add one.',
],
['layer-locked', '“Sky” is locked', 'Unlock the layer to edit it.'],
['layer-alpha-locked', '“Sky” has its transparency locked', 'Unlock its transparency to remove pixels from it.'],
['layer-loading', '“Sky” is still loading', 'Its pixels are on their way. Try again in a moment.'],
[
'layer-unpaintable',
'“Sky” has no pixels to edit',
'Its shape is empty, so there is nothing to paint on or erase.',
],
[
'selection-erase-unsupported',
'Can’t remove the selection from “Sky”',
'Erase, Delete and Cut remove pixels from paint layers, images, control layers and masks.',
],
['transform-session', '“Sky” is being transformed', 'Apply or cancel the transform first.'],
] as const)('explains a %s refusal with no offer', async (status, title, message) => {
const { emit } = await mount([layerContract('sky', 'raster', { name: 'Sky' })], 'sky');
await emit(refusal(status, 'sky', 'Sky'));
await expect.poll(toastTexts).toEqual([{ actions: [], message, title }]);
});
it.each([
['inpaint_mask', '“Sky” is left out of generation', 'Use it in generation to edit it.'],
['control', '“Sky” is left out of generation', 'Use it in generation to edit it.'],
['raster', '“Sky” is hidden', 'Show the layer to edit it.'],
] as const)('words a %s out of generation for the control that brings it back', async (type, title, message) => {
const { emit } = await mount([layerContract('sky', type, { isEnabled: false, name: 'Sky' })], 'sky');
await emit(refusal('layer-hidden', 'sky', 'Sky'));
await expect.poll(toastTexts).toEqual([{ actions: [], message, title }]);
});
it.each([
['cutout-no-target', 'Nothing to cut the shape out of', 'Select a paint layer or mask to cut the shape out of it.'],
['erase-no-target', 'Nothing to erase', 'Select a paint layer, image, control layer or mask to erase from it.'],
[
'selection-erase-no-target',
'No layer to remove the selection from',
'Select a paint layer, image, control layer or mask, then try again.',
],
] as const)('explains a %s refusal once however often it is pressed', async (status, title, message) => {
const { emit } = await mount([], null);
await emit({ status });
await emit({ status });
await expect.poll(toastTexts).toEqual([{ actions: [], message, title }]);
});
it.each([
[
'layer-hidden',
'“Sky” is hidden',
'Show the layer to paint on it, or paint on a new layer above it.',
['Paint on a new layer'],
],
[
'layer-display-hidden',
'“Sky” is hidden on the canvas',
'Show it on the canvas to paint on it, or paint on a new layer above it.',
['Paint on a new layer'],
],
[
'layer-locked',
'“Sky” is locked',
'Unlock the layer to paint on it, or paint on a new layer above it.',
['Paint on a new layer'],
],
['layer-loading', '“Sky” is still loading', 'Its pixels are on their way. Try again in a moment.', []],
[
'layer-alpha-locked',
'“Sky” has its transparency locked',
'Unlock its transparency to remove pixels from it.',
[],
],
] as const)(
'words a %s refusal that could offer a new layer in its own terms',
async (status, title, message, actions) => {
const { emit } = await mount([layerContract('sky', 'raster', { name: 'Sky' })], 'sky');
await emit(refusal(status, 'sky', 'Sky', true));
await expect.poll(toastTexts).toEqual([{ actions: [...actions], message, title }]);
}
);
it('replaces a repeated refusal without an offer, one toast per layer and reason', async () => {
const { emit } = await mount(
[layerContract('sky', 'raster', { name: 'Sky' }), layerContract('sea', 'raster', { name: 'Sea' })],
'sky'
);
for (let press = 0; press < 3; press += 1) {
await emit(refusal('layer-alpha-locked', 'sky', 'Sky'));
}
await emit(refusal('layer-locked', 'sky', 'Sky'));
await emit(refusal('layer-alpha-locked', 'sea', 'Sea'));
await emit(refusal('layer-alpha-locked', 'sea', 'Sea'));
await expect
.poll(() => toastTexts().map(({ title }) => title))
.toEqual(['“Sea” has its transparency locked', '“Sky” is locked', '“Sky” has its transparency locked']);
});
it('offers a new layer above the refused one, not the selection, selects it, and closes the other offers', async () => {
const { emit, stub } = await mount(
[layerContract('top', 'raster', { name: 'Top' }), layerContract('photo', 'raster', { name: 'Photo' })],
'top'
);
await emit(refusal('layer-locked', 'photo', 'Photo', true));
await emit(refusal('layer-locked', 'photo', 'Photo', true));
await emit(refusal('layer-hidden', 'top', 'Top', true));
await expect.poll(() => toastTexts().map(({ title }) => title)).toEqual(['“Top” is hidden', '“Photo” is locked']);
expect(toastTexts()[1]).toEqual({
actions: ['Paint on a new layer'],
message: 'Unlock the layer to paint on it, or paint on a new layer above it.',
title: '“Photo” is locked',
});
const photoOffer = openToasts().find((toast) => toast.textContent?.includes('“Photo” is locked'))!;
await userEvent.click(
[...photoOffer.querySelectorAll('button')].find((button) => button.textContent === 'Paint on a new layer')!
);
const ids = () => getDocumentLeaves(stub.document()).map((layer) => layer.id);
await expect.poll(ids).toHaveLength(3);
const added = ids().find((id) => id !== 'top' && id !== 'photo')!;
expect(ids()).toEqual(['top', added, 'photo']);
expect(stub.document().selectedLayerId).toBe(added);
await expect.poll(openToasts).toHaveLength(0);
});
it('puts the new layer above the outermost group that locks the refused layer', async () => {
const child = layerContract('child', 'raster', { name: 'Child' });
const { emit, stub } = await mount(
[groupContract('outer', [groupContract('inner', [child], { isLocked: true })], { isLocked: true })],
'child'
);
await emit(refusal('layer-locked', 'child', 'Child', true));
await userEvent.click(page.getByRole('button', { name: 'Paint on a new layer' }));
await expect.poll(() => getDocumentLeaves(stub.document())).toHaveLength(2);
const index = getDocumentIndex(stub.document());
const added = getDocumentLeaves(stub.document()).find((layer) => layer.id !== 'child')!;
expect(index.byId.get(added.id)?.path).toEqual([]);
expect(index.nodes.map(({ node }) => node.id)).toEqual([added.id, 'outer', 'inner', 'child']);
});
});