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.

4.3 KB
Raw Blame
import type { CompositeEntry } from '@workbench/canvas-operations/generationContracts';

import { createDomRasterBackend } from '@workbench/canvas-engine/render/raster';
import { describe, expect, it } from 'vitest';

import { createCompositeDedupeCache, executeControlComposite, executeMaskComposite } from './compositeForGeneration';

describe('executeControlComposite — real browser pixels', () => {
  it('uploads erased transparent pixels as opaque black', async () => {
    const backend = createDomRasterBackend();
    const layerSurface = backend.createSurface(3, 1);
    layerSurface.ctx.putImageData(
      new ImageData(
        new Uint8ClampedArray([
          255,
          255,
          255,
          255, // opaque white control edge
          120,
          80,
          40,
          128, // partially transparent antialiasing
          255,
          255,
          255,
          0, // erased pixel with non-black hidden RGB
        ]),
        3,
        1
      ),
      0,
      0
    );
    const entry: CompositeEntry = {
      bbox: { height: 1, width: 3, x: 0, y: 0 },
      key: 'control-layer|transparent-pixels',
      kind: 'control-layer',
      layerId: 'control',
      layers: [
        {
          blendMode: 'normal',
          contentOffset: { x: 0, y: 0 },
          contentSize: { height: 1, width: 3 },
          id: 'control',
          opacity: 1,
          sourceRef: 'paint:control.png',
          transform: { rotation: 0, scaleX: 1, scaleY: 1, x: 0, y: 0 },
        },
      ],
    };
    let uploadedPixels: number[] | null = null;

    await executeControlComposite(entry, {
      backend,
      dedupe: createCompositeDedupeCache(),
      getLayerSurface: () =>
        Promise.resolve({ rect: { height: 1, width: 3, x: 0, y: 0 }, release: () => undefined, surface: layerSurface }),
      hashBlob: () => Promise.resolve('control-pixels'),
      uploadImage: async (blob) => {
        const bitmap = await backend.createImageBitmap(blob);
        const decoded = backend.createSurface(3, 1);
        decoded.ctx.drawImage(bitmap, 0, 0);
        uploadedPixels = [...decoded.ctx.getImageData(0, 0, 3, 1).data];
        bitmap.close();
        return { height: 1, imageName: 'control.png', width: 3 };
      },
    });

    expect(uploadedPixels).toEqual([255, 255, 255, 255, 60, 40, 20, 255, 0, 0, 0, 255]);
  });
});

describe('executeMaskComposite — real browser pixels', () => {
  it('places a rotated, scaled mask where the canvas shows it', async () => {
    const backend = createDomRasterBackend();
    // A 2×1 local mask: covered on the left pixel only.
    const maskSurface = backend.createSurface(2, 1);
    maskSurface.ctx.fillStyle = '#ffffff';
    maskSurface.ctx.fillRect(0, 0, 1, 1);
    const entry: CompositeEntry = {
      bbox: { height: 8, width: 8, x: 0, y: 0 },
      key: 'inpaint-mask|rotated',
      kind: 'inpaint-mask',
      layers: [],
      maskLayers: [
        {
          attributeValue: 1,
          contentOffset: { x: 0, y: 0 },
          contentSize: { height: 1, width: 2 },
          id: 'mask',
          sourceRef: 'mask:mask.png',
          // A quarter turn at scale 2 about (6, 0): local (x, y) lands at document (6 - 2y, 2x).
          transform: { rotation: Math.PI / 2, scaleX: 2, scaleY: 2, x: 6, y: 0 },
        },
      ],
    };
    let uploaded: ImageData | null = null;

    const result = await executeMaskComposite(entry, {
      backend,
      dedupe: createCompositeDedupeCache(),
      getLayerSurface: () =>
        Promise.resolve({ rect: { height: 1, width: 2, x: 0, y: 0 }, release: () => undefined, surface: maskSurface }),
      hashBlob: () => Promise.resolve('mask-pixels'),
      uploadImage: async (blob) => {
        const bitmap = await backend.createImageBitmap(blob);
        const decoded = backend.createSurface(8, 8);
        decoded.ctx.drawImage(bitmap, 0, 0);
        uploaded = decoded.ctx.getImageData(0, 0, 8, 8);
        bitmap.close();
        return { height: 8, imageName: 'mask.png', width: 8 };
      },
    });

    const gray = (x: number, y: number): number => uploaded!.data[(y * 8 + x) * 4]!;
    expect(result.hasContent).toBe(true);
    // Local pixel (0, 0) covers document 4..6 × 0..2; local (1, 0), unmasked, covers 4..6 × 2..4.
    expect([gray(4, 0), gray(5, 1)]).toEqual([0, 0]);
    expect([gray(4, 2), gray(5, 3), gray(1, 0), gray(6, 0)]).toEqual([255, 255, 255, 255]);
  });
});