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

8.4 KB
Raw Blame
import { AIProviderName } from '@activepieces/core-utils';
import { describe, expect, it } from 'vitest';

import { agentEditState } from '@/app/routes/agents/lib/agent-edit-state';

const config = (over: Record<string, unknown> = {}) => ({
  displayName: 'Inbox agent',
  description: '',
  icon: 'BOT',
  color: 'PURPLE',
  draft: { instructions: 'Sort it.', modelName: 'gpt-5', provider: 'OPENAI' },
  ...over,
});

describe('sameConfig', () => {
  it('treats an identical shape as unchanged', () => {
    expect(agentEditState.sameConfig({ left: config(), right: config() })).toBe(
      true,
    );
  });

  it.each([
    ['a changed instruction', { draft: { instructions: 'Other.' } }],
    ['a changed name', { displayName: 'Renamed' }],
    [
      'a changed model',
      { draft: { instructions: 'Sort it.', modelName: 'x' } },
    ],
  ])('sees %s as changed', (_label, over) => {
    expect(
      agentEditState.sameConfig({ left: config(), right: config(over) }),
    ).toBe(false);
  });

  it('does not confuse null with undefined, so a cleared field counts as an edit', () => {
    expect(
      agentEditState.sameConfig({
        left: { modelName: null },
        right: { modelName: undefined },
      }),
    ).toBe(false);
  });

  it('sees an added empty description as a change, since the server stores it', () => {
    expect(
      agentEditState.sameConfig({ left: {}, right: { description: '' } }),
    ).toBe(false);
  });

  it('is order-sensitive on arrays, because tool order is meaningful', () => {
    expect(
      agentEditState.sameConfig({
        left: { tools: ['a', 'b'] },
        right: { tools: ['b', 'a'] },
      }),
    ).toBe(false);
  });
});

describe('createWriteLock', () => {
  it('lets the first claim through', () => {
    expect(agentEditState.createWriteLock().claim()).toBe(true);
  });

  it('refuses a second claim while the first is held, which is the race guard', () => {
    const lock = agentEditState.createWriteLock();
    expect(lock.claim()).toBe(true);
    expect(lock.claim()).toBe(false);
    expect(lock.claim()).toBe(false);
  });

  it('lets the next writer in after a release', () => {
    const lock = agentEditState.createWriteLock();
    lock.claim();
    lock.release();
    expect(lock.claim()).toBe(true);
  });

  it('survives a release that was never claimed, so a validation failure cannot lock writes out', () => {
    const lock = agentEditState.createWriteLock();
    lock.release();
    lock.release();
    expect(lock.claim()).toBe(true);
  });

  it('reports whether it is held, so the caller can reason about the window', () => {
    const lock = agentEditState.createWriteLock();
    expect(lock.held()).toBe(false);
    lock.claim();
    expect(lock.held()).toBe(true);
    lock.release();
    expect(lock.held()).toBe(false);
  });

  it('gives each screen its own lock, so one agent cannot block another', () => {
    const first = agentEditState.createWriteLock();
    const second = agentEditState.createWriteLock();
    first.claim();
    expect(second.claim()).toBe(true);
  });

  it('admits exactly one of many simultaneous writers', () => {
    const lock = agentEditState.createWriteLock();
    const admitted = Array.from({ length: 25 }, () => lock.claim()).filter(
      Boolean,
    );
    expect(admitted).toHaveLength(1);
  });
});

describe('leaveGuard', () => {
  it('stays closed when nothing is trying to leave', () => {
    expect(
      agentEditState.leaveGuard({
        blockerState: 'unblocked',
        exitRequested: false,
      }),
    ).toStrictEqual({ open: false, discardAction: 'none' });
  });

  it('opens for a blocked router navigation and lets the router proceed', () => {
    expect(
      agentEditState.leaveGuard({
        blockerState: 'blocked',
        exitRequested: false,
      }),
    ).toStrictEqual({ open: true, discardAction: 'proceed' });
  });

  it('opens for the back arrow and exits in-app, since no navigation is pending', () => {
    expect(
      agentEditState.leaveGuard({
        blockerState: 'unblocked',
        exitRequested: true,
      }),
    ).toStrictEqual({ open: true, discardAction: 'exit' });
  });

  it('prefers the router when both are pending, so the queued navigation is not dropped', () => {
    expect(
      agentEditState.leaveGuard({
        blockerState: 'blocked',
        exitRequested: true,
      }),
    ).toStrictEqual({ open: true, discardAction: 'proceed' });
  });

  it.each(['proceeding', 'unblocked'])(
    'stays closed while the blocker is %s and no in-app exit was asked for',
    (blockerState) => {
      expect(
        agentEditState.leaveGuard({ blockerState, exitRequested: false }).open,
      ).toBe(false);
    },
  );
});

describe('agentEditState.modelPickChanged', () => {
  const pick = {
    provider: 'openrouter',
    modelName: 'anthropic/claude-sonnet-4.6',
    providerConfigId: null,
  };

  it('is false when the selector reports the model the form already holds', () => {
    expect(
      agentEditState.modelPickChanged({ picked: pick, current: pick }),
    ).toBe(false);
  });

  it('is true when the model differs', () => {
    expect(
      agentEditState.modelPickChanged({
        picked: { ...pick, modelName: 'openai/gpt-5' },
        current: pick,
      }),
    ).toBe(true);
  });

  it('treats a missing field and an explicit null as the same pick', () => {
    expect(
      agentEditState.modelPickChanged({
        picked: { provider: 'openrouter', modelName: 'x' },
        current: {
          provider: 'openrouter',
          modelName: 'x',
          providerConfigId: null,
        },
      }),
    ).toBe(false);
  });

  it('is true when only the provider config differs', () => {
    expect(
      agentEditState.modelPickChanged({
        picked: { ...pick, providerConfigId: 'cfg_1' },
        current: pick,
      }),
    ).toBe(true);
  });
});

describe('serverMovedWhileTyping', () => {
  const config = (instructions: string) => ({ draft: { instructions } });

  it('is true when the agent changed under a form someone is typing in', () => {
    expect(
      agentEditState.serverMovedWhileTyping({
        fromServer: config('chat changed this'),
        lastSeen: config('what was there'),
        unsavedTyping: true,
      }),
    ).toBe(true);
  });

  it('is false when nothing moved, so a refetch of the same values is not a conflict', () => {
    expect(
      agentEditState.serverMovedWhileTyping({
        fromServer: config('same'),
        lastSeen: config('same'),
        unsavedTyping: true,
      }),
    ).toBe(false);
  });

  it('is false when nobody is typing, because the form can simply take the new values', () => {
    expect(
      agentEditState.serverMovedWhileTyping({
        fromServer: config('chat changed this'),
        lastSeen: config('what was there'),
        unsavedTyping: false,
      }),
    ).toBe(false);
  });
});

describe('agentEditState model choice', () => {
  it('maps a tier pick to only the tier id and back', () => {
    const pick = agentEditState.modelPickOf({
      choice: { type: 'tier', tierId: 'tier-1' },
    });
    expect(pick).toEqual({
      provider: null,
      modelName: null,
      providerConfigId: null,
      modelTierId: 'tier-1',
    });
    expect(agentEditState.modelChoiceOf({ config: pick })).toEqual({
      type: 'tier',
      tierId: 'tier-1',
    });
    expect(agentEditState.hasModel({ config: pick })).toBe(true);
  });

  it('maps a model pick to provider, key and model, and clears the tier', () => {
    const pick = agentEditState.modelPickOf({
      choice: {
        type: 'model',
        provider: AIProviderName.OPENAI,
        providerConfigId: 'key-1',
        modelId: 'gpt-5',
      },
    });
    expect(pick).toEqual({
      provider: AIProviderName.OPENAI,
      modelName: 'gpt-5',
      providerConfigId: 'key-1',
      modelTierId: null,
    });
    expect(
      agentEditState.modelPickChanged({
        picked: pick,
        current: { ...pick, modelTierId: 'tier-1' },
      }),
    ).toBe(true);
  });

  it('reads a legacy row without a key id and treats no model as missing', () => {
    expect(
      agentEditState.modelChoiceOf({
        config: { provider: AIProviderName.OPENAI, modelName: 'gpt-5' },
      }),
    ).toEqual({
      type: 'model',
      provider: AIProviderName.OPENAI,
      providerConfigId: '',
      modelId: 'gpt-5',
    });
    expect(agentEditState.modelChoiceOf({ config: {} })).toBeNull();
    expect(
      agentEditState.hasModel({ config: { provider: AIProviderName.OPENAI } }),
    ).toBe(false);
  });
});