Skip to content

The open alternative to Salesforce, designed for AI.

52.1 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import {
  getFieldUniversalIdentifier,
  type ApplicationManifest,
  type FieldManifest,
  type Manifest,
  type PageLayoutTabManifest,
  type PageLayoutWidgetManifest,
  type ValidationRuleManifest,
} from 'twenty-shared/application';
import {
  AggregateOperations,
  FieldMetadataType,
  PageLayoutTabLayoutMode,
  PageLayoutType,
  WidgetType,
  PageLayoutWidgetVerticalListHeightBehavior,
  RelationType,
} from 'twenty-shared/types';
import { addMissingFieldOptionIds } from '@/app/manifest/utils/add-missing-field-option-ids';
import { manifestValidate } from '@/app/manifest/manifest-validate';

const validApplication: ApplicationManifest = {
  universalIdentifier: '8e8ee827-5a0b-46b0-bb63-c13ccd734ac6',
  displayName: 'Test App',
  description: 'Test app for Twenty',
  defaultRoleUniversalIdentifier: '68bb56f3-8300-4cb5-8cc3-8da9ee66f1b2',
  packageJsonChecksum: '98592af7-4be9-4655-b5c4-9bef307a996c',
  yarnLockChecksum: '580ee05f-15fe-4146-bac2-6c382483c94e',
};

const validField: FieldManifest = {
  objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',

  universalIdentifier: '550e8400-e29b-41d4-a716-446655440001',
  type: FieldMetadataType.NUMBER,
  name: 'healthScore',
  label: 'Health Score',
};

const validManifest: Manifest = {
  commandMenuItems: [],
  timelineActivityTypes: [],
  settingsMenuItems: [],
  application: validApplication,
  objects: [],
  frontComponents: [],
  fields: [],
  logicFunctions: [],
  permissionFlags: [],
  roles: [],
  skills: [],
  agents: [],
  publicAssets: [],
  views: [],
  viewFields: [],
  navigationMenuItems: [],
  pageLayouts: [],
  pageLayoutTabs: [],
  pageLayoutWidgets: [],
};

describe('manifestValidate', () => {
  it('rejects options with colliding generated IDs before deployment', () => {
    const field = addMissingFieldOptionIds({
      universalIdentifier: validField.universalIdentifier,
      objectUniversalIdentifier: validField.objectUniversalIdentifier,
      name: 'status',
      label: 'Status',
      type: FieldMetadataType.SELECT,
      options: [
        { label: 'Same', value: 'FIRST', color: 'blue', position: 1 },
        { label: 'Same', value: 'SECOND', color: 'blue', position: 2 },
      ],
    });
    const result = manifestValidate({ ...validManifest, fields: [field] });

    expect(result.isValid).toBe(false);
    expect(result.errors.join(' ')).toContain('distinct explicit IDs');
  });

  it.each([
    FieldMetadataType.SELECT,
    FieldMetadataType.MULTI_SELECT,
    FieldMetadataType.RATING,
  ] as const)('rejects colliding and empty IDs in %s options', (type) => {
    const options = [
      {
        id: 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
        label: 'First',
        value: 'FIRST',
        color: 'blue' as const,
        position: 1,
      },
      {
        id: 'AAAAAAAA-AAAA-4AAA-8AAA-AAAAAAAAAAAA',
        label: 'Second',
        value: 'SECOND',
        color: 'blue' as const,
        position: 2,
      },
    ];
    const field = {
      universalIdentifier: validField.universalIdentifier,
      objectUniversalIdentifier: validField.objectUniversalIdentifier,
      name: 'status',
      label: 'Status',
      type,
      options,
    } as FieldManifest<
      | FieldMetadataType.SELECT
      | FieldMetadataType.MULTI_SELECT
      | FieldMetadataType.RATING
    >;
    expect(
      manifestValidate({ ...validManifest, fields: [field] }).errors.join(' '),
    ).toContain('duplicate option IDs');
    const emptyIds = manifestValidate({
      ...validManifest,
      fields: [
        { ...field, options: options.map((option) => ({ ...option, id: '' })) },
      ],
    });
    expect(emptyIds.errors.join(' ')).toContain('empty option IDs');
    expect(emptyIds.errors.join(' ')).toContain('duplicate option IDs');
    expect(
      manifestValidate({
        ...validManifest,
        fields: [{ ...field, options: [options[0]] }],
      }).isValid,
    ).toBe(true);
  });

  it('reports unresolved label references without rejecting server-owned fields', () => {
    const object = {
      universalIdentifier: validField.objectUniversalIdentifier,
      nameSingular: 'company',
      namePlural: 'companies',
      labelSingular: 'Company',
      labelPlural: 'Companies',
      fields: [],
      labelIdentifierFieldMetadataUniversalIdentifier:
        validField.universalIdentifier,
    };
    const manifest = { ...validManifest, objects: [object] };
    expect(manifestValidate(manifest).warnings.join(' ')).toContain(
      'Check the identifier',
    );
    expect(
      manifestValidate({ ...manifest, fields: [validField] }).warnings,
    ).toEqual([]);
    for (const name of ['id', 'searchVector']) {
      expect(
        manifestValidate({
          ...manifest,
          objects: [
            {
              ...object,
              labelIdentifierFieldMetadataUniversalIdentifier:
                getFieldUniversalIdentifier({
                  applicationUniversalIdentifier:
                    validApplication.universalIdentifier,
                  objectUniversalIdentifier: object.universalIdentifier,
                  name,
                }),
            },
          ],
        }).warnings,
      ).toEqual([]);
    }
  });

  describe('valid object extensions', () => {
    it('should pass validation with valid object extension by nameSingular', () => {
      const result = manifestValidate({
        ...validManifest,
        fields: [validField],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should pass validation with valid object extension by universalIdentifier', () => {
      const extensionByUuid: FieldManifest = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: '550e8400-e29b-41d4-a716-446655440002',
        type: FieldMetadataType.TEXT,
        name: 'customNote',
        label: 'Custom Note',
      };

      const result = manifestValidate({
        ...validManifest,
        fields: [extensionByUuid],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should pass validation with multiple object extensions', () => {
      const anotherExtension: FieldManifest = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: '550e8400-e29b-41d4-a716-446655440003',
        type: FieldMetadataType.TEXT,
        name: 'nickname',
        label: 'Nickname',
      };

      const result = manifestValidate({
        ...validManifest,
        fields: [validField, anotherExtension],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should pass validation with SELECT field having options', () => {
      const extensionWithSelect: FieldManifest = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',

        universalIdentifier: '550e8400-e29b-41d4-a716-446655440004',
        type: FieldMetadataType.SELECT,
        name: 'status',
        label: 'Status',
        options: [
          { value: 'active', label: 'Active', color: 'green', position: 0 },
          {
            value: 'inactive',
            label: 'Inactive',
            color: 'red',
            position: 1,
          },
        ],
      };
      const result = manifestValidate({
        ...validManifest,
        fields: [extensionWithSelect],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });
  });

  describe('duplicate universalIdentifier detection', () => {
    it('rejects differently cased copies of the same UUID', () => {
      const result = manifestValidate({
        ...validManifest,
        fields: [
          validField,
          {
            ...validField,
            name: 'otherScore',
            universalIdentifier: validField.universalIdentifier.toUpperCase(),
          },
        ],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors.join(' ')).toContain(
        'Duplicate universal identifiers',
      );
    });

    it('should fail when extension field has duplicate universalIdentifier', () => {
      const duplicateId = '550e8400-e29b-41d4-a716-446655440001';

      const fieldsWithDuplicates: FieldManifest[] = [
        {
          objectUniversalIdentifier: '91c5848c-36dc-4e7e-b9ee-aa78caeff5a8',
          universalIdentifier: duplicateId,
          type: FieldMetadataType.NUMBER,
          name: 'field1',
          label: 'Field 1',
        },
        {
          objectUniversalIdentifier: '97931020-123c-435b-ad97-9e19a5b38f1f',
          universalIdentifier: duplicateId,
          type: FieldMetadataType.TEXT,
          name: 'field2',
          label: 'Field 2',
        },
      ];

      const result = manifestValidate({
        ...validManifest,
        fields: fieldsWithDuplicates,
      });

      expect(result.isValid).toBe(false);
      expect(result.errors).toContain(
        'Duplicate universal identifiers: 550e8400-e29b-41d4-a716-446655440001',
      );
    });

    it('should fail when extension field ID conflicts with object field ID', () => {
      const sharedId = '550e8400-e29b-41d4-a716-446655440001';

      const result = manifestValidate({
        ...validManifest,
        objects: [
          {
            universalIdentifier: 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
            nameSingular: 'myObject',
            namePlural: 'myObjects',
            labelSingular: 'My Object',
            labelPlural: 'My Objects',
            labelIdentifierFieldMetadataUniversalIdentifier: sharedId,
            fields: [
              {
                universalIdentifier: sharedId,
                type: FieldMetadataType.TEXT,
                name: 'existingField',
                label: 'Existing Field',
              },
            ],
          },
        ],
        fields: [
          {
            objectUniversalIdentifier: '91c5848c-36dc-4e7e-b9ee-aa78caeff5a8',
            universalIdentifier: sharedId,
            type: FieldMetadataType.NUMBER,
            name: 'extensionField',
            label: 'Extension Field',
          },
        ],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors).toContain(
        'Duplicate universal identifiers: 550e8400-e29b-41d4-a716-446655440001',
      );
    });

    it.each([
      'identifyAccountLogicFunction',
      'onConnectLogicFunction',
      'onDisconnectLogicFunction',
    ] as const)(
      'should not flag a connection provider referencing a logic function via %s as a duplicate',
      (lifecycleHookKey) => {
        const logicFunctionId = '550e8400-e29b-41d4-a716-446655440040';

        const logicFunction = {
          universalIdentifier: logicFunctionId,
          name: lifecycleHookKey,
          sourceHandlerPath: 'src/logic-functions/lifecycle-hook.ts',
          builtHandlerPath: 'dist/lifecycle-hook.js',
          builtHandlerChecksum: '00000000-0000-4000-8000-000000000000',
          handlerName: 'handler',
        } as unknown as Manifest['logicFunctions'][number];

        const connectionProvider = {
          universalIdentifier: '550e8400-e29b-41d4-a716-446655440041',
          name: 'slack',
          displayName: 'Slack',
          type: 'oauth',
          oauth: {},
          [lifecycleHookKey]: { universalIdentifier: logicFunctionId },
        } as unknown as NonNullable<Manifest['connectionProviders']>[number];

        const result = manifestValidate({
          ...validManifest,
          logicFunctions: [logicFunction],
          connectionProviders: [connectionProvider],
        });

        expect(result.isValid).toBe(true);
        expect(result.errors).toHaveLength(0);
      },
    );

    it('should not flag a messaging provider referencing its connection provider and send logic function as duplicates', () => {
      const connectionProviderId = '550e8400-e29b-41d4-a716-446655440045';
      const sendMessageLogicFunctionId = '550e8400-e29b-41d4-a716-446655440046';

      const result = manifestValidate({
        ...validManifest,
        logicFunctions: [
          {
            universalIdentifier: sendMessageLogicFunctionId,
            name: 'send-message',
            sourceHandlerPath: 'src/logic-functions/send-message.ts',
            builtHandlerPath: 'dist/send-message.js',
            builtHandlerChecksum: '00000000-0000-4000-8000-000000000000',
            handlerName: 'handler',
          },
        ],
        connectionProviders: [
          {
            universalIdentifier: connectionProviderId,
            name: 'unipile',
            displayName: 'Unipile',
            type: 'custom',
            custom: {
              authorizeLogicFunction: {
                universalIdentifier: sendMessageLogicFunctionId,
              },
              callbackLogicFunction: {
                universalIdentifier: sendMessageLogicFunctionId,
              },
            },
          },
        ],
        messagingProviders: [
          {
            universalIdentifier: '550e8400-e29b-41d4-a716-446655440047',
            name: 'linkedin-messaging',
            displayName: 'LinkedIn Messages',
            connectionProvider: { universalIdentifier: connectionProviderId },
            sendMessageLogicFunction: {
              universalIdentifier: sendMessageLogicFunctionId,
            },
          },
        ],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should not flag an MCP server referencing its connection provider as a duplicate', () => {
      const connectionProviderId = '550e8400-e29b-41d4-a716-446655440048';

      const result = manifestValidate({
        ...validManifest,
        connectionProviders: [
          {
            universalIdentifier: connectionProviderId,
            name: 'github',
            displayName: 'GitHub',
            type: 'oauth',
            oauth: {
              authorizationEndpoint: 'https://github.com/login/oauth/authorize',
              tokenEndpoint: 'https://github.com/login/oauth/access_token',
              scopes: ['repo'],
              clientIdVariable: 'GITHUB_CLIENT_ID',
              clientSecretVariable: 'GITHUB_CLIENT_SECRET',
            },
          },
        ],
        mcpServers: [
          {
            universalIdentifier: '550e8400-e29b-41d4-a716-446655440049',
            name: 'github',
            label: 'GitHub',
            url: 'https://api.githubcopilot.com/mcp/',
            connectionProvider: { universalIdentifier: connectionProviderId },
          },
        ],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should not flag a custom connection provider referencing its authorize and callback logic functions as duplicates', () => {
      const authorizeLogicFunctionId = '550e8400-e29b-41d4-a716-446655440042';
      const callbackLogicFunctionId = '550e8400-e29b-41d4-a716-446655440043';

      const logicFunctions: Manifest['logicFunctions'] = [
        authorizeLogicFunctionId,
        callbackLogicFunctionId,
      ].map((universalIdentifier) => ({
        universalIdentifier,
        name: universalIdentifier,
        sourceHandlerPath: `src/logic-functions/${universalIdentifier}.ts`,
        builtHandlerPath: `dist/${universalIdentifier}.js`,
        builtHandlerChecksum: '00000000-0000-4000-8000-000000000000',
        handlerName: 'handler',
      }));

      const connectionProvider: NonNullable<
        Manifest['connectionProviders']
      >[number] = {
        universalIdentifier: '550e8400-e29b-41d4-a716-446655440044',
        name: 'linkedin',
        displayName: 'LinkedIn',
        type: 'custom',
        custom: {
          authorizeLogicFunction: {
            universalIdentifier: authorizeLogicFunctionId,
          },
          callbackLogicFunction: {
            universalIdentifier: callbackLogicFunctionId,
          },
        },
      };

      const result = manifestValidate({
        ...validManifest,
        logicFunctions,
        connectionProviders: [connectionProvider],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should not flag a front component referenced via settingsFrontComponent as a duplicate', () => {
      const frontComponentId = '550e8400-e29b-41d4-a716-446655440050';

      const frontComponent = {
        universalIdentifier: frontComponentId,
        name: 'app-settings',
        componentName: 'AppSettings',
        sourceComponentPath: 'src/front-components/app-settings.tsx',
        builtComponentPath: 'dist/app-settings.mjs',
        builtComponentChecksum: '00000000-0000-4000-8000-000000000000',
        isHeadless: false,
      } as unknown as Manifest['frontComponents'][number];

      const result = manifestValidate({
        ...validManifest,
        application: {
          ...validApplication,
          settingsFrontComponent: { universalIdentifier: frontComponentId },
        },
        frontComponents: [frontComponent],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should not read universalIdentifier keys inside workflow step payloads', () => {
      const stepId = '550e8400-e29b-41d4-a716-446655440062';

      const result = manifestValidate({
        ...validManifest,
        fields: [validField],
        workflows: [
          {
            universalIdentifier: '550e8400-e29b-41d4-a716-446655440060',
            name: 'Workflow',
            version: {
              trigger: {
                universalIdentifier: '550e8400-e29b-41d4-a716-446655440061',
                type: 'MANUAL',
                nextStepIds: [stepId],
              },
              steps: [
                {
                  universalIdentifier: stepId,
                  name: 'Step',
                  type: 'LOGIC_FUNCTION',
                  logicFunctionUniversalIdentifier:
                    '550e8400-e29b-41d4-a716-446655440063',
                  input: {
                    universalIdentifier: 'customer-123',
                    field: {
                      universalIdentifier: validField.universalIdentifier,
                    },
                  },
                  nextStepIds: [],
                },
              ],
            },
          },
        ],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });
  });

  describe('relation field validation', () => {
    it('should fail when a RELATION field in fields is missing relationType', () => {
      const relationFieldWithoutSettings = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: '550e8400-e29b-41d4-a716-446655440010',
        type: FieldMetadataType.RELATION,
        name: 'company',
        label: 'Company',
        relationTargetFieldMetadataUniversalIdentifier:
          '550e8400-e29b-41d4-a716-446655440011',
        relationTargetObjectMetadataUniversalIdentifier:
          '20202020-b374-4779-a561-80086cb2e17f',
      } as unknown as FieldManifest;

      const result = manifestValidate({
        ...validManifest,
        fields: [relationFieldWithoutSettings],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors[0]).toContain('company');
      expect(result.errors[0]).toContain('missing relationType');
    });

    it('should fail when a RELATION field in object fields is missing relationType', () => {
      const result = manifestValidate({
        ...validManifest,
        objects: [
          {
            universalIdentifier: 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
            nameSingular: 'recipient',
            namePlural: 'recipients',
            labelSingular: 'Recipient',
            labelPlural: 'Recipients',
            labelIdentifierFieldMetadataUniversalIdentifier:
              '7c9e6679-7425-40de-944b-e07fc1f90ae7',
            fields: [
              {
                universalIdentifier: '7c9e6679-7425-40de-944b-e07fc1f90ae7',
                type: FieldMetadataType.TEXT,
                name: 'name',
                label: 'Name',
              },
              {
                universalIdentifier: '550e8400-e29b-41d4-a716-446655440012',
                type: FieldMetadataType.RELATION,
                name: 'company',
                label: 'Company',
                relationTargetFieldMetadataUniversalIdentifier:
                  '550e8400-e29b-41d4-a716-446655440013',
                relationTargetObjectMetadataUniversalIdentifier:
                  '20202020-b374-4779-a561-80086cb2e17f',
              } as unknown as FieldManifest,
            ],
          },
        ],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors[0]).toContain('company');
      expect(result.errors[0]).toContain('missing relationType');
    });

    it('should pass when a RELATION field has valid universalSettings with relationType', () => {
      const validRelationField = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: '550e8400-e29b-41d4-a716-446655440014',
        type: FieldMetadataType.RELATION,
        name: 'company',
        label: 'Company',
        relationTargetFieldMetadataUniversalIdentifier:
          '550e8400-e29b-41d4-a716-446655440015',
        relationTargetObjectMetadataUniversalIdentifier:
          '20202020-b374-4779-a561-80086cb2e17f',
        universalSettings: {
          relationType: RelationType.MANY_TO_ONE,
          joinColumnName: 'companyId',
        },
      } as unknown as FieldManifest;

      const result = manifestValidate({
        ...validManifest,
        fields: [validRelationField],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should fail when a MANY_TO_ONE field is missing joinColumnName', () => {
      const manyToOneWithoutJoinColumn = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: '550e8400-e29b-41d4-a716-446655440018',
        type: FieldMetadataType.RELATION,
        name: 'company',
        label: 'Company',
        relationTargetFieldMetadataUniversalIdentifier:
          '550e8400-e29b-41d4-a716-446655440019',
        relationTargetObjectMetadataUniversalIdentifier:
          '20202020-b374-4779-a561-80086cb2e17f',
        universalSettings: {
          relationType: RelationType.MANY_TO_ONE,
        },
      } as unknown as FieldManifest;

      const result = manifestValidate({
        ...validManifest,
        fields: [manyToOneWithoutJoinColumn],
      });

      expect(result.isValid).toBe(false);
      expect(result.isValid).toBe(false);
      expect(result.errors[0]).toContain('company');
      expect(result.errors[0]).toContain('missing joinColumnName');
    });

    it('should pass when a ONE_TO_MANY field has no joinColumnName', () => {
      const oneToManyField = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: '550e8400-e29b-41d4-a716-446655440020',
        type: FieldMetadataType.RELATION,
        name: 'contacts',
        label: 'Contacts',
        relationTargetFieldMetadataUniversalIdentifier:
          '550e8400-e29b-41d4-a716-446655440021',
        relationTargetObjectMetadataUniversalIdentifier:
          '20202020-b374-4779-a561-80086cb2e17f',
        universalSettings: {
          relationType: RelationType.ONE_TO_MANY,
        },
      } as unknown as FieldManifest;

      const result = manifestValidate({
        ...validManifest,
        fields: [oneToManyField],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should fail when a RELATION field has an invalid relationType', () => {
      const relationFieldWithBadType = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: '550e8400-e29b-41d4-a716-446655440016',
        type: FieldMetadataType.RELATION,
        name: 'company',
        label: 'Company',
        relationTargetFieldMetadataUniversalIdentifier:
          '550e8400-e29b-41d4-a716-446655440017',
        relationTargetObjectMetadataUniversalIdentifier:
          '20202020-b374-4779-a561-80086cb2e17f',
        universalSettings: {
          relationType: 'INVALID_TYPE',
        },
      } as unknown as FieldManifest;

      const result = manifestValidate({
        ...validManifest,
        fields: [relationFieldWithBadType],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors[0]).toContain('company');
      expect(result.errors[0]).toContain('invalid relationType');
    });
  });

  describe('UUID version validation', () => {
    it('should pass with UUID v4 identifiers', () => {
      const result = manifestValidate({
        ...validManifest,
        fields: [validField],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should pass with UUID v5 identifiers', () => {
      const v5Field: FieldManifest = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: '21f7f8de-8051-5b89-8680-0195ef798b6a',
        type: FieldMetadataType.TEXT,
        name: 'v5Field',
        label: 'V5 Field',
      };

      const result = manifestValidate({
        ...validManifest,
        fields: [v5Field],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should fail with UUID v1 identifiers', () => {
      const v1Uuid = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
      const v1Field: FieldManifest = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: v1Uuid,
        type: FieldMetadataType.TEXT,
        name: 'v1Field',
        label: 'V1 Field',
      };

      const result = manifestValidate({
        ...validManifest,
        fields: [v1Field],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors).toContainEqual(
        expect.stringContaining(`"${v1Uuid}" is UUID version 1`),
      );
      expect(result.errors).toContainEqual(
        expect.stringContaining('Only UUID version 4 or higher is allowed'),
      );
    });

    it('should fail with UUID v3 identifiers', () => {
      const v3Uuid = 'a3bb189e-8bf9-3888-9912-ace4e6543002';
      const v3Field: FieldManifest = {
        objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
        universalIdentifier: v3Uuid,
        type: FieldMetadataType.TEXT,
        name: 'v3Field',
        label: 'V3 Field',
      };

      const result = manifestValidate({
        ...validManifest,
        fields: [v3Field],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors).toContainEqual(
        expect.stringContaining(`"${v3Uuid}" is UUID version 3`),
      );
    });

    it('should fail with non-UUID universal identifiers', () => {
      const result = manifestValidate({
        ...validManifest,
        objects: [
          {
            universalIdentifier: 'not-a-uuid',
            nameSingular: 'myObject',
            namePlural: 'myObjects',
            labelSingular: 'My Object',
            labelPlural: 'My Objects',
            labelIdentifierFieldMetadataUniversalIdentifier:
              '550e8400-e29b-41d4-a716-446655440030',
            fields: [
              {
                universalIdentifier: '550e8400-e29b-41d4-a716-446655440030',
                type: FieldMetadataType.TEXT,
                name: 'name',
                label: 'Name',
              },
            ],
          },
        ],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors).toContainEqual(
        expect.stringContaining('"not-a-uuid" is not a valid UUID'),
      );
    });

    it('should not report duplicate version errors for the same identifier', () => {
      const v1Uuid = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';

      const result = manifestValidate({
        ...validManifest,
        fields: [
          {
            objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
            universalIdentifier: v1Uuid,
            type: FieldMetadataType.TEXT,
            name: 'field1',
            label: 'Field 1',
          },
          {
            objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
            universalIdentifier: v1Uuid,
            type: FieldMetadataType.TEXT,
            name: 'field2',
            label: 'Field 2',
          },
        ],
      });

      const versionErrors = result.errors.filter((e) =>
        e.includes('is UUID version 1'),
      );

      expect(versionErrors).toHaveLength(1);
    });
  });

  describe('graph widget validation', () => {
    const makeGraphWidgetTab = (
      configuration: PageLayoutWidgetManifest['configuration'],
    ): PageLayoutTabManifest => ({
      universalIdentifier: 'b0a5f0f2-6c2e-4d1c-9d0b-2f8a4c3e1a01',
      title: 'Dashboard',
      position: 0,
      widgets: [
        {
          universalIdentifier: 'b0a5f0f2-6c2e-4d1c-9d0b-2f8a4c3e1a02',
          title: 'Total opportunities',
          type: WidgetType.GRAPH,
          configuration,
        },
      ],
    });

    it('should pass when a graph widget has aggregateFieldMetadataUniversalIdentifier', () => {
      const result = manifestValidate({
        ...validManifest,
        pageLayoutTabs: [
          makeGraphWidgetTab({
            configurationType: 'AGGREGATE_CHART',
            aggregateFieldMetadataUniversalIdentifier:
              'b0a5f0f2-6c2e-4d1c-9d0b-2f8a4c3e1a03',
            aggregateOperation: AggregateOperations.COUNT,
          }),
        ],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should error when a graph widget is missing the aggregate field identifier', () => {
      const result = manifestValidate({
        ...validManifest,
        pageLayoutTabs: [
          makeGraphWidgetTab({
            configurationType: 'AGGREGATE_CHART',
            aggregateFieldMetadataUniversalIdentifier: null,
            aggregateOperation: AggregateOperations.COUNT,
          }),
        ],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors).toContainEqual(
        expect.stringContaining(
          'is missing aggregateFieldMetadataUniversalIdentifier',
        ),
      );
    });

    it('should hint at the correct key when the raw aggregateFieldMetadataId was used', () => {
      const configurationWithRawKey = {
        configurationType: 'AGGREGATE_CHART',
        aggregateFieldMetadataId: 'b0a5f0f2-6c2e-4d1c-9d0b-2f8a4c3e1a03',
        aggregateOperation: AggregateOperations.COUNT,
      } as unknown as PageLayoutWidgetManifest['configuration'];

      const result = manifestValidate({
        ...validManifest,
        pageLayoutTabs: [makeGraphWidgetTab(configurationWithRawKey)],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors).toContainEqual(
        expect.stringContaining('not "aggregateFieldMetadataId"'),
      );
    });

    it('should ignore non-graph widgets that have no aggregate field', () => {
      const result = manifestValidate({
        ...validManifest,
        pageLayoutTabs: [makeGraphWidgetTab({ configurationType: 'TIMELINE' })],
      });

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('should not crash on a widget with a missing configuration', () => {
      const nullConfiguration =
        null as unknown as PageLayoutWidgetManifest['configuration'];

      const result = manifestValidate({
        ...validManifest,
        pageLayoutTabs: [makeGraphWidgetTab(nullConfiguration)],
      });

      expect(result.isValid).toBe(true);
    });
  });

  describe('page layout deprecation warnings', () => {
    it.each(['nested', 'standalone'])(
      'warns without changing a legacy %s manifest',
      (location) => {
        const legacyTab: PageLayoutTabManifest = {
          universalIdentifier: 'a0a1a2a3-a4a5-4000-8000-000000000012',
          title: 'Legacy canvas',
          position: 0,
          layoutMode: PageLayoutTabLayoutMode.CANVAS,
          widgets: [],
        };
        const manifest: Manifest = {
          ...validManifest,
          pageLayouts:
            location === 'nested'
              ? [
                  {
                    universalIdentifier: 'a0a1a2a3-a4a5-4000-8000-000000000010',
                    name: 'Record page',
                    type: PageLayoutType.RECORD_PAGE,
                    tabs: [legacyTab],
                  },
                ]
              : [],
          pageLayoutTabs: location === 'standalone' ? [legacyTab] : [],
        };
        const original = JSON.stringify(manifest);
        const result = manifestValidate(manifest);

        expect(result.isValid).toBe(true);
        expect(result.warnings).toEqual([
          expect.stringContaining('uses deprecated CANVAS'),
        ]);
        expect(result.warnings[0]).toContain(
          "heightBehavior to 'TAB_VIEWPORT'",
        );
        expect(JSON.stringify(manifest)).toBe(original);
      },
    );

    it.each(['position', 'gridPosition'])(
      'warns about legacy %s with a replacement',
      (positionKey) => {
        const result = manifestValidate({
          ...validManifest,
          pageLayoutTabs: [
            {
              universalIdentifier: 'a0a1a2a3-a4a5-4000-8000-000000000012',
              title: 'Details',
              position: 0,
              layoutMode:
                positionKey === 'position'
                  ? PageLayoutTabLayoutMode.VERTICAL_LIST
                  : PageLayoutTabLayoutMode.GRID,
              widgets: [
                {
                  universalIdentifier: 'a0a1a2a3-a4a5-4000-8000-000000000013',
                  title: 'Timeline',
                  type: WidgetType.TIMELINE,
                  configuration: { configurationType: 'TIMELINE' },
                  [positionKey]:
                    positionKey === 'position'
                      ? {
                          layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,
                          index: 99,
                        }
                      : { row: 2, column: 3, rowSpan: 4, columnSpan: 5 },
                },
              ],
            },
          ],
        });

        expect(result.isValid).toBe(true);
        expect(result.warnings).toHaveLength(1);
        expect(result.warnings[0]).toContain(
          positionKey === 'position'
            ? 'Order the widgets array by position.index'
            : "Use position with layoutMode: 'GRID'",
        );
      },
    );
  });

  describe('page layout widget height behavior validation', () => {
    const makePageLayout = (
      layoutMode: PageLayoutTabLayoutMode,
    ): Manifest['pageLayouts'][number] => ({
      universalIdentifier: 'a0a1a2a3-a4a5-4000-8000-000000000010',
      name: 'Record page',
      type: PageLayoutType.RECORD_PAGE,
      objectUniversalIdentifier: 'a0a1a2a3-a4a5-4000-8000-000000000011',
      tabs: [
        {
          universalIdentifier: 'a0a1a2a3-a4a5-4000-8000-000000000012',
          title: 'Details',
          position: 0,
          layoutMode,
          widgets: [
            {
              universalIdentifier: 'a0a1a2a3-a4a5-4000-8000-000000000013',
              title: 'App',
              type: WidgetType.FRONT_COMPONENT,
              heightBehavior:
                PageLayoutWidgetVerticalListHeightBehavior.TAB_VIEWPORT,
              configuration: {
                configurationType: 'FRONT_COMPONENT',
                frontComponentUniversalIdentifier:
                  'a0a1a2a3-a4a5-4000-8000-000000000014',
              },
            },
          ],
        },
      ],
    });

    it('should accept heightBehavior on a VERTICAL_LIST tab', () => {
      const result = manifestValidate({
        ...validManifest,
        pageLayouts: [makePageLayout(PageLayoutTabLayoutMode.VERTICAL_LIST)],
      });

      expect(result.errors).toHaveLength(0);
    });

    it.each(['nested', 'standalone'])(
      'rejects invalid heightBehavior in a %s tab',
      (location) => {
        const pageLayout = makePageLayout(
          PageLayoutTabLayoutMode.VERTICAL_LIST,
        );
        const pageLayoutTab = {
          ...pageLayout.tabs![0],
          widgets: pageLayout.tabs![0].widgets!.map((widget) => ({
            ...widget,
            heightBehavior: 'TAB_VIEPORT',
          })),
        };
        const manifest: Manifest = JSON.parse(
          JSON.stringify({
            ...validManifest,
            pageLayouts: [
              {
                ...pageLayout,
                tabs: location === 'nested' ? [pageLayoutTab] : [],
              },
            ],
            pageLayoutTabs:
              location === 'standalone'
                ? [
                    {
                      ...pageLayoutTab,
                      pageLayoutUniversalIdentifier:
                        pageLayout.universalIdentifier,
                    },
                  ]
                : [],
          }),
        );

        const result = manifestValidate(manifest);
        expect(result.isValid).toBe(false);
        expect(result.errors).toContain(
          'Page layout widget "App" defines unsupported heightBehavior "TAB_VIEPORT". Expected FIT_CONTENT or TAB_VIEWPORT.',
        );
      },
    );

    it.each([PageLayoutTabLayoutMode.GRID, PageLayoutTabLayoutMode.CANVAS])(
      'should reject heightBehavior on a %s tab',
      (layoutMode) => {
        const result = manifestValidate({
          ...validManifest,
          pageLayouts: [makePageLayout(layoutMode)],
        });

        expect(result.errors).toContain(
          `Page layout widget "App" defines heightBehavior, but its parent tab "Details" uses ${layoutMode}. heightBehavior is only supported for VERTICAL_LIST tabs.`,
        );
      },
    );
  });

  describe('validation rule validation', () => {
    const objectUniversalIdentifier = '550e8400-e29b-41d4-a716-446655440100';
    const rule: ValidationRuleManifest = {
      universalIdentifier: '550e8400-e29b-41d4-a716-446655440101',
      objectUniversalIdentifier,
      name: 'Has a score',
      expression: 'score > 0',
      message: 'Add a score',
    };
    const manifestWith = (validationRule: Partial<ValidationRuleManifest>) => ({
      ...validManifest,
      objects: [
        {
          universalIdentifier: objectUniversalIdentifier,
          nameSingular: 'ticket',
          namePlural: 'tickets',
          labelSingular: 'Ticket',
          labelPlural: 'Tickets',
          labelIdentifierFieldMetadataUniversalIdentifier:
            validField.universalIdentifier,
          fields: [{ ...validField, name: 'score' }],
        },
      ],
      validationRules: [{ ...rule, ...validationRule }],
    });

    it('accepts an expression reading a field of the object', () => {
      expect(manifestValidate(manifestWith({})).errors).toEqual([]);
    });

    it('accepts an expression reading a field Twenty generates', () => {
      expect(
        manifestValidate(manifestWith({ expression: 'isDefined(createdAt)' }))
          .errors,
      ).toEqual([]);
    });

    it('names the rule when it reads a field the object does not declare', () => {
      expect(
        manifestValidate(manifestWith({ expression: 'scor > 0' })).errors,
      ).toEqual([
        'Validation rule "Has a score" has an invalid expression: Unknown field "scor"',
      ]);
    });

    it('rejects an error field outside the object', () => {
      expect(
        manifestValidate(
          manifestWith({
            errorFieldUniversalIdentifier:
              '550e8400-e29b-41d4-a716-446655440102',
          }),
        ).errors,
      ).toEqual([
        expect.stringContaining("does not belong to the rule's object"),
      ]);
    });

    it('only checks the syntax on an object outside the app', () => {
      const outsideObject = '20202020-b374-4779-a561-80086cb2e17f';

      expect(
        manifestValidate(
          manifestWith({ objectUniversalIdentifier: outsideObject }),
        ).errors,
      ).toEqual([]);
      expect(
        manifestValidate(
          manifestWith({
            objectUniversalIdentifier: outsideObject,
            expression: 'score >',
          }),
        ).errors,
      ).toEqual([expect.stringContaining('"Has a score"')]);
    });

    it.each([
      ['company.employees > 0', []],
      ['score[0] > 0', ['Bracket access is not supported']],
      ['company . employees > 0', ['without spaces around the dot']],
    ])(
      'checks everything but the fields on an object outside the app: %s',
      (expression, expectedErrors) => {
        expect(
          manifestValidate(
            manifestWith({
              objectUniversalIdentifier: '20202020-b374-4779-a561-80086cb2e17f',
              expression,
            }),
          ).errors,
        ).toEqual(
          expectedErrors.map((error) => expect.stringContaining(error)),
        );
      },
    );
  });

  describe('timeline activity type validation', () => {
    const sourceObjectUniversalIdentifier =
      '1a111111-1111-4111-8111-111111111111';
    const relationFieldUniversalIdentifier =
      '2a222222-2222-4222-8222-222222222222';
    const triggerFieldUniversalIdentifier =
      '3a333333-3333-4333-8333-333333333333';
    const frontComponentUniversalIdentifier =
      '4a444444-4444-4444-8444-444444444444';

    const buildTimelineManifest = (): Manifest => ({
      ...validManifest,
      objects: [
        {
          universalIdentifier: sourceObjectUniversalIdentifier,
          nameSingular: 'deployment',
          namePlural: 'deployments',
          labelSingular: 'Deployment',
          labelPlural: 'Deployments',
          labelIdentifierFieldMetadataUniversalIdentifier:
            triggerFieldUniversalIdentifier,
          fields: [],
        },
      ],
      fields: [
        {
          universalIdentifier: relationFieldUniversalIdentifier,
          objectUniversalIdentifier: sourceObjectUniversalIdentifier,
          type: FieldMetadataType.RELATION,
          name: 'owner',
          label: 'Owner',
          relationTargetFieldMetadataUniversalIdentifier:
            '5a555555-5555-4555-8555-555555555555',
          relationTargetObjectMetadataUniversalIdentifier:
            '6a666666-6666-4666-8666-666666666666',
          universalSettings: {
            relationType: RelationType.MANY_TO_ONE,
            joinColumnName: 'ownerId',
          },
        },
        {
          universalIdentifier: triggerFieldUniversalIdentifier,
          objectUniversalIdentifier: sourceObjectUniversalIdentifier,
          type: FieldMetadataType.TEXT,
          name: 'status',
          label: 'Status',
        },
      ],
      frontComponents: [
        {
          universalIdentifier: frontComponentUniversalIdentifier,
          sourceComponentPath: 'src/front-components/deployment.tsx',
          builtComponentPath: 'dist/front-components/deployment.mjs',
          builtComponentChecksum: 'checksum',
          componentName: 'DeploymentTimelineActivity',
        },
      ],
      timelineActivityTypes: [
        {
          universalIdentifier: '7a777777-7777-4777-8777-777777777777',
          name: 'deploymentUpdated',
          label: 'updated a deployment',
          emit: {
            on: 'updated',
            objectUniversalIdentifier: sourceObjectUniversalIdentifier,
            through: {
              relationFieldUniversalIdentifier,
              triggerFieldUniversalIdentifiers: [
                triggerFieldUniversalIdentifier,
              ],
            },
          },
          frontComponentUniversalIdentifier,
        },
      ],
    });

    it('accepts references to source metadata and front components in the application', () => {
      const result = manifestValidate(buildTimelineManifest());

      expect(result.isValid).toBe(true);
      expect(result.errors).toHaveLength(0);
    });

    it('reports references that cannot be resolved inside the application', () => {
      const manifest = buildTimelineManifest();
      const [timelineActivityType] = manifest.timelineActivityTypes;

      timelineActivityType.frontComponentUniversalIdentifier =
        '8a888888-8888-4888-8888-888888888888';
      timelineActivityType.emit!.through!.relationFieldUniversalIdentifier =
        '9a999999-9999-4999-8999-999999999999';
      timelineActivityType.emit!.through!.triggerFieldUniversalIdentifiers = [
        '0aaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
      ];

      const result = manifestValidate(manifest);

      expect(result.errors).toEqual(
        expect.arrayContaining([
          expect.stringContaining('references front component'),
          expect.stringContaining('references relation field'),
          expect.stringContaining('references trigger fields'),
        ]),
      );
    });

    it('requires external-object emitters to declare the type they replace', () => {
      const manifest = buildTimelineManifest();
      const [timelineActivityType] = manifest.timelineActivityTypes;

      timelineActivityType.emit!.objectUniversalIdentifier =
        '0bbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';

      expect(manifestValidate(manifest).errors).toContainEqual(
        expect.stringContaining(
          'must declare replacesTimelineActivityTypeUniversalIdentifier',
        ),
      );

      timelineActivityType.replacesTimelineActivityTypeUniversalIdentifier =
        '0ccccccc-cccc-4ccc-8ccc-cccccccccccc';

      expect(manifestValidate(manifest).errors).toHaveLength(0);
    });

    it('validates external-object route identifiers without resolving their metadata', () => {
      const manifest = buildTimelineManifest();
      const [timelineActivityType] = manifest.timelineActivityTypes;

      timelineActivityType.emit!.objectUniversalIdentifier =
        '0bbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';
      timelineActivityType.replacesTimelineActivityTypeUniversalIdentifier =
        '0ccccccc-cccc-4ccc-8ccc-cccccccccccc';
      timelineActivityType.emit!.through!.relationFieldUniversalIdentifier =
        'not-a-relation-field-uuid';
      timelineActivityType.emit!.through!.triggerFieldUniversalIdentifiers = [
        'not-a-trigger-field-uuid',
      ];

      expect(manifestValidate(manifest).errors).toEqual(
        expect.arrayContaining([
          expect.stringContaining(
            'invalid through relation field universal identifier',
          ),
          expect.stringContaining(
            'invalid trigger field universal identifiers',
          ),
        ]),
      );
    });

    it('rejects an invalid emit object universal identifier', () => {
      const manifest = buildTimelineManifest();
      const [timelineActivityType] = manifest.timelineActivityTypes;

      timelineActivityType.emit!.objectUniversalIdentifier = 'POST_CARD_TYPO';
      timelineActivityType.replacesTimelineActivityTypeUniversalIdentifier =
        '0ccccccc-cccc-4ccc-8ccc-cccccccccccc';

      expect(manifestValidate(manifest).errors).toContainEqual(
        expect.stringContaining('invalid object universal identifier'),
      );
    });

    it('rejects replacement contracts that cannot be valid locally', () => {
      const explicitManifest = buildTimelineManifest();
      const [explicitTimelineActivityType] =
        explicitManifest.timelineActivityTypes;

      explicitTimelineActivityType.emit = undefined;
      explicitTimelineActivityType.replacesTimelineActivityTypeUniversalIdentifier =
        '0ccccccc-cccc-4ccc-8ccc-cccccccccccc';

      expect(manifestValidate(explicitManifest).errors).toContainEqual(
        expect.stringContaining(
          'declares replacesTimelineActivityTypeUniversalIdentifier without an automatic emit contract',
        ),
      );

      const localObjectManifest = buildTimelineManifest();
      localObjectManifest.timelineActivityTypes[0].replacesTimelineActivityTypeUniversalIdentifier =
        '0ccccccc-cccc-4ccc-8ccc-cccccccccccc';

      expect(manifestValidate(localObjectManifest).errors).toContainEqual(
        expect.stringContaining("must not replace another application's type"),
      );

      const invalidIdentifierManifest = buildTimelineManifest();
      const invalidIdentifierTimelineActivityType =
        invalidIdentifierManifest.timelineActivityTypes[0];

      invalidIdentifierTimelineActivityType.emit!.objectUniversalIdentifier =
        '0bbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';
      invalidIdentifierTimelineActivityType.replacesTimelineActivityTypeUniversalIdentifier =
        'not-a-uuid';

      expect(manifestValidate(invalidIdentifierManifest).errors).toContainEqual(
        expect.stringContaining('invalid replacement universal identifier'),
      );
    });

    it.each([
      {
        action: 'updated' as const,
        triggerFieldUniversalIdentifiers: [],
      },
      {
        action: 'updated' as const,
        triggerFieldUniversalIdentifiers: [
          triggerFieldUniversalIdentifier,
          triggerFieldUniversalIdentifier,
        ],
      },
      {
        action: 'created' as const,
        triggerFieldUniversalIdentifiers: [triggerFieldUniversalIdentifier],
      },
    ])(
      'rejects invalid trigger constraints for $action events',
      ({ action, triggerFieldUniversalIdentifiers }) => {
        const manifest = buildTimelineManifest();
        const [timelineActivityType] = manifest.timelineActivityTypes;

        timelineActivityType.emit!.on = action;
        timelineActivityType.emit!.through!.triggerFieldUniversalIdentifiers =
          triggerFieldUniversalIdentifiers;

        expect(manifestValidate(manifest).errors).toContainEqual(
          expect.stringContaining(
            'trigger fields must be a non-empty list of unique fields on an updated through event',
          ),
        );
      },
    );

    it('requires linked and unlinked emitters to declare a through relation', () => {
      const manifest = buildTimelineManifest();
      const [timelineActivityType] = manifest.timelineActivityTypes;

      timelineActivityType.emit!.on = 'linked';
      timelineActivityType.emit!.through = undefined;

      expect(manifestValidate(manifest).errors).toContainEqual(
        expect.stringContaining('must declare emit.through for a linked event'),
      );
    });

    it('rejects duplicate names and unsupported through relations', () => {
      const manifest = buildTimelineManifest();
      const [timelineActivityType] = manifest.timelineActivityTypes;

      manifest.timelineActivityTypes.push({
        ...timelineActivityType,
        universalIdentifier: '0ddddddd-dddd-4ddd-8ddd-dddddddddddd',
      });
      manifest.fields[0].universalSettings = {
        relationType: RelationType.ONE_TO_MANY,
      };

      const result = manifestValidate(manifest);

      expect(result.errors).toEqual(
        expect.arrayContaining([
          expect.stringContaining(
            'Timeline activity type name "deploymentUpdated" is used more than once',
          ),
          expect.stringContaining(
            'must route through a MANY_TO_ONE relation or a junction-backed ONE_TO_MANY relation',
          ),
        ]),
      );
    });

    it('reports a relation with missing universal settings without crashing', () => {
      const manifest = buildTimelineManifest();

      manifest.fields[0].universalSettings = undefined as never;

      expect(manifestValidate(manifest).errors).toEqual(
        expect.arrayContaining([
          expect.stringContaining('is missing relationType'),
          expect.stringContaining(
            'must route through a MANY_TO_ONE relation or a junction-backed ONE_TO_MANY relation',
          ),
        ]),
      );
    });
  });

  describe('settings menu item slug validation', () => {
    const frontComponentUniversalIdentifier =
      '7a777777-7777-4777-8777-777777777777';

    it('rejects two settings menu items sharing a slug', () => {
      const result = manifestValidate({
        ...validManifest,
        settingsMenuItems: [
          {
            universalIdentifier: '8a888888-8888-4888-8888-888888888881',
            frontComponentUniversalIdentifier,
            title: 'Sync',
            slug: 'sync',
          },
          {
            universalIdentifier: '8a888888-8888-4888-8888-888888888882',
            frontComponentUniversalIdentifier,
            title: 'My sync',
            scope: 'USER',
            slug: 'sync',
          },
        ],
      });

      expect(result.isValid).toBe(false);
      expect(result.errors).toContain(
        'Settings menu item slug "sync" is used more than once. Slugs must be unique within an application.',
      );
    });
  });
});