-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2992 from ai16z-demirix/tests/plugin-autonome
feat (chore): plugin-autonome test config and coverage
- Loading branch information
Showing
3 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
packages/plugin-autonome/__tests__/actions/launchAgent.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
import launchAgent from '../../src/actions/launchAgent'; | ||
import axios from 'axios'; | ||
import { ModelClass, elizaLogger, composeContext, generateObjectDeprecated } from '@elizaos/core'; | ||
|
||
vi.mock('axios'); | ||
vi.mock('@elizaos/core', () => ({ | ||
elizaLogger: { | ||
log: vi.fn(), | ||
error: vi.fn(), | ||
}, | ||
composeContext: vi.fn().mockReturnValue('mock-context'), | ||
generateObjectDeprecated: vi.fn(), | ||
ModelClass: { | ||
LARGE: 'large', | ||
}, | ||
})); | ||
|
||
describe('launchAgent', () => { | ||
let mockRuntime; | ||
let mockMessage; | ||
let mockState; | ||
let mockCallback; | ||
|
||
beforeEach(() => { | ||
mockRuntime = { | ||
composeState: vi.fn().mockResolvedValue({}), | ||
updateRecentMessageState: vi.fn().mockResolvedValue({}), | ||
getSetting: vi.fn((key) => { | ||
if (key === 'AUTONOME_JWT_TOKEN') return 'mock-jwt-token'; | ||
if (key === 'AUTONOME_RPC') return 'mock-rpc-url'; | ||
return null; | ||
}), | ||
}; | ||
|
||
mockMessage = {}; | ||
mockState = {}; | ||
mockCallback = vi.fn(); | ||
|
||
vi.mocked(axios.post).mockReset(); | ||
vi.mocked(generateObjectDeprecated).mockReset(); | ||
vi.mocked(composeContext).mockReset().mockReturnValue('mock-context'); | ||
}); | ||
|
||
it('should validate correctly', async () => { | ||
const result = await launchAgent.validate(mockRuntime, mockMessage); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should have correct action properties', () => { | ||
expect(launchAgent.name).toBe('LAUNCH_AGENT'); | ||
expect(launchAgent.description).toBe('Launch an Eliza agent'); | ||
expect(launchAgent.similes).toContain('CREATE_AGENT'); | ||
expect(launchAgent.examples).toBeDefined(); | ||
expect(Array.isArray(launchAgent.examples)).toBe(true); | ||
}); | ||
|
||
it('should handle successful agent launch', async () => { | ||
const mockContent = { | ||
name: 'test-agent', | ||
config: '{"key": "value"}', | ||
}; | ||
|
||
vi.mocked(axios.post).mockResolvedValueOnce({ | ||
data: { | ||
app: { | ||
id: 'mock-app-id', | ||
}, | ||
}, | ||
}); | ||
|
||
vi.mocked(generateObjectDeprecated).mockResolvedValueOnce(mockContent); | ||
|
||
const result = await launchAgent.handler( | ||
mockRuntime, | ||
mockMessage, | ||
mockState, | ||
{}, | ||
mockCallback | ||
); | ||
|
||
expect(result).toBe(true); | ||
expect(mockCallback).toHaveBeenCalledWith({ | ||
text: `Successfully launch agent ${mockContent.name}`, | ||
content: { | ||
success: true, | ||
appId: 'https://dev.autonome.fun/autonome/mock-app-id/details', | ||
}, | ||
}); | ||
|
||
expect(axios.post).toHaveBeenCalledWith( | ||
'mock-rpc-url', | ||
{ | ||
name: mockContent.name, | ||
config: mockContent.config, | ||
creationMethod: 2, | ||
envList: {}, | ||
templateId: 'Eliza', | ||
}, | ||
{ | ||
headers: { | ||
Authorization: 'Bearer mock-jwt-token', | ||
'Content-Type': 'application/json', | ||
}, | ||
} | ||
); | ||
}); | ||
|
||
it('should handle invalid launch content', async () => { | ||
const mockInvalidContent = { | ||
invalidField: 'test', | ||
}; | ||
|
||
vi.mocked(generateObjectDeprecated).mockResolvedValueOnce(mockInvalidContent); | ||
|
||
const result = await launchAgent.handler( | ||
mockRuntime, | ||
mockMessage, | ||
mockState, | ||
{}, | ||
mockCallback | ||
); | ||
|
||
expect(result).toBe(false); | ||
expect(mockCallback).toHaveBeenCalledWith({ | ||
text: 'Unable to process launch agent request. Invalid content provided.', | ||
content: { error: 'Invalid launch agent content' }, | ||
}); | ||
}); | ||
|
||
it('should handle API error', async () => { | ||
const mockContent = { | ||
name: 'test-agent', | ||
config: '{"key": "value"}', | ||
}; | ||
|
||
vi.mocked(generateObjectDeprecated).mockResolvedValueOnce(mockContent); | ||
vi.mocked(axios.post).mockResolvedValueOnce(undefined); | ||
|
||
await launchAgent.handler( | ||
mockRuntime, | ||
mockMessage, | ||
mockState, | ||
{}, | ||
mockCallback | ||
); | ||
|
||
expect(mockCallback).toHaveBeenCalledWith({ | ||
text: 'Error launching agent: Cannot read properties of undefined (reading \'data\')', | ||
content: { error: 'Cannot read properties of undefined (reading \'data\')' }, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true, | ||
environment: 'node', | ||
include: ['__tests__/**/*.test.ts'], | ||
exclude: ['**/node_modules/**', '**/dist/**'], | ||
coverage: { | ||
provider: 'v8', | ||
reporter: ['text', 'json', 'html'], | ||
exclude: [ | ||
'node_modules/**', | ||
'dist/**', | ||
'**/*.d.ts', | ||
'**/*.test.ts', | ||
'coverage/**' | ||
] | ||
} | ||
} | ||
}); |