-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·369 lines (298 loc) · 10.1 KB
/
test.sh
File metadata and controls
executable file
·369 lines (298 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/bin/bash
set -eou pipefail
# Check if jq is available
if ! command -v jq >/dev/null 2>&1; then
echo "Error: jq is required but not found. Please install jq." >&2
exit 1
fi
# Show help message
show_help() {
cat << EOF
Usage: $0 [OPTIONS] <relative_folder_path>
Test script for the Hack Language Server Protocol (LSP) server.
ARGUMENTS:
relative_folder_path Path to folder containing .asm files to test
OPTIONS:
-h, --help Show this help message and exit
--shutdown Send proper LSP shutdown sequence before exiting
DESCRIPTION:
This script exercises the LSP server by:
- Initializing the LSP server
- Opening all .asm files from the specified folder (excluding files ending with "2")
- Sending didChange notifications for files ending with "2" (e.g., Add2.asm)
- Testing completion requests at random positions
- Testing hover requests at random positions
- Optionally shutting down the server properly
EXAMPLES:
# Test with proper shutdown
$0 --shutdown tests/ | ./build/bin/hack-ls --stdio
# Test without shutdown (server will detect EOF)
$0 tests/ | ./build/bin/hack-ls --stdio
REQUIREMENTS:
- jq must be installed and available in PATH
EOF
}
# Parse arguments
SHUTDOWN=false
RELATIVE_PATH=""
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
--shutdown)
SHUTDOWN=true
shift
;;
*)
if [ -z "$RELATIVE_PATH" ]; then
RELATIVE_PATH="$1"
else
echo "Error: Multiple folder paths provided" >&2
echo "Run '$0 --help' for usage information." >&2
exit 1
fi
shift
;;
esac
done
if [ -z "$RELATIVE_PATH" ]; then
echo "Error: relative_folder_path is required" >&2
echo "Usage: $0 [OPTIONS] <relative_folder_path>" >&2
echo "Run '$0 --help' for more information." >&2
exit 1
fi
LANGUAGE_ID="hack"
SOURCE_FOLDER="$(pwd)/${RELATIVE_PATH}"
ID=0
# Check if folder exists
if [ ! -d "$SOURCE_FOLDER" ]; then
echo "Error: Folder does not exist: $SOURCE_FOLDER" >&2
exit 1
fi
log(){
local message=$1
printf "\n\033[33m[CLIENT]:\033[0m%s\n" "$message" >&2
}
echo "[INFO] Using source folder: $SOURCE_FOLDER" >&2
send_request(){
local id=$1
local method=$2
local params=$3
local body="{\"jsonrpc\":\"2.0\",\"id\":${id},\"method\":\"${method}\",\"params\":${params}}"
local content_length=${#body}
# Use actual CRLF for header, but keep body as literal string
printf "Content-Length: %d\r\n\r\n%s" "$content_length" "$body"
ID=$((ID+1))
}
send_notification(){
local method=$1
local params=$2
local body="{\"jsonrpc\":\"2.0\",\"method\":\"${method}\",\"params\":${params}}"
local content_length=${#body}
# Use actual CRLF for header, but keep body as literal string
printf "Content-Length: %d\r\n\r\n%s" "$content_length" "$body"
}
send_initialize(){
local root_uri="file://${SOURCE_FOLDER}"
local process_id=$$
# Build initialize params with proper capabilities
local params
params=$(jq -n \
--argjson processId "$process_id" \
--arg rootUri "$root_uri" \
'{
"processId": $processId,
"rootUri": $rootUri,
"capabilities": {},
"clientInfo": {
"name": "test-client",
"version": "1.0.0"
}
}')
log "REQUEST: Initialize"
send_request "$ID" "initialize" "$params"
}
send_did_open(){
# Handle case where glob doesn't match (no files)
shopt -s nullglob
for file in "$SOURCE_FOLDER"/*; do
# Skip if not a regular file
[ -f "$file" ] || continue
# Skip files ending with "2" before extension (e.g., Add2.asm)
local basename
basename=$(basename "$file")
local name_no_ext="${basename%.*}"
if [[ "$name_no_ext" =~ 2$ ]]; then
continue
fi
local file_path="$file"
local file_uri="file://${file_path}"
# Build params JSON using jq for proper escaping of all control chars including CR
local params
params=$(jq -n \
--arg uri "$file_uri" \
--arg languageId "$LANGUAGE_ID" \
--rawfile text "$file" \
'{"textDocument":{"uri":$uri,"languageId":$languageId,"version":1,"text":$text}}')
log "NOTIFICATION: textDocument/didOpen ${file}"
send_notification "textDocument/didOpen" "$params" || true
done
shopt -u nullglob
}
send_did_change(){
# Handle case where glob doesn't match (no files)
shopt -s nullglob
for file in "$SOURCE_FOLDER"/*; do
# Skip if not a regular file
[ -f "$file" ] || continue
# Only process files ending with "2" before extension (e.g., Add2.asm)
local basename
basename=$(basename "$file")
local name_no_ext="${basename%.*}"
if [[ ! "$name_no_ext" =~ 2$ ]]; then
continue
fi
# Remove "2" from filename for URI to match the file opened with didOpen
local dir_path
dir_path=$(dirname "$file")
local name_without_2="${name_no_ext%2}"
local extension="${basename##*.}"
local original_basename="${name_without_2}.${extension}"
local original_file_path="${dir_path}/${original_basename}"
local file_uri="file://${original_file_path}"
# Build params JSON for didChange - need textDocument with version and contentChanges
local params
params=$(jq -n \
--arg uri "$file_uri" \
--rawfile text "$file" \
'{"textDocument":{"uri":$uri,"version":2},"contentChanges":[{"text":$text}]}')
log "NOTIFICATION: textDocument/didChange ${file}"
send_notification "textDocument/didChange" "$params" || true
done
shopt -u nullglob
}
# Get random position in a file (returns line and character)
get_random_position(){
local file=$1
local line_count
line_count=$(wc -l < "$file" 2>/dev/null || echo "0")
# If file is empty or has no lines, use position 0,0
if [ "$line_count" -eq 0 ]; then
echo "0 0"
return
fi
# Generate random line (0-indexed for LSP, so 0 to line_count-1)
local random_line
random_line=$((RANDOM % line_count))
# Get the line content to determine max character
local line_content
line_content=$(sed -n "$((random_line + 1))p" "$file" 2>/dev/null || echo "")
local line_length=${#line_content}
# Generate random character position (0-indexed, 0 to line_length)
local random_char
random_char=$((RANDOM % (line_length + 1)))
echo "$random_line $random_char"
}
send_completion_request(){
# Get list of opened files (non-2 files)
shopt -s nullglob
local files=()
for file in "$SOURCE_FOLDER"/*; do
[ -f "$file" ] || continue
local basename
basename=$(basename "$file")
local name_no_ext="${basename%.*}"
if [[ ! "$name_no_ext" =~ 2$ ]]; then
files+=("$file")
fi
done
shopt -u nullglob
# Pick a random file
if [ ${#files[@]} -eq 0 ]; then
return
fi
local random_idx
random_idx=$((RANDOM % ${#files[@]}))
local selected_file="${files[$random_idx]}"
local file_uri="file://${selected_file}"
# Get random position
local position
position=$(get_random_position "$selected_file")
local line
line=$(echo "$position" | cut -d' ' -f1)
local character
character=$(echo "$position" | cut -d' ' -f2)
# Build params JSON
local params
params=$(jq -n \
--arg uri "$file_uri" \
--argjson line "$line" \
--argjson character "$character" \
'{"textDocument":{"uri":$uri},"position":{"line":$line,"character":$character}}')
log "REQUEST: textDocument/completion at line $line, char $character in $(basename "$selected_file")"
send_request "$ID" "textDocument/completion" "$params" || true
}
send_hover_request(){
# Get list of opened files (non-2 files)
shopt -s nullglob
local files=()
for file in "$SOURCE_FOLDER"/*; do
[ -f "$file" ] || continue
local basename
basename=$(basename "$file")
local name_no_ext="${basename%.*}"
if [[ ! "$name_no_ext" =~ 2$ ]]; then
files+=("$file")
fi
done
shopt -u nullglob
# Pick a random file
if [ ${#files[@]} -eq 0 ]; then
return
fi
local random_idx
random_idx=$((RANDOM % ${#files[@]}))
local selected_file="${files[$random_idx]}"
local file_uri="file://${selected_file}"
# Get random position
local position
position=$(get_random_position "$selected_file")
local line
line=$(echo "$position" | cut -d' ' -f1)
local character
character=$(echo "$position" | cut -d' ' -f2)
# Build params JSON
local params
params=$(jq -n \
--arg uri "$file_uri" \
--argjson line "$line" \
--argjson character "$character" \
'{"textDocument":{"uri":$uri},"position":{"line":$line,"character":$character}}')
log "REQUEST: textDocument/hover at line $line, char $character in $(basename "$selected_file")"
send_request "$ID" "textDocument/hover" "$params" || true
}
send_initialize
log "NOTIFICATION: Initialized"
send_notification "initialized" "{}"
send_did_open
send_did_change
# Send completion and hover requests at random positions
send_completion_request
send_hover_request
# Shutdown sequence (only if --shutdown flag is provided)
if [ "$SHUTDOWN" = true ]; then
# Wait for responses to be processed
sleep 0.2
log "REQUEST: Shutdown"
send_request "$ID" "shutdown" "{}"
# Wait for shutdown response
sleep 0.1
log "NOTIFICATION: Exit"
send_notification "exit" "{}"
else
sleep 2
log "Script finished (server will detect EOF and may shut down)"
log "Use --shutdown flag for proper LSP shutdown sequence"
fi