test-setup-scripts-robustness.sh 43.7 KB
Newer Older
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
#!/usr/bin/env bash
#
# Robustness tests for setup scripts
#
# Tests setup-rlcr-loop.sh under edge cases:
# - Argument parsing edge cases
# - Plan file validation edge cases
# - Git repository edge cases
# - YAML safety validation
# - Concurrent execution handling
#

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
source "$SCRIPT_DIR/../test-helpers.sh"
source "$PROJECT_ROOT/scripts/portable-timeout.sh"

setup_test_dir

echo "========================================"
echo "Setup Scripts Robustness Tests"
echo "========================================"
echo ""

# ========================================
# Helper Functions
# ========================================

create_minimal_plan() {
    local dir="$1"
    local filename="${2:-plan.md}"
    mkdir -p "$(dirname "$dir/$filename")"
    cat > "$dir/$filename" << 'EOF'
# Implementation Plan

## Goal
Test the setup script robustness.

## Acceptance Criteria
- Works correctly

## Steps
1. First step
2. Second step
3. Third step
EOF
}

init_basic_git_repo() {
    local dir="$1"
    cd "$dir"
    git init -q
    git config user.email "test@test.com"
    git config user.name "Test User"
    git config commit.gpgsign false
    git checkout -q -b main 2>/dev/null || git checkout -q main
    echo "initial" > file.txt
    git add file.txt
    git commit -q -m "Initial commit"
    cd - > /dev/null
}

# Create a minimal PATH toolset in a test bin directory so scripts using
# '/usr/bin/env bash' still run even in restricted PATH scenarios.
prepare_runtime_bin() {
    local bin_dir="$1"
    local tool
    local tool_path

    mkdir -p "$bin_dir"

    for tool in bash env git dirname cat sed awk grep mkdir date head od tr wc sort ls rm cp mv chmod ln readlink printf timeout gtimeout; do
        tool_path=$(command -v "$tool" 2>/dev/null || true)
        if [[ -n "$tool_path" && -x "$tool_path" && ! -e "$bin_dir/$tool" ]]; then
            ln -s "$tool_path" "$bin_dir/$tool"
        fi
    done
}

# Run setup-rlcr-loop.sh with proper isolation from real RLCR loop
# Usage: run_rlcr_setup <test_repo_dir> [args...]
run_rlcr_setup() {
    local repo_dir="$1"
    shift
    (
        cd "$repo_dir"
        # Set CLAUDE_PROJECT_DIR to isolate from any real active loops
        # Preserve PATH to ensure git/gh/etc are available
        CLAUDE_PROJECT_DIR="$repo_dir" "$PROJECT_ROOT/scripts/setup-rlcr-loop.sh" "$@"
    )
}

# ========================================
# Setup RLCR Loop Argument Parsing Tests
# ========================================

echo "--- Setup RLCR Loop Argument Tests ---"
echo ""

# Test 1: Help flag displays usage
echo "Test 1: Help flag displays usage"
OUTPUT=$("$PROJECT_ROOT/scripts/setup-rlcr-loop.sh" --help 2>&1) || true
if echo "$OUTPUT" | grep -q "USAGE"; then
    pass "Help flag displays usage information"
else
    fail "Help flag" "USAGE text" "no usage found"
fi

# Test 2: Missing plan file shows error
echo ""
echo "Test 2: Missing plan file shows error"
mkdir -p "$TEST_DIR/repo2"
init_basic_git_repo "$TEST_DIR/repo2"
OUTPUT=$(run_rlcr_setup "$TEST_DIR/repo2" 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "no plan file\|plan file"; then
    pass "Missing plan file shows error"
else
    fail "Missing plan file" "exit != 0 with error message" "exit=$EXIT_CODE, output=$OUTPUT"
fi

# Test 3: --max with non-numeric value rejected
echo ""
echo "Test 3: --max with non-numeric value rejected"
OUTPUT=$("$PROJECT_ROOT/scripts/setup-rlcr-loop.sh" --max abc 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "positive integer"; then
    pass "--max non-numeric rejected"
else
    fail "--max validation" "rejection" "exit=$EXIT_CODE"
fi

# Test 4: --max with actual negative number rejected
echo ""
echo "Test 4: --max with actual negative number rejected"
mkdir -p "$TEST_DIR/repo4"
init_basic_git_repo "$TEST_DIR/repo4"
create_minimal_plan "$TEST_DIR/repo4"
echo "plan.md" >> "$TEST_DIR/repo4/.gitignore"
git -C "$TEST_DIR/repo4" add .gitignore && git -C "$TEST_DIR/repo4" commit -q -m "Add gitignore"
mkdir -p "$TEST_DIR/repo4/bin"
# Test actual negative number (--max=-5 or --max -5)
# Note: bash argparse may interpret -5 as a flag, so we use --max=-5 format
OUTPUT=$(run_rlcr_setup "$TEST_DIR/repo4" plan.md --max=-5 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "positive integer\|unknown option\|invalid"; then
    pass "--max with negative number rejected (exit=$EXIT_CODE)"
else
    # Also try separate argument format
    OUTPUT=$(run_rlcr_setup "$TEST_DIR/repo4" plan.md --max -5 2>&1) || EXIT_CODE=$?
    EXIT_CODE=${EXIT_CODE:-0}
    if [[ $EXIT_CODE -ne 0 ]]; then
        pass "--max -5 rejected (exit=$EXIT_CODE)"
    else
        fail "--max negative" "rejection" "exit=$EXIT_CODE"
    fi
fi

# Test 4b: --max with empty value rejected
echo ""
echo "Test 4b: --max with empty value rejected"
OUTPUT=$("$PROJECT_ROOT/scripts/setup-rlcr-loop.sh" --max "" 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]]; then
    pass "--max with empty value rejected"
else
    fail "--max empty" "rejection" "accepted"
fi

# Test 5: --codex-timeout with non-numeric value rejected
echo ""
echo "Test 5: --codex-timeout with non-numeric value rejected"
OUTPUT=$("$PROJECT_ROOT/scripts/setup-rlcr-loop.sh" --codex-timeout "invalid" 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "positive integer"; then
    pass "--codex-timeout non-numeric rejected"
else
    fail "--codex-timeout validation" "rejection" "exit=$EXIT_CODE"
fi

# Test 6: --codex-model without argument rejected
echo ""
echo "Test 6: --codex-model without argument rejected"
OUTPUT=$("$PROJECT_ROOT/scripts/setup-rlcr-loop.sh" --codex-model 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]]; then
    pass "--codex-model without argument rejected"
else
    fail "--codex-model validation" "rejection" "accepted"
fi

# Test 7: Unknown option rejected
echo ""
echo "Test 7: Unknown option rejected"
OUTPUT=$("$PROJECT_ROOT/scripts/setup-rlcr-loop.sh" --unknown-option 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "unknown option"; then
    pass "Unknown option rejected"
else
    fail "Unknown option" "rejection" "exit=$EXIT_CODE"
fi

# Test 8: Both positional and --plan-file rejected
echo ""
echo "Test 8: Both positional and --plan-file rejected"
mkdir -p "$TEST_DIR/repo8"
init_basic_git_repo "$TEST_DIR/repo8"
create_minimal_plan "$TEST_DIR/repo8"
echo "plan.md" >> "$TEST_DIR/repo8/.gitignore"
git -C "$TEST_DIR/repo8" add .gitignore && git -C "$TEST_DIR/repo8" commit -q -m "Add gitignore"

OUTPUT=$(run_rlcr_setup "$TEST_DIR/repo8" plan.md --plan-file other.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "cannot specify both"; then
    pass "Both positional and --plan-file rejected"
else
    fail "Duplicate plan file" "rejection" "exit=$EXIT_CODE"
fi

# ========================================
# Plan File Validation Edge Cases
# ========================================

echo ""
echo "--- Plan File Validation Tests ---"
echo ""

# Test 9: Plan file with only comments rejected
echo "Test 9: Plan file with only comments rejected"
mkdir -p "$TEST_DIR/repo9"
init_basic_git_repo "$TEST_DIR/repo9"
cat > "$TEST_DIR/repo9/plan.md" << 'EOF'
# Comment 1
# Comment 2
# Comment 3
# Comment 4
# Comment 5
# Comment 6
# Comment 7
EOF
echo "plan.md" >> "$TEST_DIR/repo9/.gitignore"
git -C "$TEST_DIR/repo9" add .gitignore && git -C "$TEST_DIR/repo9" commit -q -m "Add gitignore"

# Create mock codex
mkdir -p "$TEST_DIR/repo9/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo9/bin/codex"
chmod +x "$TEST_DIR/repo9/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo9/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo9" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "insufficient content"; then
    pass "Plan with only comments rejected"
else
    fail "Comment-only plan" "rejection" "exit=$EXIT_CODE"
fi

# Test 10: Plan file with less than 5 lines rejected
echo ""
echo "Test 10: Plan file with less than 5 lines rejected"
mkdir -p "$TEST_DIR/repo10"
init_basic_git_repo "$TEST_DIR/repo10"
cat > "$TEST_DIR/repo10/plan.md" << 'EOF'
# Short Plan
Content
Line
EOF
echo "plan.md" >> "$TEST_DIR/repo10/.gitignore"
git -C "$TEST_DIR/repo10" add .gitignore && git -C "$TEST_DIR/repo10" commit -q -m "Add gitignore"

mkdir -p "$TEST_DIR/repo10/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo10/bin/codex"
chmod +x "$TEST_DIR/repo10/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo10/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo10" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "too simple"; then
    pass "Short plan rejected"
else
    fail "Short plan" "rejection" "exit=$EXIT_CODE"
fi

# Test 11: Plan file with spaces in path rejected
echo ""
echo "Test 11: Plan file with spaces in path rejected"
mkdir -p "$TEST_DIR/repo11"
init_basic_git_repo "$TEST_DIR/repo11"
mkdir -p "$TEST_DIR/repo11/path with spaces"
create_minimal_plan "$TEST_DIR/repo11" "path with spaces/plan.md"
echo "path with spaces/" >> "$TEST_DIR/repo11/.gitignore"
git -C "$TEST_DIR/repo11" add .gitignore && git -C "$TEST_DIR/repo11" commit -q -m "Add gitignore"

mkdir -p "$TEST_DIR/repo11/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo11/bin/codex"
chmod +x "$TEST_DIR/repo11/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo11/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo11" "path with spaces/plan.md" 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "cannot contain spaces"; then
    pass "Plan with spaces in path rejected"
else
    fail "Spaces in path" "rejection" "exit=$EXIT_CODE"
fi

# Test 12: Plan file with shell metacharacters rejected
echo ""
echo "Test 12: Plan file with shell metacharacters rejected"
mkdir -p "$TEST_DIR/repo12"
init_basic_git_repo "$TEST_DIR/repo12"

mkdir -p "$TEST_DIR/repo12/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo12/bin/codex"
chmod +x "$TEST_DIR/repo12/bin/codex"

# Try path with semicolon (can't create file, just test argument parsing)
OUTPUT=$(PATH="$TEST_DIR/repo12/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo12" "plan;.md" 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "metacharacters\|not found"; then
    pass "Plan with metacharacters rejected"
else
    fail "Metacharacters" "rejection" "exit=$EXIT_CODE"
fi

# Test 13: Absolute path rejected
echo ""
echo "Test 13: Absolute path rejected"
mkdir -p "$TEST_DIR/repo13"
init_basic_git_repo "$TEST_DIR/repo13"
create_minimal_plan "$TEST_DIR/repo13"

mkdir -p "$TEST_DIR/repo13/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo13/bin/codex"
chmod +x "$TEST_DIR/repo13/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo13/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo13" "/absolute/path/plan.md" 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "relative path"; then
    pass "Absolute path rejected"
else
    fail "Absolute path" "rejection" "exit=$EXIT_CODE"
fi

# ========================================
# YAML Safety Validation Tests
# ========================================

echo ""
echo "--- YAML Safety Validation Tests ---"
echo ""

# Test 14: Branch name with colon rejected
echo "Test 14: Branch name with YAML-unsafe characters handled"
mkdir -p "$TEST_DIR/repo14"
init_basic_git_repo "$TEST_DIR/repo14"
create_minimal_plan "$TEST_DIR/repo14"
echo "plan.md" >> "$TEST_DIR/repo14/.gitignore"
git -C "$TEST_DIR/repo14" add .gitignore && git -C "$TEST_DIR/repo14" commit -q -m "Add gitignore"

mkdir -p "$TEST_DIR/repo14/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo14/bin/codex"
chmod +x "$TEST_DIR/repo14/bin/codex"

# Create branch with colon (YAML-unsafe)
cd "$TEST_DIR/repo14"
git checkout -q -b "test:branch" 2>/dev/null || true
cd - > /dev/null

OUTPUT=$(PATH="$TEST_DIR/repo14/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo14" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "YAML-unsafe"; then
    pass "YAML-unsafe branch name rejected"
else
    # If branch couldn't be created with colon, skip
    if git -C "$TEST_DIR/repo14" rev-parse --abbrev-ref HEAD 2>/dev/null | grep -q ":"; then
        fail "YAML-unsafe branch" "rejection" "exit=$EXIT_CODE"
    else
        pass "YAML-unsafe branch name test (branch creation varies by git version)"
    fi
fi

# Test 15: Codex model with invalid characters rejected
echo ""
echo "Test 15: Codex model with invalid characters rejected"
mkdir -p "$TEST_DIR/repo15"
init_basic_git_repo "$TEST_DIR/repo15"
create_minimal_plan "$TEST_DIR/repo15"
echo "plan.md" >> "$TEST_DIR/repo15/.gitignore"
git -C "$TEST_DIR/repo15" add .gitignore && git -C "$TEST_DIR/repo15" commit -q -m "Add gitignore"

mkdir -p "$TEST_DIR/repo15/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo15/bin/codex"
chmod +x "$TEST_DIR/repo15/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo15/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo15" plan.md --codex-model "model;injection" 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "invalid characters"; then
    pass "Codex model with invalid characters rejected"
else
    fail "Codex model validation" "rejection" "exit=$EXIT_CODE"
fi

# ========================================
# Git Repository Edge Cases
# ========================================

echo ""
echo "--- Git Repository Edge Cases ---"
echo ""

# Test 16: Non-git directory rejected
echo "Test 16: Non-git directory rejected"
mkdir -p "$TEST_DIR/nongit"
create_minimal_plan "$TEST_DIR/nongit"

OUTPUT=$(run_rlcr_setup "$TEST_DIR/nongit" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "git repository"; then
    pass "Non-git directory rejected"
else
    fail "Non-git directory" "rejection" "exit=$EXIT_CODE"
fi

# Test 16b: Untracked .humanizeconfig still blocks setup as a dirty working tree
echo ""
echo "Test 16b: Untracked .humanizeconfig is not ignored as runtime state"
mkdir -p "$TEST_DIR/repo16b"
init_basic_git_repo "$TEST_DIR/repo16b"
create_minimal_plan "$TEST_DIR/repo16b"
echo "plan.md" >> "$TEST_DIR/repo16b/.gitignore"
git -C "$TEST_DIR/repo16b" add .gitignore && git -C "$TEST_DIR/repo16b" commit -q -m "Add gitignore"
touch "$TEST_DIR/repo16b/.humanizeconfig"

mkdir -p "$TEST_DIR/repo16b/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo16b/bin/codex"
chmod +x "$TEST_DIR/repo16b/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo16b/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo16b" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -q "Git working tree is not clean" && echo "$OUTPUT" | grep -q '\.humanizeconfig'; then
    pass "Untracked .humanizeconfig still blocks setup as dirty"
else
    fail "Untracked .humanizeconfig blocks setup" "dirty working tree error mentioning .humanizeconfig" "exit=$EXIT_CODE, output=$OUTPUT"
fi

# Test 17: Git repo without commits rejected
echo ""
echo "Test 17: Git repo without commits rejected"
mkdir -p "$TEST_DIR/repo17"
cd "$TEST_DIR/repo17"
git init -q
git config user.email "test@test.com"
git config user.name "Test User"
cd - > /dev/null
create_minimal_plan "$TEST_DIR/repo17"

OUTPUT=$(run_rlcr_setup "$TEST_DIR/repo17" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "at least one commit"; then
    pass "Git repo without commits rejected"
else
    fail "No commits" "rejection" "exit=$EXIT_CODE"
fi

# Test 18: Tracked plan file without --track-plan-file rejected
echo ""
echo "Test 18: Tracked plan file without --track-plan-file rejected"
mkdir -p "$TEST_DIR/repo18"
init_basic_git_repo "$TEST_DIR/repo18"
create_minimal_plan "$TEST_DIR/repo18"
git -C "$TEST_DIR/repo18" add plan.md && git -C "$TEST_DIR/repo18" commit -q -m "Add plan"

mkdir -p "$TEST_DIR/repo18/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo18/bin/codex"
chmod +x "$TEST_DIR/repo18/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo18/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo18" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "gitignored\|track-plan-file"; then
    pass "Tracked plan file without flag rejected"
else
    fail "Tracked plan without flag" "rejection" "exit=$EXIT_CODE"
fi

# ========================================
# Mutual Exclusion Tests
# ========================================

echo ""
echo "--- Mutual Exclusion Tests ---"
echo ""

# Test 24: RLCR loop blocks starting another RLCR loop
echo "Test 24: Active RLCR loop blocks new RLCR loop"
mkdir -p "$TEST_DIR/repo24"
init_basic_git_repo "$TEST_DIR/repo24"
create_minimal_plan "$TEST_DIR/repo24"
echo "plan.md" >> "$TEST_DIR/repo24/.gitignore"
git -C "$TEST_DIR/repo24" add .gitignore && git -C "$TEST_DIR/repo24" commit -q -m "Add gitignore"

# Create fake active RLCR loop
mkdir -p "$TEST_DIR/repo24/.humanize/rlcr/2026-01-19_00-00-00"
cat > "$TEST_DIR/repo24/.humanize/rlcr/2026-01-19_00-00-00/state.md" << 'EOF'
---
current_round: 0
max_iterations: 42
---
EOF

mkdir -p "$TEST_DIR/repo24/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo24/bin/codex"
chmod +x "$TEST_DIR/repo24/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo24/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo24" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "already active"; then
    pass "Active RLCR loop blocks new RLCR loop"
else
    fail "RLCR mutual exclusion" "rejection" "exit=$EXIT_CODE"
fi

# ========================================
# Symlink Protection Tests
# ========================================

echo ""
echo "--- Symlink Protection Tests ---"
echo ""

# Test 26: Plan file symlink rejected
echo "Test 26: Plan file symlink rejected"
mkdir -p "$TEST_DIR/repo26"
init_basic_git_repo "$TEST_DIR/repo26"
create_minimal_plan "$TEST_DIR/repo26"
ln -sf plan.md "$TEST_DIR/repo26/symlink-plan.md" 2>/dev/null || true
echo "plan.md" >> "$TEST_DIR/repo26/.gitignore"
echo "symlink-plan.md" >> "$TEST_DIR/repo26/.gitignore"
git -C "$TEST_DIR/repo26" add .gitignore && git -C "$TEST_DIR/repo26" commit -q -m "Add gitignore"

mkdir -p "$TEST_DIR/repo26/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo26/bin/codex"
chmod +x "$TEST_DIR/repo26/bin/codex"

if [[ -L "$TEST_DIR/repo26/symlink-plan.md" ]]; then
    OUTPUT=$(PATH="$TEST_DIR/repo26/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo26" symlink-plan.md 2>&1) || EXIT_CODE=$?
    EXIT_CODE=${EXIT_CODE:-0}
    if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "symbolic link"; then
        pass "Plan file symlink rejected"
    else
        fail "Symlink rejection" "rejection" "exit=$EXIT_CODE"
    fi
else
    pass "Symlink test (symlink creation not supported)"
fi

# Test 27: Symlink in parent directory rejected
echo ""
echo "Test 27: Symlink in parent directory rejected"
mkdir -p "$TEST_DIR/repo27/real-dir"
init_basic_git_repo "$TEST_DIR/repo27"
create_minimal_plan "$TEST_DIR/repo27" "real-dir/plan.md"
ln -sf real-dir "$TEST_DIR/repo27/symlink-dir" 2>/dev/null || true
echo "real-dir/" >> "$TEST_DIR/repo27/.gitignore"
echo "symlink-dir" >> "$TEST_DIR/repo27/.gitignore"
git -C "$TEST_DIR/repo27" add .gitignore && git -C "$TEST_DIR/repo27" commit -q -m "Add gitignore"

mkdir -p "$TEST_DIR/repo27/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo27/bin/codex"
chmod +x "$TEST_DIR/repo27/bin/codex"

if [[ -L "$TEST_DIR/repo27/symlink-dir" ]]; then
    OUTPUT=$(PATH="$TEST_DIR/repo27/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo27" symlink-dir/plan.md 2>&1) || EXIT_CODE=$?
    EXIT_CODE=${EXIT_CODE:-0}
    if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "symbolic link"; then
        pass "Symlink in parent directory rejected"
    else
        fail "Parent symlink rejection" "rejection" "exit=$EXIT_CODE"
    fi
else
    pass "Parent symlink test (symlink creation not supported)"
fi

# ========================================
# Positive Success Path Tests
# ========================================

echo ""
echo "--- Positive Success Path Tests ---"
echo ""

# Test 28: Valid RLCR setup proceeds past argument validation
echo "Test 28: Valid RLCR setup proceeds past argument validation"
mkdir -p "$TEST_DIR/repo28"
init_basic_git_repo "$TEST_DIR/repo28"
create_minimal_plan "$TEST_DIR/repo28"
echo "plan.md" >> "$TEST_DIR/repo28/.gitignore"
git -C "$TEST_DIR/repo28" add .gitignore && git -C "$TEST_DIR/repo28" commit -q -m "Add gitignore"

# Create empty bin dir with no codex - should fail at dependency check
mkdir -p "$TEST_DIR/repo28/bin"
# Prepend empty bin dir to hide system codex (if any)

OUTPUT=$(PATH="$TEST_DIR/repo28/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo28" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
# Should fail at dependency check (not argument parsing) - proves args were valid
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "codex"; then
    pass "Valid RLCR setup proceeds to dependency check"
else
    # If codex is actually installed, it might proceed further
    if command -v codex &>/dev/null; then
        pass "Valid RLCR setup (codex available, may proceed further)"
    else
        fail "Valid RLCR setup" "fail at dependency check" "exit=$EXIT_CODE"
    fi
fi

# Test 29: Valid arguments with --max and --codex-timeout
echo ""
echo "Test 29: Valid numeric arguments accepted"
mkdir -p "$TEST_DIR/repo29"
init_basic_git_repo "$TEST_DIR/repo29"
create_minimal_plan "$TEST_DIR/repo29"
echo "plan.md" >> "$TEST_DIR/repo29/.gitignore"
git -C "$TEST_DIR/repo29" add .gitignore && git -C "$TEST_DIR/repo29" commit -q -m "Add gitignore"

mkdir -p "$TEST_DIR/repo29/bin"

OUTPUT=$(PATH="$TEST_DIR/repo29/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo29" plan.md --max 10 --codex-timeout 3600 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
# Should NOT fail at argument parsing - should fail later (dependency check)
if echo "$OUTPUT" | grep -qi "positive integer"; then
    fail "Valid numeric args" "accepted" "rejected as invalid"
else
    pass "Valid numeric arguments accepted (--max 10, --codex-timeout 3600)"
fi

# ========================================
# Timeout Scenario Tests
# ========================================

echo ""
echo "--- Timeout Scenario Tests ---"
echo ""

# Test 31: --codex-timeout with zero accepted (current behavior)
# Note: The validation regex ^[0-9]+$ allows 0, treating it as valid non-negative integer
echo "Test 31: --codex-timeout with zero is accepted"
mkdir -p "$TEST_DIR/repo31"
init_basic_git_repo "$TEST_DIR/repo31"
create_minimal_plan "$TEST_DIR/repo31"
echo "plan.md" >> "$TEST_DIR/repo31/.gitignore"
git -C "$TEST_DIR/repo31" add .gitignore && git -C "$TEST_DIR/repo31" commit -q -m "Add gitignore"
mkdir -p "$TEST_DIR/repo31/bin"
OUTPUT=$(PATH="$TEST_DIR/repo31/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo31" plan.md --codex-timeout 0 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
# Zero should be accepted (not rejected as "positive integer" error)
if echo "$OUTPUT" | grep -qi "positive integer"; then
    fail "--codex-timeout 0" "accepted" "rejected as not positive integer"
else
    pass "--codex-timeout 0 accepted (non-negative integer validation)"
fi

# Test 33: Very large timeout value accepted
echo ""
echo "Test 33: Very large timeout value accepted"
mkdir -p "$TEST_DIR/repo33"
init_basic_git_repo "$TEST_DIR/repo33"
create_minimal_plan "$TEST_DIR/repo33"
echo "plan.md" >> "$TEST_DIR/repo33/.gitignore"
git -C "$TEST_DIR/repo33" add .gitignore && git -C "$TEST_DIR/repo33" commit -q -m "Add gitignore"

mkdir -p "$TEST_DIR/repo33/bin"

OUTPUT=$(PATH="$TEST_DIR/repo33/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo33" plan.md --codex-timeout 999999 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
# Should NOT fail at timeout validation
if echo "$OUTPUT" | grep -qi "timeout.*invalid\|positive integer"; then
    fail "Large timeout" "accepted" "rejected"
else
    pass "Very large timeout value accepted (999999)"
fi

# Test 34: Timeout scenario simulation via mock timeout command
echo ""
echo "Test 34: Timeout scenario via mock timeout/gtimeout command"
mkdir -p "$TEST_DIR/repo34"
init_basic_git_repo "$TEST_DIR/repo34"
create_minimal_plan "$TEST_DIR/repo34"
echo "plan.md" >> "$TEST_DIR/repo34/.gitignore"
git -C "$TEST_DIR/repo34" add .gitignore && git -C "$TEST_DIR/repo34" commit -q -m "Add gitignore"

# Create a mock timeout command that always returns 124 (timeout exit code)
# This simulates what happens when run_with_timeout times out
mkdir -p "$TEST_DIR/repo34/bin"

# Get real git path for mock to use
REAL_GIT=$(command -v git)

# Mock timeout that returns 124 for git rev-parse (first check in setup script)
cat > "$TEST_DIR/repo34/bin/timeout" << TIMEOUTEOF
#!/usr/bin/env bash
# Mock timeout that returns 124 for git rev-parse to simulate timeout
if [[ "\$*" == *"git"*"rev-parse"* ]]; then
    exit 124
fi
# For other commands, execute normally by stripping timeout args and running
shift  # remove timeout value
exec "\$@"
TIMEOUTEOF
chmod +x "$TEST_DIR/repo34/bin/timeout"

# Also mock gtimeout (macOS with Homebrew)
cp "$TEST_DIR/repo34/bin/timeout" "$TEST_DIR/repo34/bin/gtimeout"
chmod +x "$TEST_DIR/repo34/bin/gtimeout"

# Create mock codex
cat > "$TEST_DIR/repo34/bin/codex" << 'CODEXEOF'
#!/usr/bin/env bash
exit 0
CODEXEOF
chmod +x "$TEST_DIR/repo34/bin/codex"

set +e
OUTPUT=$(PATH="$TEST_DIR/repo34/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo34" plan.md 2>&1)
EXIT_CODE=$?
set -e

# The setup should fail with a timeout-related error message
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "timeout\|timed out"; then
    pass "Timeout error message shown (exit $EXIT_CODE)"
else
    # Even without exact message, non-zero exit for timeout mock is acceptable
    if [[ $EXIT_CODE -ne 0 ]]; then
        pass "Timeout scenario causes failure (exit $EXIT_CODE)"
    else
        fail "Timeout handling" "non-zero exit or timeout message" "exit=$EXIT_CODE"
    fi
fi

# Test 35: Non-portable git path handling
echo ""
echo "Test 35: Mock uses portable git path detection"
# Verify our mock doesn't hardcode /usr/bin/git
if grep -q "/usr/bin/git" "$TEST_DIR/repo34/bin/timeout" 2>/dev/null; then
    fail "Portable git" "no hardcoded /usr/bin/git" "found hardcoded path"
else
    pass "Timeout mock uses portable command detection"
fi

# ========================================
# Full Review Round Parameter Tests (v1.5.2+)
# ========================================

echo ""
echo "--- Full Review Round Parameter Tests ---"
echo ""

# Test 36: --full-review-round with valid value (5)
echo "Test 36: --full-review-round with valid value accepted"
mkdir -p "$TEST_DIR/repo36"
init_basic_git_repo "$TEST_DIR/repo36"
create_minimal_plan "$TEST_DIR/repo36"
echo "plan.md" >> "$TEST_DIR/repo36/.gitignore"
git -C "$TEST_DIR/repo36" add .gitignore && git -C "$TEST_DIR/repo36" commit -q -m "Add gitignore"
mkdir -p "$TEST_DIR/repo36/bin"

OUTPUT=$(PATH="$TEST_DIR/repo36/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo36" plan.md --full-review-round 5 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
# Should NOT fail at --full-review-round validation
if echo "$OUTPUT" | grep -qi "full-review-round.*invalid\|must be at least 2"; then
    fail "--full-review-round 5" "accepted" "rejected"
else
    pass "--full-review-round 5 accepted"
fi

# Test 37: --full-review-round with minimum valid value (2)
echo ""
echo "Test 37: --full-review-round with minimum value (2) accepted"
mkdir -p "$TEST_DIR/repo37"
init_basic_git_repo "$TEST_DIR/repo37"
create_minimal_plan "$TEST_DIR/repo37"
echo "plan.md" >> "$TEST_DIR/repo37/.gitignore"
git -C "$TEST_DIR/repo37" add .gitignore && git -C "$TEST_DIR/repo37" commit -q -m "Add gitignore"
mkdir -p "$TEST_DIR/repo37/bin"

OUTPUT=$(PATH="$TEST_DIR/repo37/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo37" plan.md --full-review-round 2 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
# Should NOT fail at --full-review-round validation
if echo "$OUTPUT" | grep -qi "must be at least 2"; then
    fail "--full-review-round 2" "accepted" "rejected"
else
    pass "--full-review-round 2 (minimum) accepted"
fi

# Test 38: --full-review-round with value 1 rejected (below minimum)
echo ""
echo "Test 38: --full-review-round with value 1 rejected (below minimum)"
mkdir -p "$TEST_DIR/repo38"
init_basic_git_repo "$TEST_DIR/repo38"
create_minimal_plan "$TEST_DIR/repo38"
echo "plan.md" >> "$TEST_DIR/repo38/.gitignore"
git -C "$TEST_DIR/repo38" add .gitignore && git -C "$TEST_DIR/repo38" commit -q -m "Add gitignore"
mkdir -p "$TEST_DIR/repo38/bin"

OUTPUT=$(PATH="$TEST_DIR/repo38/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo38" plan.md --full-review-round 1 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "must be at least 2"; then
    pass "--full-review-round 1 rejected (must be at least 2)"
else
    fail "--full-review-round 1" "rejection with 'must be at least 2'" "exit=$EXIT_CODE"
fi

# Test 39: --full-review-round with non-numeric value rejected
echo ""
echo "Test 39: --full-review-round with non-numeric value rejected"
mkdir -p "$TEST_DIR/repo39"
init_basic_git_repo "$TEST_DIR/repo39"
create_minimal_plan "$TEST_DIR/repo39"
echo "plan.md" >> "$TEST_DIR/repo39/.gitignore"
git -C "$TEST_DIR/repo39" add .gitignore && git -C "$TEST_DIR/repo39" commit -q -m "Add gitignore"
mkdir -p "$TEST_DIR/repo39/bin"

OUTPUT=$(PATH="$TEST_DIR/repo39/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo39" plan.md --full-review-round abc 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "positive integer"; then
    pass "--full-review-round non-numeric rejected"
else
    fail "--full-review-round non-numeric" "rejection with 'positive integer'" "exit=$EXIT_CODE"
fi

# Test 40: --full-review-round without value rejected
echo ""
echo "Test 40: --full-review-round without value rejected"
mkdir -p "$TEST_DIR/repo40"
init_basic_git_repo "$TEST_DIR/repo40"
create_minimal_plan "$TEST_DIR/repo40"
echo "plan.md" >> "$TEST_DIR/repo40/.gitignore"
git -C "$TEST_DIR/repo40" add .gitignore && git -C "$TEST_DIR/repo40" commit -q -m "Add gitignore"
mkdir -p "$TEST_DIR/repo40/bin"

OUTPUT=$(PATH="$TEST_DIR/repo40/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo40" plan.md --full-review-round 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "requires.*number\|requires.*argument"; then
    pass "--full-review-round without value rejected"
else
    fail "--full-review-round without value" "rejection" "exit=$EXIT_CODE"
fi

# ========================================
# Skip Implementation Mode Tests (v1.5.2+)
# ========================================

echo ""
echo "--- Skip Implementation Mode Tests ---"
echo ""

# Test 41: --skip-impl without plan file accepted
echo "Test 41: --skip-impl without plan file accepted"
mkdir -p "$TEST_DIR/repo41"
init_basic_git_repo "$TEST_DIR/repo41"
mkdir -p "$TEST_DIR/repo41/bin"

OUTPUT=$(PATH="$TEST_DIR/repo41/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo41" --skip-impl 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
# Should NOT fail at "No plan file provided" - skip-impl makes it optional
if echo "$OUTPUT" | grep -qi "No plan file provided"; then
    fail "--skip-impl without plan" "accepted" "rejected for missing plan"
else
    # May fail later at codex check, which is fine
    pass "--skip-impl without plan file accepted"
fi

# Test 42: --skip-impl creates review phase marker
echo ""
echo "Test 42: --skip-impl creates review phase marker file"
mkdir -p "$TEST_DIR/repo42"
init_basic_git_repo "$TEST_DIR/repo42"

# Gitignore test artifacts so git working tree stays clean
echo "bin/" >> "$TEST_DIR/repo42/.gitignore"
git -C "$TEST_DIR/repo42" add .gitignore && git -C "$TEST_DIR/repo42" commit -q -m "Add gitignore"

# Create mock codex
mkdir -p "$TEST_DIR/repo42/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo42/bin/codex"
chmod +x "$TEST_DIR/repo42/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo42/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo42" --skip-impl 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}

# Find the loop directory
LOOP_DIR=$(find "$TEST_DIR/repo42/.humanize/rlcr" -maxdepth 1 -type d -name "20*" 2>/dev/null | head -1)
if [[ -n "$LOOP_DIR" ]] && [[ -f "$LOOP_DIR/.review-phase-started" ]]; then
    if grep -q "build_finish_round=0" "$LOOP_DIR/.review-phase-started"; then
        pass "--skip-impl creates .review-phase-started marker with build_finish_round=0"
    else
        fail "--skip-impl marker content" "build_finish_round=0" "$(cat "$LOOP_DIR/.review-phase-started")"
    fi
else
    fail "--skip-impl marker" "marker file created" "marker not found"
fi

# Test 43: --skip-impl sets review_started=true in state.md
echo ""
echo "Test 43: --skip-impl sets review_started=true"
if [[ -n "$LOOP_DIR" ]] && [[ -f "$LOOP_DIR/state.md" ]]; then
    if grep -q "review_started: true" "$LOOP_DIR/state.md"; then
        pass "--skip-impl sets review_started=true"
    else
        fail "--skip-impl review_started" "true" "$(grep review_started "$LOOP_DIR/state.md" || echo 'not found')"
    fi
else
    fail "--skip-impl state.md" "state.md exists" "not found"
fi

# Test 44: --skip-impl creates goal-tracker without placeholder text
echo ""
echo "Test 44: --skip-impl creates goal-tracker without placeholder text"
if [[ -n "$LOOP_DIR" ]] && [[ -f "$LOOP_DIR/goal-tracker.md" ]]; then
    # Check that it does NOT contain "[To be" placeholder text
    if grep -q '\[To be ' "$LOOP_DIR/goal-tracker.md"; then
        fail "--skip-impl goal-tracker" "no placeholder text" "contains '[To be' placeholder"
    else
        pass "--skip-impl creates goal-tracker without placeholder"
    fi
else
    fail "--skip-impl goal-tracker" "goal-tracker.md exists" "not found"
fi

# Test 44b: --skip-impl creates summary scaffold with BitLesson Delta section
echo ""
echo "Test 44b: --skip-impl creates summary scaffold"
if [[ -n "$LOOP_DIR" ]] && [[ -f "$LOOP_DIR/round-0-summary.md" ]]; then
    if grep -q '^## BitLesson Delta$' "$LOOP_DIR/round-0-summary.md" && \
       grep -q '^Action: none$' "$LOOP_DIR/round-0-summary.md"; then
        pass "--skip-impl creates round-0 summary scaffold with BitLesson Delta defaults"
    else
        fail "--skip-impl summary scaffold" \
            "BitLesson Delta section with Action: none" \
            "$(cat "$LOOP_DIR/round-0-summary.md")"
    fi
else
    fail "--skip-impl summary scaffold" "round-0-summary.md exists" "not found"
fi

# Test 44c: --skip-impl creates round-0-contract.md
echo ""
echo "Test 44c: --skip-impl creates round-0-contract.md"
if [[ -n "$LOOP_DIR" ]] && [[ -f "$LOOP_DIR/round-0-contract.md" ]]; then
    if grep -qi "Mainline Objective" "$LOOP_DIR/round-0-contract.md"; then
        pass "--skip-impl creates round-0-contract.md with mainline objective"
    else
        fail "--skip-impl round contract content" "Mainline Objective text" "$(cat "$LOOP_DIR/round-0-contract.md")"
    fi
else
    fail "--skip-impl round contract" "round-0-contract.md exists" "not found"
fi

# Test 44d: --skip-impl prompt references the round contract
echo ""
echo "Test 44d: --skip-impl prompt references round-0-contract.md"
if [[ -n "$LOOP_DIR" ]] && [[ -f "$LOOP_DIR/round-0-prompt.md" ]]; then
    if grep -q "round-0-contract.md" "$LOOP_DIR/round-0-prompt.md"; then
        pass "--skip-impl prompt references round-0-contract.md"
    else
        fail "--skip-impl prompt contract reference" "prompt mentions round-0-contract.md" "$(cat "$LOOP_DIR/round-0-prompt.md")"
    fi
else
    fail "--skip-impl prompt contract reference" "round-0-prompt.md exists" "not found"
fi

# Test 45: --skip-impl with plan file still works
echo ""
echo "Test 45: --skip-impl with plan file still works"
mkdir -p "$TEST_DIR/repo45"
init_basic_git_repo "$TEST_DIR/repo45"
create_minimal_plan "$TEST_DIR/repo45"
printf 'plan.md\nbin/\n' >> "$TEST_DIR/repo45/.gitignore"
git -C "$TEST_DIR/repo45" add .gitignore && git -C "$TEST_DIR/repo45" commit -q -m "Add gitignore"

mkdir -p "$TEST_DIR/repo45/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo45/bin/codex"
chmod +x "$TEST_DIR/repo45/bin/codex"

OUTPUT=$(PATH="$TEST_DIR/repo45/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo45" plan.md --skip-impl 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
# Should work - skip-impl with plan file is valid
if echo "$OUTPUT" | grep -qi "SKIP-IMPL MODE"; then
    pass "--skip-impl with plan file works"
else
    # May fail at codex but should at least get past args
    if echo "$OUTPUT" | grep -qi "error"; then
        fail "--skip-impl with plan" "accepted" "error occurred"
    else
        pass "--skip-impl with plan file works"
    fi
fi

LOOP_DIR_45=$(find "$TEST_DIR/repo45/.humanize/rlcr" -maxdepth 1 -type d -name "20*" 2>/dev/null | head -1)

echo ""
echo "Test 45b: --skip-impl with plan file preserves plan goal in goal-tracker"
if [[ -n "$LOOP_DIR_45" ]] && [[ -f "$LOOP_DIR_45/goal-tracker.md" ]]; then
    if grep -q "Test the setup script robustness" "$LOOP_DIR_45/goal-tracker.md"; then
        pass "--skip-impl with plan preserves plan goal anchor"
    else
        fail "--skip-impl plan goal anchor" "goal-tracker contains plan goal" "$(cat "$LOOP_DIR_45/goal-tracker.md")"
    fi
else
    fail "--skip-impl plan goal anchor" "goal-tracker.md exists" "not found"
fi

echo ""
echo "Test 45c: --skip-impl with plan file prompt references original plan"
if [[ -n "$LOOP_DIR_45" ]] && [[ -f "$LOOP_DIR_45/round-0-prompt.md" ]]; then
    if grep -q "@plan.md" "$LOOP_DIR_45/round-0-prompt.md"; then
        pass "--skip-impl with plan prompt references original plan"
    else
        fail "--skip-impl plan prompt anchor" "round-0-prompt references @plan.md" "$(cat "$LOOP_DIR_45/round-0-prompt.md")"
    fi
else
    fail "--skip-impl plan prompt anchor" "round-0-prompt.md exists" "not found"
fi

echo ""
echo "Test 45d: --skip-impl with plan file contract references original plan alignment"
if [[ -n "$LOOP_DIR_45" ]] && [[ -f "$LOOP_DIR_45/round-0-contract.md" ]]; then
    if grep -qi "aligned with @plan.md" "$LOOP_DIR_45/round-0-contract.md"; then
        pass "--skip-impl with plan contract references original plan"
    else
        fail "--skip-impl plan contract anchor" "round-0-contract references @plan.md" "$(cat "$LOOP_DIR_45/round-0-contract.md")"
    fi
else
    fail "--skip-impl plan contract anchor" "round-0-contract.md exists" "not found"
fi

# ========================================
# Dependency Check Tests
# ========================================

echo ""
echo "--- Dependency Check Tests ---"
echo ""

# Test 46: Missing codex shows dependency error
echo "Test 46: Missing codex shows dependency error"
mkdir -p "$TEST_DIR/repo46"
init_basic_git_repo "$TEST_DIR/repo46"
create_minimal_plan "$TEST_DIR/repo46"
echo "plan.md" >> "$TEST_DIR/repo46/.gitignore"
git -C "$TEST_DIR/repo46" add .gitignore && git -C "$TEST_DIR/repo46" commit -q -m "Add gitignore"

# Create bin dir with jq but no codex
mkdir -p "$TEST_DIR/repo46/bin"
prepare_runtime_bin "$TEST_DIR/repo46/bin"
cat > "$TEST_DIR/repo46/bin/jq" << 'EOF'
#!/usr/bin/env bash
exit 0
EOF
chmod +x "$TEST_DIR/repo46/bin/jq"
# Hide system codex by making the only codex on PATH our test bin dir
OUTPUT=$(PATH="$TEST_DIR/repo46/bin" run_rlcr_setup "$TEST_DIR/repo46" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "Missing required dependencies" && echo "$OUTPUT" | grep -q "codex"; then
    pass "Missing codex detected in dependency check"
else
    if command -v codex &>/dev/null; then
        skip "Cannot test missing codex (codex is installed on this system)"
    else
        fail "Missing codex detection" "dependency error mentioning codex" "exit=$EXIT_CODE output=$OUTPUT"
    fi
fi

# Test 47: Missing jq shows dependency error
echo ""
echo "Test 47: Missing jq shows dependency error"
mkdir -p "$TEST_DIR/repo47"
init_basic_git_repo "$TEST_DIR/repo47"
create_minimal_plan "$TEST_DIR/repo47"
echo "plan.md" >> "$TEST_DIR/repo47/.gitignore"
git -C "$TEST_DIR/repo47" add .gitignore && git -C "$TEST_DIR/repo47" commit -q -m "Add gitignore"

# Create bin dir with codex but no jq
mkdir -p "$TEST_DIR/repo47/bin"
prepare_runtime_bin "$TEST_DIR/repo47/bin"
cat > "$TEST_DIR/repo47/bin/codex" << 'EOF'
#!/usr/bin/env bash
exit 0
EOF
chmod +x "$TEST_DIR/repo47/bin/codex"
# Use a restricted PATH with required runtime tools but no jq
OUTPUT=$(PATH="$TEST_DIR/repo47/bin" run_rlcr_setup "$TEST_DIR/repo47" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -qi "Missing required dependencies" && echo "$OUTPUT" | grep -q "jq"; then
    pass "Missing jq detected in dependency check"
else
    if command -v jq &>/dev/null && [[ "$(command -v jq)" == /usr/bin/jq || "$(command -v jq)" == /bin/jq ]]; then
        skip "Cannot test missing jq (jq is available in /usr/bin or /bin)"
    else
        fail "Missing jq detection" "dependency error mentioning jq" "exit=$EXIT_CODE output=$OUTPUT"
    fi
fi

# Test 48: Multiple missing dependencies listed together
echo ""
echo "Test 48: Multiple missing dependencies listed together"
mkdir -p "$TEST_DIR/repo48"
init_basic_git_repo "$TEST_DIR/repo48"
create_minimal_plan "$TEST_DIR/repo48"
echo "plan.md" >> "$TEST_DIR/repo48/.gitignore"
git -C "$TEST_DIR/repo48" add .gitignore && git -C "$TEST_DIR/repo48" commit -q -m "Add gitignore"

# Create a bin dir that has system tools but not codex or jq.
# We symlink all of /usr/bin except codex and jq, plus add git.
mkdir -p "$TEST_DIR/repo48/bin"
# Only include essential tools (bash, env, git, etc.) but NOT codex or jq
for tool in bash env cat sed awk grep mkdir date head od tr wc dirname sort ls rm cp mv chmod ln readlink printf; do
    TOOL_PATH=$(command -v "$tool" 2>/dev/null || true)
    if [[ -n "$TOOL_PATH" && -x "$TOOL_PATH" && ! -e "$TEST_DIR/repo48/bin/$tool" ]]; then
        ln -s "$TOOL_PATH" "$TEST_DIR/repo48/bin/$tool"
    fi
done
REAL_GIT_PATH=$(command -v git)
ln -s "$REAL_GIT_PATH" "$TEST_DIR/repo48/bin/git"
# Also link timeout/gtimeout if available
for tool in timeout gtimeout; do
    TOOL_PATH=$(command -v "$tool" 2>/dev/null || true)
    if [[ -n "$TOOL_PATH" && -x "$TOOL_PATH" ]]; then
        ln -s "$TOOL_PATH" "$TEST_DIR/repo48/bin/$tool"
    fi
done

OUTPUT=$(PATH="$TEST_DIR/repo48/bin" run_rlcr_setup "$TEST_DIR/repo48" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
if [[ $EXIT_CODE -ne 0 ]] && echo "$OUTPUT" | grep -q "codex" && echo "$OUTPUT" | grep -q "jq"; then
    pass "Multiple missing dependencies listed in single error"
else
    # If both tools happen to be in our restricted bin, skip
    if PATH="$TEST_DIR/repo48/bin" command -v codex &>/dev/null && PATH="$TEST_DIR/repo48/bin" command -v jq &>/dev/null; then
        skip "Cannot test multiple missing deps (both available in restricted PATH)"
    else
        fail "Multiple missing deps" "error listing codex and jq" "exit=$EXIT_CODE"
    fi
fi

# Test 49: All dependencies present passes check
echo ""
echo "Test 49: All dependencies present passes dependency check"
mkdir -p "$TEST_DIR/repo49"
init_basic_git_repo "$TEST_DIR/repo49"
create_minimal_plan "$TEST_DIR/repo49"
echo "plan.md" >> "$TEST_DIR/repo49/.gitignore"
git -C "$TEST_DIR/repo49" add .gitignore && git -C "$TEST_DIR/repo49" commit -q -m "Add gitignore"

# Create mock codex and jq
mkdir -p "$TEST_DIR/repo49/bin"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo49/bin/codex"
chmod +x "$TEST_DIR/repo49/bin/codex"
echo '#!/usr/bin/env bash
exit 0' > "$TEST_DIR/repo49/bin/jq"
chmod +x "$TEST_DIR/repo49/bin/jq"

OUTPUT=$(PATH="$TEST_DIR/repo49/bin:$PATH" run_rlcr_setup "$TEST_DIR/repo49" plan.md 2>&1) || EXIT_CODE=$?
EXIT_CODE=${EXIT_CODE:-0}
# Should NOT fail at dependency check - should proceed further
if echo "$OUTPUT" | grep -qi "Missing required dependencies"; then
    fail "All deps present" "no dependency error" "dependency error shown"
else
    pass "All dependencies present - proceeds past dependency check"
fi

# ========================================
# Summary
# ========================================

print_test_summary "Setup Scripts Robustness Test Summary"
exit $?