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
import {
resetArchitectureCapabilities,
setArchitectureCapabilities,
} from '@features/models/core/architectureCapabilities';
import {
architectureCapabilitiesFixture,
seedArchitectureCapabilities,
} from '@features/models/core/architectureCapabilities.testing';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { DEFAULT_MODEL_GRID, gridSizeForModelBase, resizeBboxAroundCentre, resolveModelGrid } from './bboxGrid';
const bbox = (width: number, height: number, x = 0, y = 0) => ({ height, width, x, y });
describe('gridSizeForModelBase', () => {
seedArchitectureCapabilities();
it('reads the grid the architecture declares', () => {
// The same number the denoise node enforces through `multiple_of` on width/height.
for (const base of ['flux', 'flux2', 'sd-3', 'qwen-image', 'z-image', 'ernie-image']) {
expect(gridSizeForModelBase(base)).toBe(16);
}
expect(gridSizeForModelBase('cogview4')).toBe(32);
for (const base of ['sd-1', 'sd-2', 'sdxl', 'anima']) {
expect(gridSizeForModelBase(base)).toBe(8);
}
});
it('reads the 32px grid Wan TI2V-5B declares, from the table the backend really serves', () => {
expect(gridSizeForModelBase('wan', 'ti2v_5b')).toBe(32);
expect(gridSizeForModelBase('wan')).toBe(16);
expect(720 % gridSizeForModelBase('wan', 'ti2v_5b')).not.toBe(0);
});
it('no longer offers 8px steps for architectures that reject them', () => {
for (const base of ['krea-2', 'wan', 'ideogram-4']) {
expect(gridSizeForModelBase(base)).toBe(16);
}
});
it('falls back to the default grid when there is no model or no row for its architecture', () => {
expect(gridSizeForModelBase(null)).toBe(DEFAULT_MODEL_GRID);
expect(gridSizeForModelBase(undefined)).toBe(DEFAULT_MODEL_GRID);
expect(gridSizeForModelBase('mystery-model')).toBe(DEFAULT_MODEL_GRID);
expect(DEFAULT_MODEL_GRID).toBe(8);
});
});
describe('resolveModelGrid before the capability table arrives', () => {
it('reports the grid as unknown rather than as 8', () => {
expect(resolveModelGrid('wan')).toBeNull();
expect(resolveModelGrid('cogview4')).toBeNull();
expect(resolveModelGrid('sdxl')).toBeNull();
});
it('still answers for "no model selected", which the backend has no say in', () => {
expect(resolveModelGrid(null)).toBe(DEFAULT_MODEL_GRID);
expect(resolveModelGrid(undefined)).toBe(DEFAULT_MODEL_GRID);
});
it('answers for an external generator too, which will never get a row to wait for', () => {
expect(resolveModelGrid('external')).toBe(1);
expect(gridSizeForModelBase('external')).toBe(1);
});
it('keeps the default for readers that must show something', () => {
expect(gridSizeForModelBase('wan')).toBe(DEFAULT_MODEL_GRID);
});
});
describe('the grid a variant declares', () => {
beforeEach(() => {
setArchitectureCapabilities(
architectureCapabilitiesFixture.map((row) =>
row.base === 'wan' && row.variant === 'ti2v_5b'
? { ...row, features: { ...row.features, dimension_grid: 32 } }
: row
)
);
});
afterEach(() => {
resetArchitectureCapabilities();
});
it('prefers the variant row over the architecture row', () => {
expect(gridSizeForModelBase('wan', 'ti2v_5b')).toBe(32);
expect(gridSizeForModelBase('wan')).toBe(16);
});
it('falls back to the architecture row for a variant that declares nothing of its own', () => {
expect(gridSizeForModelBase('wan', 'a14b')).toBe(16);
expect(gridSizeForModelBase('sdxl', 'inpaint')).toBe(8);
});
});
describe('resizeBboxAroundCentre (pure)', () => {
it('keeps the centre of the frame', () => {
// Centre (516, 708); a 1024 square around it starts at (4, 196).
expect(resizeBboxAroundCentre(bbox(832, 1216, 100, 100), { height: 1024, width: 1024 })).toEqual(
bbox(1024, 1024, 4, 196)
);
});
it('rounds a half-pixel offset from an odd size difference to whole pixels', () => {
// 10 + (513 - 512) / 2 = 10.5 and 20 + (512 - 515) / 2 = 18.5.
expect(resizeBboxAroundCentre(bbox(513, 512, 10, 20), { height: 515, width: 512 })).toEqual(bbox(512, 515, 11, 19));
// -0.5 rounds to zero, not negative zero.
expect(resizeBboxAroundCentre(bbox(513, 512, 0, 0), { height: 512, width: 514 }).x).toBe(0);
});
});