mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 09:26:58 +00:00
- Use generated Prisma types to precisely define AgentExecutionContext properties - Make sessionId public in DatabaseSessionStorage and use instanceof type guard - Explicitly type getAgentChatContextActivity and getAgentAutofillContextActivity returns - Clean up all 'as any' casts and ESLint disable comments - Refactor agent.test.ts to cast Session with DatabaseSessionMetadata
110 lines
3.3 KiB
TypeScript
110 lines
3.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { agentChatActivity, autofillAiActivity, type AgentExecutionContext } from './agent'
|
|
import * as piAgent from '@/agent'
|
|
import { type AgentHarness, type Session } from '@earendil-works/pi-agent-core'
|
|
import { type DatabaseSessionMetadata } from '@/agent/database-session-storage'
|
|
|
|
vi.mock('@/agent', async () => {
|
|
const actual = await vi.importActual('@/agent')
|
|
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<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 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<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)
|
|
})
|
|
})
|