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
import { Property } from '@activepieces/pieces-framework';
import { describe, expect, it } from 'vitest';

import { formUtils } from '@/features/pieces/utils/form-utils';

const multiSelect = Property.StaticMultiSelectDropdown({
  displayName: 'Unit to Extract',
  required: true,
  defaultValue: ['year'],
  options: { options: [{ label: 'Year', value: 'year' }] },
});
const checkbox = Property.Checkbox({
  displayName: 'Checkbox',
  required: false,
});
const shortText = Property.ShortText({ displayName: 'Text', required: false });
const color = Property.Color({ displayName: 'Brand', required: false });
const object = Property.Object({ displayName: 'Model', required: true });
const staticDropdown = Property.StaticDropdown({
  displayName: 'Channel',
  required: true,
  options: { options: [{ label: 'General', value: 'C0123' }] },
});

describe('formUtils.parseDynamicValue', () => {
  it.each([
    ['["year","day"]', multiSelect, ['year', 'day']],
    ['[]', multiSelect, []],
    ['true', checkbox, true],
    ['false', checkbox, false],
    [
      '{"provider":"openai","model":"gpt-5","configId":"key-1"}',
      object,
      { provider: 'openai', model: 'gpt-5', configId: 'key-1' },
    ],
    ['{"modelTierId":"tier-1"}', object, { modelTierId: 'tier-1' }],
  ])('restores %s', (value, property, expected) => {
    expect(formUtils.parseDynamicValue({ property, value })).toEqual(expected);
  });

  it.each([
    ["{{ variables['channel'] }}", staticDropdown],
    ['{{ trigger.body.channel || "C0123" }}', staticDropdown],
    ['{{ trigger.body.units }}', multiSelect],
    ['{{ trigger.body.enabled }}', checkbox],
    ['{{ trigger.body.brandColor }}', color],
    ['#8142E3', color],
  ])('preserves the value %s across the toggle', (value, property) => {
    expect(formUtils.parseDynamicValue({ property, value })).toEqual(value);
  });

  it('restores a stringified array whose item holds an expression', () => {
    expect(
      formUtils.parseDynamicValue({
        property: multiSelect,
        value: '["{{ trigger.body.unit }}","year"]',
      }),
    ).toEqual(['{{ trigger.body.unit }}', 'year']);
  });

  it.each([
    ['C0123', staticDropdown, 'C0123'],
    ['5', staticDropdown, '5'],
    [5, staticDropdown, 5],
    ['true', staticDropdown, 'true'],
    ['null', staticDropdown, 'null'],
    ['price {{high', staticDropdown, 'price {{high'],
    ['{"tier":"pro"}', staticDropdown, { tier: 'pro' }],
    ['["a","b"]', staticDropdown, ['a', 'b']],
  ])('keeps the dropdown literal %s as %s', (value, property, expected) => {
    expect(formUtils.parseDynamicValue({ property, value })).toEqual(expected);
  });

  it.each([
    ['year', multiSelect],
    ['{"not":"an array"}', multiSelect],
    ['price {{high', multiSelect],
    ['1', checkbox],
    ['["year","day"]', shortText],
    ['', staticDropdown],
    [undefined, staticDropdown],
    ['', color],
    [undefined, color],
    ['{{ trigger.body.model }}', object],
    ['["a"]', object],
    ['', object],
  ])('has nothing unambiguous to restore in %s', (value, property) => {
    expect(formUtils.parseDynamicValue({ property, value })).toBeUndefined();
  });
});