@@ -8,6 +8,15 @@ function projectRoot() {
8
8
return path . join ( import . meta. dirname , ".." ) ;
9
9
}
10
10
11
+ function log ( message ) {
12
+ console . log ( message ) ;
13
+ }
14
+
15
+ function logError ( error ) {
16
+ log ( "ERROR: " + error . message ) ;
17
+ log ( error ) ; // Print stack trace
18
+ }
19
+
11
20
async function runProcess ( command , args = [ ] , directory = projectRoot ( ) ) {
12
21
try {
13
22
throw new Exception ( "Not implemented yet" ) ; // TODO
@@ -19,12 +28,12 @@ async function runProcess(command, args = [], directory = projectRoot()) {
19
28
}
20
29
21
30
async function installProjectDependencies ( ) {
22
- console . log ( 'Installing dependencies...' ) ;
31
+ log ( 'Installing dependencies...' ) ;
23
32
return await runProcess ( 'pnpm' , [ 'install' , '-r' ] ) ;
24
33
}
25
34
26
35
async function buildProject ( ) {
27
- console . log ( 'Building project...' ) ;
36
+ log ( 'Building project...' ) ;
28
37
return await runProcess ( 'pnpm' , [ 'build' ] ) ;
29
38
}
30
39
@@ -36,59 +45,73 @@ async function writeEnvFile(entries) {
36
45
}
37
46
38
47
async function startAgent ( character = DEFAULT_CHARACTER ) {
39
- console . log ( `Starting agent for character: ${ character } ` ) ;
40
- const proc = spawn ( ' pnpm' , [ ' start' , `--character=characters/${ character } .character.json` , '--non-interactive' ] , { shell : true , "stdio" : "inherit" } ) ;
48
+ log ( `Starting agent for character: ${ character } ` ) ;
49
+ const proc = spawn ( " pnpm" , [ " start" , `--character=characters/${ character } .character.json` , '--non-interactive' ] , { shell : true , "stdio" : "inherit" } ) ;
41
50
log ( `proc=${ JSON . stringify ( proc ) } ` ) ;
42
51
43
- sleep ( 60000 ) ; // Wait for server to be ready
52
+ const startTime = Date . now ( ) ;
53
+ const url = "http://127.0.0.1:3000/" ;
54
+ while ( true ) {
55
+ try {
56
+ const response = await fetch ( url , { method : "GET" } ) ;
57
+ if ( response . ok ) break ;
58
+ } catch ( error ) { }
59
+ if ( Date . now ( ) - startTime > 120000 ) {
60
+ throw new Error ( "Timeout 120s waiting for server to start" ) ;
61
+ } else {
62
+ log ( "Waiting for the server to be ready..." ) ;
63
+ await sleep ( 1000 ) ;
64
+ }
65
+ }
66
+ log ( "Server is ready" ) ;
67
+ await sleep ( 1000 ) ;
44
68
return proc ;
45
69
}
46
70
47
71
async function stopAgent ( proc ) {
48
- console . log ( 'Stopping agent...' ) ;
49
- proc . kill ( 'SIGTERM' ) ;
72
+ log ( "Stopping agent..." + JSON . stringify ( proc ) ) ;
73
+ const q = proc . kill ( "SIGKILL" ) ;
74
+ console . log ( q ) ;
50
75
}
51
76
52
77
async function sleep ( ms ) {
53
78
await new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
54
79
}
55
80
56
- async function send ( message ) {
57
- const endpoint = `http://127.0.0.1:3000/${ DEFAULT_AGENT_ID } /message` ;
58
- const payload = {
59
- text : message ,
60
- userId : "user" ,
61
- userName : "User"
62
- } ;
63
-
81
+ async function sendPostRequest ( url , method , payload ) {
64
82
try {
65
- const response = await fetch ( endpoint , {
66
- method : 'POST' ,
83
+ const response = await fetch ( url , {
84
+ method : method ,
67
85
headers : {
68
- ' Content-Type' : ' application/json'
86
+ " Content-Type" : " application/json"
69
87
} ,
70
88
body : JSON . stringify ( payload )
71
89
} ) ;
72
-
73
- if ( ! response . ok ) {
74
- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
75
- }
76
-
90
+ if ( ! response . ok ) throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
77
91
const data = await response . json ( ) ;
78
92
return data [ 0 ] . text ;
79
93
} catch ( error ) {
80
94
throw new Error ( `Failed to send message: ${ error . message } ` ) ;
81
95
}
82
96
}
83
97
98
+ async function send ( message ) {
99
+ const url = `http://127.0.0.1:3000/${ DEFAULT_AGENT_ID } /message` ;
100
+ return await sendPostRequest ( url , "POST" , {
101
+ text : message ,
102
+ userId : "user" ,
103
+ userName : "User"
104
+ } ) ;
105
+ }
106
+
84
107
async function runIntegrationTest ( fn ) {
85
108
const proc = await startAgent ( ) ;
86
109
try {
87
110
fn ( ) ;
88
- console . log ( ' ✓ Test passed' ) ;
111
+ log ( " ✓ Test passed" ) ;
89
112
} catch ( error ) {
90
- console . error ( `✗ Test failed: ${ error . message } ` ) ;
91
- console . log ( error ) ;
113
+ logError ( `✗ Test failed: ${ error . message } ` ) ;
114
+ logError ( error ) ;
92
115
process . exit ( 1 ) ;
93
116
} finally {
94
117
await stopAgent ( proc ) ;
@@ -105,5 +128,6 @@ export {
105
128
stopAgent ,
106
129
send ,
107
130
runIntegrationTest ,
108
- log
131
+ log ,
132
+ logError
109
133
}
0 commit comments