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.2 KB
Raw Blame
import type { QueueCounts } from '@features/queue/core/types';
import type { TFunction } from 'i18next';

import { describe, expect, it } from 'vitest';

import type { QueueActionScope } from './queueScope';

import { getQueueActionCounts, getQueueActionDescriptors, type QueueActionCounts } from './queueMenuActions';

// Echoes keys with their values, so the copy each action would show can be read back.
const t = ((key: string, values?: Record<string, unknown>) =>
  values ? `${key} ${JSON.stringify(values)}` : key) as unknown as TFunction;

const project: QueueActionScope = { kind: 'project', project: 'Portraits' };
const counts: QueueActionCounts = {
  hasHistory: true,
  history: { canceled: 1, completed: 4, failed: 2, total: 11 },
  inProgress: 1,
  pending: 3,
};

const describeActions = (overrides: Partial<Parameters<typeof getQueueActionDescriptors>[0]> = {}) =>
  getQueueActionDescriptors({
    canManageProcessor: true,
    counts,
    cancelCurrentTarget: 'in_progress',
    hasRunningItem: true,
    includeOpenQueue: false,
    isConnected: true,
    isPaused: false,
    localUnsubmitted: 0,
    scope: project,
    t,
    ...overrides,
  });

const find = (actions: ReturnType<typeof describeActions>, id: string) => actions.find((action) => action.id === id)!;

describe('queue action model', () => {
  it('stops the running item without asking, and asks before anything that reaches many items', () => {
    const confirmed = describeActions()
      .filter((action) => action.confirmation !== null)
      .map((action) => action.id);

    expect(find(describeActions(), 'cancel-current').confirmation).toBeNull();
    expect(confirmed).toEqual([
      'cancel-all',
      'cancel-all-except-current',
      'clear-failed',
      'clear-finished',
      'clear-queue',
    ]);
  });

  it('names the scope in a whole sentence and says how many items each confirmation reaches', () => {
    const actions = describeActions();

    expect(find(actions, 'cancel-all').confirmation?.body).toBe(
      'widgets.queue.cancelAllConfirmationBody.project {"count":4,"project":"Portraits"}'
    );
    expect(find(actions, 'cancel-all-except-current').confirmation?.body).toContain('"count":3');
    expect(find(actions, 'clear-finished').confirmation?.body).toContain('"count":7');
    expect(find(actions, 'clear-failed').confirmation?.body).toContain('"count":2');
  });

  it('disables what needs the server while it is disconnected, but still opens the queue', () => {
    const offline = describeActions({ includeOpenQueue: true, isConnected: false });

    expect(offline.filter((action) => !action.disabled).map((action) => action.id)).toEqual(['open-queue']);
  });

  it("keeps cancel-all available offline for this tab's runs the backend has not accepted, counting only those", () => {
    const cancelAll = find(describeActions({ isConnected: false, localUnsubmitted: 2 }), 'cancel-all');

    expect(cancelAll.disabled).toBe(false);
    expect(cancelAll.confirmation?.body).toContain('"count":2');
  });

  it('disables bulk actions with nothing to reach', () => {
    const empty = describeActions({
      counts: {
        hasHistory: false,
        history: { canceled: 0, completed: 0, failed: 0, total: 0 },
        inProgress: 0,
        pending: 0,
      },
      cancelCurrentTarget: null,
      hasRunningItem: false,
    });

    expect(empty.filter((action) => !action.disabled).map((action) => action.id)).toEqual(['toggle-processor']);
  });
});

describe('queue action counts', () => {
  const server: QueueCounts = {
    canceled: 5,
    completed: 20,
    failed: 5,
    inProgress: 3,
    pending: 7,
    queueId: 'default',
    total: 40,
    userInProgress: 1,
    userPending: 2,
    waiting: 0,
  };

  it("counts a member's own unfinished work, and no history across all projects, where the server counts everyone's", () => {
    expect(getQueueActionCounts(server, { kind: 'mine' }, 'member')).toEqual({
      hasHistory: true,
      history: null,
      inProgress: 1,
      pending: 2,
    });
  });

  it("counts everyone's work for an administrator, whose cancel-all stops it all", () => {
    expect(getQueueActionCounts(server, { kind: 'allUsers' }, 'admin')).toMatchObject({
      history: { total: 40 },
      inProgress: 3,
      pending: 7,
    });
  });
});