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.

27.2 KB
Raw Blame
import type { ModelConfig, ModelsSnapshot } from '@features/models';
import type { SocketHub } from '@platform/transport/socketHub';
import type { RegisteredWidget } from '@workbench/widgetContracts';
import type { TFunction } from 'i18next';

import { accountLifecycle } from '@platform/state/accountLifecycle';
import { ApiError } from '@platform/transport/http';
import { getProjectWidgetValues } from '@workbench/widgetState';
import { createWorkbenchStore } from '@workbench/workbenchStore';
import { beforeEach, describe, expect, it, vi } from 'vitest';

const galleryApi = vi.hoisted(() => ({
  GALLERY_REQUEST_MAX_NAMES: 1000,
  galleryImages: { metadata: vi.fn(), resolveMany: vi.fn() },
  galleryItems: { resolve: vi.fn() },
  galleryVideos: { workflow: vi.fn() },
}));
const modelsApi = vi.hoisted(() => ({ ensureModelsLoaded: vi.fn(), getModelsSnapshot: vi.fn() }));

vi.mock('@features/gallery', () => galleryApi);
vi.mock('@features/models', async (importOriginal) => ({
  ...(await importOriginal<Record<string, unknown>>()),
  ...modelsApi,
}));

import { createDefaultVideoWidgetValues } from '@features/video';

import { createVideoRecallRuntime } from './videoRecallRuntime';

type SocketHandler = (payload: never) => void;

const createFakeSocketHub = () => {
  const handlers = new Map<string, Set<SocketHandler>>();
  const hub: Pick<SocketHub, 'on'> = {
    on: (event, handler) => {
      const eventHandlers = handlers.get(event) ?? new Set<SocketHandler>();
      eventHandlers.add(handler);
      handlers.set(event, eventHandlers);
      return () => eventHandlers.delete(handler);
    },
  };

  return {
    emit: (payload: unknown) => {
      for (const handler of handlers.get('video_recall_requested') ?? []) {
        handler(payload as never);
      }
    },
    hub,
  };
};

const model = (fields: Record<string, unknown>) =>
  ({
    file_size: 1,
    hash: 'h',
    path: '/m',
    source: 'local',
    source_type: 'path',
    type: 'main',
    ...fields,
  }) as ModelConfig;

const WAN_I2V = model({ base: 'wan', format: 'diffusers', key: 'wan-i2v', name: 'Wan I2V', variant: 'i2v_a14b' });
const WAN_T2V = model({ base: 'wan', format: 'diffusers', key: 'wan-t2v', name: 'Wan T2V', variant: 't2v_a14b' });
const REF2VA = model({
  base: 'minimax-h3',
  format: 'checkpoint',
  key: 'h3-ref2va',
  name: 'H3 Ref2VA',
  variant: 'ref2va',
});
const LTX2 = model({ base: 'ltx-2', format: 'checkpoint', key: 'ltx2', name: 'LTX-2.5 dev', variant: 'ltx2_dev' });

const t = ((key: string) => key) as unknown as TFunction;

const flush = () =>
  new Promise<void>((resolve) => {
    setTimeout(resolve, 0);
  });

const parametersEvent = (parameters: Record<string, unknown>, extra: Record<string, unknown> = {}) => ({
  action: 'parameters',
  mode: 'recall',
  parameters,
  queue_id: 'default',
  strict: false,
  user_id: 'owner',
  ...extra,
});

const placementEvent = (
  action: 'conditioning_video' | 'initial_video' | 'reference_video',
  video: Record<string, unknown> = {}
) => ({
  action,
  queue_id: 'default',
  user_id: 'owner',
  video: { duration: 5, fps: 16, height: 480, media_origin: null, video_name: 'clip.mp4', width: 832, ...video },
});

const heldFrame = { height: 480, image_name: 'held.png', width: 832 };

/** The registry's view of the Video widget: placeable on the left, nothing to preload. */
const videoWidget = {
  implementation: { preload: () => undefined },
  manifest: { allowedRegions: ['left'], id: 'video' },
  status: 'enabled',
} as unknown as RegisteredWidget;

const setup = (panelModel: ModelConfig = WAN_I2V) => {
  const store = createWorkbenchStore();
  const socket = createFakeSocketHub();
  const projectId = store.queries.getSnapshot().activeProject.id;

  store.commands.widgets.patchValues(
    'video',
    {
      ...createDefaultVideoWidgetValues([panelModel]),
      ...(panelModel === WAN_I2V ? { firstFrameImage: heldFrame } : {}),
      positivePrompt: 'before',
    },
    projectId
  );
  const runtime = createVideoRecallRuntime({
    commands: store.commands,
    hub: socket.hub,
    queries: store.queries,
    reveal: {
      getWidgetsForRegion: (region) => (region === 'left' ? [videoWidget] : []),
      isEditingText: () => false,
    },
    t,
  });
  /** Whether the Video widget is the visible tab of the project's left region. */
  const videoShown = (id = projectId) => {
    const project = store.queries.getProject(id)!;
    const left = project.widgetRegions.left;

    return (
      !left.isCollapsed &&
      left.activeInstanceId !== null &&
      project.widgetInstances[left.activeInstanceId]?.typeId === 'video'
    );
  };
  const videoValues = (id = projectId) => {
    const project = store.queries.getProject(id);
    return project ? getProjectWidgetValues(project, 'video') : {};
  };
  // Newest first.
  const lastNotice = () => store.queries.getSnapshot().notifications[0];

  return { lastNotice, projectId, runtime, socket, store, videoShown, videoValues };
};

describe('createVideoRecallRuntime', () => {
  beforeEach(() => {
    accountLifecycle.activate('owner');
    modelsApi.ensureModelsLoaded.mockReset();
    modelsApi.ensureModelsLoaded.mockResolvedValue(undefined);
    modelsApi.getModelsSnapshot.mockReset();
    modelsApi.getModelsSnapshot.mockReturnValue({
      error: null,
      models: [WAN_I2V, WAN_T2V, REF2VA, LTX2],
      status: 'loaded',
    } as ModelsSnapshot);
    galleryApi.galleryItems.resolve.mockReset();
    galleryApi.galleryItems.resolve.mockImplementation(({ name }: { name: string }) =>
      Promise.resolve({ durationSeconds: 5, fps: 16, height: 480, kind: 'video', name, width: 832 })
    );
    galleryApi.galleryImages.resolveMany.mockReset();
    galleryApi.galleryImages.resolveMany.mockImplementation((names: string[]) =>
      Promise.resolve(names.map((imageName) => ({ height: 480, imageName, width: 832 })))
    );
  });

  it('applies a non-strict recall on top of the panel and reveals the Video widget', async () => {
    const { lastNotice, videoShown, runtime, socket, videoValues } = setup();

    socket.emit(parametersEvent({ positive_prompt: 'a heron', seed: 99 }));
    await flush();

    expect(videoValues()).toMatchObject({ firstFrameImage: heldFrame, positivePrompt: 'a heron', seed: 99 });
    expect(lastNotice()).toEqual(
      expect.objectContaining({ kind: 'success', title: 'widgets.gallery.itemActions.recall.videoTitles.all' })
    );
    expect(videoShown()).toBe(true);

    runtime.dispose();
  });

  describe('frames beside an LTX-2 conditioning clip', () => {
    const pictureClip = {
      clip: { fps: 24, height: 480, numFrames: 96, video_name: 'held.mp4', width: 832 },
      fpsKnown: true,
      role: 'video',
    };
    const recalledClip = (role: 'audio' | 'video') => ({
      ltx2_conditioning_role: role,
      ltx2_conditioning_video: { video_name: 'song.mp4' },
    });

    it('lets a recalled frame displace a clip the panel holds for its picture, as setting it would', async () => {
      const { projectId, runtime, socket, store, videoValues } = setup(LTX2);
      store.commands.widgets.patchValues('video', { conditioningClip: pictureClip }, projectId);
      // The record's own clip is gone, so only its frame lands.
      galleryApi.galleryItems.resolve.mockRejectedValue(new ApiError('Not Found', 404));

      socket.emit(parametersEvent({ last_frame_image: { image_name: 'last.png' }, ...recalledClip('audio') }));
      await flush();

      expect(videoValues()).toMatchObject({ conditioningClip: null, lastFrameImage: { image_name: 'last.png' } });

      runtime.dispose();
    });

    it("keeps the panel's frames beside a recalled soundtrack and clears them for a recalled picture", async () => {
      for (const [role, frame] of [
        ['audio', heldFrame],
        ['video', null],
      ] as const) {
        const { projectId, runtime, socket, store, videoValues } = setup(LTX2);
        store.commands.widgets.patchValues('video', { firstFrameImage: heldFrame }, projectId);

        socket.emit(parametersEvent(recalledClip(role)));
        await flush();

        expect(videoValues(), role).toMatchObject({ conditioningClip: { role }, firstFrameImage: frame });

        runtime.dispose();
      }
    });
  });

  it('clears media a strict recall does not name', async () => {
    const { runtime, socket, videoValues } = setup();

    socket.emit(parametersEvent({ positive_prompt: 'a heron' }, { strict: true }));
    await flush();

    expect(videoValues()).toMatchObject({ firstFrameImage: null, positivePrompt: 'a heron' });

    runtime.dispose();
  });

  it('titles a remix as one', async () => {
    const { lastNotice, runtime, socket } = setup();

    socket.emit(parametersEvent({ positive_prompt: 'a heron' }, { mode: 'remix' }));
    await flush();

    expect(lastNotice()).toEqual(
      expect.objectContaining({ title: 'widgets.gallery.itemActions.recall.videoTitles.remix' })
    );

    runtime.dispose();
  });

  it.each([
    ['fixed', 'random'],
    ['random', 'random'],
    ['increment', 'increment'],
    ['decrement', 'decrement'],
  ])('a remix turns a %s seed into %s', async (before, after) => {
    const { runtime, socket, store, projectId, videoValues } = setup();
    store.commands.widgets.patchValues('video', { seed: 42, seedMode: before }, projectId);

    socket.emit(parametersEvent({ positive_prompt: 'a heron' }, { mode: 'remix' }));
    await flush();

    expect(videoValues()).toMatchObject({ positivePrompt: 'a heron', seed: 42, seedMode: after });

    runtime.dispose();
  });

  it('keeps a fixed seed for a recall, and for a remix that applied nothing', async () => {
    const { runtime, socket, store, projectId, videoValues } = setup();
    store.commands.widgets.patchValues('video', { seedMode: 'fixed' }, projectId);

    socket.emit(parametersEvent({ positive_prompt: 'a heron' }));
    await flush();
    socket.emit(parametersEvent({ model: { key: 'not-installed' } }, { mode: 'remix' }));
    await flush();

    expect(videoValues().seedMode).toBe('fixed');

    runtime.dispose();
  });

  it('applies to the project it arrived for, without pulling a different project to the front', async () => {
    const { videoShown, projectId, runtime, socket, store, videoValues } = setup();
    let releaseModels: () => void = () => {};
    modelsApi.ensureModelsLoaded.mockImplementation(
      () =>
        new Promise<void>((resolve) => {
          releaseModels = resolve;
        })
    );

    socket.emit(parametersEvent({ positive_prompt: 'for the first project' }));
    const second = store.commands.projects.create();
    await flush();
    releaseModels();
    await flush();

    expect(videoValues(projectId).positivePrompt).toBe('for the first project');
    expect(videoValues(second.id).positivePrompt).toBeUndefined();
    expect(videoShown()).toBe(false);

    runtime.dispose();
  });

  it('sets the initial video as the Initial Video field would', async () => {
    const { lastNotice, videoShown, runtime, socket, videoValues } = setup();

    socket.emit(placementEvent('initial_video'));
    await flush();

    expect(videoValues()).toMatchObject({ firstFrameImage: null, sourceVideo: { fps: 16, video_name: 'clip.mp4' } });
    expect(lastNotice()).toEqual(
      expect.objectContaining({ kind: 'success', title: 'widgets.video.placement.initialVideoSet' })
    );
    expect(videoShown()).toBe(true);

    runtime.dispose();
  });

  it('declines a reference video the panel model cannot take, leaving the panel alone', async () => {
    const { lastNotice, videoShown, runtime, socket, videoValues } = setup();
    const before = videoValues();

    socket.emit(placementEvent('reference_video'));
    await flush();

    expect(videoValues()).toBe(before);
    expect(lastNotice()).toEqual(
      expect.objectContaining({
        kind: 'info',
        message: 'widgets.video.placement.referenceUnsupported',
        title: 'widgets.video.placement.referenceNotAdded',
      })
    );
    expect(videoShown()).toBe(false);

    runtime.dispose();
  });

  it.each([
    ['a parameters event without its strict flag', { ...parametersEvent({}), strict: undefined }],
    ['a placement without the video it places', { ...placementEvent('initial_video'), video: null }],
    ['a placement whose video has no size', { ...placementEvent('initial_video'), video: { video_name: 'clip.mp4' } }],
    ['an unknown action', { ...placementEvent('initial_video'), action: 'delete_video' }],
    [
      'an image placement without its append flag',
      { action: 'image', image: { height: 1, image_name: 'a.png', width: 1 }, user_id: 'owner' },
    ],
    [
      'an image placement whose image has no size',
      { action: 'image', append: false, image: { image_name: 'a.png' }, user_id: 'owner' },
    ],

    ['a conditioning video without its role', placementEvent('conditioning_video')],
    [
      'a conditioning video with an unknown role',
      { ...placementEvent('conditioning_video'), conditioning_role: 'both' },
    ],
  ])('ignores %s', async (_label, payload) => {
    const { videoShown, runtime, socket, store, videoValues } = setup();
    const before = videoValues();
    const notices = store.queries.getSnapshot().notifications.length;

    socket.emit(payload);
    await flush();

    expect(videoValues()).toBe(before);
    expect(store.queries.getSnapshot().notifications).toHaveLength(notices);
    expect(videoShown()).toBe(false);

    runtime.dispose();
  });

  describe('partial media', () => {
    const referenceNames = (values: Record<string, unknown>) =>
      (
        values.references as {
          clip?: { video_name: string };
          fromSourceVideo?: boolean;
          image?: { image_name: string };
        }[]
      ).map((entry) =>
        entry.image ? entry.image.image_name : `${entry.clip?.video_name}${entry.fromSourceVideo ? '*' : ''}`
      );

    it('keeps the Ref2VA continuity reference when the references are replaced, and moves it with the clip', async () => {
      const { runtime, socket, videoValues } = setup(REF2VA);

      socket.emit(placementEvent('initial_video'));
      await flush();
      expect(referenceNames(videoValues())).toEqual(['clip.mp4*']);

      socket.emit(parametersEvent({ minimax_h3_references: [{ image_name: 'B.png', kind: 'image' }] }));
      await flush();
      expect(referenceNames(videoValues())).toEqual(['B.png', 'clip.mp4*']);
      expect(videoValues()).toMatchObject({ sourceVideo: { video_name: 'clip.mp4' } });

      socket.emit(parametersEvent({ source_video: { video_name: 'other.mp4' } }));
      await flush();
      expect(referenceNames(videoValues())).toEqual(['B.png', 'other.mp4*']);
      expect(videoValues()).toMatchObject({ sourceVideo: { video_name: 'other.mp4' } });

      runtime.dispose();
    });

    it('removes the initial video when a sent reference list leaves no slot for its continuity reference', async () => {
      const { runtime, socket, videoValues } = setup(REF2VA);

      socket.emit(placementEvent('initial_video'));
      await flush();
      socket.emit(
        parametersEvent({
          minimax_h3_references: ['a.mp4', 'b.mp4', 'c.mp4'].map((video_name) => ({ kind: 'video', video_name })),
        })
      );
      await flush();

      expect(referenceNames(videoValues())).toEqual(['a.mp4', 'b.mp4', 'c.mp4']);
      expect(videoValues().sourceVideo).toBeNull();

      runtime.dispose();
    });

    it('clears the references for an explicitly empty list sent beside an initial video', async () => {
      const { runtime, socket, videoValues } = setup(REF2VA);

      for (const name of ['a.mp4', 'b.mp4', 'c.mp4']) {
        socket.emit(placementEvent('reference_video', { video_name: name }));
        await flush();
      }
      socket.emit(parametersEvent({ minimax_h3_references: [], source_video: { video_name: 'clip.mp4' } }));
      await flush();

      expect(referenceNames(videoValues())).toEqual(['clip.mp4*']);
      expect(videoValues()).toMatchObject({ sourceVideo: { video_name: 'clip.mp4' } });

      runtime.dispose();
    });

    it('ignores a first frame a Ref2VA panel cannot use, keeping its initial video', async () => {
      const { runtime, socket, videoValues } = setup(REF2VA);

      socket.emit(placementEvent('initial_video'));
      await flush();
      socket.emit(parametersEvent({ first_frame_image: { image_name: 'first.png' } }));
      await flush();

      expect(videoValues()).toMatchObject({ firstFrameImage: null, sourceVideo: { video_name: 'clip.mp4' } });
      expect(referenceNames(videoValues())).toEqual(['clip.mp4*']);

      runtime.dispose();
    });

    it('does not let media the panel model cannot use displace what it holds', async () => {
      const { lastNotice, runtime, socket, videoValues } = setup();

      socket.emit(parametersEvent({ minimax_h3_references: [{ image_name: 'B.png', kind: 'image' }] }));
      await flush();

      expect(videoValues()).toMatchObject({ firstFrameImage: heldFrame, references: [] });
      expect(lastNotice()).toEqual(
        expect.objectContaining({ kind: 'info', title: 'widgets.video.externalRecall.nothingApplied' })
      );

      runtime.dispose();
    });
  });

  it('brings a Video widget in a background tab to the front', async () => {
    const { projectId, runtime, socket, store, videoShown } = setup();
    store.commands.widgets.open({ projectId, region: 'left', widgetId: 'video' });
    store.commands.widgets.open({ projectId, region: 'left', widgetId: 'generate' });
    expect(videoShown()).toBe(false);

    socket.emit(parametersEvent({ positive_prompt: 'a heron' }));
    await flush();

    expect(videoShown()).toBe(true);

    runtime.dispose();
  });

  describe('placements', () => {
    it('appends a reference video with the defaults its wire facts imply', async () => {
      const { lastNotice, runtime, socket, videoValues } = setup(REF2VA);

      socket.emit(placementEvent('reference_video', { duration: 10, fps: 24, media_origin: 'audio_upload' }));
      await flush();

      expect(videoValues().references).toEqual([
        expect.objectContaining({
          clip: expect.objectContaining({ endFrame: 239, fps: 24, numFrames: 240, video_name: 'clip.mp4' }),
          conditioning: 'audio',
          kind: 'video',
        }),
      ]);
      expect(lastNotice()).toEqual(
        expect.objectContaining({ kind: 'success', title: 'widgets.video.placement.referenceAdded' })
      );

      runtime.dispose();
    });

    const imageEvent = (append: boolean, image_name = 'still.png') => ({
      action: 'image',
      append,
      image: { height: 512, image_name, width: 512 },
      queue_id: 'default',
      user_id: 'owner',
    });

    it('sends an image as a Ref2VA reference image and appends the next one', async () => {
      const { lastNotice, runtime, socket, videoShown, videoValues } = setup(REF2VA);

      socket.emit(imageEvent(false, 'a.png'));
      await flush();
      socket.emit(imageEvent(true, 'b.png'));
      await flush();

      expect(videoValues().references).toEqual([
        { detail: 'match', image: { height: 512, image_name: 'a.png', width: 512 }, kind: 'image' },
        { detail: 'match', image: { height: 512, image_name: 'b.png', width: 512 }, kind: 'image' },
      ]);
      expect(lastNotice()).toMatchObject({ kind: 'success', title: 'widgets.video.placement.referenceImageAdded' });
      expect(videoShown()).toBe(true);

      runtime.dispose();
    });

    it('sends an image as the first frame of a frame model and appends the last frame', async () => {
      const { lastNotice, runtime, socket, videoValues } = setup();

      socket.emit(imageEvent(false, 'first.png'));
      await flush();
      expect(videoValues()).toMatchObject({ firstFrameImage: { image_name: 'first.png' }, lastFrameImage: null });
      expect(lastNotice()).toMatchObject({ title: 'widgets.video.placement.firstFrameSet' });

      socket.emit(imageEvent(true, 'last.png'));
      await flush();
      expect(videoValues()).toMatchObject({
        firstFrameImage: { image_name: 'first.png' },
        lastFrameImage: { image_name: 'last.png' },
      });
      expect(lastNotice()).toMatchObject({ title: 'widgets.video.placement.lastFrameSet' });

      runtime.dispose();
    });

    it('says so when an image clears a picture clip, and keeps a soundtrack quietly', async () => {
      const { lastNotice, runtime, socket, videoValues } = setup(LTX2);
      socket.emit({ ...placementEvent('conditioning_video'), conditioning_role: 'video' });
      await flush();
      expect(videoValues()).toMatchObject({ conditioningClip: { role: 'video' } });

      socket.emit(imageEvent(true));
      await flush();

      expect(videoValues()).toMatchObject({ conditioningClip: null, firstFrameImage: { image_name: 'still.png' } });
      expect(lastNotice()).toMatchObject({
        kind: 'info',
        message: 'widgets.video.placement.imageDisplaced',
        title: 'widgets.video.placement.firstFrameSet',
      });

      // A soundtrack is anchored by the frames, so the next image lands beside it.
      socket.emit({ ...placementEvent('conditioning_video'), conditioning_role: 'audio' });
      await flush();
      socket.emit(imageEvent(true, 'last.png'));
      await flush();

      expect(videoValues()).toMatchObject({
        conditioningClip: { role: 'audio' },
        firstFrameImage: { image_name: 'still.png' },
        lastFrameImage: { image_name: 'last.png' },
      });
      expect(lastNotice()).toMatchObject({ kind: 'success', title: 'widgets.video.placement.lastFrameSet' });

      runtime.dispose();
    });

    it.each([
      [WAN_T2V, false, 'widgets.video.placement.imageUnsupported'],
      [WAN_I2V, true, 'widgets.video.placement.imageFull'],
    ])('declines an image with nowhere to go, leaving the panel alone', async (panelModel, append, message) => {
      const { lastNotice, projectId, runtime, socket, store, videoShown, videoValues } = setup(panelModel);
      store.commands.widgets.patchValues('video', { lastFrameImage: heldFrame }, projectId);
      const before = videoValues();

      socket.emit(imageEvent(append));
      await flush();

      expect(videoValues()).toBe(before);
      expect(lastNotice()).toMatchObject({ kind: 'info', message, title: 'widgets.video.placement.imageNotPlaced' });
      expect(videoShown()).toBe(false);

      runtime.dispose();
    });

    it('declines an initial video a Ref2VA panel has no reference slot to anchor', async () => {
      const { lastNotice, runtime, socket, videoValues } = setup(REF2VA);

      for (const name of ['a.mp4', 'b.mp4', 'c.mp4']) {
        socket.emit(placementEvent('reference_video', { video_name: name }));
        await flush();
      }
      expect(videoValues().references).toHaveLength(3);
      const before = videoValues();

      socket.emit(placementEvent('initial_video'));
      await flush();

      expect(videoValues()).toBe(before);
      expect(lastNotice()).toEqual(
        expect.objectContaining({ kind: 'info', message: 'widgets.video.placement.initialVideoFull' })
      );

      runtime.dispose();
    });

    it('sets an initial video the panel model cannot extend, and says so', async () => {
      const { lastNotice, runtime, socket, videoValues } = setup(WAN_T2V);

      socket.emit(placementEvent('initial_video'));
      await flush();

      expect(videoValues()).toMatchObject({ sourceVideo: { video_name: 'clip.mp4' } });
      expect(lastNotice()).toEqual(
        expect.objectContaining({ kind: 'info', message: 'widgets.video.placement.initialVideoUnused' })
      );

      runtime.dispose();
    });

    it('sets a conditioning clip in the sent role, clearing the frames it displaces, and says so', async () => {
      const { lastNotice, projectId, runtime, socket, store, videoShown, videoValues } = setup(LTX2);
      store.commands.widgets.patchValues('video', { firstFrameImage: heldFrame }, projectId);

      socket.emit({ ...placementEvent('conditioning_video'), conditioning_role: 'video' });
      await flush();

      expect(videoValues()).toMatchObject({
        conditioningClip: { clip: { video_name: 'clip.mp4' }, fpsKnown: true, role: 'video' },
        firstFrameImage: null,
      });
      expect(lastNotice()).toEqual(
        expect.objectContaining({
          kind: 'info',
          message: 'widgets.video.placement.conditioningClipDisplaced',
          title: 'widgets.video.placement.conditioningClipSet',
        })
      );
      expect(videoShown()).toBe(true);

      runtime.dispose();
    });

    it('keeps the first frame beside a soundtrack, which it anchors the picture of', async () => {
      const { lastNotice, projectId, runtime, socket, store, videoValues } = setup(LTX2);
      store.commands.widgets.patchValues('video', { firstFrameImage: heldFrame }, projectId);

      socket.emit({ ...placementEvent('conditioning_video'), conditioning_role: 'audio' });
      await flush();

      expect(videoValues()).toMatchObject({ conditioningClip: { role: 'audio' }, firstFrameImage: heldFrame });
      expect(lastNotice()).toMatchObject({ kind: 'success', title: 'widgets.video.placement.conditioningClipSet' });

      runtime.dispose();
    });

    it('sets a conditioning clip on an otherwise empty panel with a plain success notice', async () => {
      const { lastNotice, runtime, socket, videoValues } = setup(LTX2);

      socket.emit({ ...placementEvent('conditioning_video'), conditioning_role: 'video' });
      await flush();

      expect(videoValues()).toMatchObject({ conditioningClip: { role: 'video' } });
      expect(lastNotice()).toMatchObject({ kind: 'success', title: 'widgets.video.placement.conditioningClipSet' });
      expect(lastNotice()?.message).toBeUndefined();

      runtime.dispose();
    });

    it.each([
      {
        event: {
          ...placementEvent('conditioning_video', { media_origin: 'audio_upload' }),
          conditioning_role: 'video',
        },
        label: 'the picture of a wrapped audio upload',
        model: LTX2,
        notice: { kind: 'info', message: 'widgets.video.placement.conditioningClipNoPicture' },
      },
      {
        event: { ...placementEvent('conditioning_video'), conditioning_role: 'video' },
        label: 'a conditioning clip the panel model cannot take',
        model: WAN_I2V,
        notice: {
          kind: 'info',
          message: 'widgets.video.placement.conditioningClipUnsupported',
          title: 'widgets.video.placement.conditioningClipNotSet',
        },
      },
    ])('declines $label, leaving the panel alone', async ({ event, model, notice }) => {
      const { lastNotice, runtime, socket, videoShown, videoValues } = setup(model);
      const before = videoValues();

      socket.emit(event);
      await flush();

      expect(videoValues()).toBe(before);
      expect(lastNotice()).toMatchObject(notice);
      expect(videoShown()).toBe(false);

      runtime.dispose();
    });

    it('places into the project it arrived for', async () => {
      const { projectId, runtime, socket, store, videoValues } = setup();
      let releaseModels: () => void = () => {};
      modelsApi.ensureModelsLoaded.mockImplementation(
        () =>
          new Promise<void>((resolve) => {
            releaseModels = resolve;
          })
      );

      socket.emit(placementEvent('initial_video'));
      const second = store.commands.projects.create();
      await flush();
      releaseModels();
      await flush();

      expect(videoValues(projectId)).toMatchObject({ sourceVideo: { video_name: 'clip.mp4' } });
      expect(videoValues(second.id).sourceVideo).toBeUndefined();

      runtime.dispose();
    });
  });
});