Files
image-studio/operations/generation/workflow.test.ts
syntaxbullet 03493a1c32 feat: add generation cancel job functionality and improve job handling
- Introduced `generationCancelJob` command ID in `commands/ids.ts`.
- Added `GenerationCancelJobPayload` type in `commands/payloads.ts`.
- Enhanced job status to include "cancelled" in `editor/state.ts`.
- Updated `runGenerationJob` to accept an `AbortSignal` and handle cancellation.
- Implemented cancellation logic in `runGenerate` and related functions.
- Added tests for job cancellation in `operations/generation/workflow.test.ts`.
- Improved layer rendering logic to prevent stack overflow in `editor/document-indexes.ts`.
- Added raster size assertions in `platform/browser/rasterLimits.ts` for image processing limits.
- Enhanced image file handling to check for size limits in `platform/browser/imageFiles.ts`.
- Updated UI components to reflect job cancellation state in `view/GenerationJobStatus.tsx` and `view/bottom-controls/GenerateActionControls.tsx`.
2026-07-11 11:37:21 +02:00

108 lines
4.1 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { documentCommands } from "@commands/document";
import { generationCommands } from "@commands/generation";
import { commandIds } from "@commands/ids";
import { createCommandRegistry } from "@commands/registry";
import { toolCommands } from "@commands/tool";
import { createInitialAppState } from "@editor/initial-state";
import { createAppStore } from "@editor/store";
import { initialToolState } from "@editor/tools";
import type { GenerationCandidate } from "@editor/state";
import { createGenerationWorkflow, type GenerationWorkflowDependencies } from "./workflow";
describe("generation workflow", () => {
test("reads canonical application state when generation starts", async () => {
const app = createTestApp();
let prompt = "";
const workflow = createGenerationWorkflow(app.store, dependencies({
runGenerate: async (options) => { prompt = options.settings.prompt; },
}));
app.store.dispatch(commandIds.toolSetGenerateSettings, { prompt: "Latest prompt" });
await workflow.generate();
expect(prompt).toBe("Latest prompt");
expect(app.store.getState().editor.generation.jobs[0]?.status).toBe("succeeded");
});
test("uses one acceptance path to add a candidate as a layer", () => {
const app = createTestApp();
const artboardId = app.store.getState().document.artboards[0]?.id;
if (!artboardId) throw new Error("Expected default artboard");
app.store.dispatch(commandIds.generationAddCandidate, { candidate: candidate(artboardId) });
const ids = ["asset-new", "layer-new"];
const workflow = createGenerationWorkflow(app.store, dependencies({ createId: () => ids.shift() ?? "unused" }));
workflow.applyCandidateAsLayer("candidate");
expect(app.store.getState().document.assets.some((asset) => asset.id === "asset-new")).toBe(true);
expect(app.store.getState().document.artboards[0]?.layers.some((layer) => layer.id === "layer-new")).toBe(true);
expect(app.store.getState().editor.generation.candidates).toHaveLength(0);
});
test("cancels the active operation and records a cancelled job", async () => {
const app = createTestApp();
let receivedSignal: AbortSignal | undefined;
const workflow = createGenerationWorkflow(app.store, dependencies({
runGenerate: async (options) => {
receivedSignal = options.signal;
await new Promise<void>((_resolve, reject) => options.signal?.addEventListener("abort", () => reject(new DOMException("Cancelled", "AbortError")), { once: true }));
},
}));
const running = workflow.generate();
await Promise.resolve();
workflow.cancel();
await running;
expect(receivedSignal?.aborted).toBe(true);
expect(app.store.getState().editor.generation.jobs[0]?.status).toBe("cancelled");
});
});
function dependencies(overrides: Partial<GenerationWorkflowDependencies>): GenerationWorkflowDependencies {
return {
runGenerate: async () => undefined,
runGenerateFromCandidate: async () => undefined,
createMaskedPixelReplacementSource: async () => "replacement",
createRefinementMask: async () => "mask",
loadGenerationResources: async () => undefined,
createId: () => crypto.randomUUID(),
...overrides,
};
}
function candidate(artboardId: string): GenerationCandidate {
return {
id: "candidate",
source: "generated",
mimeType: "image/png",
intrinsicSize: { w: 64, h: 64 },
mode: "text-to-image",
settings: initialToolState.generate,
seed: 1,
width: 64,
height: 64,
placement: {
artboardId,
layerName: "Generated",
transform: { position: { x: 0, y: 0 }, scale: { x: 1, y: 1 }, rotation: 0 },
},
};
}
function createTestApp() {
const state = createInitialAppState("Test");
state.document.artboards.push({
id: "artboard",
name: "Artboard",
bounds: { x: 0, y: 0, w: 100, h: 100 },
backgroundColor: "transparent",
visible: true,
locked: false,
layers: [],
});
const registry = createCommandRegistry([...documentCommands, ...toolCommands, ...generationCommands]);
return { store: createAppStore(state, registry) };
}