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.

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

import type { FtsSearchSyncDrainResult } from '../../apps/server/src/services/ftsSearchSync';
import { runElasticsearchFtsSearchSync, runElasticsearchFtsSearchSyncCli } from './index';

const drainResult = (
  overrides: Partial<FtsSearchSyncDrainResult> = {},
): FtsSearchSyncDrainResult => ({
  acknowledged: 1,
  bulkBytes: 100,
  bulkItems: 1,
  bulkRequests: 1,
  bulkRequestSamples: [],
  claimed: 1,
  dead: 0,
  failed: 0,
  hasMore: false,
  released: 0,
  ...overrides,
});

const createRuntime = (results: FtsSearchSyncDrainResult[]) => {
  const drainOnce = vi.fn();
  for (const result of results) drainOnce.mockResolvedValueOnce(result);
  const runtime = {
    getFtsSearchSyncService: () => ({
      drainOnce,
      hasDeadLetters: vi.fn().mockResolvedValue(false),
    }),
    verifyFtsSearchSyncReadiness: vi.fn().mockResolvedValue(undefined),
  };
  return { drainOnce, runtime };
};

describe('runElasticsearchFtsSearchSync', () => {
  beforeEach(() => vi.clearAllMocks());

  it('drains until the queue is empty within the configured bound', async () => {
    const { drainOnce, runtime } = createRuntime([
      drainResult({ hasMore: true }),
      drainResult({ acknowledged: 2, bulkItems: 2, claimed: 2 }),
    ]);

    await expect(
      runElasticsearchFtsSearchSync({ loadRuntime: async () => runtime, maxSteps: 8 }),
    ).resolves.toMatchObject({ acknowledged: 3, claimed: 3, hasMore: false, steps: 2 });
    expect(runtime.verifyFtsSearchSyncReadiness).toHaveBeenCalledOnce();
    expect(drainOnce).toHaveBeenCalledTimes(2);
  });

  it('stops at the configured bound and reports remaining work', async () => {
    const { drainOnce, runtime } = createRuntime([drainResult({ hasMore: true })]);

    await expect(
      runElasticsearchFtsSearchSync({ loadRuntime: async () => runtime, maxSteps: 1 }),
    ).resolves.toMatchObject({ hasMore: true, steps: 1 });
    expect(drainOnce).toHaveBeenCalledOnce();
  });

  it('stops after the current step when the stop signal aborts mid-run', async () => {
    const controller = new AbortController();
    const { drainOnce, runtime } = createRuntime([]);
    drainOnce
      .mockImplementationOnce(async () => {
        controller.abort();
        return drainResult({ hasMore: true });
      })
      .mockResolvedValueOnce(drainResult({ hasMore: true }));

    await expect(
      runElasticsearchFtsSearchSync({
        loadRuntime: async () => runtime,
        maxSteps: 8,
        stopSignal: controller.signal,
      }),
    ).resolves.toMatchObject({ hasMore: true, steps: 1 });
    // The claimed work of the finished step is settled; no further step is started.
    expect(drainOnce).toHaveBeenCalledOnce();
  });

  it('fails before draining when durable dead letters already exist', async () => {
    const { runtime } = createRuntime([]);
    const service = runtime.getFtsSearchSyncService();
    service.hasDeadLetters = vi.fn().mockResolvedValue(true);
    runtime.getFtsSearchSyncService = () => service;

    await expect(
      runElasticsearchFtsSearchSync({ loadRuntime: async () => runtime, maxSteps: 1 }),
    ).rejects.toThrow('blocked by existing dead-letter work');
    expect(service.drainOnce).not.toHaveBeenCalled();
  });

  it('fails when dead-letter work appears concurrently after draining', async () => {
    const { runtime } = createRuntime([drainResult()]);
    const service = runtime.getFtsSearchSyncService();
    service.hasDeadLetters = vi.fn().mockResolvedValueOnce(false).mockResolvedValueOnce(true);
    runtime.getFtsSearchSyncService = () => service;

    await expect(
      runElasticsearchFtsSearchSync({ loadRuntime: async () => runtime, maxSteps: 1 }),
    ).rejects.toThrow('blocked by dead-letter work');
  });

  it.each([
    [drainResult({ dead: 1, failed: 1 }), 'created dead-letter work'],
    [drainResult({ failed: 1 }), 'left retryable failed work'],
  ])('fails on unsettled drain results', async (result, message) => {
    const { runtime } = createRuntime([result]);

    await expect(
      runElasticsearchFtsSearchSync({ loadRuntime: async () => runtime, maxSteps: 1 }),
    ).rejects.toThrow(message);
  });
});

describe('runElasticsearchFtsSearchSyncCli', () => {
  it('requires explicit acknowledgement before loading the runtime', async () => {
    const loadRuntime = vi.fn();
    const logError = vi.fn();

    await expect(
      runElasticsearchFtsSearchSyncCli({ args: [], loadRuntime, logError }),
    ).resolves.toBe(1);
    expect(loadRuntime).not.toHaveBeenCalled();
    expect(logError).toHaveBeenCalledWith(
      'Elasticsearch full-text search sync failed:',
      'Elasticsearch full-text search sync requires --yes after reviewing its documented effects',
    );
  });

  it('returns zero and emits only bounded numeric summaries after a successful drain', async () => {
    const { runtime } = createRuntime([
      drainResult({
        bulkRequestSamples: [
          {
            bytes: 100,
            durationMs: 10,
            entities: { messages: { bytes: 100, items: 1, result: 'success' } },
            items: 1,
            messageTombstoneItems: 1,
            result: 'success',
          },
        ],
      }),
    ]);
    const logSuccess = vi.fn();

    await expect(
      runElasticsearchFtsSearchSyncCli({
        args: ['--yes'],
        loadRuntime: async () => runtime,
        logSuccess,
      }),
    ).resolves.toBe(0);
    expect(logSuccess).toHaveBeenLastCalledWith(
      expect.stringContaining('"type":"fts_search_sync_completed"'),
    );
    expect(logSuccess).toHaveBeenLastCalledWith(
      expect.stringContaining('"messageTombstoneItems":1'),
    );
  });

  it('keeps draining in interval mode until the stop signal aborts', async () => {
    const { drainOnce, runtime } = createRuntime([
      drainResult({ hasMore: true }),
      drainResult(),
      drainResult(),
    ]);
    const controller = new AbortController();
    const sleep = vi.fn(async (_milliseconds: number, signal: AbortSignal) => {
      expect(signal).toBe(controller.signal);
      if (sleep.mock.calls.length === 2) controller.abort();
    });
    const logSuccess = vi.fn();

    await expect(
      runElasticsearchFtsSearchSyncCli({
        args: ['--interval-seconds=15', '--max-steps=1', '--yes'],
        loadRuntime: async () => runtime,
        logSuccess,
        sleep,
        stopSignal: controller.signal,
      }),
    ).resolves.toBe(0);

    // Run 1 reports hasMore, so the worker continues immediately; runs 2 and 3 sleep.
    expect(drainOnce).toHaveBeenCalledTimes(3);
    expect(sleep).toHaveBeenCalledTimes(2);
    expect(sleep).toHaveBeenCalledWith(15_000, controller.signal);
    expect(logSuccess).toHaveBeenLastCalledWith(
      JSON.stringify({ type: 'fts_search_sync_interval_stopped' }),
    );
  });

  it('exits non-zero from interval mode when a bounded run fails', async () => {
    const { runtime } = createRuntime([drainResult({ dead: 1, failed: 1 })]);
    const sleep = vi.fn();
    const logError = vi.fn();

    await expect(
      runElasticsearchFtsSearchSyncCli({
        args: ['--interval-seconds=15', '--yes'],
        loadRuntime: async () => runtime,
        logError,
        sleep,
        stopSignal: new AbortController().signal,
      }),
    ).resolves.toBe(1);
    expect(sleep).not.toHaveBeenCalled();
    expect(logError).toHaveBeenCalledWith(
      'Elasticsearch full-text search sync failed:',
      'Elasticsearch full-text search sync created dead-letter work',
    );
  });
});