#!/bin/sh

# This is an interdiff(1) testcase.
# Test: --color option should not break context trimming
#
# The bug: When --color is passed to internal diff, ANSI escape codes
# break trim_context()'s ability to detect and strip "unline" content.
# This test verifies the bug is fixed by ensuring --color doesn't break interdiff.

. ${top_srcdir-.}/tests/common.sh

# Create test files that will generate interdiff output
cat << EOF > original.txt
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
EOF

# Create patches that modify overlapping regions to force interdiff generation
cat << EOF > patch1.patch
--- original.txt
+++ version1.txt
@@ -1,6 +1,6 @@
 line1
 line2
-line3
+MODIFIED3
 line4
 line5
 line6
@@ -7,4 +7,4 @@
 line7
 line8
-line9
+MODIFIED9
 line10
EOF

cat << EOF > patch2.patch
--- original.txt
+++ version2.txt
@@ -2,6 +2,6 @@
 line2
 line3
 line4
-line5
+CHANGED5
 line6
 line7
@@ -8,3 +8,3 @@
 line8
 line9
-line10
+CHANGED10
EOF

# Test the key requirement: --color should not break interdiff functionality
# The original bug would cause interdiff to fail or produce incorrect output

# First, test without color to establish baseline
${INTERDIFF} patch1.patch patch2.patch >baseline.out 2>baseline.err
baseline_exit=$?

# Then test with --color=always
${INTERDIFF} --color=always patch1.patch patch2.patch >color.out 2>color.err
color_exit=$?

# Key test 1: Both should have the same exit status
if [ $baseline_exit -ne $color_exit ]; then
    echo "ERROR: --color option changes interdiff exit status"
    echo "Baseline exit: $baseline_exit, Color exit: $color_exit"
    exit 1
fi

# Key test 2: If baseline succeeded, color version should also produce reasonable output
if [ $baseline_exit -eq 0 ]; then
    # Strip ANSI codes from color output for comparison
    sed 's/\x1b\[[0-9;]*m//g' color.out > color_stripped.out

    # The semantic content should be equivalent
    if ! diff baseline.out color_stripped.out >/dev/null 2>&1; then
        echo "ERROR: --color option changes interdiff semantic output"
        echo "This indicates the color implementation interferes with diff processing"
        echo
        echo "=== Baseline output ==="
        cat baseline.out
        echo
        echo "=== Color output (stripped) ==="
        cat color_stripped.out
        echo
        echo "=== Difference ==="
        diff baseline.out color_stripped.out || true
        exit 1
    fi
fi

# Key test 3: The color output should not contain malformed content
# Look for signs that trim_context() failed due to color codes
if grep -E '^\x1b\[[0-9;]*m[!]+' color.out >/dev/null 2>&1; then
    echo "ERROR: Found colored unline artifacts in output"
    echo "This suggests trim_context() failed to strip artificial content"
    echo "Raw output:"
    cat -v color.out
    exit 1
fi

echo "SUCCESS: --color option does not break interdiff context trimming"
