-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·559 lines (450 loc) · 19.1 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·559 lines (450 loc) · 19.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/usr/bin/env bats
# test.sh — bats tests for hat
# Run: bats test.sh
load '/usr/lib/bats/bats-support/load'
load '/usr/lib/bats/bats-assert/load'
setup_file() {
export HAT="$BATS_TEST_DIRNAME/hat"
export TEST_ASSETS="$BATS_TEST_DIRNAME/test-assets"
# Source testable bash functions
eval "$(sed -n '/^sanitize_name/,/^}/p' "$HAT")"
eval "$(sed -n '/^is_binary/,/^}/p' "$HAT")"
eval "$(sed -n '/^is_image/,/^}/p' "$HAT")"
eval "$(sed -n '/^is_audio/,/^}/p' "$HAT")"
eval "$(sed -n '/^is_video/,/^}/p' "$HAT")"
eval "$(sed -n '/^collect_metadata/,/^}/p' "$HAT")"
export -f sanitize_name is_binary is_image is_audio is_video collect_metadata
# Start mock LLM server that handles multi-turn conversations
export MOCK_PORT=18950
export MOCK_DIR=$(mktemp -d)
python3 - "$MOCK_PORT" "$MOCK_DIR" <<'MOCKSERVER' &
import sys
from http.server import HTTPServer, BaseHTTPRequestHandler
import json, os
MOCK_DIR = sys.argv[2]
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers['Content-Length'])
body = json.loads(self.rfile.read(length))
msgs = body['messages']
last_msg = msgs[-1]
content = last_msg['content'] if isinstance(last_msg['content'], str) else last_msg['content'][-1]['text']
# Save last prompt for inspection
with open(os.path.join(MOCK_DIR, 'last_prompt.txt'), 'w') as f:
f.write(content)
with open(os.path.join(MOCK_DIR, 'last_request.json'), 'w') as f:
json.dump(body, f, indent=2)
# Determine response based on request type
if 'already' in content and ('YES' in content or 'NO' in content):
# Check call: read override file if present, otherwise default NO
override = os.path.join(MOCK_DIR, 'check_response')
if os.path.exists(override):
answer = open(override).read().strip()
else:
answer = 'NO'
else:
# Naming call
answer = 'suggested-name'
resp = json.dumps({'choices': [{'message': {'content': answer}}]})
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(resp.encode())
def log_message(self, *a): pass
HTTPServer(('127.0.0.1', int(sys.argv[1])), Handler).serve_forever()
MOCKSERVER
export MOCK_PID=$!
sleep 0.5
}
teardown_file() {
kill "$MOCK_PID" 2>/dev/null || true
rm -rf "$MOCK_DIR"
}
# ── Sanitize name ───────────────────────────────────────────────────
@test "sanitize: preserves same extension" {
run sanitize_name "sunset-view.jpg" "true" "jpg"
assert_output "sunset-view.jpg"
}
@test "sanitize: strips wrong ext, appends original" {
run sanitize_name "sunset-view.png" "true" "jpg"
assert_output "sunset-view.jpg"
}
@test "sanitize: appends ext to bare stem" {
run sanitize_name "sunset-view" "true" "jpg"
assert_output "sunset-view.jpg"
}
@test "sanitize: cleans special characters" {
run sanitize_name "Sunset View!" "true" "png"
assert_output "Sunset-View.png"
}
@test "sanitize: no-ext mode keeps model extension" {
run sanitize_name "cool-photo.webp" "false" ""
assert_output "cool-photo.webp"
}
@test "sanitize: strips backticks" {
run sanitize_name '```sunset-view```' "true" "jpg"
assert_output "sunset-view.jpg"
}
@test "sanitize: strips think tags" {
run sanitize_name "<think>reasoning</think>sunset-view" "true" "pdf"
assert_output "sunset-view.pdf"
}
# ── Sanitize name: additional edge cases ───────────────────────────
@test "sanitize: collapses multiple spaces/hyphens to single hyphen" {
run sanitize_name "my report doc" "true" "pdf"
assert_output "my-report-doc.pdf"
}
@test "sanitize: strips surrounding quotes" {
run sanitize_name '"quoted-name"' "true" "txt"
assert_output "quoted-name.txt"
}
@test "sanitize: multiline input uses last non-empty line" {
run sanitize_name $'first-line\nsecond-name' "true" "txt"
assert_output "second-name.txt"
}
# Regression: a degenerate model answer (empty / think-only / punctuation) must
# NOT become a stem-less dotfile like ".jpg" — that slips past the caller's
# empty-name guard and renames the file to a hidden, stem-less file. Emit
# nothing so the rename is rejected.
@test "sanitize: empty answer yields no name (not a dotfile)" {
run sanitize_name "" "true" "jpg"
assert_output ""
}
@test "sanitize: think-only answer yields no name" {
run sanitize_name "<think>spent my whole budget reasoning</think>" "true" "jpg"
assert_output ""
}
@test "sanitize: punctuation-only answer yields no name" {
run sanitize_name "." "true" "jpg"
assert_output ""
}
# ── Binary detection ─────────────────────────────────────────────────
@test "binary: text file is not binary" {
run is_binary "$TEST_ASSETS/sample.txt"
assert_failure
}
@test "binary: random .bin is binary" {
run is_binary "$TEST_ASSETS/sample.bin"
assert_success
}
@test "binary: jpg is binary" {
run is_binary "$TEST_ASSETS/sample.jpg"
assert_success
}
@test "binary: html is not binary" {
run is_binary "$TEST_ASSETS/sample.html"
assert_failure
}
# ── Image detection ─────────────────────────────────────────────────
@test "image: jpg is detected as image" {
run is_image "$TEST_ASSETS/sample.jpg"
assert_success
}
@test "image: jpeg is detected as image" {
run is_image "$TEST_ASSETS/sample.jpeg"
assert_success
}
@test "image: png is detected as image" {
run is_image "$TEST_ASSETS/sample.png"
assert_success
}
@test "image: gif is detected as image" {
run is_image "$TEST_ASSETS/sample.gif"
assert_success
}
@test "image: bmp is detected as image" {
run is_image "$TEST_ASSETS/sample.bmp"
assert_success
}
@test "image: tiff is detected as image" {
run is_image "$TEST_ASSETS/sample.tiff"
assert_success
}
@test "image: svg is detected as image (extension fallback)" {
run is_image "$TEST_ASSETS/sample.svg"
assert_success
}
@test "image: webp is detected as image" {
run is_image "$TEST_ASSETS/sample.webp"
assert_success
}
@test "image: text file is not an image" {
run is_image "$TEST_ASSETS/sample.txt"
assert_failure
}
@test "image: html file is not an image" {
run is_image "$TEST_ASSETS/sample.html"
assert_failure
}
@test "image: binary .bin is not an image" {
run is_image "$TEST_ASSETS/sample.bin"
assert_failure
}
# ── Image integration ─────────────────────────────────────────────────
@test "image: jpg is processed as image (not skipped as binary)" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.jpg' 2>/dev/null"
assert_output "suggested-name.jpg"
}
@test "image: png is processed as image (not skipped as binary)" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.png' 2>/dev/null"
assert_output "suggested-name.png"
}
# ── Metadata collection ─────────────────────────────────────────────
@test "metadata: text file has size and modified" {
run collect_metadata "$TEST_ASSETS/sample.txt" "text"
assert_output --partial "size_bytes"
assert_output --partial "modified"
}
@test "metadata: text file has no exif" {
run collect_metadata "$TEST_ASSETS/sample.txt" "text"
refute_output --partial "exif"
}
# ── Help output ──────────────────────────────────────────────────────
@test "help: shows all flags" {
run bash "$HAT" --help
assert_output --partial "--context"
assert_output --partial "--no-metadata"
assert_output --partial "--force"
assert_output --partial "--nothink"
assert_output --partial "--quiet"
assert_output --partial "--batch"
}
# ── Integration: naming with --force (single turn) ──────────────────
@test "integration: basic naming returns sanitized result" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.txt' 2>/dev/null"
assert_output "suggested-name.txt"
}
@test "integration: --context reaches prompt" {
bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force -c 'marketing Q3' '$TEST_ASSETS/sample.txt'" >/dev/null 2>&1
run cat "$MOCK_DIR/last_prompt.txt"
assert_output --partial "Additional context: marketing Q3"
}
@test "integration: metadata in prompt by default" {
bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.txt'" >/dev/null 2>&1
run cat "$MOCK_DIR/last_prompt.txt"
assert_output --partial "File metadata:"
}
@test "integration: --no-metadata excludes metadata" {
bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force --no-metadata '$TEST_ASSETS/sample.txt'" >/dev/null 2>&1
run cat "$MOCK_DIR/last_prompt.txt"
refute_output --partial "File metadata:"
}
@test "integration: extension isolation in prompt" {
bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.txt'" >/dev/null 2>&1
run cat "$MOCK_DIR/last_prompt.txt"
assert_output --partial "Do NOT include any file extension"
}
@test "integration: binary files are skipped" {
run bash "$HAT" --quiet --dry-run --force "$TEST_ASSETS/sample.bin"
assert_output --partial "skipping"
}
# ── Integration: LLM-based guard clause (two-turn) ──────────────────
@test "guard: LLM check says NO → proceeds to rename" {
echo "NO" > "$MOCK_DIR/check_response"
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run '$TEST_ASSETS/sample.txt' 2>/dev/null"
assert_output "suggested-name.txt"
}
@test "guard: LLM check says YES → skips file" {
echo "YES" > "$MOCK_DIR/check_response"
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run '$TEST_ASSETS/sample.txt' 2>&1"
assert_output --partial "already good, skipping"
}
@test "guard: --force skips LLM check entirely" {
echo "YES" > "$MOCK_DIR/check_response"
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.txt' 2>/dev/null"
assert_output "suggested-name.txt"
}
@test "guard: two-turn naming has 3 messages (check context)" {
echo "NO" > "$MOCK_DIR/check_response"
bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run '$TEST_ASSETS/sample.txt'" >/dev/null 2>&1
run python3 -c "import json; d=json.load(open('$MOCK_DIR/last_request.json')); print(len(d['messages']))"
assert_output "3"
}
# ── Integration: error handling ──────────────────────────────────────
@test "integration: connection error shows clear message" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:19999 bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.txt' 2>&1"
assert_output --partial "could not reach LLM"
refute_output --partial "Traceback"
}
# ── Audio detection ──────────────────────────────────────────────────
@test "audio: mp3 is detected as audio" {
run is_audio "$TEST_ASSETS/sample.mp3"
assert_success
}
@test "audio: wav is detected as audio" {
run is_audio "$TEST_ASSETS/sample.wav"
assert_success
}
@test "audio: flac is detected as audio" {
run is_audio "$TEST_ASSETS/sample.flac"
assert_success
}
@test "audio: ogg is detected as audio" {
run is_audio "$TEST_ASSETS/sample.ogg"
assert_success
}
@test "audio: aac is detected as audio" {
run is_audio "$TEST_ASSETS/sample.aac"
assert_success
}
@test "audio: m4a is detected as audio" {
run is_audio "$TEST_ASSETS/sample.m4a"
assert_success
}
@test "audio: text file is not audio" {
run is_audio "$TEST_ASSETS/sample.txt"
assert_failure
}
@test "audio: image file is not audio" {
run is_audio "$TEST_ASSETS/sample.jpg"
assert_failure
}
# ── ftyp-box brand discrimination (regression: HEIC / MP4 video / MOV
# / 3GP all carry the ftyp marker. Major brand at bytes 8-11 is what
# distinguishes audio from video/image — the brand check must reject
# everything except M4A / M4B / M4P.) ─────────────────────────────────
@test "audio: MP4 isom container is NOT audio" {
local tmp; tmp=$(mktemp --suffix=.mp4)
# 8B box size + "ftyp" + "isom" + 4 bytes filler
printf '\x00\x00\x00\x20ftypisom\x00\x00\x02\x00' > "$tmp"
run is_audio "$tmp"
rm -f "$tmp"
assert_failure
}
@test "audio: MP4 mp42 container is NOT audio" {
local tmp; tmp=$(mktemp --suffix=.mp4)
printf '\x00\x00\x00\x20ftypmp42\x00\x00\x00\x00' > "$tmp"
run is_audio "$tmp"
rm -f "$tmp"
assert_failure
}
@test "audio: QuickTime mov is NOT audio" {
local tmp; tmp=$(mktemp --suffix=.mov)
printf '\x00\x00\x00\x20ftypqt \x00\x00\x00\x00' > "$tmp"
run is_audio "$tmp"
rm -f "$tmp"
assert_failure
}
@test "audio: HEIC iPhone photo is NOT audio" {
local tmp; tmp=$(mktemp --suffix=.heic)
printf '\x00\x00\x00\x20ftypheic\x00\x00\x00\x00' > "$tmp"
run is_audio "$tmp"
rm -f "$tmp"
assert_failure
}
@test "audio: HEIF mif1 brand is NOT audio" {
local tmp; tmp=$(mktemp --suffix=.heif)
printf '\x00\x00\x00\x20ftypmif1\x00\x00\x00\x00' > "$tmp"
run is_audio "$tmp"
rm -f "$tmp"
assert_failure
}
@test "audio: 3GP is NOT audio" {
local tmp; tmp=$(mktemp --suffix=.3gp)
printf '\x00\x00\x00\x20ftyp3gp4\x00\x00\x00\x00' > "$tmp"
run is_audio "$tmp"
rm -f "$tmp"
assert_failure
}
@test "audio: synthetic M4A brand IS audio" {
local tmp; tmp=$(mktemp --suffix=.m4a)
printf '\x00\x00\x00\x20ftypM4A \x00\x00\x00\x00' > "$tmp"
run is_audio "$tmp"
rm -f "$tmp"
assert_success
}
@test "audio: M4B audiobook IS audio" {
local tmp; tmp=$(mktemp --suffix=.m4b)
printf '\x00\x00\x00\x20ftypM4B \x00\x00\x00\x00' > "$tmp"
run is_audio "$tmp"
rm -f "$tmp"
assert_success
}
# ── Audio metadata ───────────────────────────────────────────────────
@test "audio metadata: mp3 has size and modified" {
run collect_metadata "$TEST_ASSETS/sample.mp3" "audio"
assert_output --partial "size_bytes"
assert_output --partial "modified"
}
@test "audio metadata: mp3 has mime_type" {
run collect_metadata "$TEST_ASSETS/sample.mp3" "audio"
assert_output --partial "mime_type"
}
# ── Audio integration ────────────────────────────────────────────────
@test "audio: mp3 is processed (not skipped as binary)" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.mp3' 2>/dev/null"
assert_output "suggested-name.mp3"
}
@test "audio: flac is processed (not skipped as binary)" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.flac' 2>/dev/null"
assert_output "suggested-name.flac"
}
@test "audio: wav is processed (not skipped as binary)" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.wav' 2>/dev/null"
assert_output "suggested-name.wav"
}
# ── Video detection ──────────────────────────────────────────────────
@test "video: mp4 is detected as video" {
run is_video "$TEST_ASSETS/sample.mp4"
assert_success
}
@test "video: webm is detected as video" {
run is_video "$TEST_ASSETS/sample.webm"
assert_success
}
@test "video: mkv is detected as video" {
run is_video "$TEST_ASSETS/sample.mkv"
assert_success
}
@test "video: text file is not video" {
run is_video "$TEST_ASSETS/sample.txt"
assert_failure
}
@test "video: audio file is not video" {
run is_video "$TEST_ASSETS/sample.mp3"
assert_failure
}
@test "video: m4a is not detected as video" {
run is_video "$TEST_ASSETS/sample.m4a"
assert_failure
}
# ── Video integration ────────────────────────────────────────────────
@test "video: mp4 is processed (not skipped as binary)" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.mp4' 2>/dev/null"
assert_output "suggested-name.mp4"
}
@test "video: webm is processed (not skipped as binary)" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.webm' 2>/dev/null"
assert_output "suggested-name.webm"
}
@test "video: mkv is processed (not skipped as binary)" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.mkv' 2>/dev/null"
assert_output "suggested-name.mkv"
}
# ── Preview flag ─────────────────────────────────────────────────────
@test "preview: --preview shows content preview on stderr for text file" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force --preview '$TEST_ASSETS/sample.txt' 2>&1 >/dev/null"
assert_output --partial "preview:"
assert_output --partial "sample.txt"
}
@test "preview: --preview shows line count for text file" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force --preview '$TEST_ASSETS/sample.txt' 2>&1 >/dev/null"
assert_output --partial "lines total"
}
@test "preview: -p shorthand works" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force -p '$TEST_ASSETS/sample.txt' 2>&1 >/dev/null"
assert_output --partial "preview:"
}
@test "preview: without --preview no preview header on stderr" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force '$TEST_ASSETS/sample.txt' 2>&1 >/dev/null"
refute_output --partial "preview:"
}
@test "preview: --preview still produces correct suggestion on stdout" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force --preview '$TEST_ASSETS/sample.txt' 2>/dev/null"
assert_output "suggested-name.txt"
}
@test "preview: --preview works with audio file" {
run bash -c "LLM_BASE_URL=http://127.0.0.1:$MOCK_PORT bash '$HAT' --quiet --dry-run --force --preview '$TEST_ASSETS/sample.mp3' 2>&1 >/dev/null"
assert_output --partial "preview:"
assert_output --partial "sample.mp3"
}