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.

chore(mock): v3 canvas fixtures with a schema floor

Josh Corbette53121591061
diff --git a/invokeai/frontend/webv2/scripts/mock-backend-fixtures.mjs b/invokeai/frontend/webv2/scripts/mock-backend-fixtures.mjs
index 49757ba998..570748e55d 100644
--- a/invokeai/frontend/webv2/scripts/mock-backend-fixtures.mjs
+++ b/invokeai/frontend/webv2/scripts/mock-backend-fixtures.mjs
@@ -553,6 +553,58 @@ const createProjectFileWorkflowNodes = () => {
];
};
+/**
+ * The canvas leaves of a v3 document, stacks in composition order, each forest in preorder. The
+ * order mirrors `LAYER_STACK_ORDER` in `src/workbench/canvas-engine/contracts.ts`, which a plain
+ * module cannot import; keep the two in step.
+ */
+export const collectCanvasLeaves = (document) => {
+ const leaves = [];
+ const visit = (nodes) => {
+ for (const node of nodes ?? []) {
+ if (node.type === 'group') {
+ visit(node.children);
+ } else {
+ leaves.push(node);
+ }
+ }
+ };
+ for (const stack of ['raster', 'control', 'regional_guidance', 'inpaint_mask']) {
+ visit(document?.stacks?.[stack]);
+ }
+ return leaves;
+};
+
+/**
+ * The representative raster forest: 64 leaves, of which 24 sit in three groups (one nested two
+ * deep), so the tree exercises indentation, ancestor-effective state and folder export.
+ */
+const createRasterForest = (layers) => {
+ if (layers.length < 32) {
+ return layers;
+ }
+ const group = (id, name, children, overrides = {}) => ({
+ children,
+ id,
+ isEnabled: true,
+ isLocked: false,
+ name,
+ type: 'group',
+ ...overrides,
+ });
+ return [
+ ...layers.slice(0, 8),
+ group('fixture-group-001', 'Fixture Group 001', [
+ ...layers.slice(8, 12),
+ group('fixture-group-002', 'Fixture Group 002', layers.slice(12, 16), { isEnabled: false }),
+ ...layers.slice(16, 20),
+ ]),
+ ...layers.slice(20, 28),
+ group('fixture-group-003', 'Fixture Group 003', layers.slice(28, 32), { isLocked: true }),
+ ...layers.slice(32),
+ ];
+};
+
const createProjectDocument = ({ index, layers = [], workflowNodes = [] }) => {
const id = `fixture-project-${ordinal(index, 3)}`;
const graphId = `${id}-graph`;
@@ -564,9 +616,9 @@ const createProjectDocument = ({ index, layers = [], workflowNodes = [] }) => {
background: 'transparent',
bbox: { height: 1024, width: 1024, x: 0, y: 0 },
height: 1024,
- layers,
selectedLayerId: layers[0]?.id ?? null,
- version: 2,
+ stacks: { control: [], inpaint_mask: [], raster: createRasterForest(layers), regional_guidance: [] },
+ version: 3,
width: 1024,
},
documentRevision: 0,
@@ -579,7 +631,7 @@ const createProjectDocument = ({ index, layers = [], workflowNodes = [] }) => {
pendingImages: [],
selectedImageIndex: 0,
},
- version: 2,
+ version: 3,
},
events: [],
graphHistory: [],
@@ -642,6 +694,7 @@ const createProjects = (count, workflowNodeCount, layerCount) =>
board_id: projectBoardId(index),
created_at: timestamp,
data,
+ minimum_canvas_schema_version: 3,
name: data.name,
project_id: data.id,
revision: 1,
@@ -728,7 +781,7 @@ const countInvocationSchemas = (fixture) =>
export const getMockBackendFixtureCounts = (fixture) => ({
images: fixture.images.length,
- layers: fixture.projects[0]?.data?.canvas?.document?.layers?.length ?? 0,
+ layers: collectCanvasLeaves(fixture.projects[0]?.data?.canvas?.document).length,
models: fixture.models.length,
nodes: countInvocationSchemas(fixture),
projects: fixture.projects.length,
diff --git a/invokeai/frontend/webv2/scripts/mock-backend-fixtures.test.mjs b/invokeai/frontend/webv2/scripts/mock-backend-fixtures.test.mjs
index e6db9dc575..f4234b4679 100644
--- a/invokeai/frontend/webv2/scripts/mock-backend-fixtures.test.mjs
+++ b/invokeai/frontend/webv2/scripts/mock-backend-fixtures.test.mjs
@@ -4,6 +4,7 @@ import test from 'node:test';
import {
assertMockBackendFixture,
+ collectCanvasLeaves,
createMockBackendFixture,
getMockBackendFixtureCounts,
MOCK_BACKEND_PROFILE_COUNTS,
@@ -76,8 +77,19 @@ test('representative fixtures keep node discovery and heavy project data coheren
project.data.projectGraph.nodes.map((node) => node.data.type),
invocationTypes
);
- assert.equal(project.data.canvas.document.layers.length, MOCK_BACKEND_PROFILE_COUNTS.representative.layers);
- assert.equal(new Set(project.data.canvas.document.layers.map((layer) => layer.id)).size, 64);
+ const leaves = collectCanvasLeaves(project.data.canvas.document);
+ assert.equal(leaves.length, MOCK_BACKEND_PROFILE_COUNTS.representative.layers);
+ assert.equal(new Set(leaves.map((layer) => layer.id)).size, 64);
+ assert.equal(project.data.canvas.document.version, 3);
+ assert.equal(project.data.canvas.version, 3);
+ assert.equal(project.minimum_canvas_schema_version, 3);
+ // The raster forest nests: three groups, one two levels deep, one disabled, one locked.
+ const roots = project.data.canvas.document.stacks.raster;
+ const groups = roots.filter((node) => node.type === 'group');
+ assert.equal(groups.length, 2);
+ assert.equal(groups[0].children.filter((node) => node.type === 'group').length, 1);
+ assert.equal(groups[0].children.find((node) => node.type === 'group').isEnabled, false);
+ assert.equal(groups[1].isLocked, true);
});
test('Fixture Project 002 carries the image and video references used by the project-file journey', () => {
@@ -86,11 +98,11 @@ test('Fixture Project 002 carries the image and video references used by the pro
assert.equal(project.project_id, 'fixture-project-002');
assert.deepEqual(
- project.data.canvas.document.layers.map((layer) => layer.source.image.imageName),
+ collectCanvasLeaves(project.data.canvas.document).map((layer) => layer.source.image.imageName),
['fixture-image-0001.png', 'fixture-image-0002.png', 'fixture-image-0003.png', 'fixture-image-0004.png']
);
assert.equal(
- project.data.canvas.document.layers.every((layer) => layer.type === 'raster'),
+ collectCanvasLeaves(project.data.canvas.document).every((layer) => layer.type === 'raster'),
true
);
assert.deepEqual(project.data.projectGraph.nodes[0]?.data.inputs.video?.value, {
@@ -124,7 +136,7 @@ test('Fixture Project 002 owns a board carrying every case the project-file jour
// The canvas draws with these, and no project owns them: on import they deduplicate against the
// destination, and on duplication they are not copied at all.
- const layerNames = project.data.canvas.document.layers.map((layer) => layer.source.image.imageName);
+ const layerNames = collectCanvasLeaves(project.data.canvas.document).map((layer) => layer.source.image.imageName);
for (const name of PROJECT_FILE_BOARD.externalImages) {
assert.ok(layerNames.includes(name), `${name} must stay a canvas reference`);
diff --git a/invokeai/frontend/webv2/scripts/mock-backend.mjs b/invokeai/frontend/webv2/scripts/mock-backend.mjs
index 43911fea92..eae6c832c1 100644
--- a/invokeai/frontend/webv2/scripts/mock-backend.mjs
+++ b/invokeai/frontend/webv2/scripts/mock-backend.mjs
@@ -5,11 +5,28 @@ import { resolve } from 'node:path';
import {
assertMockBackendFixture,
assertMockBackendProfileName,
+ collectCanvasLeaves,
createMockBackendFixture,
getMockBackendFixtureCounts,
MOCK_BACKEND_FIXED_EPOCH,
} from './mock-backend-fixtures.mjs';
+/** The canvas schema every mock project declares; the real server keeps the same floor per record. */
+const DEFAULT_CANVAS_SCHEMA_VERSION = 3;
+
+/** The 412 the real router raises when a client's maximum is below a project's floor. */
+const schemaUnsupported = (minimum, maximum) =>
+ json(412, {
+ detail: {
+ code: 'canvas_schema_unsupported',
+ max_canvas_schema_version: maximum,
+ message: `Project requires canvas schema ${minimum}; client supports up to ${maximum}.`,
+ minimum_canvas_schema_version: minimum,
+ },
+ });
+
+const clientMaximum = (value) => (Number.isInteger(value) && value >= 1 ? value : DEFAULT_CANVAS_SCHEMA_VERSION);
+
/**
* Disposable in-memory InvokeAI backend for browser release/performance tests.
*
@@ -116,6 +133,7 @@ const timestamp = (state) => {
const summaryOf = (project) => ({
board_id: project.board_id,
created_at: project.created_at,
+ minimum_canvas_schema_version: project.minimum_canvas_schema_version ?? DEFAULT_CANVAS_SCHEMA_VERSION,
name: project.name,
project_id: project.project_id,
revision: project.revision,
@@ -143,7 +161,7 @@ const invocationNodeCount = (state) =>
const getStateCounts = (state) => ({
images: state.images.size,
- layers: state.projects.values().next().value?.data?.canvas?.document?.layers?.length ?? 0,
+ layers: collectCanvasLeaves(state.projects.values().next().value?.data?.canvas?.document).length,
models: state.models.size,
nodes: invocationNodeCount(state),
projects: state.projects.size,
@@ -704,10 +722,17 @@ export const startMockBackend = async (port, { profile = 'empty' } = {}) => {
board.updated_at = now;
}
+ const minimum = requested.minimum_canvas_schema_version ?? DEFAULT_CANVAS_SCHEMA_VERSION;
+ const maximum = clientMaximum(requested.max_canvas_schema_version);
+ if (minimum > maximum) {
+ return schemaUnsupported(minimum, maximum);
+ }
+
const project = {
board_id: boardId,
created_at: now,
data: requested.data ?? {},
+ minimum_canvas_schema_version: minimum,
name,
project_id: requested.project_id ?? `mock-project-${projectNumber}`,
revision: 1,
@@ -727,7 +752,15 @@ export const startMockBackend = async (port, { profile = 'empty' } = {}) => {
const project = state.projects.get(projectId);
if (method === 'GET') {
- return project ? json(200, project) : json(404, { detail: 'Project not found' });
+ if (!project) {
+ return json(404, { detail: 'Project not found' });
+ }
+ const maximum = clientMaximum(
+ Number(url.searchParams.get('max_canvas_schema_version') ?? DEFAULT_CANVAS_SCHEMA_VERSION)
+ );
+ return project.minimum_canvas_schema_version > maximum
+ ? schemaUnsupported(project.minimum_canvas_schema_version, maximum)
+ : json(200, project);
}
if (method === 'PUT') {
if (!project) {
@@ -739,9 +772,28 @@ export const startMockBackend = async (port, { profile = 'empty' } = {}) => {
if (requested.expected_revision !== undefined && requested.expected_revision !== project.revision) {
return json(409, { detail: 'Revision conflict' });
}
+ const maximum = clientMaximum(requested.max_canvas_schema_version);
+ if (project.minimum_canvas_schema_version > maximum) {
+ return schemaUnsupported(project.minimum_canvas_schema_version, maximum);
+ }
+ const requestedMinimum = requested.minimum_canvas_schema_version;
+ if (requestedMinimum !== undefined && requestedMinimum < project.minimum_canvas_schema_version) {
+ return json(400, {
+ detail: {
+ code: 'canvas_schema_downgrade',
+ current_minimum_canvas_schema_version: project.minimum_canvas_schema_version,
+ message: 'A project never lowers its canvas schema floor.',
+ requested_minimum_canvas_schema_version: requestedMinimum,
+ },
+ });
+ }
+ if (requestedMinimum !== undefined && requestedMinimum > maximum) {
+ return schemaUnsupported(requestedMinimum, maximum);
+ }
project.data = requested.data ?? project.data;
project.name = requested.name ?? project.name;
+ project.minimum_canvas_schema_version = requestedMinimum ?? project.minimum_canvas_schema_version;
project.revision += 1;
project.updated_at = timestamp(state);
diff --git a/invokeai/frontend/webv2/scripts/run-project-file-journey.mjs b/invokeai/frontend/webv2/scripts/run-project-file-journey.mjs
index 2b0495bca3..e8a7465f1f 100644
--- a/invokeai/frontend/webv2/scripts/run-project-file-journey.mjs
+++ b/invokeai/frontend/webv2/scripts/run-project-file-journey.mjs
@@ -10,6 +10,7 @@ import { fileURLToPath } from 'node:url';
import { chromium } from 'playwright';
import {
+ collectCanvasLeaves,
MOCK_BACKEND_PROFILE_COUNTS,
MOCK_BACKEND_REPRESENTATIVE_VIDEO_NAME,
PROJECT_FILE_BOARD,
@@ -199,7 +200,7 @@ const writeArchiveWithout = async (entries, omitted, targetPath) => {
const getBoardSnapshot = (projectId) => fetchJson(`/api/v1/projects/${encodeURIComponent(projectId)}/board-snapshot`);
const getLayerImageNames = (project) =>
- project.data.canvas.document.layers.map((layer) => layer.source.image.imageName);
+ collectCanvasLeaves(project.data.canvas.document).map((layer) => layer.source.image.imageName);
const getDocumentVideoName = (project) => project.data.projectGraph.nodes[0]?.data.inputs.video?.value?.video_name;