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.

1.6 KB
Raw Blame
// @vitest-environment node
import { describe, expect, it, vi } from 'vitest';

import { agentDocumentsRuntime } from './agentDocuments';

const listDocuments = vi.fn();
const countChildren = vi.fn().mockResolvedValue(new Map([['documents-1', 3]]));

vi.mock('@/server/services/agentDocuments', () => ({
  AgentDocumentsService: vi.fn().mockImplementation(function () {
    return {
      countChildren,
      listDocuments,
    };
  }),
}));

describe('agentDocumentsRuntime', () => {
  describe('listDocuments', () => {
    it('should preserve document filenames in runtime output', async () => {
      listDocuments.mockResolvedValue([
        { filename: 'rules.md', id: 'doc-1', title: 'Rules' },
        { filename: 'notes.txt', id: 'doc-2', title: 'Notes' },
      ]);

      const runtime = agentDocumentsRuntime.factory({
        serverDB: {} as never,
        toolManifestMap: {},
        userId: 'user-1',
      });
      const result = await runtime.listDocuments({}, { agentId: 'agent-1' });

      // The agent runtime opts into seeing the archived `.tool-results`.
      expect(listDocuments).toHaveBeenCalledWith('agent-1', 'all', {
        includeArchivedToolResults: true,
      });
      expect(result).toEqual({
        content: JSON.stringify([
          { filename: 'rules.md', id: 'doc-1', title: 'Rules' },
          { filename: 'notes.txt', id: 'doc-2', title: 'Notes' },
        ]),
        state: {
          documents: [
            { filename: 'rules.md', id: 'doc-1', title: 'Rules' },
            { filename: 'notes.txt', id: 'doc-2', title: 'Notes' },
          ],
        },
        success: true,
      });
    });
  });
});