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.

16.6 KB
Raw Blame
import type { EvalCaseEnvironment, ImportedMessage } from '@lobechat/types';
import { eq } from 'drizzle-orm';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { getTestDB } from '@/database/core/getTestDB';
import { AgentEvalBenchmarkModel, AgentEvalRunTopicModel } from '@/database/models/agentEval';
import {
  agentEvalBenchmarks,
  agentEvalDatasets,
  agentEvalRuns,
  agentEvalRunTopics,
  agentEvalTestCases,
  messages,
  topics,
  users,
} from '@/database/schemas';
import { AgentEvalRunService } from '@/server/services/agentEvalRun';

// Mock AiAgentService — created inside executeTrajectory
const mockExecAgent = vi.fn();
vi.mock('@/server/services/aiAgent', () => ({
  AiAgentService: vi.fn().mockImplementation(function () {
    return {
      execAgent: mockExecAgent,
    };
  }),
}));

// Mock appEnv so APP_URL is deterministic
vi.mock('@/envs/app', () => ({
  appEnv: { APP_URL: 'https://test.example.com' },
}));

// Mock AgentRuntimeService (required by service constructor path for checkAndHandleRunTimeout)
vi.mock('@/server/services/agentRuntime/AgentRuntimeService', () => ({
  AgentRuntimeService: vi.fn().mockImplementation(function () {
    return {
      interruptOperation: vi.fn().mockResolvedValue(true),
    };
  }),
}));

const serverDB = await getTestDB();
const userId = 'trajectory-test-user';

beforeEach(async () => {
  await serverDB.delete(messages);
  await serverDB.delete(agentEvalRunTopics);
  await serverDB.delete(agentEvalRuns);
  await serverDB.delete(agentEvalTestCases);
  await serverDB.delete(agentEvalDatasets);
  await serverDB.delete(agentEvalBenchmarks);
  await serverDB.delete(topics);
  await serverDB.delete(users);

  await serverDB.insert(users).values({ id: userId });

  mockExecAgent.mockReset();
});

afterEach(() => {
  vi.unstubAllEnvs();
});

/**
 * Helper: create benchmark → dataset → testCase → run (minimal chain for loadTrajectoryData tests)
 */
async function setupTrajectoryChain(opts?: {
  envPrompt?: string;
  input?: string;
  messages?: ImportedMessage[];
  sortOrder?: number;
  targetAgentId?: string | null;
  environment?: EvalCaseEnvironment;
}) {
  const benchmarkModel = new AgentEvalBenchmarkModel(serverDB, userId);
  const benchmark = await benchmarkModel.create({
    identifier: 'traj-benchmark',
    isSystem: false,
    name: 'Trajectory Benchmark',
    rubrics: [],
  });

  const [dataset] = await serverDB
    .insert(agentEvalDatasets)
    .values({
      benchmarkId: benchmark.id,
      evalConfig: opts?.envPrompt ? { envPrompt: opts.envPrompt } : undefined,
      identifier: 'traj-dataset',
      name: 'Trajectory Dataset',
      userId,
    })
    .returning();

  const [testCase] = await serverDB
    .insert(agentEvalTestCases)
    .values({
      content: {
        ...(opts?.environment && { environment: opts.environment }),
        expected: '42',
        input: opts?.input ?? 'What is 6*7?',
        ...(opts?.messages && { messages: opts.messages }),
      },
      datasetId: dataset.id,
      sortOrder: opts?.sortOrder ?? 1,
      userId,
    })
    .returning();

  const service = new AgentEvalRunService(serverDB, userId);
  const run = await service.createRun({
    datasetId: dataset.id,
    name: 'Trajectory Run',
    targetAgentId: opts?.targetAgentId ?? undefined,
  });

  return { benchmark, dataset, run, testCase };
}

const basicTrajectoryInput = (
  run: Pick<typeof agentEvalRuns.$inferSelect, 'datasetId' | 'id'>,
  testCase: { id: string },
): Parameters<AgentEvalRunService['executeTrajectory']>[0] => ({
  run: { datasetId: run.datasetId, targetAgentId: null },
  runId: run.id,
  testCase: { content: { input: 'test' }, sortOrder: 1 },
  testCaseId: testCase.id,
});

describe('AgentEvalRunService', () => {
  describe('createRun restored history', () => {
    it('should link a child to a parent in a later message batch', async () => {
      const history: ImportedMessage[] = Array.from({ length: 501 }, (_, index) => ({
        content: `history-${index}`,
        id: `source-${index}`,
        role: 'user' as const,
      }));
      history[0]!.parentId = 'source-500';

      const { run, testCase } = await setupTrajectoryChain({ messages: history });
      const [runTopic] = await serverDB
        .select()
        .from(agentEvalRunTopics)
        .where(eq(agentEvalRunTopics.runId, run.id));
      const restored = await serverDB
        .select({ content: messages.content, id: messages.id, parentId: messages.parentId })
        .from(messages)
        .where(eq(messages.topicId, runTopic.topicId));
      const byContent = new Map(restored.map((message) => [message.content, message]));

      expect(restored).toHaveLength(history.length);
      expect(byContent.get('history-0')?.parentId).toBe(byContent.get('history-500')?.id);
      expect(runTopic.testCaseId).toBe(testCase.id);
    });
  });

  // ─── loadTrajectoryData ─────────────────────────────────────────────
  describe('loadTrajectoryData', () => {
    it('should return run, testCase, and envPrompt when all exist', async () => {
      const { run, testCase } = await setupTrajectoryChain({ envPrompt: 'You are a math tutor.' });

      const service = new AgentEvalRunService(serverDB, userId);
      const data = await service.loadTrajectoryData(run.id, testCase.id);

      expect('error' in data).toBe(false);
      if (!('error' in data)) {
        expect(data.run.id).toBe(run.id);
        expect(data.testCase.id).toBe(testCase.id);
        expect(data.envPrompt).toBe('You are a math tutor.');
      }
    });

    it('should return undefined envPrompt when dataset has no envPrompt', async () => {
      const { run, testCase } = await setupTrajectoryChain();

      const service = new AgentEvalRunService(serverDB, userId);
      const data = await service.loadTrajectoryData(run.id, testCase.id);

      expect('error' in data).toBe(false);
      if (!('error' in data)) {
        expect(data.envPrompt).toBeUndefined();
      }
    });

    it('refuses legacy unbound forwarding', async () => {
      const { run, testCase } = await setupTrajectoryChain();
      await serverDB
        .update(agentEvalTestCases)
        .set({
          content: {
            input: 'test',
            environment: {
              toolForwarding: { memory: { endpoint: 'https://old.test/exec' } },
            },
          },
        })
        .where(eq(agentEvalTestCases.id, testCase.id));
      const service = new AgentEvalRunService(serverDB, userId);
      await expect(service.loadTrajectoryData(run.id, testCase.id)).rejects.toThrow(
        'Bind these mock services',
      );
    });

    it('should return error when run not found', async () => {
      const { testCase } = await setupTrajectoryChain();

      const service = new AgentEvalRunService(serverDB, userId);
      const data = await service.loadTrajectoryData('non-existent-run-id', testCase.id);

      expect(data).toEqual({ error: 'Run not found' });
    });

    it('should return error when test case not found', async () => {
      const { run } = await setupTrajectoryChain();

      const service = new AgentEvalRunService(serverDB, userId);
      const data = await service.loadTrajectoryData(run.id, 'non-existent-test-case-id');

      expect(data).toEqual({ error: 'Test case not found' });
    });
  });

  // ─── executeTrajectory ──────────────────────────────────────────────
  describe('executeTrajectory', () => {
    it('should create topic and runTopic, call execAgent, and store operationId on success', async () => {
      const { run, testCase } = await setupTrajectoryChain({ input: 'Hello world' });

      mockExecAgent.mockResolvedValue({ operationId: 'op-123' });

      const service = new AgentEvalRunService(serverDB, userId);
      const result = await service.executeTrajectory({
        run: { datasetId: run.datasetId, targetAgentId: null },
        runId: run.id,
        testCase: { content: { input: 'Hello world' }, sortOrder: testCase.sortOrder },
        testCaseId: testCase.id,
      });

      // Should return topicId without error
      expect(result).toHaveProperty('topicId');
      expect(result).not.toHaveProperty('error');

      // Verify topic was created
      const allTopics = await serverDB.select().from(topics);
      const evalTopic = allTopics.find((t) => t.id === result.topicId);
      expect(evalTopic).toBeDefined();
      expect(evalTopic?.trigger).toBe('eval');
      expect(evalTopic?.title).toContain(`[${(testCase.sortOrder ?? 0) + 1}]`);

      // Verify runTopic was created with 'running' status
      const runTopicModel = new AgentEvalRunTopicModel(serverDB, userId);
      const runTopic = await runTopicModel.findByRunAndTestCase(run.id, testCase.id);
      expect(runTopic).toBeDefined();
      expect(runTopic?.topicId).toBe(result.topicId);

      // Verify operationId was stored in evalResult, bound to this execution
      expect(runTopic?.evalResult).toMatchObject({
        executionId: expect.any(String),
        operationId: 'op-123',
      });

      // Verify execAgent was called with correct params
      expect(mockExecAgent).toHaveBeenCalledTimes(1);
      expect(mockExecAgent).toHaveBeenCalledWith(
        expect.objectContaining({
          autoStart: true,
          hooks: expect.arrayContaining([
            expect.objectContaining({
              id: 'eval-trajectory-complete',
              type: 'onComplete',
              webhook: {
                body: {
                  executionId: runTopic?.evalResult?.executionId,
                  runId: run.id,
                  testCaseId: testCase.id,
                  topicId: result.topicId,
                  userId,
                },
                delivery: 'qstash',
                url: '/api/workflows/agent-eval-run/on-trajectory-complete',
              },
            }),
          ]),
          prompt: 'Hello world',
          userInterventionConfig: { approvalMode: 'headless' },
        }),
      );
    });

    it('should pass envPrompt as evalContext when provided', async () => {
      const { run, testCase } = await setupTrajectoryChain();

      mockExecAgent.mockResolvedValue({ operationId: 'op-456' });

      const service = new AgentEvalRunService(serverDB, userId);
      await service.executeTrajectory({
        envPrompt: 'You are a math tutor.',
        run: { datasetId: run.datasetId, targetAgentId: null },
        runId: run.id,
        testCase: { content: { input: 'What is 6*7?' }, sortOrder: 1 },
        testCaseId: testCase.id,
      });

      expect(mockExecAgent).toHaveBeenCalledWith(
        expect.objectContaining({
          evalContext: { envPrompt: 'You are a math tutor.' },
        }),
      );
    });

    it('rejects an unbound mock before starting an attempt', async () => {
      const { run, testCase } = await setupTrajectoryChain();
      const service = new AgentEvalRunService(serverDB, userId);
      await expect(
        service.executeTrajectory({
          run: { datasetId: run.datasetId, targetAgentId: null },
          runId: run.id,
          testCase: { content: { input: 'Question' } },
          testCaseId: testCase.id,
          environment: { toolForwarding: { memory: { endpoint: 'https://old.test/exec' } } },
        }),
      ).rejects.toThrow('Bind these mock services');
      expect(mockExecAgent).not.toHaveBeenCalled();
    });

    it('should pass the dataset case id through evalRuntime', async () => {
      const { run, testCase } = await setupTrajectoryChain();

      mockExecAgent.mockResolvedValue({ operationId: 'op-case-id' });

      const service = new AgentEvalRunService(serverDB, userId);
      await service.executeTrajectory({
        run: { datasetId: run.datasetId, targetAgentId: null },
        runId: run.id,
        testCase: {
          content: { input: 'What is 6*7?' },
          metadata: { caseId: 'case-42' },
          sortOrder: testCase.sortOrder,
        },
        testCaseId: testCase.id,
      });

      expect(mockExecAgent).toHaveBeenCalledWith(
        expect.objectContaining({
          appContext: expect.objectContaining({ suppressSignal: true }),
          evalRuntime: {
            attemptId: expect.any(String),
            caseId: 'case-42',
            memory: {
              fixtureHash: expect.any(String),
              runId: expect.any(String),
              topicId: expect.any(String),
            },
          },
        }),
      );
    });

    it('should not include evalContext when envPrompt is undefined', async () => {
      const { run, testCase } = await setupTrajectoryChain();

      mockExecAgent.mockResolvedValue({});

      const service = new AgentEvalRunService(serverDB, userId);
      await service.executeTrajectory(basicTrajectoryInput(run, testCase));

      const callArgs = mockExecAgent.mock.calls[0][0];
      expect(callArgs).not.toHaveProperty('evalContext');
    });

    it('should handle execAgent returning no operationId', async () => {
      const { run, testCase } = await setupTrajectoryChain();

      mockExecAgent.mockResolvedValue({}); // no operationId

      const service = new AgentEvalRunService(serverDB, userId);
      const result = await service.executeTrajectory(basicTrajectoryInput(run, testCase));

      expect(result).toHaveProperty('topicId');
      expect(result).not.toHaveProperty('error');

      // RunTopic should exist but evalResult should not have operationId stored
      const runTopicModel = new AgentEvalRunTopicModel(serverDB, userId);
      const runTopic = await runTopicModel.findByRunAndTestCase(run.id, testCase.id);
      expect(runTopic?.evalResult?.operationId).toBeUndefined();
    });

    it('should mark runTopic as error and return error when execAgent throws', async () => {
      const { run, testCase } = await setupTrajectoryChain();

      mockExecAgent.mockRejectedValue(new Error('Connection refused'));

      const service = new AgentEvalRunService(serverDB, userId);
      const result = await service.executeTrajectory(basicTrajectoryInput(run, testCase));

      // Should return error and topicId
      expect(result).toHaveProperty('error', 'Connection refused');
      expect(result).toHaveProperty('topicId');

      // RunTopic should be marked as error
      const runTopicModel = new AgentEvalRunTopicModel(serverDB, userId);
      const runTopic = await runTopicModel.findByRunAndTestCase(run.id, testCase.id);
      expect(runTopic?.status).toBe('error');
      expect(runTopic?.passed).toBe(false);
      expect(runTopic?.score).toBe(0);
      expect(runTopic?.evalResult).toMatchObject({
        completionReason: 'error',
        error: 'Connection refused',
        rubricScores: [],
      });
    });

    it('should handle non-Error thrown value', async () => {
      const { run, testCase } = await setupTrajectoryChain();

      mockExecAgent.mockRejectedValue('some string error');

      const service = new AgentEvalRunService(serverDB, userId);
      const result = await service.executeTrajectory(basicTrajectoryInput(run, testCase));

      expect(result).toHaveProperty('error', 'Agent execution failed to start');
    });

    it('should use an ordinal topic title without evaluation metadata', async () => {
      const { run, testCase } = await setupTrajectoryChain({
        input: 'A very long input that should be truncated at some point',
        sortOrder: 4,
      });

      mockExecAgent.mockResolvedValue({});

      const service = new AgentEvalRunService(serverDB, userId);
      const result = await service.executeTrajectory({
        run: { datasetId: run.datasetId, targetAgentId: null },
        runId: run.id,
        testCase: {
          content: { input: 'A very long input that should be truncated at some point' },
          sortOrder: 4,
        },
        testCaseId: testCase.id,
      });

      const allTopics = await serverDB.select().from(topics);
      const evalTopic = allTopics.find((t) => t.id === result.topicId);
      expect(evalTopic?.title).toContain('[5]');
      expect(evalTopic?.title).toContain('A very long input that should be truncated at so');
    });

    it('should use empty prompt when testCase.content.input is undefined', async () => {
      const { run, testCase } = await setupTrajectoryChain();

      mockExecAgent.mockResolvedValue({});

      const service = new AgentEvalRunService(serverDB, userId);
      await service.executeTrajectory({
        run: { datasetId: run.datasetId, targetAgentId: null },
        runId: run.id,
        testCase: { content: {}, sortOrder: null },
        testCaseId: testCase.id,
      });

      expect(mockExecAgent).toHaveBeenCalledWith(expect.objectContaining({ prompt: '' }));
    });
  });
});