@@ -4,13 +4,7 @@ import {
4
4
formatActionNames ,
5
5
formatActions ,
6
6
} from "../actions" ;
7
- import {
8
- Action ,
9
- HandlerCallback ,
10
- IAgentRuntime ,
11
- Memory ,
12
- State ,
13
- } from "../types" ;
7
+ import { Action } from "../types" ;
14
8
15
9
describe ( "Actions" , ( ) => {
16
10
const mockActions : Action [ ] = [
@@ -25,98 +19,138 @@ describe("Actions", () => {
25
19
content : { text : "Hi {{user1}}!" , action : "wave" } ,
26
20
} ,
27
21
] ,
22
+ [
23
+ { user : "user1" , content : { text : "Hey {{user2}}, how are you?" } } ,
24
+ { user : "user2" , content : { text : "I'm good {{user1}}, thanks!" } } ,
25
+ ] ,
28
26
] ,
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" ) ; } ,
46
30
} ,
47
31
{
48
32
name : "farewell" ,
49
33
description : "Say goodbye" ,
50
34
examples : [
51
35
[
52
36
{ 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}}?" } } ,
53
50
{
54
51
user : "user2" ,
55
- content : { text : "See you later {{user1}}!" } ,
52
+ content : { text : "Of course {{user1}}, what do you need?" , action : "assist" }
56
53
} ,
57
54
] ,
58
55
] ,
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" ) ; } ,
76
59
} ,
77
60
] ;
78
61
79
62
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 ( / ^ u s e r \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 ( "" ) ;
86
79
} ) ;
87
80
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 ) ;
92
84
} ) ;
93
85
} ) ;
94
86
95
87
describe ( "formatActionNames" , ( ) => {
96
88
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 ( / ^ ( g r e e t | f a r e w e l l ) ( , ( g r e e t | f a r e w e l l ) ) ? $ / ) ;
91
+ } ) ;
92
+
93
+ it ( "should handle single action" , ( ) => {
94
+ const formatted = formatActionNames ( [ mockActions [ 0 ] ] ) ;
95
+ expect ( formatted ) . toBe ( "greet" ) ;
100
96
} ) ;
101
97
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 ( "" ) ;
105
101
} ) ;
106
102
} ) ;
107
103
108
104
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 ( / ^ ( g r e e t | f a r e w e l l ) : / ) ;
115
+ expect ( parts [ 1 ] ) . toMatch ( / ^ ( g r e e t | f a r e w e l l ) : / ) ;
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
+ } ) ;
115
148
} ) ;
116
149
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 ) ;
120
154
} ) ;
121
155
} ) ;
122
156
} ) ;
0 commit comments