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
test(canvas): cover pixel undo when the cache cannot be restored
Josh Corbett895aeccb0534
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/rasterController.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/rasterController.tsindex d2d966c5ce..530e3887df 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/rasterController.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/controllers/rasterController.ts@@ -230,7 +230,7 @@ export class RasterController {: null;}- /** The memoized adjusted copy of a layer's live cache, when `surface` is that cache and its pixels are unbaked. */+ /** The adjusted copy of a layer's live cache (built and memoized on a miss), when `surface` is that cache and unbaked. */getAdjustedCacheSurface(layerId: string,surface: RasterSurface,diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.tsindex f2cbff24c6..4d66bf9b2c 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/engine.test.ts@@ -18493,7 +18493,8 @@ describe('history entries and stepBy', () => {});describe('pixel undo onto a cache that is no longer trustworthy', () => {- it('rasterizes the layer from its source before writing the patch', async () => {+ /** Records a brush stroke on `source`, then invalidates every cache so the next undo cannot trust it. */+ const strokeThenInvalidate = async (source: CanvasLayerSourceContract, imageResolver: () => Promise<Blob>) => {const raf = createControllableRaf();vi.stubGlobal('requestAnimationFrame', raf.requestFrame);vi.stubGlobal('cancelAnimationFrame', raf.cancelFrame);@@ -18507,15 +18508,20 @@ describe('pixel undo onto a cache that is no longer trustworthy', () => {});const document = paintDoc();- const persisted = {- ...getDocumentLeaves(document)[0]!,- source: { bitmap: { height: 100, imageName: 'persisted', width: 100 }, type: 'paint' as const },- };- const { store } = createReactiveStore({ ...document, stacks: stacksFrom([persisted]) });- const imageResolver = vi.fn(() => Promise.resolve(new Blob()));+ const layer = { ...getDocumentLeaves(document)[0]!, source } as CanvasLayerContract;+ const { store } = createReactiveStore({ ...document, stacks: stacksFrom([layer]) });+ const base = createTestStubRasterBackend();+ const surfaces: { width: number; height: number }[] = [];+ const bitmapStore = createSpyBitmapStore();const engine = createCanvasEngine({- backend: createTestStubRasterBackend(),- bitmapStore: createSpyBitmapStore(),+ backend: {+ ...base,+ createSurface: (width: number, height: number) => {+ surfaces.push({ height, width });+ return base.createSurface(width, height);+ },+ },+ bitmapStore,imageResolver,projectId: 'p1',store,@@ -18530,15 +18536,50 @@ describe('pixel undo onto a cache that is no longer trustworthy', () => {overlay.fire('pointermove', pointerAt(40, 40));overlay.fire('pointerup', pointerAt(40, 40, { buttons: 0 }));expect(engine.history.getEntries().past).toEqual(['Brush stroke']);-await engine.diagnostics.clearCaches();+ bitmapStore.markLayerDirty.mockClear();+ return { bitmapStore, engine, surfaces };+ };+ const persisted: CanvasLayerSourceContract = {+ bitmap: { height: 100, imageName: 'persisted', width: 100 },+ type: 'paint',+ };++ it('rasterizes the layer from its source before writing the patch', async () => {+ const imageResolver = vi.fn(() => Promise.resolve(new Blob()));+ const { engine } = await strokeThenInvalidate(persisted, imageResolver);const fetchesBeforeUndo = imageResolver.mock.calls.length;- expect(await engine.history.undo()).toBe('applied');+ expect(await engine.history.undo()).toBe('applied');expect(imageResolver.mock.calls.length).toBeGreaterThan(fetchesBeforeUndo);expect(engine.history.getEntries()).toEqual({ future: ['Brush stroke'], past: [] });engine.lifecycle.dispose();});++ it('keeps the step in place, unpersisted, when the pixels cannot be rasterized', async () => {+ let fail = false;+ const imageResolver = vi.fn(() => (fail ? Promise.reject(new Error('offline')) : Promise.resolve(new Blob())));+ const { bitmapStore, engine } = await strokeThenInvalidate(persisted, imageResolver);+ fail = true;++ await expectReplayFailure(engine.history.undo(), /could not be restored/);+ expect(engine.history.getEntries()).toEqual({ future: [], past: ['Brush stroke'] });+ expect(bitmapStore.markLayerDirty).not.toHaveBeenCalled();+ engine.lifecycle.dispose();+ });++ it('writes the patch into a fresh cache when the source now holds no pixels', async () => {+ const { bitmapStore, engine, surfaces } = await strokeThenInvalidate({ bitmap: null, type: 'paint' }, () =>+ Promise.resolve(new Blob())+ );+ const allocatedBeforeUndo = surfaces.length;++ expect(await engine.history.undo()).toBe('applied');+ // The stale cache is dropped rather than patched, so its old pixels never persist around the patch.+ expect(surfaces.length).toBeGreaterThan(allocatedBeforeUndo);+ expect(bitmapStore.markLayerDirty).toHaveBeenCalledWith('paint1');+ engine.lifecycle.dispose();+ });});describe('refused strokes', () => {diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/rasterComposite.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/rasterComposite.tsindex c13acf322e..4f4202a2ab 100644--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/rasterComposite.ts+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/rasterComposite.ts@@ -55,7 +55,7 @@ export interface RenderRasterCompositeDeps {};/** Owned access to a layer's pixels, released once they are drawn. */getLayerSurface(layerId: string): Promise<{ surface: RasterSurface; rect: Rect; release(): void }>;- /** The display's memoized, layer-local adjusted copy of `surface`, or null when the owner keeps none for it. */+ /** The display's layer-local adjusted copy of `surface` (built and memoized on a miss), or null when `surface` is not a live cache. */adjustedSurface?(layerId: string,surface: RasterSurface,