feat: implement generation workflow with resource loading and candidate management
This commit is contained in:
88
operations/generation/workflow.test.ts
Normal file
88
operations/generation/workflow.test.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
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) };
|
||||
}
|
||||
Reference in New Issue
Block a user