import { describe, it, expect, vi, beforeEach } from 'vitest' import { agentChatActivity, autofillAiActivity, type AgentExecutionContext } from './agent' import * as piAgent from '../index' import { type AgentHarness, type Session } from '@earendil-works/pi-agent-core' import { type DatabaseSessionMetadata } from '../database-session-storage' vi.mock('../index', async () => { const actual = await vi.importActual('../index') return { ...actual, createAgentSession: vi.fn(), fieldsToTypeBoxSchema: vi.fn(), } }) describe('Agent Activities', () => { beforeEach(() => { vi.clearAllMocks() }) it('should call agentChatActivity and prompt the harness', async () => { const mockHarness = { prompt: vi.fn().mockResolvedValue({ content: [{ type: 'text', text: 'AI response' }], usage: { input: 10, output: 20 }, }), } const mockSession = { getEntries: vi.fn().mockResolvedValue([]), getStorage: vi.fn().mockReturnValue({ sessionId: 'mock-session-id' }), } vi.mocked(piAgent.createAgentSession).mockResolvedValue({ session: mockSession as unknown as Session, harness: mockHarness as unknown as AgentHarness, }) const context = { agent: { id: 'b1', provider: { name: 'google' }, modelRef: { modelId: 'gemini' } }, dbProviders: [], teamSkills: [], allowedDomains: [], } as unknown as AgentExecutionContext const res = await agentChatActivity({ teamId: 't1', agentId: 'b1', message: 'Hi', imageUrls: [], projectId: 'p1', folderId: 'f1', sessionId: 'mock-session-id', context, }) expect(res.text).toBe('AI response') expect(res.usage.inputTokens).toBe(10) expect(res.usage.outputTokens).toBe(20) expect(piAgent.createAgentSession).toHaveBeenCalled() }) it('should call autofillAiActivity and run autofill tool', async () => { const mockHarness = { prompt: vi.fn().mockResolvedValue({ content: [{ type: 'text', text: 'Captured' }], usage: { input: 5, output: 5 }, }), } const mockSession = { getEntries: vi.fn().mockResolvedValue([]), getStorage: vi.fn().mockReturnValue({ sessionId: 'mock-session-id' }), } vi.mocked(piAgent.createAgentSession).mockImplementation(async (config: unknown) => { // Simulate tool execution const params = config as { customTools: Array<{ name: string execute: (id: string, args: Record) => Promise }> } const tool = params.customTools.find((t) => t.name === 'autofill_metadata') if (tool) { await tool.execute('1', { f1: 'val' }) } return { session: mockSession as unknown as Session, harness: mockHarness as unknown as AgentHarness, } }) const context = { agent: { id: 'b1', provider: { name: 'google' }, modelRef: { modelId: 'gemini' } }, dbProviders: [], teamSkills: [], allowedDomains: [], } as unknown as AgentExecutionContext const res = await autofillAiActivity({ teamId: 't1', images: [], fields: [{ id: 'f1', config: { name: 'F1', type: 'text' } }], context, }) expect(res.text).toBe('{"f1":"val"}') expect(res.usage.inputTokens).toBe(5) }) })