Skip to content

Commit c7ae1f5

Browse files
committed
test: adding tests. changed files actions.test.ts, messages.test.ts, models.test.ts
1 parent d0c6c55 commit c7ae1f5

File tree

3 files changed

+408
-98
lines changed

3 files changed

+408
-98
lines changed

packages/core/src/tests/actions.test.ts

+101-67
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ import {
44
formatActionNames,
55
formatActions,
66
} from "../actions";
7-
import {
8-
Action,
9-
HandlerCallback,
10-
IAgentRuntime,
11-
Memory,
12-
State,
13-
} from "../types";
7+
import { Action } from "../types";
148

159
describe("Actions", () => {
1610
const mockActions: Action[] = [
@@ -25,98 +19,138 @@ describe("Actions", () => {
2519
content: { text: "Hi {{user1}}!", action: "wave" },
2620
},
2721
],
22+
[
23+
{ user: "user1", content: { text: "Hey {{user2}}, how are you?" } },
24+
{ user: "user2", content: { text: "I'm good {{user1}}, thanks!" } },
25+
],
2826
],
29-
similes: [],
30-
handler: function (
31-
_runtime: IAgentRuntime,
32-
_message: Memory,
33-
_state?: State,
34-
_options?: { [key: string]: unknown },
35-
_callback?: HandlerCallback
36-
): Promise<unknown> {
37-
throw new Error("Function not implemented.");
38-
},
39-
validate: function (
40-
_runtime: IAgentRuntime,
41-
_message: Memory,
42-
_state?: State
43-
): Promise<boolean> {
44-
throw new Error("Function not implemented.");
45-
},
27+
similes: ["say hi", "welcome"],
28+
handler: async () => { throw new Error("Not implemented"); },
29+
validate: async () => { throw new Error("Not implemented"); },
4630
},
4731
{
4832
name: "farewell",
4933
description: "Say goodbye",
5034
examples: [
5135
[
5236
{ user: "user1", content: { text: "Goodbye {{user2}}!" } },
37+
{ user: "user2", content: { text: "Bye {{user1}}!" } },
38+
],
39+
],
40+
similes: ["say bye", "leave"],
41+
handler: async () => { throw new Error("Not implemented"); },
42+
validate: async () => { throw new Error("Not implemented"); },
43+
},
44+
{
45+
name: "help",
46+
description: "Get assistance",
47+
examples: [
48+
[
49+
{ user: "user1", content: { text: "Can you help me {{user2}}?" } },
5350
{
5451
user: "user2",
55-
content: { text: "See you later {{user1}}!" },
52+
content: { text: "Of course {{user1}}, what do you need?", action: "assist" }
5653
},
5754
],
5855
],
59-
similes: [],
60-
handler: function (
61-
_runtime: IAgentRuntime,
62-
_message: Memory,
63-
_state?: State,
64-
_options?: { [key: string]: unknown },
65-
_callback?: HandlerCallback
66-
): Promise<unknown> {
67-
throw new Error("Function not implemented.");
68-
},
69-
validate: function (
70-
_runtime: IAgentRuntime,
71-
_message: Memory,
72-
_state?: State
73-
): Promise<boolean> {
74-
throw new Error("Function not implemented.");
75-
},
56+
similes: ["assist", "support"],
57+
handler: async () => { throw new Error("Not implemented"); },
58+
validate: async () => { throw new Error("Not implemented"); },
7659
},
7760
];
7861

7962
describe("composeActionExamples", () => {
80-
it("should generate the correct number of examples", () => {
81-
const result = composeActionExamples(mockActions, 1);
82-
const exampleLines = result
83-
.split("\n")
84-
.filter((line) => line.length > 0);
85-
expect(exampleLines.length).toBe(2); // Each example has 2 messages
63+
it("should generate examples with correct format", () => {
64+
const examples = composeActionExamples(mockActions, 1);
65+
const lines = examples.trim().split("\n");
66+
expect(lines.length).toBeGreaterThan(0);
67+
expect(lines[0]).toMatch(/^user\d: .+/);
68+
});
69+
70+
it("should replace user placeholders with generated names", () => {
71+
const examples = composeActionExamples(mockActions, 1);
72+
expect(examples).not.toContain("{{user1}}");
73+
expect(examples).not.toContain("{{user2}}");
74+
});
75+
76+
it("should handle empty actions array", () => {
77+
const examples = composeActionExamples([], 5);
78+
expect(examples).toBe("");
8679
});
8780

88-
it("should replace placeholder names with generated names", () => {
89-
const result = composeActionExamples(mockActions, 1);
90-
expect(result).not.toContain("{{user1}}");
91-
expect(result).not.toContain("{{user2}}");
81+
it("should handle count larger than available examples", () => {
82+
const examples = composeActionExamples(mockActions, 10);
83+
expect(examples.length).toBeGreaterThan(0);
9284
});
9385
});
9486

9587
describe("formatActionNames", () => {
9688
it("should format action names correctly", () => {
97-
const result = formatActionNames(mockActions);
98-
const names = result.split(", ").sort();
99-
expect(names).toEqual(["farewell", "greet"].sort());
89+
const formatted = formatActionNames([mockActions[0], mockActions[1]]);
90+
expect(formatted).toMatch(/^(greet|farewell)(, (greet|farewell))?$/);
91+
});
92+
93+
it("should handle single action", () => {
94+
const formatted = formatActionNames([mockActions[0]]);
95+
expect(formatted).toBe("greet");
10096
});
10197

102-
it("should return empty string for empty array", () => {
103-
const result = formatActionNames([]);
104-
expect(result).toBe("");
98+
it("should handle empty actions array", () => {
99+
const formatted = formatActionNames([]);
100+
expect(formatted).toBe("");
105101
});
106102
});
107103

108104
describe("formatActions", () => {
109-
it("should format actions with descriptions correctly", () => {
110-
const result = formatActions(mockActions);
111-
const formattedActions = result.split(",\n").sort();
112-
expect(formattedActions).toEqual(
113-
["farewell: Say goodbye", "greet: Greet someone"].sort()
114-
);
105+
it("should format actions with descriptions", () => {
106+
const formatted = formatActions([mockActions[0]]);
107+
expect(formatted).toBe("greet: Greet someone");
108+
});
109+
110+
it("should include commas and newlines between multiple actions", () => {
111+
const formatted = formatActions([mockActions[0], mockActions[1]]);
112+
const parts = formatted.split(",\n");
113+
expect(parts.length).toBe(2);
114+
expect(parts[0]).toMatch(/^(greet|farewell): /);
115+
expect(parts[1]).toMatch(/^(greet|farewell): /);
116+
});
117+
118+
it("should handle empty actions array", () => {
119+
const formatted = formatActions([]);
120+
expect(formatted).toBe("");
121+
});
122+
});
123+
124+
describe("Action Structure", () => {
125+
it("should validate action structure", () => {
126+
mockActions.forEach(action => {
127+
expect(action).toHaveProperty("name");
128+
expect(action).toHaveProperty("description");
129+
expect(action).toHaveProperty("examples");
130+
expect(action).toHaveProperty("similes");
131+
expect(action).toHaveProperty("handler");
132+
expect(action).toHaveProperty("validate");
133+
expect(Array.isArray(action.examples)).toBe(true);
134+
expect(Array.isArray(action.similes)).toBe(true);
135+
});
136+
});
137+
138+
it("should validate example structure", () => {
139+
mockActions.forEach(action => {
140+
action.examples.forEach(example => {
141+
example.forEach(message => {
142+
expect(message).toHaveProperty("user");
143+
expect(message).toHaveProperty("content");
144+
expect(message.content).toHaveProperty("text");
145+
});
146+
});
147+
});
115148
});
116149

117-
it("should return empty string for empty array", () => {
118-
const result = formatActions([]);
119-
expect(result).toBe("");
150+
it("should have unique action names", () => {
151+
const names = mockActions.map(action => action.name);
152+
const uniqueNames = new Set(names);
153+
expect(names.length).toBe(uniqueNames.size);
120154
});
121155
});
122156
});

0 commit comments

Comments
 (0)