Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

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

import { schema as discordSchema } from '@/server/services/bot/platforms/discord/schema';
import type { FieldSchema } from '@/server/services/bot/platforms/types';
import { BOT_WAKE_SETTING_KEYS } from '@/server/services/conversationWake/config';

import {
  extractSettingsDefaults,
  findOverLimitSettings,
  getChannelFormValues,
  isFieldReleased,
  mergeCredentialsForSave,
  mergeSettingsWithDefaults,
  shouldAdoptIncomingConfig,
  withHeterogeneousWakeOff,
  withNewBotReactionOff,
} from './formState';

const slackSchema: FieldSchema[] = [
  {
    key: 'settings',
    label: 'Settings',
    properties: [
      { default: 'websocket', key: 'connectionMode', label: '', type: 'string' },
      { default: 4000, key: 'charLimit', label: '', type: 'number' },
      { key: 'showUsageStats', label: '', type: 'boolean' },
      {
        key: 'nested',
        label: '',
        properties: [{ default: 'queue', key: 'concurrency', label: '', type: 'string' }],
        type: 'object',
      },
    ],
    type: 'object',
  },
];

describe('getChannelFormValues', () => {
  it('should keep bot settings nested under the settings field', () => {
    expect(
      getChannelFormValues({
        applicationId: 'bot-123',
        credentials: { botToken: 'secret' },
        settings: { debounceMs: 300, enableDM: true },
      }),
    ).toEqual({
      applicationId: 'bot-123',
      credentials: { botToken: 'secret' },
      settings: { debounceMs: 300, enableDM: true },
    });
  });

  it('should default missing nested objects to empty objects', () => {
    expect(
      getChannelFormValues({
        applicationId: 'bot-123',
      }),
    ).toEqual({
      applicationId: 'bot-123',
      credentials: {},
      settings: {},
    });
  });

  it('migrates a legacy comma-separated allowFrom string to object-list shape', () => {
    // Operators on the very first cut of allowFrom stored a single string.
    // The new editor is bound to an array β€” the form would crash on a string
    // unless we lift it into `[{id}]` on read.
    const result = getChannelFormValues({
      settings: { allowFrom: 'alice, bob\n carol' },
    });
    expect(result.settings).toEqual({
      allowFrom: [{ id: 'alice' }, { id: 'bob' }, { id: 'carol' }],
    });
  });

  it('migrates a legacy bare string[] allowFrom into object-list shape', () => {
    const result = getChannelFormValues({
      settings: { groupAllowFrom: ['channel-1', '  channel-2 ', ''] },
    });
    expect(result.settings).toEqual({
      groupAllowFrom: [{ id: 'channel-1' }, { id: 'channel-2' }],
    });
  });

  it('passes through the new object-list shape unchanged (preserves names)', () => {
    const result = getChannelFormValues({
      settings: {
        allowFrom: [{ id: 'alice', name: 'Alice β€” PM' }, { id: 'bob' }],
      },
    });
    expect(result.settings).toEqual({
      allowFrom: [{ id: 'alice', name: 'Alice β€” PM' }, { id: 'bob' }],
    });
  });

  it('drops object entries with empty / missing ids during migration', () => {
    const result = getChannelFormValues({
      settings: {
        allowFrom: [{ id: '' }, { id: '   ' }, { id: 'bob', name: 'Bob' }],
      },
    });
    expect(result.settings).toEqual({ allowFrom: [{ id: 'bob', name: 'Bob' }] });
  });
});

describe('extractSettingsDefaults', () => {
  it('returns empty object when schema is undefined or has no settings section', () => {
    expect(extractSettingsDefaults(undefined)).toEqual({});
    expect(extractSettingsDefaults([])).toEqual({});
  });

  it('extracts top-level default values and skips fields without a default', () => {
    expect(extractSettingsDefaults(slackSchema)).toEqual({
      charLimit: 4000,
      concurrency: 'queue',
      connectionMode: 'websocket',
    });
  });
});

describe('mergeCredentialsForSave', () => {
  it('carries a credential key the form does not mention', () => {
    // The persisted copy keeps every key, but a form that never registered a
    // field for one of them would drop it β€” the server replaces the blob
    // wholesale, so the carried mask is what stops the key being deleted.
    expect(
      mergeCredentialsForSave(
        { botId: 'β€’β€’β€’β€’β€’β€’β€’β€’', botToken: 'β€’β€’β€’β€’β€’β€’β€’β€’' },
        { botToken: 'β€’β€’β€’β€’β€’β€’β€’β€’' },
      ),
    ).toEqual({ botId: 'β€’β€’β€’β€’β€’β€’β€’β€’', botToken: 'β€’β€’β€’β€’β€’β€’β€’β€’' });
  });

  it('lets a form edit win over the config value', () => {
    expect(mergeCredentialsForSave({ botToken: 'β€’β€’β€’β€’β€’β€’β€’β€’' }, { botToken: 'rotated' })).toEqual({
      botToken: 'rotated',
    });
  });

  it('keeps clearing a credential meaningful', () => {
    // An explicit empty overrides the carried value and is then dropped, so the
    // server deletes the key instead of silently resurrecting it.
    expect(mergeCredentialsForSave({ botToken: 'β€’β€’β€’β€’β€’β€’β€’β€’' }, { botToken: '' })).toEqual({});
  });

  it('drops undefined values and tolerates a missing config', () => {
    expect(mergeCredentialsForSave(undefined, { botToken: undefined } as never)).toEqual({});
  });
});

describe('shouldAdoptIncomingConfig', () => {
  const provider = { id: 'provider-1', platform: 'wechat' };

  it('ignores a same-provider replacement while the form is dirty', () => {
    // The replica-backed background revalidation must not wipe a mid-edit form.
    expect(shouldAdoptIncomingConfig(provider, provider, true)).toBe(false);
  });

  it('adopts a same-provider replacement once the form is clean', () => {
    expect(shouldAdoptIncomingConfig(provider, provider, false)).toBe(true);
  });

  it('adopts a different provider even while the previous form was dirty', () => {
    expect(
      shouldAdoptIncomingConfig(provider, { id: 'provider-2', platform: 'wechat' }, true),
    ).toBe(true);
  });

  it('adopts on the first paint, when there is no previous config', () => {
    expect(shouldAdoptIncomingConfig(undefined, provider, true)).toBe(true);
  });
});

describe('mergeSettingsWithDefaults', () => {
  it('fills in schema defaults for missing keys', () => {
    expect(mergeSettingsWithDefaults(slackSchema, {})).toEqual({
      charLimit: 4000,
      concurrency: 'queue',
      connectionMode: 'websocket',
    });
  });

  it('user-provided values override schema defaults', () => {
    expect(mergeSettingsWithDefaults(slackSchema, { connectionMode: 'webhook' })).toEqual({
      charLimit: 4000,
      concurrency: 'queue',
      connectionMode: 'webhook',
    });
  });

  it('preserves keys not declared in the schema', () => {
    expect(mergeSettingsWithDefaults(slackSchema, { extra: 'keep-me' })).toEqual({
      charLimit: 4000,
      concurrency: 'queue',
      connectionMode: 'websocket',
      extra: 'keep-me',
    });
  });
});

describe('bot-wake channel lists', () => {
  it.each([BOT_WAKE_SETTING_KEYS.allowChannels, BOT_WAKE_SETTING_KEYS.denyChannels])(
    'migrates a hand-written %s to the row shape the list editor needs',
    (key) => {
      const { settings } = getChannelFormValues({
        settings: { [key]: ['c1', ' c2 '] },
      }) as { settings: Record<string, unknown> };

      expect(settings[key]).toEqual([{ id: 'c1' }, { id: 'c2' }]);
    },
  );

  it.each([BOT_WAKE_SETTING_KEYS.allowChannels, BOT_WAKE_SETTING_KEYS.denyChannels])(
    'keeps numeric snowflake %s entries a hand-written config may carry',
    (key) => {
      // The runtime parser accepts bare numbers (`parseIdList`), so dropping them here would
      // save an allowlist back as "unrestricted" β€” or a denylist as "nothing blocked" β€” just
      // because the operator opened the form.
      const { settings } = getChannelFormValues({
        settings: { [key]: [22, '33', 445566] },
      }) as { settings: Record<string, unknown> };

      expect(settings[key]).toEqual([{ id: '22' }, { id: '33' }, { id: '445566' }]);
    },
  );
});

describe('isFieldReleased', () => {
  it('shows a field that names no rollout flag', () => {
    expect(isFieldReleased({}, undefined)).toBe(true);
    expect(isFieldReleased({}, { enableExampleRollout: false })).toBe(true);
  });

  it('shows a gated field only when its flag is explicitly on', () => {
    const field = { featureFlag: 'enableExampleRollout' };

    expect(isFieldReleased(field, { enableExampleRollout: true })).toBe(true);
    expect(isFieldReleased(field, { enableExampleRollout: false })).toBe(false);
  });

  it('hides a gated field while the flags are unset or have not loaded', () => {
    const field = { featureFlag: 'enableExampleRollout' };

    expect(isFieldReleased(field, undefined)).toBe(false);
    expect(isFieldReleased(field, {})).toBe(false);
    expect(isFieldReleased(field, { enableExampleRollout: undefined })).toBe(false);
    expect(isFieldReleased(field, { enableExampleRollout: 'yes' })).toBe(false);
  });
});

describe('findOverLimitSettings', () => {
  const listSchema: FieldSchema[] = [
    {
      key: 'settings',
      label: 'Settings',
      properties: [
        {
          key: 'allowChannels',
          label: 'channel.allow',
          maximum: 2,
          type: 'array',
          items: { key: 'item', label: '', type: 'object' },
        },
        {
          key: 'group',
          label: '',
          properties: [
            {
              key: 'denyChannels',
              label: 'channel.deny',
              maximum: 2,
              type: 'array',
              items: { key: 'item', label: '', type: 'object' },
            },
          ],
          type: 'object',
        },
        {
          key: 'keywords',
          label: 'channel.keywords',
          type: 'array',
          items: { key: 'item', label: '', type: 'string' },
        },
      ],
      type: 'object',
    },
  ];

  it('returns nothing while every list stays within its cap', () => {
    expect(
      findOverLimitSettings(listSchema, {
        allowChannels: [{ id: '1' }, { id: '2' }],
        denyChannels: [{ id: '3' }],
      }),
    ).toEqual([]);
  });

  it('reports the field, the real entry count and the cap once a list overflows', () => {
    expect(
      findOverLimitSettings(listSchema, {
        allowChannels: [{ id: '1' }, { id: '2' }, { id: '3' }],
      }),
    ).toEqual([{ count: 3, key: 'allowChannels', max: 2 }]);
  });

  it('reaches list fields nested in a settings group', () => {
    expect(
      findOverLimitSettings(listSchema, {
        denyChannels: [{ id: '1' }, { id: '2' }, { id: '3' }],
      }),
    ).toEqual([{ count: 3, key: 'denyChannels', max: 2 }]);
  });

  it('ignores blank rows and lists whose schema declares no cap', () => {
    expect(
      findOverLimitSettings(listSchema, {
        allowChannels: [{ id: '1' }, { id: '2' }, { id: '' }, { id: '   ' }],
        keywords: ['a', 'b', 'c', 'd'],
      }),
    ).toEqual([]);
  });

  it('ignores values that are not lists at all', () => {
    expect(findOverLimitSettings(listSchema, { allowChannels: '1,2,3' })).toEqual([]);
    expect(findOverLimitSettings(undefined, { allowChannels: [{ id: '1' }] })).toEqual([]);
  });
});

describe('withHeterogeneousWakeOff', () => {
  const schema = [
    {
      key: 'settings',
      label: 'settings',
      properties: [{ default: false, key: 'botWakeDisabled', label: 'x', type: 'boolean' }],
      type: 'object',
    },
  ] as FieldSchema[];

  it('starts the wake switch off, over the schema default', () => {
    expect(
      mergeSettingsWithDefaults(schema, withHeterogeneousWakeOff(schema, {})).botWakeDisabled,
    ).toBe(true);
  });

  it('keeps a choice already made', () => {
    const settings = { botWakeDisabled: false };

    expect(withHeterogeneousWakeOff(schema, settings)).toBe(settings);
  });

  it('adds nothing on a platform without the switch', () => {
    expect(withHeterogeneousWakeOff([], { charLimit: 2000 })).toEqual({ charLimit: 2000 });
  });
});

describe('withNewBotReactionOff', () => {
  it('starts a new bot with status reactions off, over the schema default', () => {
    expect(
      mergeSettingsWithDefaults(discordSchema, withNewBotReactionOff(discordSchema, {}))
        .reactionMode,
    ).toBe('none');
  });

  it('leaves the schema default on, which a saved bot without the field reads as', () => {
    expect(extractSettingsDefaults(discordSchema).reactionMode).toBe('minimal');
  });

  it('keeps a mode the owner already picked', () => {
    const settings = { reactionMode: 'full' };

    expect(withNewBotReactionOff(discordSchema, settings)).toBe(settings);
  });

  it('adds nothing on a platform without status reactions', () => {
    expect(withNewBotReactionOff(slackSchema, { charLimit: 2000 })).toEqual({ charLimit: 2000 });
  });
});