Files
shumai/packages/agent/src/activities/agent.test.ts
T

110 lines
3.3 KiB
TypeScript
Raw Normal View History

2026-05-19 16:05:09 +08:00
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'
2026-05-19 16:05:09 +08:00
vi.mock('../index', async () => {
const actual = await vi.importActual('../index')
return {
...actual,
createAgentSession: vi.fn(),
fieldsToTypeBoxSchema: vi.fn(),
}
})
2026-05-19 16:05:09 +08:00
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<DatabaseSessionMetadata>,
harness: mockHarness as unknown as AgentHarness,
2026-05-19 16:05:09 +08:00
})
const context = {
agent: { id: 'b1', provider: { name: 'google' }, modelRef: { modelId: 'gemini' } },
dbProviders: [],
teamSkills: [],
allowedDomains: [],
} as unknown as AgentExecutionContext
const res = await agentChatActivity({
2026-05-19 16:05:09 +08:00
teamId: 't1',
agentId: 'b1',
message: 'Hi',
imageUrls: [],
projectId: 'p1',
folderId: 'f1',
sessionId: 'mock-session-id',
context,
2026-05-19 16:05:09 +08:00
})
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<string, unknown>) => Promise<unknown>
}>
}
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<DatabaseSessionMetadata>,
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)
2026-05-19 16:05:09 +08:00
})
})