test-commit-history-section.sh 10.5 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
#!/usr/bin/env bash
#
# Test script for the Integral (I) component: commit-history-section
#
# Validates:
# 1. Round 0: "(no commits yet)" and "(first round, no prior history)"
# 2. Round 2+: commit log and round file references rendered correctly
# 3. Corrupted BASE_COMMIT: graceful fallback with annotation
# 4. Template missing: fallback renders the full section including round files
#

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/hooks/lib/template-loader.sh"

TEMPLATE_DIR="$PROJECT_ROOT/prompt-template"

echo "========================================"
echo "Testing commit-history-section (I component)"
echo "========================================"
echo ""

# ========================================
# Setup: create a temporary git repo
# ========================================
setup_test_dir
init_test_git_repo "$TEST_DIR/repo"

# ========================================
# Test 1: Round 0 - no commits since base, first round
# ========================================
echo "Test 1: Round 0 - no commits, first round"

CURRENT_ROUND=0
BASE_COMMIT=$(git -C "$TEST_DIR/repo" rev-parse HEAD)

# No commits since BASE_COMMIT..HEAD (same commit)
COMMIT_HISTORY=$(git -C "$TEST_DIR/repo" log --oneline --no-decorate --reverse "$BASE_COMMIT"..HEAD 2>/dev/null | tail -80)
[[ -z "$COMMIT_HISTORY" ]] && COMMIT_HISTORY="(no commits yet)"

RECENT_ROUND_FILES=""
LOOP_TIMESTAMP="2026-01-01_00-00-00"
for (( r = CURRENT_ROUND - 1; r >= 0 && r >= CURRENT_ROUND - 3; r-- )); do
    RECENT_ROUND_FILES+="- @.humanize/rlcr/${LOOP_TIMESTAMP}/round-${r}-summary.md
- @.humanize/rlcr/${LOOP_TIMESTAMP}/round-${r}-review-result.md
"
done
[[ -z "$RECENT_ROUND_FILES" ]] && RECENT_ROUND_FILES="(first round, no prior history)"

RESULT=$(load_and_render_safe "$TEMPLATE_DIR" "codex/commit-history-section.md" "FALLBACK" \
    "COMMIT_HISTORY=$COMMIT_HISTORY" \
    "RECENT_ROUND_FILES=$RECENT_ROUND_FILES")

if echo "$RESULT" | grep -q "(no commits yet)" && echo "$RESULT" | grep -q "(first round, no prior history)"; then
    pass "Round 0 shows correct placeholders"
else
    fail "Round 0 placeholders" "(no commits yet) and (first round, no prior history)" "$RESULT"
fi

# ========================================
# Test 2: Round 3 - with commits and round history
# ========================================
echo ""
echo "Test 2: Round 3 - commits and round file references"

# Make some commits
cd "$TEST_DIR/repo"
echo "feat1" > feat1.txt && git add feat1.txt && git commit -q -m "feat: add feature 1"
echo "feat2" > feat2.txt && git add feat2.txt && git commit -q -m "feat: add feature 2"
echo "fix1" > fix1.txt && git add fix1.txt && git commit -q -m "fix: resolve bug in feature 1"
cd - > /dev/null

CURRENT_ROUND=3
COMMIT_HISTORY=$(git -C "$TEST_DIR/repo" log --oneline --no-decorate --reverse "$BASE_COMMIT"..HEAD 2>/dev/null | tail -80)
[[ -z "$COMMIT_HISTORY" ]] && COMMIT_HISTORY="(no commits yet)"

RECENT_ROUND_FILES=""
for (( r = CURRENT_ROUND - 1; r >= 0 && r >= CURRENT_ROUND - 3; r-- )); do
    RECENT_ROUND_FILES+="- @.humanize/rlcr/${LOOP_TIMESTAMP}/round-${r}-summary.md
- @.humanize/rlcr/${LOOP_TIMESTAMP}/round-${r}-review-result.md
"
done
[[ -z "$RECENT_ROUND_FILES" ]] && RECENT_ROUND_FILES="(first round, no prior history)"

RESULT=$(load_and_render_safe "$TEMPLATE_DIR" "codex/commit-history-section.md" "FALLBACK" \
    "COMMIT_HISTORY=$COMMIT_HISTORY" \
    "RECENT_ROUND_FILES=$RECENT_ROUND_FILES")

HAS_COMMITS=true
HAS_ROUNDS=true

echo "$RESULT" | grep -q "feat: add feature 1" || HAS_COMMITS=false
echo "$RESULT" | grep -q "feat: add feature 2" || HAS_COMMITS=false
echo "$RESULT" | grep -q "fix: resolve bug in feature 1" || HAS_COMMITS=false

echo "$RESULT" | grep -q "round-2-summary.md" || HAS_ROUNDS=false
echo "$RESULT" | grep -q "round-1-summary.md" || HAS_ROUNDS=false
echo "$RESULT" | grep -q "round-0-summary.md" || HAS_ROUNDS=false
echo "$RESULT" | grep -q "round-2-review-result.md" || HAS_ROUNDS=false

if [[ "$HAS_COMMITS" == "true" ]]; then
    pass "Round 3 shows all 3 commits"
else
    fail "Round 3 commits" "3 commit messages" "$RESULT"
fi

if [[ "$HAS_ROUNDS" == "true" ]]; then
    pass "Round 3 shows round 0-2 file references"
else
    fail "Round 3 round files" "round-0/1/2 summary and review files" "$RESULT"
fi

# ========================================
# Test 3: Corrupted BASE_COMMIT - nonexistent object
# ========================================
echo ""
echo "Test 3: Corrupted BASE_COMMIT graceful fallback"

BAD_COMMIT="deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"

# Simulate the exact logic from the stop hook (merge-base --is-ancestor)
if [[ -n "$BAD_COMMIT" ]] && git -C "$TEST_DIR/repo" merge-base --is-ancestor "$BAD_COMMIT" HEAD 2>/dev/null; then
    COMMIT_HISTORY=$(git -C "$TEST_DIR/repo" log --oneline --no-decorate --reverse "$BAD_COMMIT"..HEAD 2>/dev/null | tail -80)
else
    COMMIT_HISTORY=$(git -C "$TEST_DIR/repo" log --oneline --no-decorate --reverse -30 2>/dev/null)
    [[ -n "$COMMIT_HISTORY" ]] && COMMIT_HISTORY="(base commit unavailable, showing recent branch commits)
${COMMIT_HISTORY}"
fi
[[ -z "$COMMIT_HISTORY" ]] && COMMIT_HISTORY="(no commits yet)"

if echo "$COMMIT_HISTORY" | grep -q "base commit unavailable"; then
    pass "Corrupted BASE_COMMIT triggers annotation"
else
    fail "Corrupted BASE_COMMIT annotation" "base commit unavailable" "$COMMIT_HISTORY"
fi

if echo "$COMMIT_HISTORY" | grep -q "feat: add feature"; then
    pass "Corrupted BASE_COMMIT still shows recent commits"
else
    fail "Corrupted BASE_COMMIT recent commits" "recent branch commits" "$COMMIT_HISTORY"
fi

# Verify no crash (we got here = no set -e crash)
pass "Corrupted BASE_COMMIT did not crash (set -e safe)"

# ========================================
# Test 3b: Valid but unrelated commit (not ancestor of HEAD)
# ========================================
echo ""
echo "Test 3b: Valid but unrelated BASE_COMMIT (orphan branch)"

# Create an orphan branch with its own commit, then switch back
cd "$TEST_DIR/repo"
ORIG_BRANCH=$(git rev-parse --abbrev-ref HEAD)
git checkout -q --orphan orphan-test
echo "orphan" > orphan.txt && git add orphan.txt && git commit -q -m "orphan commit"
ORPHAN_COMMIT=$(git rev-parse HEAD)
git checkout -q "$ORIG_BRANCH"
cd - > /dev/null

# ORPHAN_COMMIT exists but is NOT an ancestor of HEAD
if [[ -n "$ORPHAN_COMMIT" ]] && git -C "$TEST_DIR/repo" merge-base --is-ancestor "$ORPHAN_COMMIT" HEAD 2>/dev/null; then
    COMMIT_HISTORY="should not reach here"
else
    COMMIT_HISTORY=$(git -C "$TEST_DIR/repo" log --oneline --no-decorate --reverse -30 2>/dev/null)
    [[ -n "$COMMIT_HISTORY" ]] && COMMIT_HISTORY="(base commit unavailable, showing recent branch commits)
${COMMIT_HISTORY}"
fi
[[ -z "$COMMIT_HISTORY" ]] && COMMIT_HISTORY="(no commits yet)"

if echo "$COMMIT_HISTORY" | grep -q "base commit unavailable"; then
    pass "Unrelated valid commit triggers annotation"
else
    fail "Unrelated valid commit annotation" "base commit unavailable" "$COMMIT_HISTORY"
fi

# ========================================
# Test 4: Missing template - fallback renders full section
# ========================================
echo ""
echo "Test 4: Missing template fallback renders full section"

CURRENT_ROUND=2
COMMIT_HISTORY=$(git -C "$TEST_DIR/repo" log --oneline --no-decorate --reverse "$BASE_COMMIT"..HEAD 2>/dev/null | tail -80)

RECENT_ROUND_FILES=""
for (( r = CURRENT_ROUND - 1; r >= 0 && r >= CURRENT_ROUND - 3; r-- )); do
    RECENT_ROUND_FILES+="- @.humanize/rlcr/${LOOP_TIMESTAMP}/round-${r}-summary.md
- @.humanize/rlcr/${LOOP_TIMESTAMP}/round-${r}-review-result.md
"
done

# Use the exact fallback format from the stop hook
COMMIT_HISTORY_SECTION_FALLBACK="## Development History (Integral Context)
\`\`\`
${COMMIT_HISTORY}
\`\`\`
### Recent Round Files
Read these files before conducting your review to understand the trajectory of work:
${RECENT_ROUND_FILES}"

# Point to a non-existent template to force fallback
RESULT=$(load_and_render_safe "$TEMPLATE_DIR" "codex/non-existent-template.md" "$COMMIT_HISTORY_SECTION_FALLBACK" \
    "COMMIT_HISTORY=$COMMIT_HISTORY" \
    "RECENT_ROUND_FILES=$RECENT_ROUND_FILES")

FALLBACK_OK=true
echo "$RESULT" | grep -q "Development History" || FALLBACK_OK=false
echo "$RESULT" | grep -q "feat: add feature 1" || FALLBACK_OK=false
echo "$RESULT" | grep -q "Recent Round Files" || FALLBACK_OK=false
echo "$RESULT" | grep -q "round-1-summary.md" || FALLBACK_OK=false
echo "$RESULT" | grep -q "round-0-review-result.md" || FALLBACK_OK=false
echo "$RESULT" | grep -q "Read these files" || FALLBACK_OK=false

if [[ "$FALLBACK_OK" == "true" ]]; then
    pass "Fallback renders full section with commits, round files, and directive"
else
    fail "Fallback full section" "commits + round files + directive" "$RESULT"
fi

# ========================================
# Test 5: Round 1 - only 1 prior round (boundary)
# ========================================
echo ""
echo "Test 5: Round 1 - only 1 prior round"

CURRENT_ROUND=1
RECENT_ROUND_FILES=""
for (( r = CURRENT_ROUND - 1; r >= 0 && r >= CURRENT_ROUND - 3; r-- )); do
    RECENT_ROUND_FILES+="- @.humanize/rlcr/${LOOP_TIMESTAMP}/round-${r}-summary.md
- @.humanize/rlcr/${LOOP_TIMESTAMP}/round-${r}-review-result.md
"
done
[[ -z "$RECENT_ROUND_FILES" ]] && RECENT_ROUND_FILES="(first round, no prior history)"

if echo "$RECENT_ROUND_FILES" | grep -q "round-0-summary.md" && \
   ! echo "$RECENT_ROUND_FILES" | grep -q "round-1-"; then
    pass "Round 1 references only round 0"
else
    fail "Round 1 boundary" "only round-0 references" "$RECENT_ROUND_FILES"
fi

# ========================================
# Test 6: Empty BASE_COMMIT (legacy loop)
# ========================================
echo ""
echo "Test 6: Empty BASE_COMMIT fallback"

EMPTY_BASE=""
if [[ -n "$EMPTY_BASE" ]] && git -C "$TEST_DIR/repo" merge-base --is-ancestor "$EMPTY_BASE" HEAD 2>/dev/null; then
    COMMIT_HISTORY="should not reach here"
else
    COMMIT_HISTORY=$(git -C "$TEST_DIR/repo" log --oneline --no-decorate --reverse -30 2>/dev/null)
    [[ -n "$COMMIT_HISTORY" ]] && COMMIT_HISTORY="(base commit unavailable, showing recent branch commits)
${COMMIT_HISTORY}"
fi
[[ -z "$COMMIT_HISTORY" ]] && COMMIT_HISTORY="(no commits yet)"

if echo "$COMMIT_HISTORY" | grep -q "base commit unavailable"; then
    pass "Empty BASE_COMMIT triggers annotation"
else
    fail "Empty BASE_COMMIT annotation" "base commit unavailable" "$COMMIT_HISTORY"
fi

# ========================================
# Summary
# ========================================
print_test_summary "Commit History Section (I Component) Tests"