-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exporting twitter action and schema and adding twitter tool test
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
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,58 @@ | ||
import { | ||
TwitterAction, | ||
TwitterActionSchemaAny, | ||
TwitterAgentkit, | ||
} from "@coinbase/cdp-agentkit-core"; | ||
import { TwitterTool } from "../twitter_tool"; | ||
import { z } from "zod"; | ||
|
||
const MOCK_DESCRIPTION = "Twitter Test Action"; | ||
const MOCK_NAME = "test_action"; | ||
|
||
describe("TwitterTool", () => { | ||
let mockAgentkit: jest.Mocked<TwitterAgentkit>; | ||
let mockAction: jest.Mocked<TwitterAction<TwitterActionSchemaAny>>; | ||
let twitterTool: TwitterTool<any>; | ||
|
||
beforeEach(() => { | ||
mockAgentkit = { | ||
run: jest.fn((action, args) => action.func(mockAgentkit, args)), | ||
} as unknown as jest.Mocked<TwitterAgentkit>; | ||
|
||
mockAction = { | ||
name: MOCK_NAME, | ||
description: MOCK_DESCRIPTION, | ||
argsSchema: z.object({ test_param: z.string() }), | ||
func: jest.fn().mockResolvedValue("success"), | ||
} as unknown as jest.Mocked<TwitterAction<TwitterActionSchemaAny>>; | ||
|
||
twitterTool = new TwitterTool(mockAction, mockAgentkit); | ||
}); | ||
|
||
test("should initialize with correct properties", () => { | ||
expect(twitterTool.name).toBe(MOCK_NAME); | ||
expect(twitterTool.description).toBe(MOCK_DESCRIPTION); | ||
expect(twitterTool.schema).toEqual(mockAction.argsSchema); | ||
}); | ||
|
||
test("should execute action with valid args", async () => { | ||
const args = { test_param: "test" }; | ||
const response = await twitterTool.call(args); | ||
|
||
expect(mockAction.func).toHaveBeenCalledWith(mockAgentkit, args); | ||
expect(response).toBe("success"); | ||
}); | ||
|
||
test("should handle schema validation errors", async () => { | ||
const invalidargs = { invalid_param: "test" }; | ||
await expect(twitterTool.call(invalidargs)).rejects.toThrow(); | ||
expect(mockAction.func).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("should return error message on action execution failure", async () => { | ||
mockAction.func.mockRejectedValue(new Error("Execution failed")); | ||
const args = { test_param: "test" }; | ||
const response = await twitterTool.call(args); | ||
expect(response).toContain("Error executing test_action: Execution failed"); | ||
}); | ||
}); |