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.

17.5 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import {
  assertWatchKeywordsWritable,
  invalidateBotAfterUpdate,
  mergeBotSettingsForCreate,
  mergeBotSettingsPatch,
} from '../agentBotProviderSettings';

const mockAssertBotFeatureAccess = vi.hoisted(() => vi.fn());
const mockStopClient = vi.hoisted(() => vi.fn());
const mockStartClient = vi.hoisted(() => vi.fn());
const inProcess = vi.hoisted(() => ({
  canHost: true,
  host: 'in-process' as 'in-process' | 'message-gateway',
}));

vi.mock('@/server/services/gateway/replyConnectionHost', () => ({
  resolveReplyConnectionHost: () => (inProcess.canHost ? inProcess.host : undefined),
}));
const mockInvalidateBot = vi.hoisted(() => vi.fn());
const gatewayState = vi.hoisted(() => ({ useMessageGateway: true }));

vi.mock('@/business/server/bot/featureAccess', () => ({
  assertBotFeatureAccess: mockAssertBotFeatureAccess,
}));

vi.mock('@/server/services/gateway', () => ({
  GatewayService: vi.fn(function () {
    return {
      startClient: mockStartClient,
      stopClient: mockStopClient,
      get useMessageGateway() {
        return gatewayState.useMessageGateway;
      },
    };
  }),
}));

vi.mock('../BotMessageRouter', () => ({
  getBotMessageRouter: () => ({ invalidateBot: mockInvalidateBot }),
}));

describe('assertWatchKeywordsWritable', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockAssertBotFeatureAccess.mockResolvedValue(undefined);
  });

  const base = { platform: 'discord', userId: 'user-1' };

  it('skips when settings are not part of the write', async () => {
    await assertWatchKeywordsWritable({ ...base, settings: undefined });
    expect(mockAssertBotFeatureAccess).not.toHaveBeenCalled();
  });

  it('skips when no keywords are configured (including clearing them)', async () => {
    await assertWatchKeywordsWritable({
      ...base,
      existingSettings: { watchKeywords: [{ keyword: 'bug' }] },
      settings: { watchKeywords: [] },
    });
    expect(mockAssertBotFeatureAccess).not.toHaveBeenCalled();
  });

  it('skips when the keyword set is unchanged (unrelated settings save)', async () => {
    await assertWatchKeywordsWritable({
      ...base,
      existingSettings: { watchKeywords: [{ keyword: 'bug' }] },
      settings: { charLimit: 500, watchKeywords: [{ keyword: 'bug' }] },
    });
    expect(mockAssertBotFeatureAccess).not.toHaveBeenCalled();
  });

  it('asserts feature access when keywords are first added', async () => {
    await assertWatchKeywordsWritable({
      ...base,
      applicationId: 'app-1',
      settings: { watchKeywords: [{ keyword: 'bug' }] },
      workspaceId: 'ws-1',
    });

    expect(mockAssertBotFeatureAccess).toHaveBeenCalledWith({
      action: 'manage',
      applicationId: 'app-1',
      feature: 'messageMonitoring',
      platform: 'discord',
      userId: 'user-1',
      workspaceId: 'ws-1',
    });
  });

  it('asserts feature access when the keyword set changes', async () => {
    await assertWatchKeywordsWritable({
      ...base,
      existingSettings: { watchKeywords: [{ keyword: 'bug' }] },
      settings: { watchKeywords: [{ keyword: 'bug' }, { keyword: 'outage' }] },
    });
    expect(mockAssertBotFeatureAccess).toHaveBeenCalledTimes(1);
  });

  it('skips pure removals that leave other keywords in place', async () => {
    // Downgrade path: a locked plan must be able to prune stale rows one at
    // a time, not only via a single clear-everything edit.
    await assertWatchKeywordsWritable({
      ...base,
      existingSettings: { watchKeywords: [{ keyword: 'bug' }, { keyword: 'outage' }] },
      settings: { watchKeywords: [{ keyword: 'bug' }] },
    });
    expect(mockAssertBotFeatureAccess).not.toHaveBeenCalled();
  });

  it('skips reorders of already-saved keywords', async () => {
    await assertWatchKeywordsWritable({
      ...base,
      existingSettings: { watchKeywords: [{ keyword: 'bug' }, { keyword: 'outage' }] },
      settings: { watchKeywords: [{ keyword: 'outage' }, { keyword: 'bug' }] },
    });
    expect(mockAssertBotFeatureAccess).not.toHaveBeenCalled();
  });

  it('asserts feature access when a keyword is edited in place', async () => {
    await assertWatchKeywordsWritable({
      ...base,
      existingSettings: { watchKeywords: [{ keyword: 'bug' }] },
      settings: { watchKeywords: [{ keyword: 'incident' }] },
    });
    expect(mockAssertBotFeatureAccess).toHaveBeenCalledTimes(1);
  });

  it('propagates the denial from assertBotFeatureAccess', async () => {
    mockAssertBotFeatureAccess.mockRejectedValueOnce(new Error('paid plan required'));

    await expect(
      assertWatchKeywordsWritable({
        ...base,
        settings: { watchKeywords: [{ keyword: 'bug' }] },
      }),
    ).rejects.toThrow('paid plan required');
  });
});

describe('invalidateBotAfterUpdate — monitoring capability flips', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    gatewayState.useMessageGateway = true;
    mockStopClient.mockResolvedValue(undefined);
    mockInvalidateBot.mockResolvedValue(undefined);
  });

  const target = {
    applicationId: 'app-1',
    platform: 'discord',
    userId: 'user-1',
  };

  it('stops the runtime when watch keywords go from none to some', async () => {
    await invalidateBotAfterUpdate(
      { ...target, settings: { botWakeDisabled: true } },
      { settings: { botWakeDisabled: true, watchKeywords: [{ keyword: 'bug' }] } },
    );

    expect(mockStopClient).toHaveBeenCalledWith('discord', 'app-1', 'user-1');
    expect(mockInvalidateBot).toHaveBeenCalledWith('discord', 'app-1');
  });

  it('stops the runtime when watch keywords are cleared', async () => {
    await invalidateBotAfterUpdate(
      { ...target, settings: { botWakeDisabled: true, watchKeywords: [{ keyword: 'bug' }] } },
      { settings: { botWakeDisabled: true, watchKeywords: [] } },
    );

    expect(mockStopClient).toHaveBeenCalledTimes(1);
  });

  it('does not stop the runtime on keyword flips without the external gateway', async () => {
    // Local/Vercel runtimes have no reconcile loop to restart a stopped
    // client — and they never consume the edge capability the flip feeds.
    gatewayState.useMessageGateway = false;

    await invalidateBotAfterUpdate(
      { ...target, settings: { botWakeDisabled: true } },
      { settings: { botWakeDisabled: true, watchKeywords: [{ keyword: 'bug' }] } },
    );

    expect(mockStopClient).not.toHaveBeenCalled();
    expect(mockInvalidateBot).toHaveBeenCalledTimes(1);
  });

  it('still stops the runtime for disable/rebind even without the external gateway', async () => {
    gatewayState.useMessageGateway = false;

    await invalidateBotAfterUpdate(
      { ...target, settings: { botWakeDisabled: true } },
      { enabled: false },
    );

    expect(mockStopClient).toHaveBeenCalledTimes(1);
  });

  it('does not stop webhook-mode runtimes on keyword flips (no auto-reconnect exists)', async () => {
    // Gateway reconciliation skips webhook-mode providers, so a stop here
    // would take the bot offline until the next manual config save.
    await invalidateBotAfterUpdate(
      { ...target, platform: 'telegram', settings: { botWakeDisabled: true } },
      { settings: { botWakeDisabled: true, watchKeywords: [{ keyword: 'bug' }] } },
    );

    expect(mockStopClient).not.toHaveBeenCalled();
    expect(mockInvalidateBot).toHaveBeenCalledTimes(1);
  });

  it('does not stop the runtime when settings switch the bot into webhook mode', async () => {
    await invalidateBotAfterUpdate(
      { ...target, settings: { botWakeDisabled: true } },
      { settings: { connectionMode: 'webhook', watchKeywords: [{ keyword: 'bug' }] } },
    );

    expect(mockStopClient).not.toHaveBeenCalled();
  });

  it('does not stop the runtime when keyword presence is unchanged', async () => {
    await invalidateBotAfterUpdate(
      { ...target, settings: { botWakeDisabled: true, watchKeywords: [{ keyword: 'bug' }] } },
      { settings: { watchKeywords: [{ keyword: 'outage' }] } },
    );

    expect(mockStopClient).not.toHaveBeenCalled();
    expect(mockInvalidateBot).toHaveBeenCalledTimes(1);
  });

  it('does not stop the runtime for settings-free updates', async () => {
    await invalidateBotAfterUpdate({ ...target, settings: { botWakeDisabled: true } }, {});

    expect(mockStopClient).not.toHaveBeenCalled();
    expect(mockInvalidateBot).toHaveBeenCalledTimes(1);
  });
});

describe('mergeBotSettingsPatch', () => {
  const stored = {
    allowFrom: [{ id: 'u-1', name: 'Ada' }],
    botWakeDisabled: false,
    botWakeScope: 'bot-threads',
    dmPolicy: 'pairing',
    userId: 'owner-1',
    watchKeywords: [{ keyword: 'bug' }],
  };

  // The agent maintains its own duties through `updateBot`: one key in, every other key kept.
  it('writes the duties without resetting the settings the caller did not send', () => {
    const merged = mergeBotSettingsPatch('discord', stored, {
      botWakeDuties: '评估群里描述的需求',
      botWakeDutiesThreshold: 0.7,
    });

    expect(merged).toMatchObject({
      ...stored,
      botWakeDuties: '评估群里描述的需求',
      botWakeDutiesThreshold: 0.7,
    });
  });

  it('replaces a key it carries whole, arrays included, and fills schema defaults', () => {
    const merged = mergeBotSettingsPatch('discord', stored, { allowFrom: [] });

    expect(merged?.allowFrom).toEqual([]);
    expect(merged?.userId).toBe('owner-1');
    // A field neither stored nor sent still gets its schema default.
    expect(merged?.botWakeDutiesThreshold).toBe(0.65);
  });

  it('writes a flat key the runtime reads, even for a bot configured with a nested botWake', () => {
    const merged = mergeBotSettingsPatch(
      'discord',
      { botWake: { enabled: true, rules: { scope: 'bot-threads' } }, userId: 'owner-1' },
      { botWakeDuties: 'Triage bug reports.' },
    );

    expect(merged).toMatchObject({
      botWakeDuties: 'Triage bug reports.',
      botWakeDisabled: false,
      botWakeScope: 'bot-threads',
      userId: 'owner-1',
    });
    expect(merged?.botWake).toBeUndefined();
  });

  it('starts from the defaults when nothing is stored', () => {
    expect(mergeBotSettingsPatch('discord', null, { botWakeDuties: 'x' })).toMatchObject({
      botWakeDuties: 'x',
      botWakeDisabled: false,
    });
  });
});

describe('mergeBotSettingsForCreate', () => {
  const dbWith = (agencyConfig: unknown) =>
    ({
      query: { agents: { findFirst: vi.fn(async () => ({ agencyConfig })) } },
    }) as never;

  it('leaves bot wake on for an ordinary agent', async () => {
    const settings = await mergeBotSettingsForCreate(dbWith({}), 'agt', 'discord', {});

    expect(settings?.botWakeDisabled).toBe(false);
  });

  it('switches bot wake off for a heterogeneous agent that did not choose', async () => {
    const settings = await mergeBotSettingsForCreate(
      dbWith({ heterogeneousProvider: { type: 'claude-code' } }),
      'agt',
      'discord',
      { dmPolicy: 'open' },
    );

    expect(settings).toMatchObject({ botWakeDisabled: true, dmPolicy: 'open' });
  });

  it('writes the defaults for an ordinary agent created without settings, reactions off', async () => {
    const settings = await mergeBotSettingsForCreate(dbWith({}), 'agt', 'discord', undefined);

    expect(settings).toMatchObject({ botWakeDisabled: false, reactionMode: 'none' });
  });

  it('creates a bot with status reactions off when the caller did not choose', async () => {
    const settings = await mergeBotSettingsForCreate(dbWith({}), 'agt', 'discord', {
      dmPolicy: 'open',
    });

    expect(settings).toMatchObject({ dmPolicy: 'open', reactionMode: 'none' });
  });

  it('keeps the status reaction mode the caller chose', async () => {
    const settings = await mergeBotSettingsForCreate(dbWith({}), 'agt', 'discord', {
      reactionMode: 'full',
    });

    expect(settings?.reactionMode).toBe('full');
  });

  it('turns reactions off for a heterogeneous agent created without settings too', async () => {
    const db = dbWith({ heterogeneousProvider: { type: 'claude-code' } });

    expect(await mergeBotSettingsForCreate(db, 'agt', 'discord', undefined)).toMatchObject({
      botWakeDisabled: true,
      reactionMode: 'none',
    });
  });

  it('adds no reaction mode on a platform without status reactions', async () => {
    const settings = await mergeBotSettingsForCreate(dbWith({}), 'agt', 'wechat', {});

    expect(settings).not.toHaveProperty('reactionMode');
  });

  describe('DingTalk behind the message gateway', () => {
    afterEach(() => {
      gatewayState.useMessageGateway = true;
    });

    it('defaults a new bot to webhook mode when the gateway cannot host its Stream connection', async () => {
      gatewayState.useMessageGateway = true;

      expect(
        (await mergeBotSettingsForCreate(dbWith({}), 'agt', 'dingtalk', undefined))?.connectionMode,
      ).toBe('webhook');
      expect(
        (await mergeBotSettingsForCreate(dbWith({}), 'agt', 'dingtalk', {}))?.connectionMode,
      ).toBe('webhook');
    });

    it('keeps the mode the caller chose', async () => {
      gatewayState.useMessageGateway = true;
      const settings = await mergeBotSettingsForCreate(dbWith({}), 'agt', 'dingtalk', {
        connectionMode: 'websocket',
      });

      expect(settings?.connectionMode).toBe('websocket');
    });

    it('keeps Stream mode without the gateway', async () => {
      gatewayState.useMessageGateway = false;
      const settings = await mergeBotSettingsForCreate(dbWith({}), 'agt', 'dingtalk', {});

      expect(settings?.connectionMode).toBe('websocket');
    });

    it('leaves platforms the gateway can host on their default', async () => {
      gatewayState.useMessageGateway = true;
      const settings = await mergeBotSettingsForCreate(dbWith({}), 'agt', 'discord', {});

      expect(settings?.connectionMode).not.toBe('webhook');
    });
  });

  it('keeps a heterogeneous agent opted in when the caller turned it on', async () => {
    const db = dbWith({ heterogeneousProvider: { type: 'codex' } });
    const settings = await mergeBotSettingsForCreate(db, 'agt', 'discord', {
      botWakeDisabled: false,
    });

    expect(settings?.botWakeDisabled).toBe(false);
  });
});

describe('status reactions of an existing bot', () => {
  it('keep the stored mode on a partial update', () => {
    expect(
      mergeBotSettingsPatch('discord', { reactionMode: 'minimal' }, { charLimit: 1000 }),
    ).toMatchObject({ charLimit: 1000, reactionMode: 'minimal' });
  });

  it('stay on for a row saved before the field existed', () => {
    expect(mergeBotSettingsPatch('discord', {}, { charLimit: 1000 })?.reactionMode).toBe('minimal');
  });
});

describe('invalidateBotAfterUpdate — credential changes on an in-process-only platform', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockStartClient.mockResolvedValue('started');
    inProcess.canHost = true;
    inProcess.host = 'in-process';
    gatewayState.useMessageGateway = false;
  });

  const existing = { applicationId: 'bot-1', platform: 'wecom', userId: 'user-1' };

  it('restarts the WeCom connection on the new secret', async () => {
    await invalidateBotAfterUpdate(existing, { credentials: { secret: 'new' } });

    expect(mockInvalidateBot).toHaveBeenCalledWith('wecom', 'bot-1');
    expect(mockStartClient).toHaveBeenCalledWith('wecom', 'bot-1', 'user-1');
  });

  it('leaves other platforms and settings-only saves alone', async () => {
    await invalidateBotAfterUpdate(
      { ...existing, platform: 'discord' },
      { credentials: { botToken: 'x' } },
    );
    await invalidateBotAfterUpdate(existing, { settings: { charLimit: 500 } });
    expect(mockStartClient).not.toHaveBeenCalled();
  });

  it('does not restart where WeCom cannot run, or when the bot is being turned off', async () => {
    inProcess.canHost = false;
    await invalidateBotAfterUpdate(existing, { credentials: { secret: 'new' } });
    inProcess.canHost = true;
    await invalidateBotAfterUpdate(existing, { credentials: { secret: 'new' }, enabled: false });

    expect(mockStartClient).not.toHaveBeenCalled();
    expect(mockStopClient).toHaveBeenCalledTimes(1);
  });

  it('does not restart when a full save resends the stored secret unchanged', async () => {
    await invalidateBotAfterUpdate(
      { ...existing, credentials: { botId: 'bot-1', secret: 'same' } },
      { credentials: { botId: 'bot-1', secret: 'same' } },
    );

    expect(mockInvalidateBot).toHaveBeenCalledWith('wecom', 'bot-1');
    expect(mockStartClient).not.toHaveBeenCalled();
  });

  it('restarts when the stored secret differs from the saved one', async () => {
    await invalidateBotAfterUpdate(
      { ...existing, credentials: { botId: 'bot-1', secret: 'old' } },
      { credentials: { botId: 'bot-1', secret: 'new' } },
    );

    expect(mockStartClient).toHaveBeenCalledWith('wecom', 'bot-1', 'user-1');
  });

  it('waits for the restart when the message gateway holds the connection', async () => {
    inProcess.host = 'message-gateway';
    let connected = false;
    mockStartClient.mockImplementationOnce(async () => {
      await Promise.resolve();
      connected = true;
      return 'started';
    });

    await invalidateBotAfterUpdate(
      { ...existing, credentials: { secret: 'old' } },
      { credentials: { secret: 'new' } },
    );

    // A serverless invocation may be frozen once it responds, so the gateway connect must land first.
    expect(connected).toBe(true);
  });

  it('keeps the save successful when the restart fails', async () => {
    mockStartClient.mockRejectedValueOnce(new Error('subscribe rejected'));
    await expect(
      invalidateBotAfterUpdate(existing, { credentials: { secret: 'bad' } }),
    ).resolves.toBeUndefined();
  });
});