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.

2.5 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import {
  mergeDenoiseProgressImage,
  progressImageToStreamingSource,
  resolveStreamingImageSource,
  type DenoiseProgressImage,
  type StreamingImageSource,
} from './streamingImageSource';

const liveImage: StreamingImageSource = {
  alt: 'Live preview',
  height: 32,
  kind: 'live',
  src: 'data:image/png;base64,live',
  width: 64,
};
const finalImage: StreamingImageSource = {
  alt: 'Final image',
  height: 512,
  kind: 'final',
  src: '/api/final.png',
  width: 512,
};
const fallbackImage: StreamingImageSource = {
  alt: 'Fallback image',
  height: 512,
  kind: 'fallback',
  src: '/api/fallback.png',
  width: 512,
};

describe('resolveStreamingImageSource', () => {
  it('prioritizes final images over live and fallback images', () => {
    expect(resolveStreamingImageSource({ fallbackImage, finalImage, liveImage })).toBe(finalImage);
  });

  it('uses the live image before the fallback', () => {
    expect(resolveStreamingImageSource({ fallbackImage, liveImage })).toBe(liveImage);
  });

  it('falls back when no final or live image is available', () => {
    expect(resolveStreamingImageSource({ fallbackImage })).toBe(fallbackImage);
  });
});

describe('mergeDenoiseProgressImage', () => {
  it('keeps the previous denoise image when the next update is omitted', () => {
    const previous: DenoiseProgressImage = { dataUrl: 'data:image/png;base64,old', height: 32, width: 64 };

    expect(mergeDenoiseProgressImage(previous, undefined)).toBe(previous);
  });

  it('replaces the previous denoise image when a next update is provided', () => {
    const previous: DenoiseProgressImage = { dataUrl: 'data:image/png;base64,old', height: 32, width: 64 };
    const next: DenoiseProgressImage = { dataUrl: 'data:image/png;base64,next', height: 48, width: 96 };

    expect(mergeDenoiseProgressImage(previous, next)).toBe(next);
  });

  it('clears the previous denoise image when the next update is null', () => {
    const previous: DenoiseProgressImage = { dataUrl: 'data:image/png;base64,old', height: 32, width: 64 };

    expect(mergeDenoiseProgressImage(previous, null)).toBeNull();
  });
});

describe('progressImageToStreamingSource', () => {
  it('maps a denoise progress image to a live streaming source', () => {
    expect(
      progressImageToStreamingSource({ dataUrl: 'data:image/png;base64,abc', height: 32, width: 64 }, 'Preview')
    ).toEqual({
      alt: 'Preview',
      height: 32,
      kind: 'live',
      src: 'data:image/png;base64,abc',
      width: 64,
    });
  });
});