Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.1 KB
Raw Blame
// @vitest-environment node
import { describe, expect, it, vi } from 'vitest';

import {
  runDiscard,
  RunDiscardDeps,
} from '@/app/builder/flow-canvas/widgets/discard-draft';

const makePublishedFlow = () =>
  ({
    id: 'flow-1',
    publishedVersionId: 'v3',
    version: { id: 'v3', state: 'LOCKED', displayName: 'Test' },
  } as unknown as Awaited<ReturnType<RunDiscardDeps['fetchFlow']>>);

const makeDeps = (overrides: Partial<RunDiscardDeps> = {}): RunDiscardDeps => ({
  flow: { id: 'flow-1', publishedVersionId: 'v3' },
  savePendingEdits: vi.fn(async () => undefined),
  overWriteDraftWithVersion: vi.fn(async () => undefined),
  fetchFlow: vi.fn(async () => makePublishedFlow()),
  setFlow: vi.fn(),
  setVersion: vi.fn(),
  ...overrides,
});

describe('runDiscard', () => {
  it('is a no-op when the flow has no published version', async () => {
    const deps = makeDeps({
      flow: { id: 'flow-1', publishedVersionId: null },
    });

    await runDiscard(deps);

    expect(deps.overWriteDraftWithVersion).not.toHaveBeenCalled();
    expect(deps.fetchFlow).not.toHaveBeenCalled();
    expect(deps.setFlow).not.toHaveBeenCalled();
    expect(deps.setVersion).not.toHaveBeenCalled();
  });

  it('saves the pending edits before it overwrites the draft', async () => {
    const order: string[] = [];
    const deps = makeDeps({
      savePendingEdits: vi.fn(async () => {
        order.push('save');
      }),
      overWriteDraftWithVersion: vi.fn(async () => {
        order.push('overwrite');
      }),
    });

    await runDiscard(deps);

    expect(order).toEqual(['save', 'overwrite']);
  });

  it('stops quietly when a pending edit fails to save', async () => {
    const deps = makeDeps({
      savePendingEdits: vi.fn(async () => {
        throw new Error('Request failed with status code 400');
      }),
    });

    await expect(runDiscard(deps)).resolves.toBeUndefined();

    expect(deps.overWriteDraftWithVersion).not.toHaveBeenCalled();
    expect(deps.setFlow).not.toHaveBeenCalled();
  });

  it('overwrites the draft with the published version content first', async () => {
    const deps = makeDeps();

    await runDiscard(deps);

    expect(deps.overWriteDraftWithVersion).toHaveBeenCalledWith({
      flowId: 'flow-1',
      versionId: 'v3',
    });
  });

  it('navigates the user to the published locked version after discard', async () => {
    const published = makePublishedFlow();
    const deps = makeDeps({
      fetchFlow: vi.fn(async () => published),
    });

    await runDiscard(deps);

    expect(deps.fetchFlow).toHaveBeenCalledWith('flow-1', { versionId: 'v3' });
    expect(deps.setFlow).toHaveBeenCalledWith(published);
    expect(deps.setVersion).toHaveBeenCalledWith(published.version);
  });

  it('overwrites the draft before navigating to the published version', async () => {
    const order: string[] = [];
    const deps = makeDeps({
      overWriteDraftWithVersion: vi.fn(async () => {
        order.push('overwrite');
      }),
      fetchFlow: vi.fn(async () => {
        order.push('fetch');
        return makePublishedFlow();
      }),
    });

    await runDiscard(deps);

    expect(order).toEqual(['overwrite', 'fetch']);
  });
});